Skip to content

Commit 43bb17e

Browse files
authored
fix: fix secrets commands (#53)
1 parent bd5ccc5 commit 43bb17e

File tree

10 files changed

+57
-83
lines changed

10 files changed

+57
-83
lines changed

.github/workflows/test.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ jobs:
2121
run: |
2222
echo "${{secrets.CERTIFICATE}}" > tls.crt
2323
echo "${{secrets.PRIVATE_KEY}}" > tls.key
24-
2524
cat > okms.yaml <<-EOF
2625
version: 1
2726
profile: default
@@ -59,4 +58,3 @@ jobs:
5958
./tests/out/coverage.txt
6059
./tests/out/coverage.html
6160
retention-days: 5
62-

cmd/okms/keys/keys.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ func newListServiceKeysCmd() *cobra.Command {
4343
}
4444
// Let's list all the keys by putting them all in memory. The memory is not an issue, unless a domain has hundreds of thousands of keys
4545
// Filter keys by activation state
46-
stateFilter := types.Active
46+
stateFilter := types.KeyStatesActive
4747
if listAll {
48-
stateFilter = types.All
48+
stateFilter = types.KeyStatesAll
4949
}
5050
for key, err := range common.Client().ListAllServiceKeys(&keysPageSize, &stateFilter).Iter(cmd.Context()) {
5151
exit.OnErr(err)

cmd/okms/secrets/config.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
//go:build unstable
2-
31
package secrets
42

53
import (
@@ -54,7 +52,7 @@ func kvReadConfigCommand() *cobra.Command {
5452
func kvWriteConfigCommand() *cobra.Command {
5553
var (
5654
casRequired bool
57-
maxVersions int32
55+
maxVersions uint32
5856
deleteVersionAfter string
5957
)
6058

@@ -73,7 +71,7 @@ func kvWriteConfigCommand() *cobra.Command {
7371
d = &deleteVersionAfter
7472
}
7573

76-
var m *int32
74+
var m *uint32
7775
if cmd.Flag("max-versions").Changed {
7876
m = &maxVersions
7977
}
@@ -89,7 +87,7 @@ func kvWriteConfigCommand() *cobra.Command {
8987
}
9088

9189
cmd.Flags().BoolVar(&casRequired, "cas-required", false, "If true all keys will require the cas parameter to be set on all write requests.")
92-
cmd.Flags().Int32Var(&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. ")
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. ")
9391
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")
9492
return cmd
9593
}

cmd/okms/secrets/metadata.go

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
//go:build unstable
2-
31
package secrets
42

53
import (
@@ -112,7 +110,7 @@ func kvGetMetadataCommand() *cobra.Command {
112110
func kvPutMetadataCommand() *cobra.Command {
113111
var (
114112
casRequired bool
115-
maxVersions int32
113+
maxVersions uint32
116114
deleteVersionAfter string
117115
customMetadata map[string]string
118116
)
@@ -132,34 +130,24 @@ func kvPutMetadataCommand() *cobra.Command {
132130
d = &deleteVersionAfter
133131
}
134132

135-
var m *int32
133+
var m *uint32
136134
if cmd.Flag("max-versions").Changed {
137135
m = &maxVersions
138136
}
139137

140-
var cm *map[string]interface{}
141-
if len(customMetadata) > 0 {
142-
tmp := make(map[string]interface{})
143-
cm = &tmp
144-
for k, v := range customMetadata {
145-
(*cm)[k] = v
146-
}
147-
}
148-
149138
body := types.SecretUpdatableMetadata{
150139
CasRequired: c,
151140
DeleteVersionAfter: d,
152141
MaxVersions: m,
153-
CustomMetadata: new(any),
142+
CustomMetadata: &customMetadata,
154143
}
155-
*body.CustomMetadata = cm
156144

157145
exit.OnErr(common.Client().PostSecretMetadata(cmd.Context(), args[0], body))
158146
},
159147
}
160148

161149
cmd.Flags().BoolVar(&casRequired, "cas-required", false, "If true all keys will require the cas parameter to be set on all write requests.")
162-
cmd.Flags().Int32Var(&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. ")
150+
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. ")
163151
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")
164152
cmd.Flags().StringToStringVar(&customMetadata, "custom-metadata", map[string]string{}, "Specifies arbitrary version-agnostic key=value metadata meant to describe a secret.\nThis can be specified multiple times to add multiple pieces of metadata.")
165153
return cmd
@@ -168,7 +156,7 @@ func kvPutMetadataCommand() *cobra.Command {
168156
func kvPatchMetadataCommand() *cobra.Command {
169157
var (
170158
casRequired bool
171-
maxVersions int32
159+
maxVersions uint32
172160
deleteVersionAfter string
173161
customMetadata map[string]string
174162
)
@@ -188,34 +176,24 @@ func kvPatchMetadataCommand() *cobra.Command {
188176
d = &deleteVersionAfter
189177
}
190178

191-
var m *int32
179+
var m *uint32
192180
if cmd.Flag("max-versions").Changed {
193181
m = &maxVersions
194182
}
195183

196-
var cm *map[string]interface{}
197-
if len(customMetadata) > 0 {
198-
tmp := make(map[string]interface{})
199-
cm = &tmp
200-
for k, v := range customMetadata {
201-
(*cm)[k] = v
202-
}
203-
}
204-
205184
body := types.SecretUpdatableMetadata{
206185
CasRequired: c,
207186
DeleteVersionAfter: d,
208187
MaxVersions: m,
209-
CustomMetadata: new(any),
188+
CustomMetadata: &customMetadata,
210189
}
211-
*body.CustomMetadata = cm
212190

213191
exit.OnErr(common.Client().PatchSecretMetadata(cmd.Context(), args[0], body))
214192
},
215193
}
216194

217195
cmd.Flags().BoolVar(&casRequired, "cas-required", false, "If true all keys will require the cas parameter to be set on all write requests.")
218-
cmd.Flags().Int32Var(&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. ")
196+
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. ")
219197
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")
220198
cmd.Flags().StringToStringVar(&customMetadata, "custom-metadata", map[string]string{}, "Specifies arbitrary version-agnostic key=value metadata meant to describe a secret.\nThis can be specified multiple times to add multiple pieces of metadata.")
221199
return cmd

cmd/okms/secrets/root.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
//go:build unstable
2-
31
package secrets
42

53
import (

cmd/okms/secrets/secrets.go

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
//go:build unstable
2-
31
package secrets
42

53
import (
@@ -21,15 +19,15 @@ import (
2119

2220
func kvGetCmd() *cobra.Command {
2321
var (
24-
version int32
22+
version uint32
2523
)
2624

2725
cmd := &cobra.Command{
2826
Use: "get PATH",
2927
Short: "Retrieves the value from KMS's key-value store at the given key name",
3028
Args: cobra.ExactArgs(1),
3129
Run: func(cmd *cobra.Command, args []string) {
32-
var v *int32
30+
var v *uint32
3331
if version != 0 {
3432
v = &version
3533
}
@@ -56,7 +54,7 @@ func kvGetCmd() *cobra.Command {
5654
},
5755
}
5856

59-
cmd.Flags().Int32Var(&version, "version", 0, "If passed, the value at the version number will be returned")
57+
cmd.Flags().Uint32Var(&version, "version", 0, "If passed, the value at the version number will be returned")
6058
return cmd
6159
}
6260

@@ -78,14 +76,14 @@ func kvPutCmd() *cobra.Command {
7876
os.Exit(1)
7977
}
8078

81-
var c *int32
79+
var c uint32
8280
if cas != -1 {
83-
c = &cas
81+
c = utils.ToUint32(c)
8482
}
8583
body := types.PostSecretRequest{
8684
Data: new(any),
8785
Options: &types.PostSecretOptions{
88-
Cas: c,
86+
Cas: &c,
8987
},
9088
}
9189

@@ -122,14 +120,14 @@ func kvPatchCmd() *cobra.Command {
122120
os.Exit(1)
123121
}
124122

125-
var c *int32
123+
var c uint32
126124
if cas != -1 {
127-
c = &cas
125+
c = utils.ToUint32(cas)
128126
}
129127
body := types.PostSecretRequest{
130128
Data: new(any),
131129
Options: &types.PostSecretOptions{
132-
Cas: c,
130+
Cas: &c,
133131
},
134132
}
135133

@@ -150,7 +148,7 @@ func kvPatchCmd() *cobra.Command {
150148

151149
func kvDeleteCmd() *cobra.Command {
152150
var (
153-
versions []int32
151+
versions []uint
154152
)
155153

156154
cmd := &cobra.Command{
@@ -161,70 +159,70 @@ func kvDeleteCmd() *cobra.Command {
161159
if len(versions) == 0 {
162160
exit.OnErr(common.Client().DeleteSecretRequest(cmd.Context(), args[0]))
163161
} else {
164-
exit.OnErr(common.Client().DeleteSecretVersions(cmd.Context(), args[0], versions))
162+
exit.OnErr(common.Client().DeleteSecretVersions(cmd.Context(), args[0], utils.ToUint32Array(versions)))
165163
}
166164
},
167165
}
168166

169-
cmd.Flags().Int32SliceVar(&versions, "versions", []int32{}, "Specifies the version numbers to delete. (Comma separated list of versions)")
167+
cmd.Flags().UintSliceVar(&versions, "versions", []uint{}, "Specifies the version numbers to delete. (Comma separated list of versions)")
170168
return cmd
171169
}
172170

173171
func kvUndeleteCmd() *cobra.Command {
174172
var (
175-
versions []int32
173+
versions []uint
176174
)
177175

178176
cmd := &cobra.Command{
179177
Use: "undelete PATH",
180178
Short: "Undeletes the data for the provided version and path in the key-value store.",
181179
Args: cobra.ExactArgs(1),
182180
Run: func(cmd *cobra.Command, args []string) {
183-
exit.OnErr(common.Client().PostSecretUndelete(cmd.Context(), args[0], versions))
181+
exit.OnErr(common.Client().PostSecretUndelete(cmd.Context(), args[0], utils.ToUint32Array(versions)))
184182
},
185183
}
186184

187-
cmd.Flags().Int32SliceVar(&versions, "versions", []int32{}, "Specifies the version numbers to delete. (Comma separated list of versions)")
185+
cmd.Flags().UintSliceVar(&versions, "versions", []uint{}, "Specifies the version numbers to delete. (Comma separated list of versions)")
188186
_ = cmd.MarkFlagRequired("versions")
189187
return cmd
190188
}
191189

192190
func kvDestroyCmd() *cobra.Command {
193191
var (
194-
versions []int32
192+
versions []uint
195193
)
196194

197195
cmd := &cobra.Command{
198196
Use: "destroy PATH",
199197
Short: "Permanently removes the specified versions' data from the key-value store.",
200198
Args: cobra.ExactArgs(1),
201199
Run: func(cmd *cobra.Command, args []string) {
202-
exit.OnErr(common.Client().PostSecretDestroy(cmd.Context(), args[0], versions))
200+
exit.OnErr(common.Client().PutSecretDestroy(cmd.Context(), args[0], utils.ToUint32Array(versions)))
203201
},
204202
}
205203

206-
cmd.Flags().Int32SliceVar(&versions, "versions", []int32{}, "Specifies the version numbers to delete. (Comma separated list of versions)")
204+
cmd.Flags().UintSliceVar(&versions, "versions", []uint{}, "Specifies the version numbers to delete. (Comma separated list of versions)")
207205
_ = cmd.MarkFlagRequired("versions")
208206
return cmd
209207
}
210208

211209
func kvSubkeysCmd() *cobra.Command {
212210
var (
213-
version int32
214-
depth int32
211+
version uint32
212+
depth uint32
215213
)
216214

217215
cmd := &cobra.Command{
218216
Use: "subkeys PATH",
219217
Short: "Provides the subkeys within a secret entry that exists at the requested path.",
220218
Args: cobra.ExactArgs(1),
221219
Run: func(cmd *cobra.Command, args []string) {
222-
var v *int32
220+
var v *uint32
223221
if cmd.Flag("version").Changed {
224222
v = &version
225223
}
226224

227-
var d *int32
225+
var d *uint32
228226
if cmd.Flag("depth").Changed {
229227
d = &depth
230228
}
@@ -251,8 +249,8 @@ func kvSubkeysCmd() *cobra.Command {
251249
},
252250
}
253251

254-
cmd.Flags().Int32Var(&version, "version", 0, "The version to return")
255-
cmd.Flags().Int32Var(&depth, "depth", 0, "Deepest nesting level to provide in the output")
252+
cmd.Flags().Uint32Var(&version, "version", 0, "The version to return")
253+
cmd.Flags().Uint32Var(&depth, "depth", 0, "Deepest nesting level to provide in the output")
256254
return cmd
257255
}
258256

common/utils/int.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,17 @@ func ToUint64[N Integer](n N) uint64 {
2828
return uint64(n)
2929
}
3030

31-
// func ToUint32[N Integer](n N) uint32 {
32-
// if n < 0 || uint64(n) > math.MaxUint32 {
33-
// panic("Integer overflow")
34-
// }
35-
// return uint32(n)
36-
// }
31+
func ToUint32[N Integer](n N) uint32 {
32+
if n < 0 || uint64(n) > math.MaxUint32 {
33+
panic("Integer overflow")
34+
}
35+
return uint32(n)
36+
}
3737

38-
// func ToUint16[N Integer](n N) uint16 {
39-
// if n < 0 || uint64(n) > math.MaxUint16 {
40-
// panic("Integer overflow")
41-
// }
42-
// return uint16(n)
43-
// }
38+
func ToUint32Array[N Integer](l []N) []uint32 {
39+
var v []uint32
40+
for _, val := range l {
41+
v = append(v, ToUint32(val))
42+
}
43+
return v
44+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ require (
1111
github.com/knadh/koanf/v2 v2.1.2
1212
github.com/olekukonko/tablewriter v0.0.5
1313
github.com/ovh/kmip-go v0.3.3
14-
github.com/ovh/okms-sdk-go v0.4.3-0.20250312132334-73bb8f020781
14+
github.com/ovh/okms-sdk-go v0.4.3-0.20250326103329-2a75059822d8
1515
github.com/pterm/pterm v0.12.80
1616
github.com/schollz/progressbar/v3 v3.18.0
1717
github.com/spf13/cobra v1.9.1

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ github.com/ovh/okms-sdk-go v0.4.2 h1:Vr1HQA0tWoREq5b94Ze2BnG+M1/J87ekWB2/9Cm9wAA
107107
github.com/ovh/okms-sdk-go v0.4.2/go.mod h1:qHignKksvZNNywbHvwJCmy5C6Ro1ZZgNKu2PZO7XTJs=
108108
github.com/ovh/okms-sdk-go v0.4.3-0.20250312132334-73bb8f020781 h1:6zYOcxm6Zqs0rgpNLtN6a1OnJlwWXmOr4NF+okj5oDM=
109109
github.com/ovh/okms-sdk-go v0.4.3-0.20250312132334-73bb8f020781/go.mod h1:qHignKksvZNNywbHvwJCmy5C6Ro1ZZgNKu2PZO7XTJs=
110+
github.com/ovh/okms-sdk-go v0.4.3-0.20250325141909-a44a71a4b427 h1:oUJrxf2kcO/Y/I8wGFMIiP35Of9WHemWKF0uhlSBazY=
111+
github.com/ovh/okms-sdk-go v0.4.3-0.20250325141909-a44a71a4b427/go.mod h1:qHignKksvZNNywbHvwJCmy5C6Ro1ZZgNKu2PZO7XTJs=
112+
github.com/ovh/okms-sdk-go v0.4.3-0.20250326103329-2a75059822d8 h1:Xj2clTOAYD2dQSmHVBoT0SDXK+FHoZqgfx/5aloH7wQ=
113+
github.com/ovh/okms-sdk-go v0.4.3-0.20250326103329-2a75059822d8/go.mod h1:qHignKksvZNNywbHvwJCmy5C6Ro1ZZgNKu2PZO7XTJs=
110114
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
111115
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
112116
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=

0 commit comments

Comments
 (0)