|
| 1 | +# Cosmian CLI |
| 2 | + |
| 3 | +Cosmian CLI is a Rust-based Command Line Interface that manages KMS (Key Management System) and Findex server operations. It provides cryptographic key management and searchable symmetric encryption capabilities. |
| 4 | + |
| 5 | +Always reference these instructions first and fallback to search or bash commands only when you encounter unexpected information that does not match the info here. |
| 6 | + |
| 7 | +## Working Effectively |
| 8 | + |
| 9 | +### Prerequisites and Environment Setup |
| 10 | + |
| 11 | +- Install system dependencies with OpenSSL 3.2.0: |
| 12 | + |
| 13 | + ```bash |
| 14 | + sudo apt-get update && sudo apt-get install -y libssl-dev pkg-config docker.io docker-compose-plugin |
| 15 | + # Ensure OpenSSL 3.2.0 is installed for optimal compatibility |
| 16 | + ``` |
| 17 | + |
| 18 | +- Set required environment variables: |
| 19 | + |
| 20 | + ```bash |
| 21 | + export OPENSSL_LIB_DIR=/usr/lib/x86_64-linux-gnu |
| 22 | + export OPENSSL_INCLUDE_DIR=/usr/include/openssl |
| 23 | + export PKG_CONFIG_PATH=/usr/lib/x86_64-linux-gnu/pkgconfig |
| 24 | + ``` |
| 25 | + |
| 26 | +### Repository Initialization |
| 27 | + |
| 28 | +- Initialize git submodules (REQUIRED for builds): |
| 29 | + |
| 30 | + ```bash |
| 31 | + git submodule update --init --recursive |
| 32 | + ``` |
| 33 | + |
| 34 | + - Takes ~30 seconds to complete |
| 35 | + - Downloads KMS, Findex server, test data, and reusable scripts |
| 36 | + |
| 37 | +### Building the CLI |
| 38 | + |
| 39 | +- Development build (recommended for testing changes): |
| 40 | + |
| 41 | + ```bash |
| 42 | + cargo check --features non-fips -p cosmian_cli |
| 43 | + ``` |
| 44 | + |
| 45 | + - Takes ~1 minute. NEVER CANCEL. Set timeout to 120+ seconds. |
| 46 | + |
| 47 | +- Release build: |
| 48 | + |
| 49 | + ```bash |
| 50 | + cargo build --release --features non-fips -p cosmian_cli |
| 51 | + ``` |
| 52 | + |
| 53 | + - Takes ~8-10 minutes. NEVER CANCEL. Set timeout to 900+ seconds. |
| 54 | + - Binary available at `./target/release/cosmian` |
| 55 | + |
| 56 | +### Testing |
| 57 | + |
| 58 | +- Start Docker services for integration tests: |
| 59 | + |
| 60 | + ```bash |
| 61 | + docker compose up -d |
| 62 | + ``` |
| 63 | + |
| 64 | + - Takes ~3 seconds to start Redis container |
| 65 | + |
| 66 | +- Run full test suite: |
| 67 | + |
| 68 | + ```bash |
| 69 | + export RUST_LOG="cosmian_cli=error,cosmian_findex_client=debug,cosmian_kms_client=debug" |
| 70 | + cargo test --release --features non-fips -p cosmian_cli -- --nocapture |
| 71 | + ``` |
| 72 | + |
| 73 | + - Takes ~11-15 minutes to complete. NEVER CANCEL. Set timeout to 1200+ seconds. |
| 74 | + - Includes downloading additional test dependencies (~2 minutes) |
| 75 | + - Compilation of test binaries (~4 minutes) |
| 76 | + - Test execution (~5-9 minutes) |
| 77 | + - Note: Some tests may fail in restricted environments (HSM, auth tests) - this is expected |
| 78 | + |
| 79 | +### Code Quality and Linting |
| 80 | + |
| 81 | +- Install clippy (if not available): |
| 82 | + |
| 83 | + ```bash |
| 84 | + rustup component add clippy |
| 85 | + ``` |
| 86 | + |
| 87 | +- Format code: |
| 88 | + |
| 89 | + ```bash |
| 90 | + cargo fmt |
| 91 | + ``` |
| 92 | + |
| 93 | + - Takes <1 second |
| 94 | +- Run clippy linter: |
| 95 | + |
| 96 | + ```bash |
| 97 | + cargo clippy --features non-fips -- -D warnings |
| 98 | + ``` |
| 99 | + |
| 100 | + - Takes ~17 seconds for initial run |
| 101 | +- ALWAYS run formatting and linting before committing changes or CI will fail |
| 102 | + |
| 103 | +## Validation Scenarios |
| 104 | + |
| 105 | +### Basic CLI Functionality |
| 106 | + |
| 107 | +After making changes, ALWAYS validate: |
| 108 | + |
| 109 | +1. CLI help works: |
| 110 | + |
| 111 | + ```bash |
| 112 | + ./target/release/cosmian --help |
| 113 | + ``` |
| 114 | + |
| 115 | +2. KMS subcommand works: |
| 116 | + |
| 117 | + ```bash |
| 118 | + ./target/release/cosmian kms --help |
| 119 | + ``` |
| 120 | + |
| 121 | +3. Findex subcommand works: |
| 122 | + |
| 123 | + ```bash |
| 124 | + ./target/release/cosmian findex --help |
| 125 | + ``` |
| 126 | + |
| 127 | +### Basic CLI Functionality |
| 128 | + |
| 129 | +After making changes, ALWAYS validate: |
| 130 | + |
| 131 | +1. CLI help works: |
| 132 | + |
| 133 | + ```bash |
| 134 | + ./target/release/cosmian --help |
| 135 | + ``` |
| 136 | + |
| 137 | +2. KMS subcommand works: |
| 138 | + |
| 139 | + ```bash |
| 140 | + ./target/release/cosmian kms --help |
| 141 | + ``` |
| 142 | + |
| 143 | +3. Findex subcommand works: |
| 144 | + |
| 145 | + ```bash |
| 146 | + ./target/release/cosmian findex --help |
| 147 | + ``` |
| 148 | + |
| 149 | +### End-to-End Testing |
| 150 | + |
| 151 | +When making significant changes, test complete workflows: |
| 152 | + |
| 153 | +1. **KMS Workflow (requires running KMS server):** |
| 154 | + |
| 155 | + ```bash |
| 156 | + # Start KMS server |
| 157 | + docker run -d -p 9998:9998 --name kms ghcr.io/cosmian/kms |
| 158 | + |
| 159 | + # Test key creation, encryption, decryption |
| 160 | + ./target/release/cosmian --kms-url http://localhost:9998 kms sym keys create --number-of-bits 256 --algorithm aes --tag test-key |
| 161 | + echo "Test data" > /tmp/test.txt |
| 162 | + ./target/release/cosmian --kms-url http://localhost:9998 kms sym encrypt --tag test-key --output-file /tmp/test.enc /tmp/test.txt |
| 163 | + ./target/release/cosmian --kms-url http://localhost:9998 kms sym decrypt --tag test-key --output-file /tmp/test_dec.txt /tmp/test.enc |
| 164 | + cat /tmp/test_dec.txt # Should show "Test data" |
| 165 | + ``` |
| 166 | + |
| 167 | +2. **Findex Workflow (requires running Findex server):** |
| 168 | + |
| 169 | + ```bash |
| 170 | + # Start Findex server |
| 171 | + docker run -d -p 6668:6668 --name findex ghcr.io/cosmian/findex-server |
| 172 | + |
| 173 | + # Test index creation (may need time for server startup) |
| 174 | + ./target/release/cosmian --findex-url http://localhost:6668 findex permissions create |
| 175 | + ``` |
| 176 | + |
| 177 | +**Note**: The integration test script `.github/scripts/cosmian_tests.sh` uses incorrect port configurations and will fail. Use the manual workflows above instead. |
| 178 | + |
| 179 | +## Common Commands Reference |
| 180 | + |
| 181 | +### Quick Development Cycle |
| 182 | + |
| 183 | +```bash |
| 184 | +# 1. Initialize repository (first time only) |
| 185 | +git submodule update --init --recursive |
| 186 | + |
| 187 | +# 2. Set up environment |
| 188 | +export OPENSSL_LIB_DIR=/usr/lib/x86_64-linux-gnu |
| 189 | +export OPENSSL_INCLUDE_DIR=/usr/include/openssl |
| 190 | +export PKG_CONFIG_PATH=/usr/lib/x86_64-linux-gnu/pkgconfig |
| 191 | + |
| 192 | +# 3. Quick check during development |
| 193 | +cargo check --features non-fips -p cosmian_cli |
| 194 | + |
| 195 | +# 4. Build for testing |
| 196 | +cargo build --release --features non-fips -p cosmian_cli |
| 197 | + |
| 198 | +# 5. Run specific tests |
| 199 | +cargo test --features non-fips -p cosmian_cli [test_name] |
| 200 | + |
| 201 | +# 6. Format and lint before committing |
| 202 | +cargo fmt && cargo clippy --features non-fips -- -D warnings |
| 203 | +``` |
| 204 | + |
| 205 | +### Docker Services Management |
| 206 | + |
| 207 | +```bash |
| 208 | +# Start all services |
| 209 | +docker compose up -d |
| 210 | + |
| 211 | +# Start KMS only |
| 212 | +docker run -d -p 9998:9998 --name kms ghcr.io/cosmian/kms |
| 213 | + |
| 214 | +# Start Findex only |
| 215 | +docker run -d -p 6668:6668 --name findex ghcr.io/cosmian/findex-server |
| 216 | + |
| 217 | +# Check service status |
| 218 | +docker ps |
| 219 | + |
| 220 | +# View logs |
| 221 | +docker logs kms |
| 222 | +docker logs findex |
| 223 | + |
| 224 | +# Stop and clean up |
| 225 | +docker stop kms findex cli-redis-1 |
| 226 | +docker rm kms findex cli-redis-1 |
| 227 | +docker compose down |
| 228 | +``` |
| 229 | + |
| 230 | +### Build Troubleshooting |
| 231 | + |
| 232 | +```bash |
| 233 | +# Clean build cache if encountering issues |
| 234 | +cargo clean |
| 235 | + |
| 236 | +# Update toolchain if needed |
| 237 | +rustup update |
| 238 | + |
| 239 | +# Check current toolchain |
| 240 | +rustup show |
| 241 | + |
| 242 | +# Install missing components |
| 243 | +rustup component add clippy rustfmt |
| 244 | +``` |
| 245 | + |
| 246 | +## Important File Locations |
| 247 | + |
| 248 | +### Source Code Structure |
| 249 | + |
| 250 | +- Main CLI source: `crate/cli/src/` |
| 251 | +- KMS integration: `kms/` (git submodule) |
| 252 | +- Findex integration: `findex-server/` (git submodule) |
| 253 | +- Test data: `test_data/` (git submodule) |
| 254 | +- CI/CD scripts: `.github/scripts/` |
| 255 | + |
| 256 | +### Configuration Files |
| 257 | + |
| 258 | +- Cargo workspace: `Cargo.toml` |
| 259 | +- Rust toolchain: `rust-toolchain.toml` (nightly-2025-03-31) |
| 260 | +- Formatting config: `.rustfmt.toml` |
| 261 | +- Docker services: `docker-compose.yml` |
| 262 | + |
| 263 | +### Key Build Scripts |
| 264 | + |
| 265 | +- Main build script: `.github/scripts/cargo_build.sh` |
| 266 | +- Test script: `.github/scripts/cosmian_tests.sh` |
| 267 | +- Release script: `.github/scripts/release.sh` |
| 268 | + |
| 269 | +## Common Issues and Workarounds |
| 270 | + |
| 271 | +### OpenSSL Build Issues |
| 272 | + |
| 273 | +- If OpenSSL linking fails, ensure environment variables are set: |
| 274 | + |
| 275 | + ```bash |
| 276 | + export OPENSSL_LIB_DIR=/usr/lib/x86_64-linux-gnu |
| 277 | + export OPENSSL_INCLUDE_DIR=/usr/include/openssl |
| 278 | + ``` |
| 279 | + |
| 280 | +- The repository requires OpenSSL v3.2.0 for optimal compatibility |
| 281 | + |
| 282 | +### Network Connectivity Issues |
| 283 | + |
| 284 | +- If external OpenSSL download fails (package.cosmian.com), ensure OpenSSL 3.2.0 is available |
| 285 | +- Some test dependencies may timeout - increase timeout values accordingly |
| 286 | +- Docker registry access required for Redis container |
| 287 | +- Integration test script has wrong port configurations (19998/16668 vs 9998/6668) |
| 288 | + |
| 289 | +### Build Performance |
| 290 | + |
| 291 | +- First build downloads many dependencies (~2 minutes) |
| 292 | +- Subsequent builds are faster due to caching |
| 293 | +- Use `cargo check` for quick validation instead of full builds |
| 294 | +- Release builds are significantly slower than debug builds |
| 295 | + |
| 296 | +### Test Environment Limitations |
| 297 | + |
| 298 | +- Some tests expect HSM hardware/software that may not be available |
| 299 | +- Authentication tests may fail without proper server configuration |
| 300 | +- Certificate tests may fail in restricted environments |
| 301 | +- These failures are expected in sandboxed/CI environments |
| 302 | + |
| 303 | +## Critical Timing Expectations |
| 304 | + |
| 305 | +**NEVER CANCEL these long-running operations:** |
| 306 | + |
| 307 | +- `git submodule update --init --recursive`: 30 seconds |
| 308 | +- `cargo build --release`: 8-10 minutes |
| 309 | +- `cargo test`: 11-15 minutes |
| 310 | +- `cargo clippy`: 17 seconds (first run) |
| 311 | +- `docker compose up -d`: 3 seconds |
| 312 | + |
| 313 | +Always set appropriate timeouts (at least 2x expected time) and wait for completion. |
| 314 | + |
| 315 | +## Repository Context |
| 316 | + |
| 317 | +This is a Rust workspace with multiple crates: |
| 318 | + |
| 319 | +- Main CLI crate (`cosmian_cli`) |
| 320 | +- PKCS11 provider and module crates |
| 321 | +- Integrated KMS and Findex server submodules |
| 322 | +- Comprehensive test suites with Docker-based services |
| 323 | + |
| 324 | +The CLI supports both KMS operations (key management, encryption/decryption) and Findex operations (searchable symmetric encryption). All operations can work with local test servers or remote production instances. |
| 325 | + |
| 326 | +### Architecture Overview |
| 327 | + |
| 328 | +- **Workspace Structure**: Multi-crate Rust workspace using Cargo workspaces |
| 329 | +- **Git Submodules**: KMS, Findex server, test data, and reusable scripts |
| 330 | +- **Docker Integration**: Redis for tests, KMS/Findex servers for integration testing |
| 331 | +- **Cross-platform**: Supports Linux, macOS, and Windows (requires OpenSSL 3.2.0 setup) |
| 332 | +- **CI/CD**: GitHub Actions with comprehensive build, test, and release pipelines |
| 333 | + |
| 334 | +### Development Workflow |
| 335 | + |
| 336 | +1. Clone repository and initialize submodules |
| 337 | +2. Set up OpenSSL 3.2.0 environment variables |
| 338 | +3. Use `cargo check` for rapid iteration |
| 339 | +4. Use `cargo test` for comprehensive validation |
| 340 | +5. Use manual end-to-end testing for critical paths |
| 341 | +6. Always format and lint before committing |
| 342 | +7. Validate CLI functionality with real servers when possible |
| 343 | + |
| 344 | +### Production Considerations |
| 345 | + |
| 346 | +- The CLI is designed to work with production KMS and Findex deployments |
| 347 | +- Security features include certificate validation, authentication, and encrypted communications |
| 348 | +- Performance optimizations are present in release builds |
| 349 | +- Configuration can be done via files, environment variables, or command-line arguments |
0 commit comments