Building a Command Line Tool for SSH Key Management with F#
Managing SSH keys by hand becomes painful when a consultant, administrator, or hobbyist works across a laptop, several Linux servers, and a Raspberry Pi. Public keys drift into old accounts, comments stop matching reality, and an emergency revocation turns into a search through shell history. A focused command-line utility can make those operations predictable while leaving OpenSSH responsible for cryptographic behaviour.
F# suits this combination of domain rules, operating-system integration, and text-heavy automation. Its discriminated unions and immutable records help express invalid states clearly, while .NET provides process, networking, and cross-platform libraries. The result can feel like a native Unix tool on a Brisbane workstation and still run cleanly on a Windows server managed by an Australian service provider.
Start with the key lifecycle
A useful SSH key management tool begins with a lifecycle rather than a collection of shell scripts. Key generation, fingerprint inspection, installation, rotation, retirement, and revocation each need explicit inputs and outcomes. A model such as Generate, Inspect, Install, Rotate, and Revoke makes the supported operations visible in the code and in the command interface.
Private keys should remain private throughout that lifecycle. The tool can invoke ssh-keygen for generation and use ssh-keygen -lf for fingerprinting, but it should not implement cryptographic primitives itself. Public keys can be stored in an inventory; private key material belongs in a protected location or, preferably, an SSH agent.
For Australian deployments, that inventory can support governance as well as convenience. The Privacy Act 1988 and Australian Privacy Principles apply when systems hold personal information, while the ASD Essential Eight offers a useful control benchmark. SSH tooling cannot satisfy either framework by itself, but least privilege, prompt revocation, auditable changes, and restricted key custody support the controls an organisation must implement.
Model key operations in F#
F# makes illegal states awkward to represent accidentally. A key might be assigned an Admin, Automation, or Recovery purpose, while its lifecycle state can be Active, Retiring, or Revoked. Combining those dimensions in records gives the rest of the program a clear vocabulary without relying on loosely related strings.
Command handling can use discriminated unions with explicit results. Validation failures, missing hosts, unknown fingerprints, and subprocess errors should remain distinguishable rather than collapsing into a generic exception. An F# Result type works well for expected operational failures, with exceptions reserved for genuinely exceptional conditions.
The core domain should remain independent of the operating system. Process invocation, filesystem access, the clock, and configuration can sit behind small interfaces or modules. That separation makes the rules testable and prevents an OpenSSH error message from leaking into every layer of the application.
Design commands for people and scripts
The command vocabulary should be compact enough to remember in an incident. Operations such as sshkey list, sshkey inspect, sshkey plan, sshkey rotate, and sshkey revoke follow common command-line conventions. Options should expose the details an administrator needs without requiring a configuration file for every routine task.
Human-readable output and machine-readable output need different shapes. The default view can show a fingerprint, purpose, comment, source, and last observed use, while --json provides stable fields for scripts and inventory systems. Predictable exit codes also matter: zero for success, one for an operational failure, and two for invalid usage.
Command names and error messages form a small culture, much as Nordic folklore in fantasy shows how inherited motifs become shared language. Memorable wording is useful only when its meaning stays exact, so prompts should identify the affected key and host before requesting confirmation.
Protect local key material
OpenSSH already handles key formats, passphrases, and private-key permissions correctly on supported platforms. The management tool should delegate those jobs rather than creating parallel cryptographic code. On macOS and Linux, private keys commonly live under ~/.ssh; on Windows, equivalent paths and access rules are required.
The tool can maintain a separate public-key inventory without storing passphrases or private key contents. Configuration updates should use atomic replacement: write a temporary file, apply restrictive permissions, and rename it into place. New files should receive mode 0600, with directories limited to the current user where the platform supports POSIX permissions.
Windows requires equivalent protection through access-control lists rather than Unix modes. Race conditions, symlink attacks, and ownership changes should all be considered before an existing file is replaced. Even metadata such as hostnames and key comments can reveal useful information, so inventory files deserve the same care as other operational records.
Apply remote changes carefully
Installing an authorised key remotely is more sensitive than appending a line to a local file. The tool should verify the destination host key against known_hosts, create a timestamped backup, preserve unrelated entries, and apply the required ownership and permissions. A dry-run mode should show the exact proposed change without opening a write session.
A home-lab Raspberry Pi reached over an Australian NBN connection may sit behind a dynamic address and household router. A Raspberry Pi WireGuard setup can provide a stable private path, allowing the tool to restrict a key to that network rather than trusting every public address. Source restrictions remain useful even when access is limited to a jump host.
Updates must also resist command injection and accidental duplication. Arguments should be passed directly to ssh, scp, or sftp rather than interpolated into a shell command. A remote lock or compare-and-swap mechanism can prevent two administrators from overwriting each other, while idempotent operations make retries safer after a dropped connection.
Test against messy reality
Unit tests should cover fingerprint validation, lifecycle transitions, command parsing, and configuration migration without touching a real server. Property-based tests can generate sequences of install and revoke operations and confirm that authorised-key entries are neither lost nor duplicated. They also expose assumptions about ordering and concurrency that ordinary examples may miss.
Integration tests need an actual OpenSSH implementation because successful argument handling does not guarantee correct remote behaviour. Containers or disposable virtual machines can test authentication, permission failures, interrupted transfers, and unusual known_hosts states. A fake SSH executable is useful for deterministic error-path tests, but it should supplement rather than replace real protocol testing.
Australian IT teams frequently manage mixed estates containing macOS, Windows, and Linux systems. The test matrix should include path separators, local conventions, time zones, line endings, and differing OpenSSH versions. Running those tests in continuous integration prevents a tool that works on a developer’s Melbourne laptop from failing on a customer’s Windows server.
Ship and invite contributors
Packaging determines whether the tool survives beyond its author. A .NET global tool provides a straightforward installation path, while self-contained builds can reduce runtime dependencies on locked-down servers. Signed binaries, reproducible build information, and an explicit version command make later support easier, particularly for small consultancies managing several client environments.
Operational logs should record actions, affected fingerprints, hosts, timestamps, and outcomes without capturing private keys or passphrases. Telemetry should be optional and limited to information that provides clear value. This matters in the Australian market, where AWS, Microsoft, and local managed-service providers all support customers with strict data-residency and privacy expectations.
Build the first vertical slice today: implement generate, list, and dry-run, exercise them against a disposable test host, and enable remote installation only after rollback and verification are proven.
Karl Katzke