Go SDK
Go SDK
Section titled “Go SDK”Official Go SDK for SSH-KLM.
Installation
Section titled “Installation”go get github.com/qcecuring/ssh-klm-goRequirements
Section titled “Requirements”- Go 1.21 or later
Quick Start
Section titled “Quick Start”package main
import ( "context" "fmt" "log"
sshklm "github.com/qcecuring/ssh-klm-go")
func main() { client := sshklm.NewClient("sk_live_...")
hosts, err := client.SSH.ListHosts(context.Background(), nil) if err != nil { log.Fatal(err) }
for _, host := range hosts { fmt.Printf("%s: %s\n", host.Hostname, host.Status) }}Configuration
Section titled “Configuration”client := sshklm.NewClient( "sk_live_...", sshklm.WithBaseURL("https://api.qcecuring.com"), sshklm.WithTimeout(30 * time.Second), sshklm.WithRetries(3),)Common Operations
Section titled “Common Operations”Discovery
Section titled “Discovery”ctx := context.Background()
// Start discoveryscan, err := client.SSH.StartDiscovery(ctx, &sshklm.DiscoveryRequest{ Hosts: []string{"server01.example.com"}, ScanType: "full",})if err != nil { log.Fatal(err)}
// Wait for completionresult, err := client.SSH.WaitForScan(ctx, scan.ID, 5*time.Minute)if err != nil { log.Fatal(err)}
fmt.Printf("Discovered %d keys\n", result.KeysFound)Rotation
Section titled “Rotation”rotation, err := client.SSH.RotateKey(ctx, &sshklm.RotateKeyRequest{ KeyID: "key_123", Algorithm: "ed25519", Reason: "Scheduled rotation",})if err != nil { log.Fatal(err)}Ephemeral Access
Section titled “Ephemeral Access”creds, err := client.SSH.RequestEphemeral(ctx, &sshklm.EphemeralRequest{ Host: "server.example.com", Username: "deploy", TTL: 300, Reason: "Deployment",})if err != nil { log.Fatal(err)}
fmt.Println(creds.PrivateKey)Error Handling
Section titled “Error Handling”import "github.com/qcecuring/ssh-klm-go/errors"
_, err := client.SSH.RotateKey(ctx, &sshklm.RotateKeyRequest{ KeyID: "invalid",})
if err != nil { switch e := err.(type) { case *errors.NotFoundError: fmt.Println("Key not found") case *errors.RateLimitError: fmt.Printf("Rate limited, retry after %ds\n", e.RetryAfter) case *errors.QcError: fmt.Printf("Error: %s - %s\n", e.Code, e.Message) default: log.Fatal(err) }}Pagination
Section titled “Pagination”var allHosts []sshklm.Hostopts := &sshklm.ListHostsOptions{Limit: 100}
for { hosts, err := client.SSH.ListHosts(ctx, opts) if err != nil { log.Fatal(err) }
allHosts = append(allHosts, hosts.Data...)
if !hosts.HasMore { break } opts.Offset = hosts.Offset + hosts.Limit}