Skip to content

Commit 1fee7b8

Browse files
committed
feat(secretv2): add secret v2 implementation
1 parent 43bb17e commit 1fee7b8

File tree

7 files changed

+445
-9
lines changed

7 files changed

+445
-9
lines changed

cmd/okms/main.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"github.com/ovh/okms-cli/cmd/okms/configure"
99
"github.com/ovh/okms-cli/cmd/okms/keys"
1010
"github.com/ovh/okms-cli/cmd/okms/kmip"
11+
"github.com/ovh/okms-cli/cmd/okms/secrets"
12+
secretsv2 "github.com/ovh/okms-cli/cmd/okms/secretsV2"
1113

1214
"github.com/ovh/okms-cli/cmd/okms/x509"
1315
"github.com/ovh/okms-cli/common/commands"
@@ -36,7 +38,8 @@ func createRootCommand() *cobra.Command {
3638
command.AddCommand(
3739
// rnd.CreateCommand(nil),
3840
keys.CreateCommand(nil),
39-
// secrets.CreateCommand(nil),
41+
secrets.CreateCommand(nil),
42+
secretsv2.CreateCommand(nil),
4043
x509.CreateX509Command(nil),
4144
kmip.NewCommand(nil),
4245
configure.CreateCommand(),

cmd/okms/secrets/root.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77

88
func CreateCommand(cust common.CustomizeFunc) *cobra.Command {
99
var kvCmd = &cobra.Command{
10-
Use: "secrets",
11-
Aliases: []string{"kv", "secret"},
10+
Use: "vault",
11+
Aliases: []string{"kv"},
1212
Short: "This command has subcommands for interacting with KMS's key-value store.",
1313
}
1414

cmd/okms/secretsV2/config.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
}

cmd/okms/secretsV2/root.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package secretsv2
2+
3+
import (
4+
"github.com/ovh/okms-cli/cmd/okms/common"
5+
"github.com/spf13/cobra"
6+
)
7+
8+
func CreateCommand(cust common.CustomizeFunc) *cobra.Command {
9+
var kvCmd = &cobra.Command{
10+
Use: "secrets",
11+
Aliases: []string{"kv2", "secret"}, // TODO discuss keywords to use for better UX
12+
Short: "This command has subcommands for interacting with KMS's key-value store.",
13+
}
14+
15+
common.SetupRestApiFlags(kvCmd, cust)
16+
17+
kvCmd.AddCommand(
18+
secretConfigCommand(),
19+
secretListCmd(),
20+
secretPostCmd(),
21+
secretGetCmd(),
22+
secretPutCmd(),
23+
secretDeleteCmd(),
24+
)
25+
26+
return kvCmd
27+
}

0 commit comments

Comments
 (0)