Skip to content

Commit 17fbde0

Browse files
feat(core): create subject mapping with subject condition sets (#79)
https://github.com/opentdf/tructl/assets/83739412/68fa29eb-73ee-4d79-aaae-2eff8056e10c --------- Co-authored-by: Ryan Schumacher <[email protected]>
1 parent 26f6fcc commit 17fbde0

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

cmd/policy-subject_condition_sets.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,10 @@ a Subject Mapping and, by said mapping, an Attribute Value.`,
171171
ssFlagJSON := flagHelper.GetOptionalString("subject-sets")
172172

173173
var ss []*policy.SubjectSet
174-
if err := json.Unmarshal([]byte(ssFlagJSON), &ss); err != nil {
175-
cli.ExitWithError("Error unmarshalling subject sets", err)
174+
if ssFlagJSON != "" {
175+
if err := json.Unmarshal([]byte(ssFlagJSON), &ss); err != nil {
176+
cli.ExitWithError("Error unmarshalling subject sets", err)
177+
}
176178
}
177179

178180
_, err := h.UpdateSubjectConditionSet(id, ss, getMetadataMutable(metadataLabels), getMetadataUpdateBehavior())
@@ -250,7 +252,7 @@ func init() {
250252

251253
policy_subject_condition_setCmd.AddCommand(policy_subject_condition_setCreateCmd)
252254
injectLabelFlags(policy_subject_condition_setCreateCmd, false)
253-
policy_subject_condition_setCreateCmd.Flags().StringP("subject-sets", "s", "", "A JSON array of subject sets, containing a list of condition groups, each with one or more conditions.")
255+
policy_subject_condition_setCreateCmd.Flags().StringP("subject-sets", "s", "", "A JSON array of subject sets, containing a list of condition groups, each with one or more conditions")
254256
policy_subject_condition_setCreateCmd.Flags().StringP("subject-sets-file-json", "j", "", "A JSON file with path from $HOME containing an array of subject sets")
255257

256258
policy_subject_condition_setCmd.AddCommand(policy_subject_condition_setGetCmd)
@@ -261,7 +263,7 @@ func init() {
261263
policy_subject_condition_setCmd.AddCommand(policy_subject_condition_setUpdateCmd)
262264
policy_subject_condition_setUpdateCmd.Flags().StringP("id", "i", "", "Id of the subject condition set")
263265
injectLabelFlags(policy_subject_condition_setUpdateCmd, true)
264-
policy_subject_condition_setUpdateCmd.Flags().StringP("subject-sets", "s", "", "A JSON array of subject sets, containing a list of condition groups, each with one or more conditions.")
266+
policy_subject_condition_setUpdateCmd.Flags().StringP("subject-sets", "s", "", "A JSON array of subject sets, containing a list of condition groups, each with one or more conditions")
265267

266268
policy_subject_condition_setCmd.AddCommand(policy_subject_condition_setDeleteCmd)
267269
policy_subject_condition_setDeleteCmd.Flags().StringP("id", "i", "", "Id of the subject condition set")

cmd/policy-subject_mappings.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"strings"
77

88
"github.com/opentdf/platform/protocol/go/policy"
9+
"github.com/opentdf/platform/protocol/go/policy/subjectmapping"
910
"github.com/opentdf/tructl/pkg/cli"
1011
"github.com/spf13/cobra"
1112
)
@@ -141,10 +142,10 @@ Note: SubjectConditionSets are reusable among SubjectMappings and are available
141142
attrValueId := flagHelper.GetRequiredString("attribute-value-id")
142143
standardActions := flagHelper.GetStringSlice("action-standard", standardActions, cli.FlagHelperStringSliceOptions{Min: 0})
143144
customActions := flagHelper.GetStringSlice("action-custom", customActions, cli.FlagHelperStringSliceOptions{Min: 0})
144-
existingSCSId := flagHelper.GetOptionalString("subject-condition-set-id")
145-
// TODO: do we need to support creating a SM & SCS simultaneously? If so, it gets more complex.
146-
// newScs := flagHelper.GetOptionalString("new-subject-condition-set")
147145
metadataLabels := flagHelper.GetStringSlice("label", metadataLabels, cli.FlagHelperStringSliceOptions{Min: 0})
146+
existingSCSId := flagHelper.GetOptionalString("subject-condition-set-id")
147+
// NOTE: labels within a new Subject Condition Set created on a SM creation are not supported
148+
newScsJSON := flagHelper.GetOptionalString("subject-condition-set-new")
148149

149150
// validations
150151
if len(standardActions) == 0 && len(customActions) == 0 {
@@ -160,7 +161,15 @@ Note: SubjectConditionSets are reusable among SubjectMappings and are available
160161
}
161162
actions := getFullActionsList(standardActions, customActions)
162163

163-
mapping, err := h.CreateNewSubjectMapping(attrValueId, actions, existingSCSId, nil, getMetadataMutable(metadataLabels))
164+
var ss []*policy.SubjectSet
165+
if err := json.Unmarshal([]byte(newScsJSON), &ss); err != nil {
166+
cli.ExitWithError("Error unmarshalling subject sets", err)
167+
}
168+
scs := &subjectmapping.SubjectConditionSetCreate{
169+
SubjectSets: ss,
170+
}
171+
172+
mapping, err := h.CreateNewSubjectMapping(attrValueId, actions, existingSCSId, scs, getMetadataMutable(metadataLabels))
164173
if err != nil {
165174
cli.ExitWithError("Could not create subject mapping", err)
166175
}
@@ -171,8 +180,10 @@ Note: SubjectConditionSets are reusable among SubjectMappings and are available
171180
}
172181

173182
var subjectSetsJSON []byte
174-
if subjectSetsJSON, err = json.Marshal(mapping.SubjectConditionSet.SubjectSets); err != nil {
175-
cli.ExitWithError("Error marshalling subject condition set", err)
183+
if mapping.SubjectConditionSet != nil {
184+
if subjectSetsJSON, err = json.Marshal(mapping.SubjectConditionSet.SubjectSets); err != nil {
185+
cli.ExitWithError("Error marshalling subject condition set", err)
186+
}
176187
}
177188

178189
rows := [][]string{
@@ -306,9 +317,8 @@ func init() {
306317
policy_subject_mappingCreateCmd.Flags().StringP("attribute-value-id", "a", "", "Id of the mapped Attribute Value")
307318
policy_subject_mappingCreateCmd.Flags().StringSliceVarP(&standardActions, "action-standard", "s", []string{}, "Standard Action: [DECRYPT, TRANSMIT]")
308319
policy_subject_mappingCreateCmd.Flags().StringSliceVarP(&customActions, "action-custom", "c", []string{}, "Custom Action")
309-
policy_subject_mappingCreateCmd.Flags().String("subject-condition-set-id", "", "Pre-existing Subject Condition Set Id")
310-
// TODO: do we need to support creating a SM & SCS simultaneously? If so, it gets more complex.
311-
// policy_subject_mappingCreateCmd.Flags().StringP("new-subject-condition-set", "scs", "", "New Subject Condition Set (optional)")
320+
policy_subject_mappingCreateCmd.Flags().String("subject-condition-set-id", "", "Known pre-existing Subject Condition Set Id")
321+
policy_subject_mappingCreateCmd.Flags().String("subject-condition-set-new", "", "JSON array of Subject Sets to create a new Subject Condition Set associated with the created Subject Mapping")
312322
injectLabelFlags(policy_subject_mappingCreateCmd, false)
313323

314324
policy_subject_mappingsCmd.AddCommand(policy_subject_mappingUpdateCmd)

0 commit comments

Comments
 (0)