|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + |
| 6 | + "github.com/opentdf/tructl/pkg/cli" |
| 7 | + "github.com/spf13/cobra" |
| 8 | +) |
| 9 | + |
| 10 | +var ( |
| 11 | + kasGrants_crudCommands = []string{ |
| 12 | + kasGrantsUpdateCmd.Use, |
| 13 | + kasGrantsDeleteCmd.Use, |
| 14 | + } |
| 15 | + |
| 16 | + // KasGrantsCmd is the command for managing KAS grants |
| 17 | + kasGrantsCmd = &cobra.Command{ |
| 18 | + Use: "kas-grants", |
| 19 | + Short: "Manage Key Access Server grants [" + strings.Join(kasGrants_crudCommands, ", ") + "]", |
| 20 | + } |
| 21 | + |
| 22 | + // Update one KAS registry entry |
| 23 | + kasGrantsUpdateCmd = &cobra.Command{ |
| 24 | + Use: "update", |
| 25 | + Short: "Update a KAS grant", |
| 26 | + Run: func(cmd *cobra.Command, args []string) { |
| 27 | + h := cli.NewHandler(cmd) |
| 28 | + defer h.Close() |
| 29 | + |
| 30 | + flagHelper := cli.NewFlagHelper(cmd) |
| 31 | + |
| 32 | + attr := flagHelper.GetOptionalString("attribute") |
| 33 | + val := flagHelper.GetOptionalString("value") |
| 34 | + kas := flagHelper.GetRequiredString("kas") |
| 35 | + |
| 36 | + if attr == "" && val == "" { |
| 37 | + cli.ExitWithError("Must specify and Attribute Definition id or Value id to update.", nil) |
| 38 | + } |
| 39 | + var ( |
| 40 | + id string |
| 41 | + header string |
| 42 | + res interface{} |
| 43 | + err error |
| 44 | + ) |
| 45 | + |
| 46 | + if attr != "" { |
| 47 | + res, err = h.UpdateKasGrantForAttribute(attr, kas) |
| 48 | + if err != nil { |
| 49 | + cli.ExitWithError("Could not update KAS grant for attribute", err) |
| 50 | + } |
| 51 | + id = attr |
| 52 | + header = "Attribute ID" |
| 53 | + } else { |
| 54 | + res, err = h.UpdateKasGrantForValue(val, kas) |
| 55 | + if err != nil { |
| 56 | + cli.ExitWithError("Could not update KAS grant for attribute value", err) |
| 57 | + } |
| 58 | + id = val |
| 59 | + header = "Value ID" |
| 60 | + } |
| 61 | + |
| 62 | + t := cli.NewTabular(). |
| 63 | + Rows([][]string{ |
| 64 | + {header, id}, |
| 65 | + {"KAS ID", kas}, |
| 66 | + }...) |
| 67 | + HandleSuccess(cmd, id, t, res) |
| 68 | + }, |
| 69 | + } |
| 70 | + |
| 71 | + kasGrantsDeleteCmd = &cobra.Command{ |
| 72 | + Use: "delete", |
| 73 | + Short: "Delete a KAS grant", |
| 74 | + Run: func(cmd *cobra.Command, args []string) { |
| 75 | + h := cli.NewHandler(cmd) |
| 76 | + defer h.Close() |
| 77 | + |
| 78 | + flagHelper := cli.NewFlagHelper(cmd) |
| 79 | + attr := flagHelper.GetOptionalString("attribute") |
| 80 | + val := flagHelper.GetOptionalString("value") |
| 81 | + kas := flagHelper.GetRequiredString("kas") |
| 82 | + |
| 83 | + if attr == "" && val == "" { |
| 84 | + cli.ExitWithError("Must specify and Attribute Definition id or Value id to delete.", nil) |
| 85 | + } |
| 86 | + var ( |
| 87 | + id string |
| 88 | + header string |
| 89 | + res interface{} |
| 90 | + err error |
| 91 | + ) |
| 92 | + |
| 93 | + cli.ConfirmDelete("KAS ID: ", kas) |
| 94 | + |
| 95 | + if attr != "" { |
| 96 | + res, err = h.DeleteKasGrantFromAttribute(attr, kas) |
| 97 | + if err != nil { |
| 98 | + cli.ExitWithError("Could not update KAS grant for attribute", err) |
| 99 | + } |
| 100 | + id = attr |
| 101 | + header = "Attribute ID" |
| 102 | + } else { |
| 103 | + _, err := h.DeleteKasGrantFromValue(val, kas) |
| 104 | + if err != nil { |
| 105 | + cli.ExitWithError("Could not update KAS grant for attribute value", err) |
| 106 | + } |
| 107 | + id = val |
| 108 | + header = "Value ID" |
| 109 | + } |
| 110 | + |
| 111 | + t := cli.NewTabular(). |
| 112 | + Rows([][]string{ |
| 113 | + {header, id}, |
| 114 | + {"KAS ID", kas}, |
| 115 | + }...) |
| 116 | + HandleSuccess(cmd, id, t, res) |
| 117 | + }, |
| 118 | + } |
| 119 | +) |
| 120 | + |
| 121 | +func init() { |
| 122 | + policyCmd.AddCommand(kasGrantsCmd) |
| 123 | + |
| 124 | + kasGrantsCmd.AddCommand(kasGrantsUpdateCmd) |
| 125 | + kasGrantsUpdateCmd.Flags().StringP("attribute", "a", "", "Attribute Definition ID") |
| 126 | + kasGrantsUpdateCmd.Flags().StringP("value", "v", "", "Attribute Value ID") |
| 127 | + kasGrantsUpdateCmd.Flags().StringP("kas", "k", "", "Key Access Server (KAS) ID") |
| 128 | + injectLabelFlags(kasGrantsUpdateCmd, true) |
| 129 | + |
| 130 | + kasGrantsCmd.AddCommand(kasGrantsDeleteCmd) |
| 131 | + kasGrantsDeleteCmd.Flags().StringP("attribute", "a", "", "Attribute Definition ID") |
| 132 | + kasGrantsDeleteCmd.Flags().StringP("value", "v", "", "Attribute Value ID") |
| 133 | + kasGrantsDeleteCmd.Flags().StringP("kas", "k", "", "Key Access Server (KAS) ID") |
| 134 | +} |
0 commit comments