Skip to content

Commit

Permalink
feat: display only secret keys (#151)
Browse files Browse the repository at this point in the history
resolves #131
  • Loading branch information
tim-goto authored Aug 23, 2024
1 parent 54db519 commit 87d5079
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ Medusa import will take a [vault path] with [flags]

```
Flags:
--display-keys-only Display only keys of secrets but not their values
-e, --encrypt Encrypt the exported Vault data
-m, --engine-type string Specify the secret engine type [kv1|kv2] (default "kv2")
-f, --format string Specify the export format [yaml|json] (default "yaml")
Expand Down
25 changes: 25 additions & 0 deletions cmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func init() {
exportCmd.PersistentFlags().BoolP("encrypt", "e", false, "Encrypt the exported Vault data")
exportCmd.PersistentFlags().StringP("public-key", "p", "", "Location of the RSA public key")
exportCmd.PersistentFlags().StringP("engine-type", "m", "kv2", "Specify the secret engine type [kv1|kv2]")
exportCmd.PersistentFlags().BoolP("display-keys-only", "", false, "Display only keys of secrets but not their values")
}

var exportCmd = &cobra.Command{
Expand All @@ -37,6 +38,7 @@ var exportCmd = &cobra.Command{
doEncrypt, _ := cmd.Flags().GetBool("encrypt")
exportFormat, _ := cmd.Flags().GetString("format")
output, _ := cmd.Flags().GetString("output")
keysOnly, _ := cmd.Flags().GetBool("display-keys-only")

client := vaultengine.NewClient(vaultAddr, vaultToken, insecure, namespace, vaultRole, kubernetes, authPath)
engine, path, err := client.MountpathSplitPrefix(path)
Expand All @@ -54,6 +56,13 @@ var exportCmd = &cobra.Command{
return err
}

if keysOnly {
err = removeValues(exportData)
if err != nil {
return err
}
}

// Convert export to json or yaml
var data []byte
switch exportFormat {
Expand Down Expand Up @@ -113,3 +122,19 @@ var exportCmd = &cobra.Command{
return nil
},
}

func removeValues(exportData vaultengine.Folder) error {
for k, v := range exportData {
switch r := v.(type) {
case vaultengine.Folder:
removeValues(r)
case map[string]interface{}:
removeValues(r)
case string:
exportData[k] = "********"
default:
return errors.New(fmt.Sprintf("Unknown type %T", r))
}
}
return nil
}

0 comments on commit 87d5079

Please sign in to comment.