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

feat(core): support update of SCS read from JSON file #250

Merged
merged 1 commit into from
Aug 10, 2024
Merged
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
40 changes: 33 additions & 7 deletions cmd/policy-subjectConditionSets.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,16 +181,36 @@ func policy_updateSubjectConditionSet(cmd *cobra.Command, args []string) {
id := flagHelper.GetRequiredString("id")
metadataLabels := flagHelper.GetStringSlice("label", metadataLabels, cli.FlagHelperStringSliceOptions{Min: 0})
ssFlagJSON := flagHelper.GetOptionalString("subject-sets")
ssFileJSON := flagHelper.GetOptionalString("subject-sets-file-json")

var (
ss []*policy.SubjectSet
err error
)
if ssFlagJSON != "" {
ss, err = unmarshalSubjectSetsProto([]byte(ssFlagJSON))
var ssBytes []byte
// validate no flag conflicts
if ssFileJSON == "" && ssFlagJSON == "" {
cli.ExitWithError("At least one subject set must be provided ('--subject-sets', '--subject-sets-file-json')", nil)
} else if ssFileJSON != "" && ssFlagJSON != "" {
cli.ExitWithError("Only one of '--subject-sets' or '--subject-sets-file-json' can be provided", nil)
}

// read subject sets into bytes from either the flagged json file or json string
if ssFileJSON != "" {
jsonFile, err := os.Open(ssFileJSON)
if err != nil {
cli.ExitWithError(fmt.Sprintf("Failed to open file at path: %s", ssFileJSON), err)
}
defer jsonFile.Close()

bytes, err := io.ReadAll(jsonFile)
if err != nil {
cli.ExitWithError("Error unmarshalling subject sets", err)
cli.ExitWithError(fmt.Sprintf("Failed to read bytes from file at path: %s", ssFileJSON), err)
}
ssBytes = bytes
} else {
ssBytes = []byte(ssFlagJSON)
}

ss, err := unmarshalSubjectSetsProto(ssBytes)
if err != nil {
cli.ExitWithError("Error unmarshalling subject sets", err)
}

_, err = h.UpdateSubjectConditionSet(id, ss, getMetadataMutable(metadataLabels), getMetadataUpdateBehavior())
Expand Down Expand Up @@ -306,6 +326,12 @@ func init() {
updateDoc.GetDocFlag("subject-sets").Default,
updateDoc.GetDocFlag("subject-sets").Description,
)
updateDoc.Flags().StringP(
createDoc.GetDocFlag("subject-sets-file-json").Name,
createDoc.GetDocFlag("subject-sets-file-json").Shorthand,
createDoc.GetDocFlag("subject-sets-file-json").Default,
createDoc.GetDocFlag("subject-sets-file-json").Description,
)

deleteDoc := man.Docs.GetCommand(
"policy/subject-condition-sets/delete",
Expand Down
5 changes: 5 additions & 0 deletions docs/man/policy/subject-condition-sets/update.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ command:
description: A JSON array of subject sets, containing a list of condition groups, each with one or more conditions
shorthand: s
default: ""
- name: subject-sets-file-json
description: A JSON file with path from the current working directory containing an array of subject sets
shorthand: j
default: ''
required: false
- name: label
description: "Optional metadata 'labels' in the format: key=value"
shorthand: l
Expand Down