|
1 | 1 | package cmd
|
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "io/ioutil" |
| 7 | + "os" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/opentdf/platform/protocol/go/policy" |
| 11 | + "github.com/opentdf/tructl/pkg/cli" |
| 12 | + "github.com/spf13/cobra" |
| 13 | +) |
| 14 | + |
| 15 | +var ( |
| 16 | + policy_subject_condition_setsCmds = []string{ |
| 17 | + policy_subject_condition_setCreateCmd.Use, |
| 18 | + policy_subject_condition_setGetCmd.Use, |
| 19 | + policy_subject_condition_setListCmd.Use, |
| 20 | + policy_subject_condition_setUpdateCmd.Use, |
| 21 | + policy_subject_condition_setDeleteCmd.Use, |
| 22 | + } |
| 23 | + |
| 24 | + policy_subject_condition_setCmd = &cobra.Command{ |
| 25 | + Use: "subject-condition-sets", |
| 26 | + Short: "Manage subject condition sets" + strings.Join(policy_subject_condition_setsCmds, ", ") + "]", |
| 27 | + Long: ` |
| 28 | +Subject Condition Sets - fields and values known to an external user source that are utilized to relate a Subject (PE/NPE) to |
| 29 | +a Subject Mapping and, by said mapping, an Attribute Value.`, |
| 30 | + } |
| 31 | + |
| 32 | + policy_subject_condition_setCreateCmd = &cobra.Command{ |
| 33 | + Use: "create", |
| 34 | + Short: "Create a subject condition set", |
| 35 | + Run: func(cmd *cobra.Command, args []string) { |
| 36 | + h := cli.NewHandler(cmd) |
| 37 | + defer h.Close() |
| 38 | + var ( |
| 39 | + ss []*policy.SubjectSet |
| 40 | + ssBytes []byte |
| 41 | + ) |
| 42 | + |
| 43 | + flagHelper := cli.NewFlagHelper(cmd) |
| 44 | + ssFlagJSON := flagHelper.GetOptionalString("subject-sets") |
| 45 | + ssFileJSON := flagHelper.GetOptionalString("subject-sets-file-json") |
| 46 | + metadataLabels := flagHelper.GetStringSlice("label", metadataLabels, cli.FlagHelperStringSliceOptions{Min: 0}) |
| 47 | + |
| 48 | + // validate no flag conflicts |
| 49 | + if ssFileJSON == "" && ssFlagJSON == "" { |
| 50 | + cli.ExitWithError("At least one subject set must be provided ('--subject-sets', '--subject-sets-file-json')", nil) |
| 51 | + } else if ssFileJSON != "" && ssFlagJSON != "" { |
| 52 | + cli.ExitWithError("Only one of '--subject-sets' or '--subject-sets-file-json' can be provided", nil) |
| 53 | + } |
| 54 | + |
| 55 | + // read subject sets into bytes from either the flagged json file or json string |
| 56 | + if ssFileJSON != "" { |
| 57 | + jsonFile, err := os.Open(ssFileJSON) |
| 58 | + if err != nil { |
| 59 | + cli.ExitWithError(fmt.Sprintf("Failed to open file at path: %s", ssFileJSON), err) |
| 60 | + } |
| 61 | + defer jsonFile.Close() |
| 62 | + |
| 63 | + bytes, err := ioutil.ReadAll(jsonFile) |
| 64 | + if err != nil { |
| 65 | + cli.ExitWithError(fmt.Sprintf("Failed to read bytes from file at path: %s", ssFileJSON), err) |
| 66 | + } |
| 67 | + ssBytes = bytes |
| 68 | + } else { |
| 69 | + ssBytes = []byte(ssFlagJSON) |
| 70 | + } |
| 71 | + |
| 72 | + if err := json.Unmarshal(ssBytes, &ss); err != nil { |
| 73 | + cli.ExitWithError("Error unmarshalling subject sets", err) |
| 74 | + } |
| 75 | + |
| 76 | + scs, err := h.CreateSubjectConditionSet(ss, getMetadataMutable(metadataLabels)) |
| 77 | + if err != nil { |
| 78 | + cli.ExitWithError("Error creating subject condition set", err) |
| 79 | + } |
| 80 | + |
| 81 | + var subjectSetsJSON []byte |
| 82 | + if subjectSetsJSON, err = json.Marshal(scs.SubjectSets); err != nil { |
| 83 | + cli.ExitWithError("Error marshalling subject condition set", err) |
| 84 | + } |
| 85 | + |
| 86 | + rows := [][]string{ |
| 87 | + {"Id", scs.Id}, |
| 88 | + {"SubjectSets", string(subjectSetsJSON)}, |
| 89 | + } |
| 90 | + |
| 91 | + if mdRows := getMetadataRows(scs.Metadata); mdRows != nil { |
| 92 | + rows = append(rows, mdRows...) |
| 93 | + } |
| 94 | + |
| 95 | + t := cli.NewTabular().Rows(rows...) |
| 96 | + HandleSuccess(cmd, scs.Id, t, scs) |
| 97 | + }, |
| 98 | + } |
| 99 | + |
| 100 | + policy_subject_condition_setGetCmd = &cobra.Command{ |
| 101 | + Use: "get", |
| 102 | + Short: "Get a subject condition set by id", |
| 103 | + Run: func(cmd *cobra.Command, args []string) { |
| 104 | + h := cli.NewHandler(cmd) |
| 105 | + defer h.Close() |
| 106 | + |
| 107 | + flagHelper := cli.NewFlagHelper(cmd) |
| 108 | + id := flagHelper.GetRequiredString("id") |
| 109 | + |
| 110 | + scs, err := h.GetSubjectConditionSet(id) |
| 111 | + if err != nil { |
| 112 | + cli.ExitWithNotFoundError(fmt.Sprintf("Subject Condition Set with id %s not found", id), err) |
| 113 | + } |
| 114 | + |
| 115 | + var subjectSetsJSON []byte |
| 116 | + if subjectSetsJSON, err = json.Marshal(scs.SubjectSets); err != nil { |
| 117 | + cli.ExitWithError("Error marshalling subject condition set", err) |
| 118 | + } |
| 119 | + |
| 120 | + rows := [][]string{ |
| 121 | + {"Id", scs.Id}, |
| 122 | + {"SubjectSets", string(subjectSetsJSON)}, |
| 123 | + } |
| 124 | + |
| 125 | + if mdRows := getMetadataRows(scs.Metadata); mdRows != nil { |
| 126 | + rows = append(rows, mdRows...) |
| 127 | + } |
| 128 | + |
| 129 | + t := cli.NewTabular().Rows(rows...) |
| 130 | + HandleSuccess(cmd, scs.Id, t, scs) |
| 131 | + }, |
| 132 | + } |
| 133 | + |
| 134 | + policy_subject_condition_setListCmd = &cobra.Command{ |
| 135 | + Use: "list", |
| 136 | + Short: "List subject condition sets", |
| 137 | + Run: func(cmd *cobra.Command, args []string) { |
| 138 | + h := cli.NewHandler(cmd) |
| 139 | + defer h.Close() |
| 140 | + |
| 141 | + scsList, err := h.ListSubjectConditionSets() |
| 142 | + if err != nil { |
| 143 | + cli.ExitWithError("Error listing subject condition sets", err) |
| 144 | + } |
| 145 | + |
| 146 | + t := cli.NewTable() |
| 147 | + t.Headers("Id", "SubjectSets") |
| 148 | + for _, scs := range scsList { |
| 149 | + var subjectSetsJSON []byte |
| 150 | + if subjectSetsJSON, err = json.Marshal(scs.SubjectSets); err != nil { |
| 151 | + cli.ExitWithError("Error marshalling subject condition set", err) |
| 152 | + } |
| 153 | + rowCells := []string{scs.Id, string(subjectSetsJSON)} |
| 154 | + t.Row(rowCells...) |
| 155 | + } |
| 156 | + |
| 157 | + HandleSuccess(cmd, "", t, scsList) |
| 158 | + }, |
| 159 | + } |
| 160 | + |
| 161 | + policy_subject_condition_setUpdateCmd = &cobra.Command{ |
| 162 | + Use: "update", |
| 163 | + Short: "Update a subject condition set", |
| 164 | + Run: func(cmd *cobra.Command, args []string) { |
| 165 | + h := cli.NewHandler(cmd) |
| 166 | + defer h.Close() |
| 167 | + |
| 168 | + flagHelper := cli.NewFlagHelper(cmd) |
| 169 | + id := flagHelper.GetRequiredString("id") |
| 170 | + metadataLabels := flagHelper.GetStringSlice("label", metadataLabels, cli.FlagHelperStringSliceOptions{Min: 0}) |
| 171 | + ssFlagJSON := flagHelper.GetOptionalString("subject-sets") |
| 172 | + |
| 173 | + var ss []*policy.SubjectSet |
| 174 | + if err := json.Unmarshal([]byte(ssFlagJSON), &ss); err != nil { |
| 175 | + cli.ExitWithError("Error unmarshalling subject sets", err) |
| 176 | + } |
| 177 | + |
| 178 | + _, err := h.UpdateSubjectConditionSet(id, ss, getMetadataMutable(metadataLabels), getMetadataUpdateBehavior()) |
| 179 | + if err != nil { |
| 180 | + cli.ExitWithError("Error updating subject condition set", err) |
| 181 | + } |
| 182 | + |
| 183 | + scs, err := h.GetSubjectConditionSet(id) |
| 184 | + if err != nil { |
| 185 | + cli.ExitWithError("Error getting subject condition set", err) |
| 186 | + } |
| 187 | + |
| 188 | + var subjectSetsJSON []byte |
| 189 | + if subjectSetsJSON, err = json.Marshal(scs.SubjectSets); err != nil { |
| 190 | + cli.ExitWithError("Error marshalling subject condition set", err) |
| 191 | + } |
| 192 | + |
| 193 | + rows := [][]string{ |
| 194 | + {"Id", scs.Id}, |
| 195 | + {"SubjectSets", string(subjectSetsJSON)}, |
| 196 | + } |
| 197 | + |
| 198 | + if mdRows := getMetadataRows(scs.Metadata); mdRows != nil { |
| 199 | + rows = append(rows, mdRows...) |
| 200 | + } |
| 201 | + |
| 202 | + t := cli.NewTabular().Rows(rows...) |
| 203 | + HandleSuccess(cmd, scs.Id, t, scs) |
| 204 | + }, |
| 205 | + } |
| 206 | + |
| 207 | + policy_subject_condition_setDeleteCmd = &cobra.Command{ |
| 208 | + Use: "delete", |
| 209 | + Short: "Delete a subject condition set", |
| 210 | + Run: func(cmd *cobra.Command, args []string) { |
| 211 | + h := cli.NewHandler(cmd) |
| 212 | + defer h.Close() |
| 213 | + |
| 214 | + flagHelper := cli.NewFlagHelper(cmd) |
| 215 | + id := flagHelper.GetRequiredString("id") |
| 216 | + |
| 217 | + scs, err := h.GetSubjectConditionSet(id) |
| 218 | + if err != nil { |
| 219 | + cli.ExitWithNotFoundError(fmt.Sprintf("Subject Condition Set with id %s not found", id), err) |
| 220 | + } |
| 221 | + |
| 222 | + cli.ConfirmDelete("Subject Condition Set", id) |
| 223 | + |
| 224 | + if err := h.DeleteSubjectConditionSet(id); err != nil { |
| 225 | + cli.ExitWithNotFoundError(fmt.Sprintf("Subject Condition Set with id %s not found", id), err) |
| 226 | + } |
| 227 | + |
| 228 | + var subjectSetsJSON []byte |
| 229 | + if subjectSetsJSON, err = json.Marshal(scs.SubjectSets); err != nil { |
| 230 | + cli.ExitWithError("Error marshalling subject condition set", err) |
| 231 | + } |
| 232 | + |
| 233 | + rows := [][]string{ |
| 234 | + {"Id", scs.Id}, |
| 235 | + {"SubjectSets", string(subjectSetsJSON)}, |
| 236 | + } |
| 237 | + |
| 238 | + if mdRows := getMetadataRows(scs.Metadata); mdRows != nil { |
| 239 | + rows = append(rows, mdRows...) |
| 240 | + } |
| 241 | + |
| 242 | + t := cli.NewTabular().Rows(rows...) |
| 243 | + HandleSuccess(cmd, scs.Id, t, scs) |
| 244 | + }, |
| 245 | + } |
| 246 | +) |
| 247 | + |
| 248 | +func init() { |
| 249 | + policyCmd.AddCommand(policy_subject_condition_setCmd) |
| 250 | + |
| 251 | + policy_subject_condition_setCmd.AddCommand(policy_subject_condition_setCreateCmd) |
| 252 | + 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.") |
| 254 | + policy_subject_condition_setCreateCmd.Flags().StringP("subject-sets-file-json", "j", "", "A JSON file with path from $HOME containing an array of subject sets") |
| 255 | + |
| 256 | + policy_subject_condition_setCmd.AddCommand(policy_subject_condition_setGetCmd) |
| 257 | + policy_subject_condition_setGetCmd.Flags().StringP("id", "i", "", "Id of the subject condition set") |
| 258 | + |
| 259 | + policy_subject_condition_setCmd.AddCommand(policy_subject_condition_setListCmd) |
| 260 | + |
| 261 | + policy_subject_condition_setCmd.AddCommand(policy_subject_condition_setUpdateCmd) |
| 262 | + policy_subject_condition_setUpdateCmd.Flags().StringP("id", "i", "", "Id of the subject condition set") |
| 263 | + 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.") |
| 265 | + |
| 266 | + policy_subject_condition_setCmd.AddCommand(policy_subject_condition_setDeleteCmd) |
| 267 | + policy_subject_condition_setDeleteCmd.Flags().StringP("id", "i", "", "Id of the subject condition set") |
| 268 | +} |
| 269 | + |
| 270 | +func getSubjectConditionSetOperatorFromChoice(choice string) (policy.SubjectMappingOperatorEnum, error) { |
| 271 | + switch choice { |
| 272 | + case "IN": |
| 273 | + return policy.SubjectMappingOperatorEnum_SUBJECT_MAPPING_OPERATOR_ENUM_IN, nil |
| 274 | + case "NOT_IN": |
| 275 | + return policy.SubjectMappingOperatorEnum_SUBJECT_MAPPING_OPERATOR_ENUM_NOT_IN, nil |
| 276 | + default: |
| 277 | + return policy.SubjectMappingOperatorEnum_SUBJECT_MAPPING_OPERATOR_ENUM_UNSPECIFIED, fmt.Errorf("Unknown operator must be specified ['IN', 'NOT_IN']: %s", choice) |
| 278 | + } |
| 279 | +} |
| 280 | + |
| 281 | +func getSubjectConditionSetBooleanTypeFromChoice(choice string) (policy.ConditionBooleanTypeEnum, error) { |
| 282 | + switch choice { |
| 283 | + case "AND": |
| 284 | + return policy.ConditionBooleanTypeEnum_CONDITION_BOOLEAN_TYPE_ENUM_AND, nil |
| 285 | + case "OR": |
| 286 | + return policy.ConditionBooleanTypeEnum_CONDITION_BOOLEAN_TYPE_ENUM_OR, nil |
| 287 | + default: |
| 288 | + return policy.ConditionBooleanTypeEnum_CONDITION_BOOLEAN_TYPE_ENUM_UNSPECIFIED, fmt.Errorf("Unknown boolean type must be specified ['AND', 'OR']: %s", choice) |
| 289 | + } |
| 290 | +} |
| 291 | + |
| 292 | +func getMarshaledSubjectSets(subjectSets string) ([]*policy.SubjectSet, error) { |
| 293 | + var ss []*policy.SubjectSet |
| 294 | + |
| 295 | + if err := json.Unmarshal([]byte(subjectSets), &ss); err != nil { |
| 296 | + return nil, err |
| 297 | + } |
| 298 | + |
| 299 | + return ss, nil |
| 300 | +} |
0 commit comments