|
| 1 | +package secretsv2 |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + |
| 7 | + "github.com/olekukonko/tablewriter" |
| 8 | + "github.com/ovh/okms-cli/cmd/okms/common" |
| 9 | + "github.com/ovh/okms-cli/common/flagsmgmt" |
| 10 | + "github.com/ovh/okms-cli/common/output" |
| 11 | + "github.com/ovh/okms-cli/common/utils" |
| 12 | + "github.com/ovh/okms-cli/common/utils/exit" |
| 13 | + "github.com/ovh/okms-sdk-go/types" |
| 14 | + "github.com/spf13/cobra" |
| 15 | +) |
| 16 | + |
| 17 | +func secretConfigCommand() *cobra.Command { |
| 18 | + cmd := &cobra.Command{ |
| 19 | + Use: "config", |
| 20 | + Short: "Manages secret engine configuration", |
| 21 | + } |
| 22 | + |
| 23 | + cmd.AddCommand( |
| 24 | + secretGetConfigCommand(), |
| 25 | + secretUpdateConfigCommand(), |
| 26 | + ) |
| 27 | + return cmd |
| 28 | +} |
| 29 | + |
| 30 | +func secretGetConfigCommand() *cobra.Command { |
| 31 | + return &cobra.Command{ |
| 32 | + Use: "get", |
| 33 | + Short: "Retrieve secrets configuration", |
| 34 | + Args: cobra.NoArgs, |
| 35 | + Run: func(cmd *cobra.Command, args []string) { |
| 36 | + resp := exit.OnErr2(common.Client().GetSecretConfigV2(cmd.Context())) |
| 37 | + if cmd.Flag("output").Value.String() == string(flagsmgmt.JSON_OUTPUT_FORMAT) { |
| 38 | + output.JsonPrint(resp) |
| 39 | + } else { |
| 40 | + table := tablewriter.NewWriter(os.Stdout) |
| 41 | + table.AppendBulk([][]string{ |
| 42 | + {"cas", fmt.Sprintf("%t", utils.DerefOrDefault(resp.CasRequired))}, |
| 43 | + {"Deactivate version after", utils.DerefOrDefault(resp.DeactivateVersionAfter)}, |
| 44 | + {"Max. number of versions", fmt.Sprintf("%d", utils.DerefOrDefault(resp.MaxVersions))}, |
| 45 | + }) |
| 46 | + table.Render() |
| 47 | + } |
| 48 | + }, |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +func secretUpdateConfigCommand() *cobra.Command { |
| 53 | + var ( |
| 54 | + casRequired bool |
| 55 | + maxVersions uint32 |
| 56 | + deleteVersionAfter string |
| 57 | + ) |
| 58 | + |
| 59 | + cmd := &cobra.Command{ |
| 60 | + Use: "update", |
| 61 | + Short: "Update secrets configuration", |
| 62 | + Args: cobra.MinimumNArgs(1), |
| 63 | + Run: func(cmd *cobra.Command, args []string) { |
| 64 | + var c *bool |
| 65 | + if cmd.Flag("cas-required").Changed { |
| 66 | + c = &casRequired |
| 67 | + } |
| 68 | + |
| 69 | + var d *string |
| 70 | + if cmd.Flag("delete-after").Changed { |
| 71 | + d = &deleteVersionAfter |
| 72 | + } |
| 73 | + |
| 74 | + var m *uint32 |
| 75 | + if cmd.Flag("max-versions").Changed { |
| 76 | + m = &maxVersions |
| 77 | + } |
| 78 | + |
| 79 | + body := types.PostConfigRequest{ |
| 80 | + CasRequired: c, |
| 81 | + DeleteVersionAfter: d, |
| 82 | + MaxVersions: m, |
| 83 | + } |
| 84 | + |
| 85 | + exit.OnErr(common.Client().PostSecretConfig(cmd.Context(), body)) |
| 86 | + }, |
| 87 | + } |
| 88 | + |
| 89 | + cmd.Flags().BoolVar(&casRequired, "cas-required", false, "If true all keys will require the cas parameter to be set on all write requests.") |
| 90 | + cmd.Flags().Uint32Var(&maxVersions, "max-versions", 0, "The number of versions to keep per key. This value applies to all keys, but a key's metadata setting can overwrite this value. Once a key has more than the configured allowed versions, the oldest version will be permanently deleted. ") |
| 91 | + cmd.Flags().StringVar(&deleteVersionAfter, "delete-after", "0s", "If set, specifies the length of time before a version is deleted.\nDate format, see: https://developer.hashicorp.com/vault/docs/concepts/duration-format") |
| 92 | + return cmd |
| 93 | +} |
0 commit comments