Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix secrets commands #53

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 36 additions & 24 deletions cmd/okms/secrets/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ import (

func kvGetCmd() *cobra.Command {
var (
version int32
version uint32
)

cmd := &cobra.Command{
Use: "get PATH",
Short: "Retrieves the value from KMS's key-value store at the given key name",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
var v *int32
var v *uint32
if version != 0 {
v = &version
}
Expand All @@ -56,7 +56,7 @@ func kvGetCmd() *cobra.Command {
},
}

cmd.Flags().Int32Var(&version, "version", 0, "If passed, the value at the version number will be returned")
cmd.Flags().Uint32Var(&version, "version", 0, "If passed, the value at the version number will be returned")
return cmd
}

Expand All @@ -78,14 +78,14 @@ func kvPutCmd() *cobra.Command {
os.Exit(1)
}

var c *int32
var c uint32
if cas != -1 {
c = &cas
c = utils.ToUint32(c)
}
body := types.PostSecretRequest{
Data: new(any),
Options: &types.PostSecretOptions{
Cas: c,
Cas: &c,
},
}

Expand Down Expand Up @@ -122,14 +122,14 @@ func kvPatchCmd() *cobra.Command {
os.Exit(1)
}

var c *int32
var c uint32
if cas != -1 {
c = &cas
c = utils.ToUint32(cas)
}
body := types.PostSecretRequest{
Data: new(any),
Options: &types.PostSecretOptions{
Cas: c,
Cas: &c,
},
}

Expand All @@ -150,7 +150,7 @@ func kvPatchCmd() *cobra.Command {

func kvDeleteCmd() *cobra.Command {
var (
versions []int32
versions []uint
)

cmd := &cobra.Command{
Expand All @@ -161,70 +161,82 @@ func kvDeleteCmd() *cobra.Command {
if len(versions) == 0 {
exit.OnErr(common.Client().DeleteSecretRequest(cmd.Context(), args[0]))
} else {
exit.OnErr(common.Client().DeleteSecretVersions(cmd.Context(), args[0], versions))
var v []uint32
for _, val := range versions {
v = append(v, utils.ToUint32(val))
}
exit.OnErr(common.Client().DeleteSecretVersions(cmd.Context(), args[0], v))
}
},
}

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

func kvUndeleteCmd() *cobra.Command {
var (
versions []int32
versions []uint
)

cmd := &cobra.Command{
Use: "undelete PATH",
Short: "Undeletes the data for the provided version and path in the key-value store.",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
exit.OnErr(common.Client().PostSecretUndelete(cmd.Context(), args[0], versions))
var v []uint32
for _, val := range versions {
v = append(v, utils.ToUint32(val))
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This loop is repeated twice, maybe write a function ToUint32Array that does it.

exit.OnErr(common.Client().PostSecretUndelete(cmd.Context(), args[0], v))
},
}

cmd.Flags().Int32SliceVar(&versions, "versions", []int32{}, "Specifies the version numbers to delete. (Comma separated list of versions)")
cmd.Flags().UintSliceVar(&versions, "versions", []uint{}, "Specifies the version numbers to delete. (Comma separated list of versions)")
_ = cmd.MarkFlagRequired("versions")
return cmd
}

func kvDestroyCmd() *cobra.Command {
var (
versions []int32
versions []uint
)

cmd := &cobra.Command{
Use: "destroy PATH",
Short: "Permanently removes the specified versions' data from the key-value store.",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
exit.OnErr(common.Client().PostSecretDestroy(cmd.Context(), args[0], versions))
var v []uint32
for _, val := range versions {
v = append(v, utils.ToUint32(val))
}
exit.OnErr(common.Client().PostSecretDestroy(cmd.Context(), args[0], v))
},
}

cmd.Flags().Int32SliceVar(&versions, "versions", []int32{}, "Specifies the version numbers to delete. (Comma separated list of versions)")
cmd.Flags().UintSliceVar(&versions, "versions", []uint{}, "Specifies the version numbers to delete. (Comma separated list of versions)")
_ = cmd.MarkFlagRequired("versions")
return cmd
}

func kvSubkeysCmd() *cobra.Command {
var (
version int32
depth int32
version uint32
depth uint32
)

cmd := &cobra.Command{
Use: "subkeys PATH",
Short: "Provides the subkeys within a secret entry that exists at the requested path.",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
var v *int32
var v *uint32
if cmd.Flag("version").Changed {
v = &version
}

var d *int32
var d *uint32
if cmd.Flag("depth").Changed {
d = &depth
}
Expand All @@ -251,8 +263,8 @@ func kvSubkeysCmd() *cobra.Command {
},
}

cmd.Flags().Int32Var(&version, "version", 0, "The version to return")
cmd.Flags().Int32Var(&depth, "depth", 0, "Deepest nesting level to provide in the output")
cmd.Flags().Uint32Var(&version, "version", 0, "The version to return")
cmd.Flags().Uint32Var(&depth, "depth", 0, "Deepest nesting level to provide in the output")
return cmd
}

Expand Down
1 change: 0 additions & 1 deletion common/flagsmgmt/kmipflags/object_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ const (
ObjectTypePublicKey = ObjectType(kmip.ObjectTypePublicKey)
ObjectTypePrivateKey = ObjectType(kmip.ObjectTypePrivateKey)
ObjectTypeSplitKey = ObjectType(kmip.ObjectTypeSplitKey)
//nolint:staticcheck // Needed to support legacy templates
ObjectTypeTemplate = ObjectType(kmip.ObjectTypeTemplate)
ObjectTypeSecretData = ObjectType(kmip.ObjectTypeSecretData)
ObjectTypeOpaqueObject = ObjectType(kmip.ObjectTypeOpaqueObject)
Expand Down
12 changes: 6 additions & 6 deletions common/utils/int.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ func ToUint64[N Integer](n N) uint64 {
return uint64(n)
}

// func ToUint32[N Integer](n N) uint32 {
// if n < 0 || uint64(n) > math.MaxUint32 {
// panic("Integer overflow")
// }
// return uint32(n)
// }
func ToUint32[N Integer](n N) uint32 {
if n < 0 || uint64(n) > math.MaxUint32 {
panic("Integer overflow")
}
return uint32(n)
}

// func ToUint16[N Integer](n N) uint16 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove unnecessary code

// if n < 0 || uint64(n) > math.MaxUint16 {
Expand Down