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: new ns, def, value key mapping commands #486

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
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
541 changes: 541 additions & 0 deletions cmd/kas-public-keys.go

Large diffs are not rendered by default.

15 changes: 4 additions & 11 deletions cmd/kas-registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"google.golang.org/protobuf/encoding/protojson"
)

var policy_kasRegistryCmd *cobra.Command
var policy_kasRegistryCmd = man.Docs.GetCommand("policy/kas-registry")

func policy_getKeyAccessRegistry(cmd *cobra.Command, args []string) {
c := cli.New(cmd, args)
Expand All @@ -34,6 +34,7 @@ func policy_getKeyAccessRegistry(cmd *cobra.Command, args []string) {
if kas.GetPublicKey().GetRemote() != "" {
key.PublicKey = &policy.PublicKey_Remote{Remote: kas.GetPublicKey().GetRemote()}
}

rows := [][]string{
{"Id", kas.GetId()},
{"URI", kas.GetUri()},
Expand Down Expand Up @@ -69,7 +70,6 @@ func policy_listKeyAccessRegistries(cmd *cobra.Command, args []string) {
cli.NewUUIDColumn(),
table.NewFlexColumn("uri", "URI", cli.FlexColumnWidthFour),
table.NewFlexColumn("name", "Name", cli.FlexColumnWidthThree),
table.NewFlexColumn("pk", "PublicKey", cli.FlexColumnWidthFour),
)
rows := []table.Row{}
for _, kas := range list {
Expand Down Expand Up @@ -102,10 +102,6 @@ func policy_createKeyAccessRegistry(cmd *cobra.Command, args []string) {
name := c.Flags.GetOptionalString("name")
metadataLabels = c.Flags.GetStringSlice("label", metadataLabels, cli.FlagsStringSliceOptions{Min: 0})

if cachedJSON == "" && remote == "" {
cli.ExitWithError("Empty flags 'public-keys' and 'public-key-remote'", errors.New("error: a public key is required"))
}

key := new(policy.PublicKey)
if cachedJSON != "" {
if remote != "" {
Expand Down Expand Up @@ -353,9 +349,6 @@ func init() {
deleteDoc.GetDocFlag("force").Description,
)

doc := man.Docs.GetCommand("policy/kas-registry",
man.WithSubcommands(createDoc, getDoc, listDoc, updateDoc, deleteDoc),
)
policy_kasRegistryCmd = &doc.Command
policyCmd.AddCommand(policy_kasRegistryCmd)
policy_kasRegistryCmd.AddSubcommands(createDoc, getDoc, listDoc, updateDoc, deleteDoc)
policyCmd.AddCommand(&policy_kasRegistryCmd.Command)
}
154 changes: 153 additions & 1 deletion cmd/policy-attributeNamespaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"strconv"

"github.com/charmbracelet/lipgloss"
"github.com/evertras/bubble-table/table"
"github.com/opentdf/otdfctl/pkg/cli"
"github.com/opentdf/otdfctl/pkg/man"
Expand All @@ -12,6 +13,7 @@ import (

var (
policy_attributeNamespacesCmd = man.Docs.GetCommand("policy/attributes/namespaces")
policy_NamespaceKeysCmd = man.Docs.GetCommand("policy/attributes/namespaces/keys")

forceUnsafe bool
)
Expand Down Expand Up @@ -265,6 +267,107 @@ func policy_unsafeUpdateAttributeNamespace(cmd *cobra.Command, args []string) {
HandleSuccess(cmd, ns.GetId(), t, ns)
}

func policy_NamespaceKeysAdd(cmd *cobra.Command, args []string) {
c := cli.New(cmd, args)
h := NewHandler(c)
defer h.Close()

ns := c.Flags.GetRequiredString("namespace")
pkID := c.Flags.GetRequiredID("public-key-id")

_, err := h.AddPublicKeyToNamespace(c.Context(), ns, pkID)
if err != nil {
cli.ExitWithError("Failed to add public key to namespace", err)
}

rows := [][]string{
{"Public Key Id", pkID},
{"Namespace", ns},
}

t := cli.NewTabular(rows...)

HandleSuccess(cmd, "Public key added to namespace", t, nil)
}

func policy_NamespaceKeysRemove(cmd *cobra.Command, args []string) {
c := cli.New(cmd, args)
h := NewHandler(c)
defer h.Close()

ns := c.Flags.GetRequiredString("namespace")
pkID := c.Flags.GetRequiredID("public-key-id")

_, err := h.RemovePublicKeyFromNamespace(c.Context(), ns, pkID)
if err != nil {
cli.ExitWithError("Failed to remove public key from namespace", err)
}

rows := [][]string{
{"Public Key Id", pkID},
{"Namespace", ns},
}

t := cli.NewTabular(rows...)

HandleSuccess(cmd, "Public key removed from namespace", t, nil)
}

func policy_NamespaceKeysListcmd(cmd *cobra.Command, args []string) {
c := cli.New(cmd, args)
h := NewHandler(c)
defer h.Close()

ns := c.Flags.GetRequiredString("namespace")
showPublicKey := c.Flags.GetOptionalBool("show-public-key")

list, err := h.GetNamespace(ns)
if err != nil {
cli.ExitWithError("Failed to list namespace keys", err)
}

columns := []table.Column{
table.NewFlexColumn("kas_name", "KAS Name", cli.FlexColumnWidthThree),
table.NewFlexColumn("kas_uri", "KAS URI", cli.FlexColumnWidthThree),
table.NewFlexColumn("kid", "Key ID", cli.FlexColumnWidthThree),
table.NewFlexColumn("alg", "Algorithm", cli.FlexColumnWidthThree),
}

if showPublicKey {
columns = append(columns, table.NewFlexColumn("public_key", "Public Key", cli.FlexColumnWidthFour))
}

t := cli.NewTable(columns...)
rows := []table.Row{}
for _, key := range list.GetKeys() {
alg, err := enumToAlg(key.GetPublicKey().GetAlg())
if err != nil {
cli.ExitWithError("Failed to get algorithm", err)
}

rowStyle := lipgloss.NewStyle().BorderBottom(true).BorderStyle(lipgloss.NormalBorder())

if key.GetIsActive().GetValue() {
rowStyle = rowStyle.Background(cli.ColorGreen.Background)
} else {
rowStyle = rowStyle.Background(cli.ColorRed.Background)
}

rd := table.RowData{
"key_id": key.GetPublicKey().GetKid(),
"algorithm": alg,
"is_active": key.GetIsActive().GetValue(),
"kas_id": key.GetKas().GetId(),
"kas_name": key.GetKas().GetName(),
"kas_uri": key.GetKas().GetUri(),
"public_key": key.GetPublicKey().GetPem(),
}
rows = append(rows, table.NewRow(rd).WithStyle(rowStyle))
}
t = t.WithRows(rows)
HandleSuccess(cmd, "", t, list)
}

func init() {
getCmd := man.Docs.GetCommand("policy/attributes/namespaces/get",
man.WithRun(policy_getAttributeNamespace),
Expand Down Expand Up @@ -367,6 +470,55 @@ func init() {
)
unsafeCmd.AddSubcommands(deleteCmd, reactivateCmd, unsafeUpdateCmd)

policy_attributeNamespacesCmd.AddSubcommands(getCmd, listCmd, createDoc, updateCmd, deactivateCmd, unsafeCmd)
namespaceKeysAddDoc := man.Docs.GetCommand("policy/attributes/namespaces/keys/add",
man.WithRun(policy_NamespaceKeysAdd),
)
namespaceKeysAddDoc.Flags().StringP(
namespaceKeysAddDoc.GetDocFlag("namespace").Name,
namespaceKeysAddDoc.GetDocFlag("namespace").Shorthand,
namespaceKeysAddDoc.GetDocFlag("namespace").Default,
namespaceKeysAddDoc.GetDocFlag("namespace").Description,
)
namespaceKeysAddDoc.Flags().StringP(
namespaceKeysAddDoc.GetDocFlag("public-key-id").Name,
namespaceKeysAddDoc.GetDocFlag("public-key-id").Shorthand,
namespaceKeysAddDoc.GetDocFlag("public-key-id").Default,
namespaceKeysAddDoc.GetDocFlag("public-key-id").Description,
)

namespaceKeysRemoveDoc := man.Docs.GetCommand("policy/attributes/namespaces/keys/remove",
man.WithRun(policy_NamespaceKeysRemove),
)
namespaceKeysRemoveDoc.Flags().StringP(
namespaceKeysRemoveDoc.GetDocFlag("namespace").Name,
namespaceKeysRemoveDoc.GetDocFlag("namespace").Shorthand,
namespaceKeysRemoveDoc.GetDocFlag("namespace").Default,
namespaceKeysRemoveDoc.GetDocFlag("namespace").Description,
)
namespaceKeysRemoveDoc.Flags().StringP(
namespaceKeysRemoveDoc.GetDocFlag("public-key-id").Name,
namespaceKeysRemoveDoc.GetDocFlag("public-key-id").Shorthand,
namespaceKeysRemoveDoc.GetDocFlag("public-key-id").Default,
namespaceKeysRemoveDoc.GetDocFlag("public-key-id").Description,
)

namespaceKeysListDoc := man.Docs.GetCommand("policy/attributes/namespaces/keys/list",
man.WithRun(policy_NamespaceKeysListcmd),
)
namespaceKeysListDoc.Flags().StringP(
namespaceKeysListDoc.GetDocFlag("namespace").Name,
namespaceKeysListDoc.GetDocFlag("namespace").Shorthand,
namespaceKeysListDoc.GetDocFlag("namespace").Default,
namespaceKeysListDoc.GetDocFlag("namespace").Description,
)
namespaceKeysListDoc.Flags().BoolP(
namespaceKeysListDoc.GetDocFlag("show-public-key").Name,
namespaceKeysListDoc.GetDocFlag("show-public-key").Shorthand,
namespaceKeysListDoc.GetDocFlag("show-public-key").DefaultAsBool(),
namespaceKeysListDoc.GetDocFlag("show-public-key").Description,
)

policy_NamespaceKeysCmd.AddSubcommands(namespaceKeysAddDoc, namespaceKeysRemoveDoc, namespaceKeysListDoc)
policy_attributeNamespacesCmd.AddSubcommands(getCmd, listCmd, createDoc, updateCmd, deactivateCmd, unsafeCmd, policy_NamespaceKeysCmd)
policy_attributesCmd.AddCommand(&policy_attributeNamespacesCmd.Command)
}
Loading
Loading