From 932daeafe1b7bf0f6e4fe64d22964d3fb6c081f1 Mon Sep 17 00:00:00 2001 From: "David J. Allen" Date: Wed, 19 Mar 2025 11:10:26 -0600 Subject: [PATCH] refactor: added func to remove secrets from store --- cmd/secrets.go | 18 ++++++++++++++++++ pkg/secrets/localstore.go | 5 +++++ pkg/secrets/main.go | 1 + 3 files changed, 24 insertions(+) diff --git a/cmd/secrets.go b/cmd/secrets.go index 581c332..ab838a2 100644 --- a/cmd/secrets.go +++ b/cmd/secrets.go @@ -197,6 +197,23 @@ var secretsListCmd = &cobra.Command{ }, } +var secretsRemoveCmd = &cobra.Command{ + Use: "remove secretIDs...", + Args: cobra.MinimumNArgs(2), + Short: "Remove secrets by IDs from secret store.", + Run: func(cmd *cobra.Command, args []string) { + for _, secretID := range args { + store, err := secrets.OpenStore(secretsFile) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + + store.RemoveSecretByID(secretID) + } + }, +} + func init() { secretsCmd.Flags().StringVarP(&secretsFile, "file", "f", "nodes.json", "") secretsStoreCmd.Flags().StringVar(&secretsStoreFormat, "format", "json", "set the input format for the secrets file (json|base64)") @@ -206,6 +223,7 @@ func init() { secretsCmd.AddCommand(secretsStoreCmd) secretsCmd.AddCommand(secretsRetrieveCmd) secretsCmd.AddCommand(secretsListCmd) + secretsCmd.AddCommand(secretsRemoveCmd) rootCmd.AddCommand(secretsCmd) diff --git a/pkg/secrets/localstore.go b/pkg/secrets/localstore.go index 1cf862a..e87a7cc 100644 --- a/pkg/secrets/localstore.go +++ b/pkg/secrets/localstore.go @@ -101,6 +101,11 @@ func (l *LocalSecretStore) ListSecrets() (map[string]string, error) { return secretsCopy, nil } +// RemoveSecretByID removes the specified secretID stored locally +func (l *LocalSecretStore) RemoveSecretByID(secretID string) { + delete(l.Secrets, secretID) +} + // openStore tries to create or open the LocalSecretStore based on the environment // variable MASTER_KEY. If not found, it prints an error. func OpenStore(filename string) (SecretStore, error) { diff --git a/pkg/secrets/main.go b/pkg/secrets/main.go index 5925d53..6d27976 100644 --- a/pkg/secrets/main.go +++ b/pkg/secrets/main.go @@ -4,4 +4,5 @@ type SecretStore interface { GetSecretByID(secretID string) (string, error) StoreSecretByID(secretID, secret string) error ListSecrets() (map[string]string, error) + RemoveSecretByID(secretID string) }