Skip to content

Commit c126630

Browse files
fix(core): Move key management under policy. (#597)
1.) Move key management commands under policy. 2.) Refactor key management tests. 3.) Remove unneeded json checks (cherry picked from commit d657e96)
1 parent 9fbb99f commit c126630

File tree

14 files changed

+159
-153
lines changed

14 files changed

+159
-153
lines changed

cmd/key-management.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
)
66

77
// KeyCmd is the command for managing keys
8-
var keyMngmtCmd = man.Docs.GetCommand("key-management")
8+
var keyMngmtCmd = man.Docs.GetCommand("policy/key-management")
99

1010
func init() {
1111
keyMngmtCmd.PersistentFlags().BoolVar(
@@ -14,5 +14,5 @@ func init() {
1414
keyMngmtCmd.GetDocFlag("json").DefaultAsBool(),
1515
keyMngmtCmd.GetDocFlag("json").Description,
1616
)
17-
RootCmd.AddCommand(&keyMngmtCmd.Command)
17+
policyCmd.AddCommand(&keyMngmtCmd.Command)
1818
}

cmd/keymanagement-provider.go

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
11
package cmd
22

33
import (
4-
"encoding/json"
4+
"fmt"
55

66
"github.com/evertras/bubble-table/table"
77
"github.com/opentdf/otdfctl/pkg/cli"
88
"github.com/opentdf/otdfctl/pkg/man"
99
"github.com/spf13/cobra"
1010
)
1111

12-
func isJSON(str string) bool {
13-
var js json.RawMessage
14-
return json.Unmarshal([]byte(str), &js) == nil
15-
}
16-
1712
func createProviderConfig(cmd *cobra.Command, args []string) {
1813
c := cli.New(cmd, args)
1914
h := NewHandler(c)
@@ -23,10 +18,6 @@ func createProviderConfig(cmd *cobra.Command, args []string) {
2318
config := c.Flags.GetRequiredString("config")
2419
metadataLabels = c.Flags.GetStringSlice("label", metadataLabels, cli.FlagsStringSliceOptions{Min: 0})
2520

26-
if !isJSON(config) {
27-
cli.ExitWithError("Invalid JSON format for config ", nil)
28-
}
29-
3021
// Do not need to get provider config after, since this endpoint returns the created config.
3122
pc, err := h.CreateProviderConfig(c.Context(), name, []byte(config), getMetadataMutable(metadataLabels))
3223
if err != nil {
@@ -90,21 +81,11 @@ func updateProviderConfig(cmd *cobra.Command, args []string) {
9081
cli.ExitWithError("At least one field (name, config, or metadata labels) must be updated", nil)
9182
}
9283

93-
if config != "" && !isJSON(config) {
94-
cli.ExitWithError("Cannot update provider config with invalid json", nil)
95-
}
96-
97-
_, err := h.UpdateProviderConfig(c.Context(), id, name, []byte(config), getMetadataMutable(metadataLabels), getMetadataUpdateBehavior())
84+
pc, err := h.UpdateProviderConfig(c.Context(), id, name, []byte(config), getMetadataMutable(metadataLabels), getMetadataUpdateBehavior())
9885
if err != nil {
9986
cli.ExitWithError("Failed to update provider config", err)
10087
}
10188

102-
// Get updated provider config.
103-
pc, err := h.GetProviderConfig(c.Context(), id, "")
104-
if err != nil {
105-
cli.ExitWithError("Failed to get provider config", err)
106-
}
107-
10889
rows := [][]string{
10990
{"ID", pc.GetId()},
11091
{"Name", pc.GetName()},
@@ -166,8 +147,17 @@ func deleteProviderConfig(cmd *cobra.Command, args []string) {
166147
defer h.Close()
167148

168149
id := c.Flags.GetRequiredID("id")
150+
force := c.Flags.GetOptionalBool("force")
169151

170-
err := h.DeleteProviderConfig(c.Context(), id)
152+
// Get provider config.
153+
pc, err := h.GetProviderConfig(c.Context(), id, "")
154+
if err != nil {
155+
cli.ExitWithError("Failed to get provider config", err)
156+
}
157+
158+
cli.ConfirmAction(cli.ActionDelete, fmt.Sprintf("key provider config with id: %s", id), fmt.Sprintf("Provider Name: %s", pc.GetName()), force)
159+
160+
err = h.DeleteProviderConfig(c.Context(), id)
171161
if err != nil {
172162
cli.ExitWithError("Failed to delete provider config", err)
173163
}
@@ -183,7 +173,7 @@ func deleteProviderConfig(cmd *cobra.Command, args []string) {
183173

184174
func init() {
185175
// Create Provider Config
186-
createDoc := man.Docs.GetCommand("key-management/provider/create",
176+
createDoc := man.Docs.GetCommand("policy/key-management/provider/create",
187177
man.WithRun(createProviderConfig),
188178
)
189179
createDoc.Flags().StringP(
@@ -201,7 +191,7 @@ func init() {
201191
injectLabelFlags(&createDoc.Command, false)
202192

203193
// Get Provider Config
204-
getDoc := man.Docs.GetCommand("key-management/provider/get",
194+
getDoc := man.Docs.GetCommand("policy/key-management/provider/get",
205195
man.WithRun(getProviderConfig),
206196
)
207197
getDoc.Flags().StringP(
@@ -220,7 +210,7 @@ func init() {
220210
getDoc.MarkFlagsMutuallyExclusive("id", "name")
221211

222212
// Update Provider Config
223-
updateDoc := man.Docs.GetCommand("key-management/provider/update",
213+
updateDoc := man.Docs.GetCommand("policy/key-management/provider/update",
224214
man.WithRun(updateProviderConfig),
225215
)
226216
updateDoc.Flags().StringP(
@@ -244,13 +234,13 @@ func init() {
244234
injectLabelFlags(&updateDoc.Command, true)
245235

246236
// List Provider Configs
247-
listDoc := man.Docs.GetCommand("key-management/provider/list",
237+
listDoc := man.Docs.GetCommand("policy/key-management/provider/list",
248238
man.WithRun(listProviderConfig),
249239
)
250240
injectListPaginationFlags(listDoc)
251241

252242
// Add Delete Provider Config
253-
deleteDoc := man.Docs.GetCommand("key-management/provider/delete",
243+
deleteDoc := man.Docs.GetCommand("policy/key-management/provider/delete",
254244
man.WithRun(deleteProviderConfig),
255245
)
256246
deleteDoc.Flags().StringP(
@@ -259,8 +249,14 @@ func init() {
259249
deleteDoc.GetDocFlag("id").Default,
260250
deleteDoc.GetDocFlag("id").Description,
261251
)
252+
deleteDoc.Flags().BoolP(
253+
deleteDoc.GetDocFlag("force").Name,
254+
deleteDoc.GetDocFlag("force").Shorthand,
255+
false,
256+
deleteDoc.GetDocFlag("force").Description,
257+
)
262258

263-
doc := man.Docs.GetCommand("key-management/provider",
259+
doc := man.Docs.GetCommand("policy/key-management/provider",
264260
man.WithSubcommands(createDoc, getDoc, updateDoc, listDoc, deleteDoc))
265261

266262
keyMngmtCmd.AddCommand(&doc.Command)

cmd/policy-attributes.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ func policyAssignKeyToAttribute(cmd *cobra.Command, args []string) {
315315
// Get the attribute to show meaningful information in case of error
316316
attrKey, err := h.AssignKeyToAttribute(c.Context(), attribute, keyID)
317317
if err != nil {
318-
errMsg := fmt.Sprintf("Failed to assign key: (%s) to attribue: (%s)", keyID, attribute)
318+
errMsg := fmt.Sprintf("Failed to assign key: (%s) to attribute: (%s)", keyID, attribute)
319319
cli.ExitWithError(errMsg, err)
320320
}
321321

docs/man/key-management/provider/_index.md

Lines changed: 0 additions & 15 deletions
This file was deleted.

docs/man/key-management/_index.md renamed to docs/man/policy/key-management/_index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ command:
1212
default: 'false'
1313
---
1414

15-
Set of commands for managing provider configuration.
15+
Set of commands for managing key configuration, currently supports managing key provider configuration via the `provider` command.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
title: Provider configuration for Key Management
3+
4+
command:
5+
name: provider
6+
hidden: true
7+
aliases:
8+
- p
9+
---
10+
11+
Commands used for managing a key providers configuration. You should register key providers when creating keys where the key is either:
12+
13+
1. Wrapped by a key stored outside of your KAS server. For example. if you created a key that is of `mode``provider`
14+
2. The actual wrapped key is not stored within the platform database, but a reference to the key is. For example, if you created a key that is of `mode` `remote`.
15+
16+
**You should not** create provider configurations for keys of mode:
17+
18+
- `local`
19+
- `public_key`

docs/man/key-management/provider/delete.md renamed to docs/man/policy/key-management/provider/delete.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ command:
66
- d
77
- remove
88
flags:
9+
- name: force
10+
shorthand: f
11+
description: Force the deletion of a provider configuration without confirmation
912
- name: id
1013
shorthand: i
1114
description: ID of the provider config to delete
@@ -23,3 +26,7 @@ otdfctl keymanagement provider delete --id <provider-config-id>
2326
```shell
2427
otdfctl keymanagement provider delete --id '04ba179c-2f77-4e0d-90c5-fe4d1c9aa3f7'
2528
```
29+
30+
```shell
31+
otdfctl keymanagement provider delete --id '04ba179c-2f77-4e0d-90c5-fe4d1c9aa3f7' --force
32+
```

docs/man/key-management/provider/list.md renamed to docs/man/policy/key-management/provider/list.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ command:
66
- l
77
flags:
88
- name: limit
9-
shorthand: L
9+
shorthand: l
1010
description: Maximum number of results to return
1111
required: true
1212
- name: offset

e2e/kas-keys.bats

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ run_otdfctl_kas_registry_create() {
1313
}
1414

1515
run_otdfctl_provider_create() {
16-
run sh -c "./otdfctl keymanagement provider create $HOST $WITH_CREDS $*"
16+
run sh -c "./otdfctl policy keymanagement provider create $HOST $WITH_CREDS $*"
1717
}
1818

1919
setup_file() {
@@ -41,7 +41,7 @@ setup() {
4141
}
4242

4343
teardown_file() {
44-
./otdfctl keymanagement provider "$HOST" "$WITH_CREDS" delete --id "$PC_ID"
44+
./otdfctl keymanagement provider "$HOST" "$WITH_CREDS" delete --id "$PC_ID" --force
4545
# Cannot cleanup KAS registry and keys, since keys cannot be deleted currently.
4646
unset HOST WITH_CREDS KAS_REGISTRY_ID KAS_NAME KAS_URI PEM_B64 WRAPPING_KEY PC_ID
4747
}

e2e/namespaces.bats

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ setup() {
3434

3535
teardown_file() {
3636
./otdfctl $HOST $WITH_CREDS policy attributes namespace unsafe delete --id "$NS_ID" --force
37-
# Cant delete kas registry with keys attached
38-
#./otdfctl $HOST $WITH_CREDS policy kas-registry delete --id "$KAS_REG_ID" --force
3937

4038
# clear out all test env vars
4139
unset HOST WITH_CREDS NS_NAME NS_FQN NS_ID NS_ID_FLAG KAS_REG_ID KAS_KEY_ID KAS_URI PEM_B64 PEM KAS_KEY_SYSTEM_ID

0 commit comments

Comments
 (0)