Skip to content

Commit acb88cf

Browse files
authored
feat(core): add metadata behavior and fix subject mappings (#67)
1. adds subject-mappings CRUD back (with only support at first for creating a Subject Mapping with a known, existing Subject Condition Set Id at least until SCS functionality is added to the CLI as well) 2. adds consistent universal string slice `--label` flag to Policy Object CREATE flows for metadata creation 3. adds consistent universal string slice `--label` and a boolean `--force-replace-labels` to drive update behavior in Policy Object UPDATE flows for metadata update (i.e. `policy <policy object> update --label key1=value1 --label key2=value2 --force-replace-labels` overwrites all labels with a new map containing just `key1` and `key2`) Partially addresses: #60 Related to: #72 Closes: #10
1 parent d20a94b commit acb88cf

14 files changed

+608
-567
lines changed

cmd/dev.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cmd
33
import (
44
"encoding/json"
55
"fmt"
6+
"strings"
67

78
"github.com/charmbracelet/lipgloss/table"
89
"github.com/opentdf/platform/protocol/go/common"
@@ -77,6 +78,29 @@ func unMarshalMetadata(m string) *common.MetadataMutable {
7778
return nil
7879
}
7980

81+
func getMetadata(labels []string) *common.MetadataMutable {
82+
var metadata *common.MetadataMutable
83+
if len(labels) > 0 {
84+
metadata.Labels = map[string]string{}
85+
for _, label := range labels {
86+
kv := strings.Split(label, "=")
87+
if len(kv) != 2 {
88+
cli.ExitWithError("Invalid label format", nil)
89+
}
90+
metadata.Labels[kv[0]] = kv[1]
91+
}
92+
return metadata
93+
}
94+
return nil
95+
}
96+
97+
func getMetadataUpdateBehavior() common.MetadataUpdateEnum {
98+
if forceReplaceMetadataLabels {
99+
return common.MetadataUpdateEnum_METADATA_UPDATE_ENUM_REPLACE
100+
}
101+
return common.MetadataUpdateEnum_METADATA_UPDATE_ENUM_EXTEND
102+
}
103+
80104
// HandleSuccess prints a success message according to the configured format (styled table or JSON)
81105
func HandleSuccess(command *cobra.Command, id string, t *table.Table, policyObject interface{}) {
82106
if TructlCfg.Output.Format == config.OutputJSON || configFlagOverrides.OutputFormatJSON {

cmd/policy-attributes.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@ import (
99
"github.com/spf13/cobra"
1010
)
1111

12+
// TODO: add metadata to outputs once [https://github.com/opentdf/tructl/issues/73] is addressed
13+
1214
var (
13-
attrValues []string
15+
attrValues []string
16+
metadataLabels []string
17+
forceReplaceMetadataLabels bool
1418

1519
policy_attributeCommands = []string{
1620
policy_attributesCreateCmd.Use,
@@ -44,8 +48,9 @@ used to define the access controls based on subject encodings and entity entitle
4448
rule := flagHelper.GetRequiredString("rule")
4549
values := flagHelper.GetStringSlice("values", attrValues, cli.FlagHelperStringSliceOptions{})
4650
namespace := flagHelper.GetRequiredString("namespace")
51+
metadataLabels := flagHelper.GetStringSlice("label", metadataLabels, cli.FlagHelperStringSliceOptions{Min: 0})
4752

48-
attr, err := h.CreateAttribute(name, rule, namespace)
53+
attr, err := h.CreateAttribute(name, rule, namespace, getMetadata(metadataLabels))
4954
if err != nil {
5055
cli.ExitWithError("Could not create attribute", err)
5156
}
@@ -194,8 +199,9 @@ used to define the access controls based on subject encodings and entity entitle
194199

195200
flagHelper := cli.NewFlagHelper(cmd)
196201
id := flagHelper.GetRequiredString("id")
202+
labels := flagHelper.GetStringSlice("label", metadataLabels, cli.FlagHelperStringSliceOptions{Min: 0})
197203

198-
if a, err := h.UpdateAttribute(id); err != nil {
204+
if a, err := h.UpdateAttribute(id, getMetadata(labels), getMetadataUpdateBehavior()); err != nil {
199205
cli.ExitWithError("Could not update attribute", err)
200206
} else {
201207
HandleSuccess(cmd, id, nil, a)
@@ -214,6 +220,7 @@ func init() {
214220
policy_attributesCreateCmd.Flags().StringSliceVarP(&attrValues, "values", "v", []string{}, "Values of the attribute")
215221
policy_attributesCreateCmd.Flags().StringP("namespace", "s", "", "Namespace of the attribute")
216222
policy_attributesCreateCmd.Flags().StringP("description", "d", "", "Description of the attribute")
223+
policy_attributesCreateCmd.Flags().StringSliceVarP(&metadataLabels, "label", "l", []string{}, "Labels for the attribute")
217224

218225
// Get an attribute
219226
policy_attributesCmd.AddCommand(policy_attributeGetCmd)
@@ -225,6 +232,8 @@ func init() {
225232
// Update an attribute
226233
policy_attributesCmd.AddCommand(policy_attributeUpdateCmd)
227234
policy_attributeUpdateCmd.Flags().StringP("id", "i", "", "Id of the attribute")
235+
policy_attributeUpdateCmd.Flags().StringSliceVarP(&metadataLabels, "label", "l", []string{}, "Optional new metadata 'labels' in the format: key=value")
236+
policy_attributeUpdateCmd.Flags().BoolVar(&forceReplaceMetadataLabels, "force-replace-labels", false, "Destructively replace entire set of existing metadata 'labels' with any provided to this command.")
228237

229238
// Delete an attribute
230239
policy_attributesCmd.AddCommand(policy_attributesDeleteCmd)

cmd/policy-namespaces.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"github.com/spf13/cobra"
99
)
1010

11+
// TODO: add metadata to outputs once [https://github.com/opentdf/tructl/issues/73] is addressed
12+
1113
var (
1214
policy_namespacesCommands = []string{
1315
policy_namespacesCreateCmd.Use,
@@ -88,8 +90,9 @@ or different attributes tied to each.
8890

8991
flagHelper := cli.NewFlagHelper(cmd)
9092
name := flagHelper.GetRequiredString("name")
93+
metadataLabels := flagHelper.GetStringSlice("label", metadataLabels, cli.FlagHelperStringSliceOptions{Min: 0})
9194

92-
created, err := h.CreateNamespace(name)
95+
created, err := h.CreateNamespace(name, getMetadata(metadataLabels))
9396
if err != nil {
9497
cli.ExitWithError("Could not create namespace", err)
9598
}
@@ -145,17 +148,18 @@ or different attributes tied to each.
145148
defer h.Close()
146149

147150
flagHelper := cli.NewFlagHelper(cmd)
148-
149151
id := flagHelper.GetRequiredString("id")
150-
name := flagHelper.GetRequiredString("name")
152+
labels := flagHelper.GetStringSlice("label", metadataLabels, cli.FlagHelperStringSliceOptions{Min: 0})
151153

152154
ns, err := h.UpdateNamespace(
153155
id,
154-
name,
156+
getMetadata(labels),
157+
getMetadataUpdateBehavior(),
155158
)
156159
if err != nil {
157160
cli.ExitWithError("Could not update namespace", err)
158161
}
162+
159163
t := cli.NewTabular().Rows([][]string{
160164
{"Id", ns.Id},
161165
{"Name", ns.Name},
@@ -175,10 +179,12 @@ func init() {
175179

176180
policy_namespacesCmd.AddCommand(policy_namespacesCreateCmd)
177181
policy_namespacesCreateCmd.Flags().StringP("name", "n", "", "Name value of the namespace")
182+
policy_namespacesCreateCmd.Flags().StringSliceVarP(&metadataLabels, "label", "l", []string{}, "Optional metadata 'labels' in the format: key=value")
178183

179184
policy_namespacesCmd.AddCommand(policy_namespaceUpdateCmd)
180185
policy_namespaceUpdateCmd.Flags().StringP("id", "i", "", "Id of the namespace")
181-
policy_namespaceUpdateCmd.Flags().StringP("name", "n", "", "Name value of the namespace")
186+
policy_namespaceUpdateCmd.Flags().StringSliceVarP(&metadataLabels, "label", "l", []string{}, "Optional new metadata 'labels' in the format: key=value")
187+
policy_namespaceUpdateCmd.Flags().BoolVar(&forceReplaceMetadataLabels, "force-replace-labels", false, "Destructively replace entire set of existing metadata 'labels' with any provided to this command.")
182188

183189
policy_namespacesCmd.AddCommand(policy_namespaceDeleteCmd)
184190
policy_namespaceDeleteCmd.Flags().StringP("id", "i", "", "Id of the namespace")

0 commit comments

Comments
 (0)