Skip to content

Commit fb7759b

Browse files
authored
feat: Implement server CLI interface (bssh-server binary) (#131) (#150)
* feat: Implement server CLI interface (bssh-server binary) (#131) Implement the bssh-server binary with comprehensive CLI interface for managing the SSH server. Features: - Main commands: run, gen-config, hash-password, check-config, gen-host-key, version - Global CLI arguments: -c/--config, -b/--bind-address, -p/--port, -k/--host-key, -v/--verbose, -D/--foreground, --pid-file - Configuration file loading with CLI overrides - Signal handling for graceful shutdown (SIGTERM, SIGINT) - Password hashing with bcrypt (cost factor 12) - SSH host key generation (Ed25519/RSA) - Configuration validation and checking - Proper error handling and exit codes Technical details: - Uses clap for CLI parsing with derive API - Integrates with existing ServerFileConfig from issue #130 - Supports both file-based and CLI-based configuration - Added dependencies: bcrypt 0.16, rand 0.8, ssh-key 0.6 - All clippy checks pass with -D warnings Resolves #131 * fix: Address critical and high security issues in server CLI - Fix host key file race condition by using atomic file creation with mode 0o600 - Add exclusive PID file lock check to prevent multiple server instances - Add password complexity warning for passwords shorter than 8 characters - Set restrictive permissions (0600) on generated config files * test: Add comprehensive tests for server CLI binary - Add 19 new unit tests covering CLI parsing, subcommands, and options - Test gen-config with file output and permissions validation - Test gen-host-key for Ed25519/RSA with permission checks - Test write_pid_file including stale PID handling - Test all CLI parsing scenarios and global options - Update ARCHITECTURE.md with Server CLI Binary documentation - Update docs/architecture/README.md with server CLI references - Update docs/architecture/server-configuration.md with CLI commands section - Fix formatting issues in bssh_server.rs (cargo fmt)
1 parent 0a42a3e commit fb7759b

File tree

7 files changed

+1089
-4
lines changed

7 files changed

+1089
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ Thumbs.db
2525
.gemini/
2626
references/
2727
vendor/
28+
bssh-server.yaml

ARCHITECTURE.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,52 @@ Common utilities for code reuse between bssh client and server implementations:
189189

190190
The `security` and `jump::rate_limiter` modules re-export from shared for backward compatibility.
191191

192+
### Server CLI Binary
193+
**Binary**: `bssh-server`
194+
195+
The `bssh-server` binary provides a command-line interface for managing and operating the SSH server:
196+
197+
**Subcommands**:
198+
- **run** - Start the SSH server (default when no subcommand specified)
199+
- **gen-config** - Generate a configuration file template with secure defaults
200+
- **hash-password** - Hash passwords for configuration using bcrypt
201+
- **check-config** - Validate configuration files and display settings
202+
- **gen-host-key** - Generate SSH host keys (Ed25519 or RSA)
203+
- **version** - Show version and build information
204+
205+
**Global Options**:
206+
- `-c, --config <FILE>` - Configuration file path
207+
- `-b, --bind-address <ADDR>` - Override bind address
208+
- `-p, --port <PORT>` - Override listen port
209+
- `-k, --host-key <FILE>` - Host key file(s) (can be repeated)
210+
- `-v, --verbose` - Verbosity level (repeatable: -v, -vv, -vvv)
211+
- `-D, --foreground` - Run in foreground (don't daemonize)
212+
- `--pid-file <FILE>` - PID file path
213+
214+
**Usage Examples**:
215+
```bash
216+
# Generate configuration template
217+
bssh-server gen-config -o /etc/bssh/server.yaml
218+
219+
# Generate Ed25519 host key (recommended)
220+
bssh-server gen-host-key -t ed25519 -o /etc/bssh/ssh_host_ed25519_key
221+
222+
# Generate RSA host key (for compatibility)
223+
bssh-server gen-host-key -t rsa -o /etc/bssh/ssh_host_rsa_key --bits 4096
224+
225+
# Hash a password for configuration
226+
bssh-server hash-password
227+
228+
# Validate configuration
229+
bssh-server check-config -c /etc/bssh/server.yaml
230+
231+
# Start server with configuration file
232+
bssh-server -c /etc/bssh/server.yaml
233+
234+
# Start server with CLI overrides
235+
bssh-server -c /etc/bssh/server.yaml -p 2222 -b 0.0.0.0 -k /path/to/key
236+
```
237+
192238
### SSH Server Module
193239
**Documentation**: [docs/architecture/server-configuration.md](./docs/architecture/server-configuration.md)
194240

Cargo.lock

Lines changed: 74 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ tokio-util = "0.7.17"
5454
shell-words = "1.1.1"
5555
libc = "0.2"
5656
ipnetwork = "0.20"
57+
bcrypt = "0.16"
58+
rand = "0.8"
59+
ssh-key = { version = "0.6", features = ["std"] }
5760

5861
[target.'cfg(target_os = "macos")'.dependencies]
5962
security-framework = "3.5.1"
@@ -72,3 +75,7 @@ mockall = "0.14"
7275
name = "large_output_benchmark"
7376
harness = false
7477

78+
[[bin]]
79+
name = "bssh-server"
80+
path = "src/bin/bssh_server.rs"
81+

docs/architecture/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ bssh is a high-performance parallel SSH command execution tool with SSH-compatib
3333
### Server Components
3434

3535
- **[Server Configuration](./server-configuration.md)** - YAML-based server configuration, environment overrides, validation
36+
- **Server CLI (`bssh-server`)** - Server management commands including host key generation, password hashing, config validation (see main ARCHITECTURE.md)
3637
- **SSH Server Module** - SSH server implementation using russh (see main ARCHITECTURE.md)
3738
- **Server Authentication** - Authentication providers including public key verification (see main ARCHITECTURE.md)
3839

@@ -59,6 +60,7 @@ Each component document includes:
5960
- **CLI options and modes**[CLI Interface](./cli-interface.md)
6061
- **Client configuration file format**[Configuration Management](./configuration.md)
6162
- **Server configuration file format**[Server Configuration](./server-configuration.md)
63+
- **Server CLI commands** → Main ARCHITECTURE.md (Server CLI Binary section)
6264
- **Parallel execution behavior**[Parallel Executor](./executor.md)
6365
- **SSH connection details**[SSH Client](./ssh-client.md)
6466
- **Interactive terminal usage**[TUI](./tui.md) or [Interactive Mode](./interactive-mode.md)
@@ -70,6 +72,8 @@ Each component document includes:
7072

7173
```
7274
src/
75+
├── bin/
76+
│ └── bssh_server.rs → Server CLI Binary (bssh-server)
7377
├── cli/ → CLI Interface
7478
├── config/ → Configuration Management
7579
├── executor/ → Parallel Executor

docs/architecture/server-configuration.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,9 +335,70 @@ export BSSH_AUTH_METHODS=publickey,password
335335
bssh-server
336336
```
337337

338+
## Server CLI Commands
339+
340+
The `bssh-server` binary provides several management commands:
341+
342+
### Generate Configuration Template
343+
344+
```bash
345+
# Output to stdout
346+
bssh-server gen-config
347+
348+
# Write to file with secure permissions (0600)
349+
bssh-server gen-config -o /etc/bssh/server.yaml
350+
```
351+
352+
### Generate Host Keys
353+
354+
```bash
355+
# Generate Ed25519 key (recommended, fast, secure)
356+
bssh-server gen-host-key -t ed25519 -o /etc/bssh/ssh_host_ed25519_key
357+
358+
# Generate RSA key with custom size
359+
bssh-server gen-host-key -t rsa -o /etc/bssh/ssh_host_rsa_key --bits 4096
360+
```
361+
362+
Generated keys have secure permissions (0600) and are in OpenSSH format.
363+
364+
### Hash Passwords
365+
366+
```bash
367+
# Interactive password hashing with bcrypt
368+
bssh-server hash-password
369+
```
370+
371+
This prompts for a password, confirms it, and outputs a bcrypt hash suitable for use in the configuration file.
372+
373+
### Validate Configuration
374+
375+
```bash
376+
# Check default config locations
377+
bssh-server check-config
378+
379+
# Check specific config file
380+
bssh-server check-config -c /etc/bssh/server.yaml
381+
```
382+
383+
Displays all configuration settings and validates the file format.
384+
385+
### Start Server
386+
387+
```bash
388+
# Start with config file
389+
bssh-server -c /etc/bssh/server.yaml
390+
391+
# Start with CLI overrides
392+
bssh-server -c /etc/bssh/server.yaml -p 2222 -b 0.0.0.0
393+
394+
# Run in foreground with verbose logging
395+
bssh-server -c /etc/bssh/server.yaml -D -vvv
396+
```
397+
338398
---
339399

340400
**Related Documentation:**
401+
- [Server CLI Binary](../../ARCHITECTURE.md#server-cli-binary)
341402
- [SSH Server Module](../../ARCHITECTURE.md#ssh-server-module)
342403
- [Server Authentication](../../ARCHITECTURE.md#server-authentication-module)
343404
- [Client Configuration Management](./configuration.md)

0 commit comments

Comments
 (0)