|
7 | 7 |
|
8 | 8 | "github.com/evertras/bubble-table/table" |
9 | 9 | "github.com/opentdf/otdfctl/pkg/cli" |
| 10 | + "github.com/opentdf/otdfctl/pkg/handlers" |
10 | 11 | "github.com/opentdf/otdfctl/pkg/man" |
11 | 12 | "github.com/opentdf/platform/protocol/go/policy" |
12 | 13 | "github.com/opentdf/platform/protocol/go/policy/subjectmapping" |
@@ -253,6 +254,70 @@ func policy_updateSubjectMapping(cmd *cobra.Command, args []string) { |
253 | 254 | HandleSuccess(cmd, id, t, updated) |
254 | 255 | } |
255 | 256 |
|
| 257 | +func policy_matchSubjectMappings(cmd *cobra.Command, args []string) { |
| 258 | + c := cli.New(cmd, args) |
| 259 | + h := NewHandler(c) |
| 260 | + defer h.Close() |
| 261 | + |
| 262 | + subject := c.Flags.GetOptionalString("subject") |
| 263 | + selectors = c.Flags.GetStringSlice("selector", selectors, cli.FlagsStringSliceOptions{Min: 0}) |
| 264 | + |
| 265 | + if len(selectors) > 0 && subject != "" { |
| 266 | + cli.ExitWithError("Must provide either '--subject' or '--selector' flag values, not both", nil) |
| 267 | + } |
| 268 | + |
| 269 | + if subject != "" { |
| 270 | + flattened, err := handlers.FlattenSubjectContext(subject) |
| 271 | + if err != nil { |
| 272 | + cli.ExitWithError("Could not process '--subject' value", err) |
| 273 | + } |
| 274 | + for _, item := range flattened { |
| 275 | + selectors = append(selectors, item.Key) |
| 276 | + } |
| 277 | + } |
| 278 | + |
| 279 | + matched, err := h.MatchSubjectMappings(selectors) |
| 280 | + if err != nil { |
| 281 | + cli.ExitWithError(fmt.Sprintf("Failed to match subject mappings with selectors %v", selectors), err) |
| 282 | + } |
| 283 | + |
| 284 | + t := cli.NewTable( |
| 285 | + cli.NewUUIDColumn(), |
| 286 | + table.NewFlexColumn("subject_attrval_id", "Subject AttrVal: Id", cli.FlexColumnWidthFour), |
| 287 | + table.NewFlexColumn("subject_attrval_value", "Subject AttrVal: Value", cli.FlexColumnWidthThree), |
| 288 | + table.NewFlexColumn("actions", "Actions", cli.FlexColumnWidthTwo), |
| 289 | + table.NewFlexColumn("subject_condition_set_id", "Subject Condition Set: Id", cli.FlexColumnWidthFour), |
| 290 | + table.NewFlexColumn("subject_condition_set", "Subject Condition Set", cli.FlexColumnWidthThree), |
| 291 | + ) |
| 292 | + rows := []table.Row{} |
| 293 | + for _, sm := range matched { |
| 294 | + var actionsJSON []byte |
| 295 | + if actionsJSON, err = json.Marshal(sm.GetActions()); err != nil { |
| 296 | + cli.ExitWithError("Error marshalling subject mapping actions", err) |
| 297 | + } |
| 298 | + |
| 299 | + var subjectSetsJSON []byte |
| 300 | + if subjectSetsJSON, err = json.Marshal(sm.GetSubjectConditionSet().GetSubjectSets()); err != nil { |
| 301 | + cli.ExitWithError("Error marshalling subject condition set", err) |
| 302 | + } |
| 303 | + metadata := cli.ConstructMetadata(sm.GetMetadata()) |
| 304 | + |
| 305 | + rows = append(rows, table.NewRow(table.RowData{ |
| 306 | + "id": sm.GetId(), |
| 307 | + "subject_attrval_id": sm.GetAttributeValue().GetId(), |
| 308 | + "subject_attrval_value": sm.GetAttributeValue().GetValue(), |
| 309 | + "actions": string(actionsJSON), |
| 310 | + "subject_condition_set_id": sm.GetSubjectConditionSet().GetId(), |
| 311 | + "subject_condition_set": string(subjectSetsJSON), |
| 312 | + "labels": metadata["Labels"], |
| 313 | + "created_at": metadata["Created At"], |
| 314 | + "updated_at": metadata["Updated At"], |
| 315 | + })) |
| 316 | + } |
| 317 | + t = t.WithRows(rows) |
| 318 | + HandleSuccess(cmd, "", t, matched) |
| 319 | +} |
| 320 | + |
256 | 321 | func getSubjectMappingMappingActionEnumFromChoice(readable string) policy.Action_StandardAction { |
257 | 322 | switch readable { |
258 | 323 | case actionStandardDecrypt: |
@@ -378,13 +443,31 @@ func init() { |
378 | 443 | deleteDoc.GetDocFlag("force").Description, |
379 | 444 | ) |
380 | 445 |
|
| 446 | + matchDoc := man.Docs.GetCommand("policy/subject-mappings/match", |
| 447 | + man.WithRun(policy_matchSubjectMappings), |
| 448 | + ) |
| 449 | + matchDoc.Flags().StringP( |
| 450 | + matchDoc.GetDocFlag("subject").Name, |
| 451 | + matchDoc.GetDocFlag("subject").Shorthand, |
| 452 | + matchDoc.GetDocFlag("subject").Default, |
| 453 | + matchDoc.GetDocFlag("subject").Description, |
| 454 | + ) |
| 455 | + matchDoc.Flags().StringSliceVarP( |
| 456 | + &selectors, |
| 457 | + matchDoc.GetDocFlag("selector").Name, |
| 458 | + matchDoc.GetDocFlag("selector").Shorthand, |
| 459 | + []string{}, |
| 460 | + matchDoc.GetDocFlag("selector").Description, |
| 461 | + ) |
| 462 | + |
381 | 463 | doc := man.Docs.GetCommand("policy/subject-mappings", |
382 | 464 | man.WithSubcommands( |
383 | 465 | createDoc, |
384 | 466 | getDoc, |
385 | 467 | listDoc, |
386 | 468 | updateDoc, |
387 | 469 | deleteDoc, |
| 470 | + matchDoc, |
388 | 471 | ), |
389 | 472 | ) |
390 | 473 | policy_subjectMappingCmd := &doc.Command |
|
0 commit comments