Skip to content

Commit f53b61d

Browse files
feat(core): kas-grants CRUD (#80)
Co-authored-by: Jake Van Vorhis <[email protected]>
1 parent c340a22 commit f53b61d

File tree

2 files changed

+199
-0
lines changed

2 files changed

+199
-0
lines changed

cmd/kas-grants.go

+134
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package cmd
2+
3+
import (
4+
"strings"
5+
6+
"github.com/opentdf/tructl/pkg/cli"
7+
"github.com/spf13/cobra"
8+
)
9+
10+
var (
11+
kasGrants_crudCommands = []string{
12+
kasGrantsUpdateCmd.Use,
13+
kasGrantsDeleteCmd.Use,
14+
}
15+
16+
// KasGrantsCmd is the command for managing KAS grants
17+
kasGrantsCmd = &cobra.Command{
18+
Use: "kas-grants",
19+
Short: "Manage Key Access Server grants [" + strings.Join(kasGrants_crudCommands, ", ") + "]",
20+
}
21+
22+
// Update one KAS registry entry
23+
kasGrantsUpdateCmd = &cobra.Command{
24+
Use: "update",
25+
Short: "Update a KAS grant",
26+
Run: func(cmd *cobra.Command, args []string) {
27+
h := cli.NewHandler(cmd)
28+
defer h.Close()
29+
30+
flagHelper := cli.NewFlagHelper(cmd)
31+
32+
attr := flagHelper.GetOptionalString("attribute")
33+
val := flagHelper.GetOptionalString("value")
34+
kas := flagHelper.GetRequiredString("kas")
35+
36+
if attr == "" && val == "" {
37+
cli.ExitWithError("Must specify and Attribute Definition id or Value id to update.", nil)
38+
}
39+
var (
40+
id string
41+
header string
42+
res interface{}
43+
err error
44+
)
45+
46+
if attr != "" {
47+
res, err = h.UpdateKasGrantForAttribute(attr, kas)
48+
if err != nil {
49+
cli.ExitWithError("Could not update KAS grant for attribute", err)
50+
}
51+
id = attr
52+
header = "Attribute ID"
53+
} else {
54+
res, err = h.UpdateKasGrantForValue(val, kas)
55+
if err != nil {
56+
cli.ExitWithError("Could not update KAS grant for attribute value", err)
57+
}
58+
id = val
59+
header = "Value ID"
60+
}
61+
62+
t := cli.NewTabular().
63+
Rows([][]string{
64+
{header, id},
65+
{"KAS ID", kas},
66+
}...)
67+
HandleSuccess(cmd, id, t, res)
68+
},
69+
}
70+
71+
kasGrantsDeleteCmd = &cobra.Command{
72+
Use: "delete",
73+
Short: "Delete a KAS grant",
74+
Run: func(cmd *cobra.Command, args []string) {
75+
h := cli.NewHandler(cmd)
76+
defer h.Close()
77+
78+
flagHelper := cli.NewFlagHelper(cmd)
79+
attr := flagHelper.GetOptionalString("attribute")
80+
val := flagHelper.GetOptionalString("value")
81+
kas := flagHelper.GetRequiredString("kas")
82+
83+
if attr == "" && val == "" {
84+
cli.ExitWithError("Must specify and Attribute Definition id or Value id to delete.", nil)
85+
}
86+
var (
87+
id string
88+
header string
89+
res interface{}
90+
err error
91+
)
92+
93+
cli.ConfirmDelete("KAS ID: ", kas)
94+
95+
if attr != "" {
96+
res, err = h.DeleteKasGrantFromAttribute(attr, kas)
97+
if err != nil {
98+
cli.ExitWithError("Could not update KAS grant for attribute", err)
99+
}
100+
id = attr
101+
header = "Attribute ID"
102+
} else {
103+
_, err := h.DeleteKasGrantFromValue(val, kas)
104+
if err != nil {
105+
cli.ExitWithError("Could not update KAS grant for attribute value", err)
106+
}
107+
id = val
108+
header = "Value ID"
109+
}
110+
111+
t := cli.NewTabular().
112+
Rows([][]string{
113+
{header, id},
114+
{"KAS ID", kas},
115+
}...)
116+
HandleSuccess(cmd, id, t, res)
117+
},
118+
}
119+
)
120+
121+
func init() {
122+
policyCmd.AddCommand(kasGrantsCmd)
123+
124+
kasGrantsCmd.AddCommand(kasGrantsUpdateCmd)
125+
kasGrantsUpdateCmd.Flags().StringP("attribute", "a", "", "Attribute Definition ID")
126+
kasGrantsUpdateCmd.Flags().StringP("value", "v", "", "Attribute Value ID")
127+
kasGrantsUpdateCmd.Flags().StringP("kas", "k", "", "Key Access Server (KAS) ID")
128+
injectLabelFlags(kasGrantsUpdateCmd, true)
129+
130+
kasGrantsCmd.AddCommand(kasGrantsDeleteCmd)
131+
kasGrantsDeleteCmd.Flags().StringP("attribute", "a", "", "Attribute Definition ID")
132+
kasGrantsDeleteCmd.Flags().StringP("value", "v", "", "Attribute Value ID")
133+
kasGrantsDeleteCmd.Flags().StringP("kas", "k", "", "Key Access Server (KAS) ID")
134+
}

pkg/handlers/kas-grants.go

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package handlers
2+
3+
import (
4+
"github.com/opentdf/platform/protocol/go/policy/attributes"
5+
)
6+
7+
func (h Handler) UpdateKasGrantForAttribute(attr_id string, kas_id string) (*attributes.AttributeKeyAccessServer, error) {
8+
kas := &attributes.AttributeKeyAccessServer{
9+
AttributeId: attr_id,
10+
KeyAccessServerId: kas_id,
11+
}
12+
resp, err := h.sdk.Attributes.AssignKeyAccessServerToAttribute(h.ctx, &attributes.AssignKeyAccessServerToAttributeRequest{
13+
AttributeKeyAccessServer: kas,
14+
})
15+
if err != nil {
16+
return nil, err
17+
}
18+
19+
return resp.AttributeKeyAccessServer, nil
20+
}
21+
22+
func (h Handler) DeleteKasGrantFromAttribute(attr_id string, kas_id string) (*attributes.AttributeKeyAccessServer, error) {
23+
kas := &attributes.AttributeKeyAccessServer{
24+
AttributeId: attr_id,
25+
KeyAccessServerId: kas_id,
26+
}
27+
resp, err := h.sdk.Attributes.RemoveKeyAccessServerFromAttribute(h.ctx, &attributes.RemoveKeyAccessServerFromAttributeRequest{
28+
AttributeKeyAccessServer: kas,
29+
})
30+
if err != nil {
31+
return nil, err
32+
}
33+
34+
return resp.AttributeKeyAccessServer, nil
35+
}
36+
37+
func (h Handler) UpdateKasGrantForValue(val_id string, kas_id string) (*attributes.ValueKeyAccessServer, error) {
38+
kas := &attributes.ValueKeyAccessServer{
39+
ValueId: val_id,
40+
KeyAccessServerId: kas_id,
41+
}
42+
resp, err := h.sdk.Attributes.AssignKeyAccessServerToValue(h.ctx, &attributes.AssignKeyAccessServerToValueRequest{
43+
ValueKeyAccessServer: kas,
44+
})
45+
if err != nil {
46+
return nil, err
47+
}
48+
49+
return resp.ValueKeyAccessServer, nil
50+
}
51+
52+
func (h Handler) DeleteKasGrantFromValue(val_id string, kas_id string) (*attributes.ValueKeyAccessServer, error) {
53+
kas := &attributes.ValueKeyAccessServer{
54+
ValueId: val_id,
55+
KeyAccessServerId: kas_id,
56+
}
57+
resp, err := h.sdk.Attributes.RemoveKeyAccessServerFromValue(h.ctx, &attributes.RemoveKeyAccessServerFromValueRequest{
58+
ValueKeyAccessServer: kas,
59+
})
60+
if err != nil {
61+
return nil, err
62+
}
63+
64+
return resp.ValueKeyAccessServer, nil
65+
}

0 commit comments

Comments
 (0)