|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "encoding/pem" |
| 6 | + "fmt" |
| 7 | + |
| 8 | + "github.com/spf13/cobra" |
| 9 | + |
| 10 | + "go.step.sm/crypto/kms/apiv1" |
| 11 | + "go.step.sm/crypto/pemutil" |
| 12 | + |
| 13 | + "github.com/smallstep/step-kms-plugin/internal/flagutil" |
| 14 | +) |
| 15 | + |
| 16 | +var searchCmd = &cobra.Command{ |
| 17 | + Use: "search <kms-uri>", |
| 18 | + Short: "search for keys stored in a KMS and list their names or details", |
| 19 | + Long: `Search for one or more keys stored in a Key Management System (KMS). |
| 20 | +
|
| 21 | +OUTPUT FORMATS: |
| 22 | + By default, this command prints a simple list of key names (URIs), one per line. |
| 23 | + Use the --json flag to output detailed information in JSON format, including |
| 24 | + each key's name and corresponding public key in PEM format. |
| 25 | +
|
| 26 | +SUPPORTED KMS TYPES: |
| 27 | + - mackms: macOS Keychain and Secure Enclave (Secure Enclave requires signed binaries) |
| 28 | + - tpmkms: Trusted Platform Module (TPM) 2.0 |
| 29 | +
|
| 30 | +URI SYNTAX: |
| 31 | + The <kms-uri> format is: <kms-type>:[query-params] |
| 32 | +
|
| 33 | + Query parameters are optional and allow you to filter search results. |
| 34 | + Multiple parameters can be combined using standard URI query syntax. |
| 35 | +
|
| 36 | +QUERY PARAMETERS: |
| 37 | +
|
| 38 | + TPM (tpmkms:) |
| 39 | + - ak=true Search only attestation keys (AKs) |
| 40 | + - ak=false Search only application keys (non-AKs) |
| 41 | + - name=<name> Search for a key with the specified name |
| 42 | + - (no param) Search all keys (both AKs and application keys) |
| 43 | +
|
| 44 | + macOS Keychain and Secure Enclave (mackms:) |
| 45 | + - label=<name> Search keys with the specified label/name |
| 46 | + - hash=<hex> Search keys with the given hash (hexadecimal format) |
| 47 | + - tag=<tag> Search keys with the given tag (default: com.smallstep.crypto) |
| 48 | + - se=true Search only keys on the Secure Enclave (requires signed binary) |
| 49 | + - (no param) Search all keys on both Keychain and Secure Enclave`, |
| 50 | + Example: ` # Search all keys on macOS Keychain/Secure Enclave: |
| 51 | + step-kms-plugin search mackms: |
| 52 | +
|
| 53 | + # Search all keys with a custom tag: |
| 54 | + step-kms-plugin search mackms:tag=com.example.crypto |
| 55 | +
|
| 56 | + # Search all keys (application and attestation keys) on a TPM: |
| 57 | + step-kms-plugin search tpmkms: |
| 58 | +
|
| 59 | + # Search all keys on a TPM with a custom storage directory: |
| 60 | + step-kms-plugin search tpmkms:storage-directory=/tmp/tpmobjects |
| 61 | +
|
| 62 | + # Search only attestation keys (AKs) on a TPM: |
| 63 | + step-kms-plugin search tpmkms:ak=true |
| 64 | +
|
| 65 | + # Search only application keys (non-AKs) on a TPM: |
| 66 | + step-kms-plugin search tpmkms:ak=false |
| 67 | +
|
| 68 | + # Search TPM keys and output details in JSON format with public keys: |
| 69 | + step-kms-plugin search --json tpmkms:ak=false`, |
| 70 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 71 | + if len(args) != 1 { |
| 72 | + return showErrUsage(cmd) |
| 73 | + } |
| 74 | + |
| 75 | + flags := cmd.Flags() |
| 76 | + |
| 77 | + kuri, name, err := getURIAndNameForFS(flagutil.MustString(flags, "kms"), args[0]) |
| 78 | + if err != nil { |
| 79 | + return err |
| 80 | + } |
| 81 | + |
| 82 | + km, err := openKMS(cmd.Context(), kuri) |
| 83 | + if err != nil { |
| 84 | + return fmt.Errorf("failed to load key manager: %w", err) |
| 85 | + } |
| 86 | + defer km.Close() |
| 87 | + |
| 88 | + searchKMS, ok := km.(interface { |
| 89 | + SearchKeys(*apiv1.SearchKeysRequest) (*apiv1.SearchKeysResponse, error) |
| 90 | + }) |
| 91 | + if !ok { |
| 92 | + return fmt.Errorf("the KMS does not implement the SearchKeys method") |
| 93 | + } |
| 94 | + |
| 95 | + results, err := searchKMS.SearchKeys(&apiv1.SearchKeysRequest{ |
| 96 | + Query: name, |
| 97 | + }) |
| 98 | + if err != nil { |
| 99 | + return err |
| 100 | + } |
| 101 | + |
| 102 | + if flagutil.MustBool(flags, "json") { |
| 103 | + keys := []map[string]any{} |
| 104 | + for _, r := range results.Results { |
| 105 | + block, err := pemutil.Serialize(r.PublicKey) |
| 106 | + if err != nil { |
| 107 | + return fmt.Errorf("failed to serialize the public key: %w", err) |
| 108 | + } |
| 109 | + |
| 110 | + keys = append(keys, map[string]any{ |
| 111 | + "name": r.Name, |
| 112 | + "publicKey": string(pem.EncodeToMemory(block)), |
| 113 | + }) |
| 114 | + } |
| 115 | + |
| 116 | + b, err := json.MarshalIndent(map[string]any{ |
| 117 | + "keys": keys, |
| 118 | + }, "", " ") |
| 119 | + if err != nil { |
| 120 | + return fmt.Errorf("failed to marshal: %w", err) |
| 121 | + } |
| 122 | + fmt.Println(string(b)) |
| 123 | + } else { |
| 124 | + for _, r := range results.Results { |
| 125 | + fmt.Println(r.Name) |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + return nil |
| 130 | + }, |
| 131 | +} |
| 132 | + |
| 133 | +func init() { |
| 134 | + rootCmd.AddCommand(searchCmd) |
| 135 | + searchCmd.SilenceUsage = true |
| 136 | + |
| 137 | + flags := searchCmd.Flags() |
| 138 | + flags.SortFlags = false |
| 139 | + flags.Bool("json", false, "Show the output using JSON") |
| 140 | +} |
0 commit comments