|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/golang-jwt/jwt" |
| 8 | + "github.com/opentdf/otdfctl/pkg/cli" |
| 9 | + "github.com/opentdf/otdfctl/pkg/handlers" |
| 10 | + "github.com/opentdf/otdfctl/pkg/man" |
| 11 | + "github.com/opentdf/platform/protocol/go/policy" |
| 12 | + "github.com/spf13/cobra" |
| 13 | +) |
| 14 | + |
| 15 | +var ( |
| 16 | + selectors []string |
| 17 | + |
| 18 | + dev_selectorsCmd *cobra.Command |
| 19 | +) |
| 20 | + |
| 21 | +func dev_selectorsGen(cmd *cobra.Command, args []string) { |
| 22 | + h := cli.NewHandler(cmd) |
| 23 | + defer h.Close() |
| 24 | + |
| 25 | + flagHelper := cli.NewFlagHelper(cmd) |
| 26 | + subject := flagHelper.GetRequiredString("subject") |
| 27 | + contextType := flagHelper.GetRequiredString("type") |
| 28 | + |
| 29 | + var value any |
| 30 | + if contextType == "json" || contextType == "" { |
| 31 | + if err := json.Unmarshal([]byte(subject), &value); err != nil { |
| 32 | + cli.ExitWithError(fmt.Sprintf("Could not unmarshal JSON subject context input: %s", subject), err) |
| 33 | + } |
| 34 | + } else if contextType == "jwt" { |
| 35 | + // get the payload from the decoded JWT |
| 36 | + token, _, err := new(jwt.Parser).ParseUnverified(subject, jwt.MapClaims{}) |
| 37 | + if err != nil { |
| 38 | + cli.ExitWithError("Failed to parse JWT token", err) |
| 39 | + } |
| 40 | + |
| 41 | + if claims, ok := token.Claims.(jwt.MapClaims); ok { |
| 42 | + value = claims |
| 43 | + } else { |
| 44 | + cli.ExitWithError("Failed to get claims from JWT token", nil) |
| 45 | + } |
| 46 | + } else { |
| 47 | + cli.ExitWithError("Invalid subject context type. Must be of type: [json, jwt]", nil) |
| 48 | + } |
| 49 | + |
| 50 | + result, err := handlers.ProcessSubjectContext(value, "", []*policy.SubjectProperty{}) |
| 51 | + if err != nil { |
| 52 | + cli.ExitWithError("Failed to process subject context keys and values", err) |
| 53 | + } |
| 54 | + |
| 55 | + rows := [][]string{} |
| 56 | + for _, r := range result { |
| 57 | + rows = append(rows, []string{r.ExternalField, r.ExternalValue}) |
| 58 | + } |
| 59 | + |
| 60 | + t := cli.NewTabular().Rows(rows...) |
| 61 | + cli.PrintSuccessTable(cmd, "", t) |
| 62 | +} |
| 63 | + |
| 64 | +func dev_selectorsTest(cmd *cobra.Command, args []string) { |
| 65 | + h := cli.NewHandler(cmd) |
| 66 | + defer h.Close() |
| 67 | + |
| 68 | + flagHelper := cli.NewFlagHelper(cmd) |
| 69 | + subject := flagHelper.GetRequiredString("subject") |
| 70 | + contextType := flagHelper.GetRequiredString("type") |
| 71 | + selectors := flagHelper.GetStringSlice("selectors", selectors, cli.FlagHelperStringSliceOptions{Min: 1}) |
| 72 | + |
| 73 | + var value any |
| 74 | + if contextType == "json" || contextType == "" { |
| 75 | + if err := json.Unmarshal([]byte(subject), &value); err != nil { |
| 76 | + cli.ExitWithError(fmt.Sprintf("Could not unmarshal JSON subject context input: %s", subject), err) |
| 77 | + } |
| 78 | + } else if contextType == "jwt" { |
| 79 | + token, _, err := new(jwt.Parser).ParseUnverified(subject, jwt.MapClaims{}) |
| 80 | + if err != nil { |
| 81 | + cli.ExitWithError("Failed to parse JWT token", err) |
| 82 | + } |
| 83 | + |
| 84 | + if claims, ok := token.Claims.(jwt.MapClaims); ok { |
| 85 | + value = claims |
| 86 | + } else { |
| 87 | + cli.ExitWithError("Failed to get claims from JWT token", nil) |
| 88 | + } |
| 89 | + } else { |
| 90 | + cli.ExitWithError("Invalid subject context type. Must be of type: [json, jwt]", nil) |
| 91 | + } |
| 92 | + |
| 93 | + result, err := handlers.TestSubjectContext(value, selectors) |
| 94 | + if err != nil { |
| 95 | + cli.ExitWithError("Failed to process subject context keys and values", err) |
| 96 | + } |
| 97 | + |
| 98 | + rows := [][]string{} |
| 99 | + for _, r := range result { |
| 100 | + rows = append(rows, []string{r.ExternalField, r.ExternalValue}) |
| 101 | + } |
| 102 | + |
| 103 | + t := cli.NewTabular().Rows(rows...) |
| 104 | + cli.PrintSuccessTable(cmd, "", t) |
| 105 | +} |
| 106 | + |
| 107 | +func init() { |
| 108 | + genCmd := man.Docs.GetCommand("dev/selectors/gen", |
| 109 | + man.WithRun(dev_selectorsGen), |
| 110 | + ) |
| 111 | + genCmd.Flags().StringP( |
| 112 | + genCmd.GetDocFlag("subject").Name, |
| 113 | + genCmd.GetDocFlag("subject").Shorthand, |
| 114 | + genCmd.GetDocFlag("subject").Default, |
| 115 | + genCmd.GetDocFlag("subject").Description, |
| 116 | + ) |
| 117 | + genCmd.Flags().StringP( |
| 118 | + genCmd.GetDocFlag("type").Name, |
| 119 | + genCmd.GetDocFlag("type").Shorthand, |
| 120 | + genCmd.GetDocFlag("type").Default, |
| 121 | + genCmd.GetDocFlag("type").Description, |
| 122 | + ) |
| 123 | + |
| 124 | + testCmd := man.Docs.GetCommand("dev/selectors/test", |
| 125 | + man.WithRun(dev_selectorsTest), |
| 126 | + ) |
| 127 | + testCmd.Flags().StringP( |
| 128 | + testCmd.GetDocFlag("subject").Name, |
| 129 | + testCmd.GetDocFlag("subject").Shorthand, |
| 130 | + testCmd.GetDocFlag("subject").Default, |
| 131 | + testCmd.GetDocFlag("subject").Description, |
| 132 | + ) |
| 133 | + testCmd.Flags().StringP( |
| 134 | + testCmd.GetDocFlag("type").Name, |
| 135 | + testCmd.GetDocFlag("type").Shorthand, |
| 136 | + testCmd.GetDocFlag("type").Default, |
| 137 | + testCmd.GetDocFlag("type").Description, |
| 138 | + ) |
| 139 | + testCmd.Flags().StringArrayVarP( |
| 140 | + &selectors, |
| 141 | + testCmd.GetDocFlag("selector").Name, |
| 142 | + testCmd.GetDocFlag("selector").Shorthand, |
| 143 | + []string{}, |
| 144 | + testCmd.GetDocFlag("selector").Description, |
| 145 | + ) |
| 146 | + |
| 147 | + doc := man.Docs.GetCommand("dev/selectors", |
| 148 | + man.WithSubcommands(genCmd, testCmd), |
| 149 | + ) |
| 150 | + |
| 151 | + dev_selectorsCmd = &doc.Command |
| 152 | + devCmd.AddCommand(dev_selectorsCmd) |
| 153 | +} |
0 commit comments