Skip to content

Development Guide

This guide covers setting up a development environment for XTM Composer and contributing to the project.

Prerequisites

Required Tools

  • Rust: 1.70.0 or higher
  • Git: For version control
  • Docker: For testing orchestration features
  • OpenSSL: For generating RSA keys
  • IntelliJ IDEA with Rust Plugin
  • cargo-watch: For auto-reloading during development
  • cargo-clippy: For linting
  • cargo-fmt: For code formatting

Setting Up Development Environment

1. Install Rust

# Install Rust using rustup
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source $HOME/.cargo/env

# Verify installation
rustc --version
cargo --version

2. Clone Repository

git clone https://github.com/OpenCTI-Platform/xtm-composer.git
cd xtm-composer

3. Install Development Dependencies

# Install cargo-watch for auto-reloading
cargo install cargo-watch

# Install clippy for linting
rustup component add clippy

# Install rustfmt for formatting
rustup component add rustfmt

4. Generate Development Keys

# Generate RSA private key for development
openssl genrsa -out private_key_4096.pem 4096

5. Create Development Configuration

Create config/development.yaml:

manager:
  id: dev-manager-${USER}  # Unique ID for your dev instance
  credentials_key_filepath: ./private_key_4096.pem
  logger:
    level: debug
    format: pretty
    directory: true
    console: true
  debug:
    show_env_vars: true
    show_sensitive_env_vars: false  # Set to true if debugging auth issues

opencti:
  enable: true
  url: http://host.docker.internal:4000  # Your local OpenCTI instance
  token: your-dev-token
  daemon:
    selector: docker
    docker:
      network_mode: bridge

Running in Development

Basic Development Run

# Set development environment
export COMPOSER_ENV=development

# Run with cargo
cargo run

# Or with auto-reload on file changes
cargo watch -x run

Running with Environment Variables

COMPOSER_ENV=development \
MANAGER__ID=dev-test \
OPENCTI__URL=http://localhost:4000 \
OPENCTI__TOKEN=your-token \
cargo run

Debug Output

Enable detailed debug output:

# Set log level to trace
export MANAGER__LOGGER__LEVEL=trace

# Show environment variables at startup
export MANAGER__DEBUG__SHOW_ENV_VARS=true

# Run with backtrace for errors
RUST_BACKTRACE=1 cargo run

Project Structure

xtm-composer/
├── src/
│   ├── main.rs              # Application entry point
│   ├── api/                 # External API integrations
│   │   ├── opencti/         # OpenCTI API client
│   │   └── openbas/         # OpenBAS API client (Coming Soon)
│   ├── config/              # Configuration management
│   │   ├── mod.rs
│   │   └── settings.rs      # Settings structure definitions
│   ├── engine/              # Core business logic
│   │   ├── mod.rs
│   │   └── manager.rs       # Manager implementation
│   └── orchestrator/        # Container orchestration
│       ├── mod.rs
│       ├── kubernetes.rs    # Kubernetes implementation
│       ├── docker.rs        # Docker implementation
│       └── portainer.rs     # Portainer implementation
├── config/                  # Configuration files
│   ├── default.yaml         # Default configuration
│   └── development.yaml     # Development overrides
├── Cargo.toml              # Rust dependencies
└── build.rs                # Build script

Development Workflow

1. Feature Development

# Create feature branch
git switch -c feature/your-feature-name

# Make changes and test
cargo build
cargo test

# Format code
cargo fmt

# Run linter
cargo clippy -- -D warnings

# Commit changes
git add .
git commit -m "[composer] Add your feature description (#issue)"

2. Building

# Development build (with debug symbols)
cargo build

# Release build (optimized)
cargo build --release

# Check compilation without building
cargo check

Code Style Guidelines

Rust Best Practices

  1. Follow Rust naming conventions:
  2. Use snake_case for functions and variables
  3. Use PascalCase for types and traits
  4. Use SCREAMING_SNAKE_CASE for constants

  5. Error Handling:

  6. Use Result<T, E> for recoverable errors
  7. Use panic! only for unrecoverable errors
  8. Provide meaningful error messages

  9. Documentation:

  10. Add doc comments (///) for public APIs
  11. Include examples in documentation
  12. Document panics and errors

Example Code Style

/// Manages connector lifecycle in OpenCTI
///
/// # Examples
///
/// ```
/// let manager = ConnectorManager::new(config)?;
/// manager.start()?;
/// ```
pub struct ConnectorManager {
    config: ManagerConfig,
    orchestrator: Box<dyn Orchestrator>,
}

impl ConnectorManager {
    /// Creates a new connector manager
    ///
    /// # Errors
    ///
    /// Returns an error if configuration is invalid
    pub fn new(config: ManagerConfig) -> Result<Self, ManagerError> {
        // Implementation
    }
}

Testing with Local Services

Running Local OpenCTI

# Clone OpenCTI
git clone https://github.com/OpenCTI-Platform/opencti.git
cd opencti/opencti-platform/opencti-dev

# Start OpenCTI stack
docker compose up -d

# OpenCTI will be available at http://localhost:4000
# Default credentials: admin@opencti.io / admin

Running Local Docker Registry

# Start local registry for testing
docker run -d -p 5000:5000 --name registry registry:2

# Tag and push test images
docker tag your-image localhost:5000/your-image
docker push localhost:5000/your-image

Debugging

Using IntelliJ IDEA

  1. Install IntelliJ Rust Plugin:
  2. Open IntelliJ IDEA
  3. Go to File > Settings (or IntelliJ IDEA > Preferences on macOS)
  4. Navigate to Plugins
  5. Search for "Rust" and install the official JetBrains Rust plugin
  6. Restart IntelliJ IDEA

  7. Open the Project:

  8. Use File > Open and select the xtm-composer directory
  9. IntelliJ will automatically detect the Cargo.toml file

  10. Using .run Configuration Files:

The project already includes IntelliJ run configurations in the dev/ directory: - dev/Run xtm-composer.run.xml - For running the application - dev/Test xtm-composer.run.xml - For running tests

These should be automatically detected by IntelliJ.

  1. Set Breakpoints and Debug:
  2. Click in the gutter next to line numbers to set breakpoints
  3. Use Run > Debug 'Debug XTM Composer' or click the debug icon
  4. Use the debug panel to step through code, inspect variables, etc.

  5. Additional IntelliJ Settings:

  6. Enable format on save: Settings > Tools > Actions on Save > Reformat code
  7. Configure Rust fmt: Settings > Languages & Frameworks > Rust > Rustfmt
  8. Enable Clippy: Settings > Languages & Frameworks > Rust > External Linters

Common Issues

1. Connection Refused

# Check if OpenCTI is running
curl http://localhost:4000/graphql

# Check Docker daemon
docker info

2. Compilation Errors

# Clean build artifacts
cargo clean

# Update dependencies
cargo update

# Check for breaking changes
cargo check

Contributing

For detailed information about contributing to XTM Composer, including: - Commit message format and conventions - Pull request process - Code of conduct - Testing requirements - GPG signing setup

Please refer to our Contributing Guide.

Resources