From 989e3a4967761c9c32409219b0450637cc95c44b Mon Sep 17 00:00:00 2001 From: alice-yin Date: Sun, 8 Nov 2026 22:13:08 -0800 Subject: [PATCH] Fix encryption validation CLI issues from the EVE bug bash - Remove `encryption-validation enable`; `set --mode warn|deny` is the single way to configure validation, so users don't mix enable and set. - `get` shows the server defaults (metadata key `encoding`, values `[binary/encrypted]`) when the key or values are unset. - `set` and `disable` error when nothing would change unless --idempotent is set, matching their help text and other commands. - Reject blank --metadata-value before prompting instead of failing at the API. Co-Authored-By: Claude Opus 5.5 --- temporalcloudcli/commands.gen.go | 50 +-- ...ommands.namespace.encryption_validation.go | 90 +++--- ...ds.namespace.encryption_validation_test.go | 300 +++++++----------- temporalcloudcli/commands.yml | 56 ++-- 4 files changed, 178 insertions(+), 318 deletions(-) diff --git a/temporalcloudcli/commands.gen.go b/temporalcloudcli/commands.gen.go index 7bb14ff..5da860d 100644 --- a/temporalcloudcli/commands.gen.go +++ b/temporalcloudcli/commands.gen.go @@ -2633,7 +2633,6 @@ func NewCloudNamespaceEncryptionValidationCommand(cctx *CommandContext, parent * s.Command.Long = "Commands for managing payload encryption validation configuration of Temporal Cloud namespaces." s.Command.Args = cobra.NoArgs s.Command.AddCommand(&NewCloudNamespaceEncryptionValidationDisableCommand(cctx, &s).Command) - s.Command.AddCommand(&NewCloudNamespaceEncryptionValidationEnableCommand(cctx, &s).Command) s.Command.AddCommand(&NewCloudNamespaceEncryptionValidationGetCommand(cctx, &s).Command) s.Command.AddCommand(&NewCloudNamespaceEncryptionValidationSetCommand(cctx, &s).Command) return &s @@ -2655,9 +2654,9 @@ func NewCloudNamespaceEncryptionValidationDisableCommand(cctx *CommandContext, p s.Command.Use = "disable [flags]" s.Command.Short = "[Experimental] Disable payload encryption validation for a namespace" if hasHighlighting { - s.Command.Long = "Disable payload encryption validation for a Temporal Cloud namespace.\nExisting metadata and inspect settings are kept. If validation is not\nconfigured or is already disabled, nothing is changed.\n\nExample:\n\n\x1b[1mtemporal cloud namespace encryption-validation disable --namespace my-namespace.my-account\x1b[0m" + s.Command.Long = "Disable payload encryption validation for a Temporal Cloud namespace.\nExisting metadata and inspect settings are kept. To turn validation back\non, use \x1b[1mset --mode warn\x1b[0m or \x1b[1mset --mode deny\x1b[0m. If validation is not\nconfigured or is already disabled, the command errors unless\n--idempotent is set.\n\nExample:\n\n\x1b[1mtemporal cloud namespace encryption-validation disable --namespace my-namespace.my-account\x1b[0m" } else { - s.Command.Long = "Disable payload encryption validation for a Temporal Cloud namespace.\nExisting metadata and inspect settings are kept. If validation is not\nconfigured or is already disabled, nothing is changed.\n\nExample:\n\n```\ntemporal cloud namespace encryption-validation disable --namespace my-namespace.my-account\n```" + s.Command.Long = "Disable payload encryption validation for a Temporal Cloud namespace.\nExisting metadata and inspect settings are kept. To turn validation back\non, use `set --mode warn` or `set --mode deny`. If validation is not\nconfigured or is already disabled, the command errors unless\n--idempotent is set.\n\nExample:\n\n```\ntemporal cloud namespace encryption-validation disable --namespace my-namespace.my-account\n```" } s.Command.Args = cobra.NoArgs s.ClientOptions.BuildFlags(s.Command.Flags()) @@ -2672,41 +2671,6 @@ func NewCloudNamespaceEncryptionValidationDisableCommand(cctx *CommandContext, p return &s } -type CloudNamespaceEncryptionValidationEnableCommand struct { - Parent *CloudNamespaceEncryptionValidationCommand - Command cobra.Command - ClientOptions - NamespaceOptions - AsyncOperationOptions - ResourceVersionOptions - Deny bool -} - -func NewCloudNamespaceEncryptionValidationEnableCommand(cctx *CommandContext, parent *CloudNamespaceEncryptionValidationCommand) *CloudNamespaceEncryptionValidationEnableCommand { - var s CloudNamespaceEncryptionValidationEnableCommand - s.Parent = parent - s.Command.DisableFlagsInUseLine = true - s.Command.Use = "enable [flags]" - s.Command.Short = "[Experimental] Enable payload encryption validation for a namespace" - if hasHighlighting { - s.Command.Long = "Enable payload encryption validation for a Temporal Cloud namespace.\nIf validation has not been configured, it is enabled in warn mode with\nno metadata key or values, so the defaults apply\n(metadata-key=encoding, metadata-values=[binary/encrypted]).\nIf validation is disabled, the existing configuration is re-enabled.\nUse --deny to enable in deny mode; this also switches a namespace that\nis already in warn mode to deny. Without --deny, a namespace that is\nalready in warn or deny mode is left unchanged.\n\nExample:\n\n\x1b[1mtemporal cloud namespace encryption-validation enable --namespace my-namespace.my-account\x1b[0m" - } else { - s.Command.Long = "Enable payload encryption validation for a Temporal Cloud namespace.\nIf validation has not been configured, it is enabled in warn mode with\nno metadata key or values, so the defaults apply\n(metadata-key=encoding, metadata-values=[binary/encrypted]).\nIf validation is disabled, the existing configuration is re-enabled.\nUse --deny to enable in deny mode; this also switches a namespace that\nis already in warn mode to deny. Without --deny, a namespace that is\nalready in warn or deny mode is left unchanged.\n\nExample:\n\n```\ntemporal cloud namespace encryption-validation enable --namespace my-namespace.my-account\n```" - } - s.Command.Args = cobra.NoArgs - s.Command.Flags().BoolVar(&s.Deny, "deny", false, "Enable validation in deny mode instead of warn. Switches warn mode to deny.") - s.ClientOptions.BuildFlags(s.Command.Flags()) - s.NamespaceOptions.BuildFlags(s.Command.Flags()) - s.AsyncOperationOptions.BuildFlags(s.Command.Flags()) - s.ResourceVersionOptions.BuildFlags(s.Command.Flags()) - s.Command.Run = func(c *cobra.Command, args []string) { - if err := s.run(cctx, args); err != nil { - cctx.Options.Fail(err) - } - } - return &s -} - type CloudNamespaceEncryptionValidationGetCommand struct { Parent *CloudNamespaceEncryptionValidationCommand Command cobra.Command @@ -2721,9 +2685,9 @@ func NewCloudNamespaceEncryptionValidationGetCommand(cctx *CommandContext, paren s.Command.Use = "get [flags]" s.Command.Short = "[Experimental] Get namespace payload encryption validation configuration" if hasHighlighting { - s.Command.Long = "Retrieve the current payload encryption validation configuration for a Temporal Cloud namespace.\n\nExample:\n\n\x1b[1mtemporal cloud namespace encryption-validation get --namespace my-namespace.my-account\x1b[0m" + s.Command.Long = "Retrieve the current payload encryption validation configuration for a Temporal Cloud namespace.\nIf the metadata key or values are not set, the server defaults that apply are shown\n(metadata-key=encoding, metadata-values=[binary/encrypted]).\n\nExample:\n\n\x1b[1mtemporal cloud namespace encryption-validation get --namespace my-namespace.my-account\x1b[0m" } else { - s.Command.Long = "Retrieve the current payload encryption validation configuration for a Temporal Cloud namespace.\n\nExample:\n\n```\ntemporal cloud namespace encryption-validation get --namespace my-namespace.my-account\n```" + s.Command.Long = "Retrieve the current payload encryption validation configuration for a Temporal Cloud namespace.\nIf the metadata key or values are not set, the server defaults that apply are shown\n(metadata-key=encoding, metadata-values=[binary/encrypted]).\n\nExample:\n\n```\ntemporal cloud namespace encryption-validation get --namespace my-namespace.my-account\n```" } s.Command.Args = cobra.NoArgs s.ClientOptions.BuildFlags(s.Command.Flags()) @@ -2757,14 +2721,14 @@ func NewCloudNamespaceEncryptionValidationSetCommand(cctx *CommandContext, paren s.Command.Use = "set [flags]" s.Command.Short = "[Experimental] Set namespace payload encryption validation configuration" if hasHighlighting { - s.Command.Long = "Update the payload encryption validation configuration for a Temporal Cloud\nnamespace. At least one flag must be set. Omitted flags keep the\nexisting values. An empty metadata key or values means the server\ndefaults apply (metadata-key=encoding, metadata-values=[binary/encrypted]).\n\nExample:\n\n\x1b[1mtemporal cloud namespace encryption-validation set --namespace my-namespace.my-account --mode warn --metadata-key encoding --metadata-value binary/encrypted\x1b[0m" + s.Command.Long = "Configure payload encryption validation for a Temporal Cloud namespace.\nUse this command to turn validation on (--mode warn or --mode deny) and to\nchange its settings. At least one flag must be set. Omitted flags keep the\nexisting values. An empty metadata key means the server default applies\n(metadata-key=encoding); omit --metadata-value to keep the existing values,\nwhich default to [binary/encrypted]. If nothing would change, the command\nerrors unless --idempotent is set.\n\nExamples:\n\n\x1b[1mtemporal cloud namespace encryption-validation set --namespace my-namespace.my-account --mode warn\ntemporal cloud namespace encryption-validation set --namespace my-namespace.my-account --mode deny --metadata-key encoding --metadata-value binary/encrypted\x1b[0m" } else { - s.Command.Long = "Update the payload encryption validation configuration for a Temporal Cloud\nnamespace. At least one flag must be set. Omitted flags keep the\nexisting values. An empty metadata key or values means the server\ndefaults apply (metadata-key=encoding, metadata-values=[binary/encrypted]).\n\nExample:\n\n```\ntemporal cloud namespace encryption-validation set --namespace my-namespace.my-account --mode warn --metadata-key encoding --metadata-value binary/encrypted\n```" + s.Command.Long = "Configure payload encryption validation for a Temporal Cloud namespace.\nUse this command to turn validation on (--mode warn or --mode deny) and to\nchange its settings. At least one flag must be set. Omitted flags keep the\nexisting values. An empty metadata key means the server default applies\n(metadata-key=encoding); omit --metadata-value to keep the existing values,\nwhich default to [binary/encrypted]. If nothing would change, the command\nerrors unless --idempotent is set.\n\nExamples:\n\n```\ntemporal cloud namespace encryption-validation set --namespace my-namespace.my-account --mode warn\ntemporal cloud namespace encryption-validation set --namespace my-namespace.my-account --mode deny --metadata-key encoding --metadata-value binary/encrypted\n```" } s.Command.Args = cobra.NoArgs s.Command.Flags().StringVar(&s.Mode, "mode", "", "Payload encryption validation mode. Valid values: disabled, warn, deny. If omitted, the existing mode is kept. Required when encryption validation is not configured yet.") s.Command.Flags().StringVar(&s.MetadataKey, "metadata-key", "", "Payload metadata key used to identify encrypted payloads. If omitted, the existing key is kept.") - s.Command.Flags().StringArrayVar(&s.MetadataValue, "metadata-value", nil, "Payload metadata value used to identify encrypted payloads. Repeat to add additional values. Replaces the existing values. If omitted, the existing values are kept.") + s.Command.Flags().StringArrayVar(&s.MetadataValue, "metadata-value", nil, "Payload metadata value used to identify encrypted payloads. Repeat to add additional values. Replaces the existing values. Values cannot be blank. If omitted, the existing values are kept.") s.Command.Flags().BoolVar(&s.InspectHeader, "inspect-header", false, "Inspect workflow headers for encryption validation. If omitted, the existing value is kept.") s.Command.Flags().BoolVar(&s.InspectFailure, "inspect-failure", false, "Inspect Failure payloads for encryption validation. If omitted, the existing value is kept.") s.ClientOptions.BuildFlags(s.Command.Flags()) diff --git a/temporalcloudcli/commands.namespace.encryption_validation.go b/temporalcloudcli/commands.namespace.encryption_validation.go index 41b4d87..b47b5f7 100644 --- a/temporalcloudcli/commands.namespace.encryption_validation.go +++ b/temporalcloudcli/commands.namespace.encryption_validation.go @@ -18,7 +18,9 @@ const ( encryptionValidationModeWarn = "warn" encryptionValidationModeDeny = "deny" - alreadyInDesiredStateMessage = "Resource already in desired state" + // Server-side defaults applied when the metadata key or values are left empty. + defaultEncryptionValidationMetadataKey = "encoding" + defaultEncryptionValidationMetadataValue = "binary/encrypted" ) var encryptionValidationSetFlags = []string{"mode", "metadata-key", "metadata-value", "inspect-header", "inspect-failure"} @@ -35,7 +37,7 @@ func (c *CloudNamespaceEncryptionValidationGetCommand) run(cctx *CommandContext, var spec *namespacev1.EncryptionValidationSpec if res.Namespace.Spec != nil { - spec = res.Namespace.Spec.EncryptionValidation + spec = withEncryptionValidationDefaults(res.Namespace.Spec.EncryptionValidation) } return cctx.Printer.PrintResource(struct { Namespace string `json:"namespace"` @@ -52,6 +54,9 @@ func (c *CloudNamespaceEncryptionValidationSetCommand) run(cctx *CommandContext, return errors.New("at least one of --mode, --metadata-key, --metadata-value, --inspect-header, " + "or --inspect-failure must be set") } + if flags.Changed("metadata-value") && slices.ContainsFunc(c.MetadataValue, isBlank) { + return errors.New("--metadata-value cannot be blank") + } var mode namespacev1.EncryptionValidationSpec_EncryptionValidationMode if flags.Changed("mode") { var err error @@ -94,8 +99,7 @@ func (c *CloudNamespaceEncryptionValidationSetCommand) run(cctx *CommandContext, newSpec.EncryptionValidation.InspectFailure = c.InspectFailure } if proto.Equal(ns.Spec, newSpec) { - cctx.Printer.Println(alreadyInDesiredStateMessage) - return nil + return encryptionValidationNothingChanged(cctx, c.Idempotent) } yes, err := cctx.GetPrompter().PromptApply(ns.Spec, newSpec, false) @@ -119,7 +123,7 @@ func (c *CloudNamespaceEncryptionValidationSetCommand) run(cctx *CommandContext, return cctx.GetPoller(client, c.AsyncOperationOptions).HandleUpdateOperation(cctx, resp, err) } -func (c *CloudNamespaceEncryptionValidationEnableCommand) run(cctx *CommandContext, _ []string) error { +func (c *CloudNamespaceEncryptionValidationDisableCommand) run(cctx *CommandContext, _ []string) error { client, err := cctx.GetCloudClient(c.ClientOptions) if err != nil { return err @@ -130,21 +134,13 @@ func (c *CloudNamespaceEncryptionValidationEnableCommand) run(cctx *CommandConte } ns := res.Namespace - newSpec := proto.Clone(ns.Spec).(*namespacev1.NamespaceSpec) - if newSpec.EncryptionValidation == nil { - newSpec.EncryptionValidation = &namespacev1.EncryptionValidationSpec{} - } - ev := newSpec.EncryptionValidation - // --deny always results in deny mode. Without it, an existing warn/deny mode is kept. - if c.Deny { - ev.Mode = namespacev1.EncryptionValidationSpec_ENCRYPTION_VALIDATION_MODE_DENY - } else if ev.Mode == namespacev1.EncryptionValidationSpec_ENCRYPTION_VALIDATION_MODE_UNSPECIFIED || - ev.Mode == namespacev1.EncryptionValidationSpec_ENCRYPTION_VALIDATION_MODE_DISABLED { - ev.Mode = namespacev1.EncryptionValidationSpec_ENCRYPTION_VALIDATION_MODE_WARN + if ns.Spec.EncryptionValidation == nil { + return encryptionValidationNothingChanged(cctx, c.Idempotent) } + newSpec := proto.Clone(ns.Spec).(*namespacev1.NamespaceSpec) + newSpec.EncryptionValidation.Mode = namespacev1.EncryptionValidationSpec_ENCRYPTION_VALIDATION_MODE_DISABLED if proto.Equal(ns.Spec, newSpec) { - cctx.Printer.Println(alreadyInDesiredStateMessage) - return nil + return encryptionValidationNothingChanged(cctx, c.Idempotent) } yes, err := cctx.GetPrompter().PromptApply(ns.Spec, newSpec, false) @@ -152,7 +148,7 @@ func (c *CloudNamespaceEncryptionValidationEnableCommand) run(cctx *CommandConte return err } if !yes { - return errors.New("Aborting enable.") + return errors.New("Aborting disable.") } rv := ns.ResourceVersion @@ -168,47 +164,35 @@ func (c *CloudNamespaceEncryptionValidationEnableCommand) run(cctx *CommandConte return cctx.GetPoller(client, c.AsyncOperationOptions).HandleUpdateOperation(cctx, resp, err) } -func (c *CloudNamespaceEncryptionValidationDisableCommand) run(cctx *CommandContext, _ []string) error { - client, err := cctx.GetCloudClient(c.ClientOptions) - if err != nil { - return err - } - res, err := client.GetNamespace(cctx, &cloudservice.GetNamespaceRequest{Namespace: c.Namespace}) - if err != nil { - return err +// encryptionValidationNothingChanged handles a request that would not change the namespace. +// It matches the server's "nothing to change" handling used by other commands: an error by +// default, or an "unchanged" result when --idempotent is set. +func encryptionValidationNothingChanged(cctx *CommandContext, idempotent bool) error { + if idempotent { + return cctx.Printer.PrintStructured(newUnchangedResult(), printer.StructuredOptions{}) } + return errors.New("nothing to change: encryption validation is already in the requested state " + + "(use --idempotent to succeed without changes)") +} - ns := res.Namespace - if ns.Spec.EncryptionValidation == nil { - cctx.Printer.Println(alreadyInDesiredStateMessage) +// withEncryptionValidationDefaults returns a copy of spec with the server defaults filled in +// for an empty metadata key or empty metadata values, so `get` shows what is actually enforced. +func withEncryptionValidationDefaults(spec *namespacev1.EncryptionValidationSpec) *namespacev1.EncryptionValidationSpec { + if spec == nil { return nil } - newSpec := proto.Clone(ns.Spec).(*namespacev1.NamespaceSpec) - newSpec.EncryptionValidation.Mode = namespacev1.EncryptionValidationSpec_ENCRYPTION_VALIDATION_MODE_DISABLED - if proto.Equal(ns.Spec, newSpec) { - cctx.Printer.Println(alreadyInDesiredStateMessage) - return nil - } - - yes, err := cctx.GetPrompter().PromptApply(ns.Spec, newSpec, false) - if err != nil { - return err + out := proto.Clone(spec).(*namespacev1.EncryptionValidationSpec) + if out.MetadataKey == "" { + out.MetadataKey = defaultEncryptionValidationMetadataKey } - if !yes { - return errors.New("Aborting disable.") + if len(out.MetadataValues) == 0 { + out.MetadataValues = []string{defaultEncryptionValidationMetadataValue} } + return out +} - rv := ns.ResourceVersion - if c.ResourceVersion != "" { - rv = c.ResourceVersion - } - resp, err := client.UpdateNamespace(cctx, &cloudservice.UpdateNamespaceRequest{ - Namespace: c.Namespace, - Spec: newSpec, - ResourceVersion: rv, - AsyncOperationId: c.AsyncOperationId, - }) - return cctx.GetPoller(client, c.AsyncOperationOptions).HandleUpdateOperation(cctx, resp, err) +func isBlank(s string) bool { + return strings.TrimSpace(s) == "" } func parseEncryptionValidationMode(value string) (namespacev1.EncryptionValidationSpec_EncryptionValidationMode, error) { diff --git a/temporalcloudcli/commands.namespace.encryption_validation_test.go b/temporalcloudcli/commands.namespace.encryption_validation_test.go index cf6931e..23409a7 100644 --- a/temporalcloudcli/commands.namespace.encryption_validation_test.go +++ b/temporalcloudcli/commands.namespace.encryption_validation_test.go @@ -60,6 +60,51 @@ func TestNamespaceEncryptionValidationGet(t *testing.T) { }, }, }, + { + // The server applies defaults for an empty key or values; get shows them. + name: "DefaultsShownWhenUnset", + cmd: temporalcloudcli.CloudNamespaceEncryptionValidationGetCommand{ + NamespaceOptions: temporalcloudcli.NamespaceOptions{Namespace: "my-ns.my-acct"}, + }, + cloudClientExpectations: func(c *cloudmock.MockCloudServiceClient) { + c.EXPECT(). + GetNamespace(mock.Anything, &cloudservice.GetNamespaceRequest{Namespace: "my-ns.my-acct"}, mock.Anything). + Return(&cloudservice.GetNamespaceResponse{Namespace: namespaceWithEncryptionValidation(&namespacev1.EncryptionValidationSpec{ + Mode: namespacev1.EncryptionValidationSpec_ENCRYPTION_VALIDATION_MODE_DENY, + })}, nil) + }, + expectedJsonOutput: map[string]any{ + "namespace": "my-ns.my-acct", + "spec": map[string]any{ + "mode": "ENCRYPTION_VALIDATION_MODE_DENY", + "metadataKey": "encoding", + "metadataValues": []any{"binary/encrypted"}, + }, + }, + }, + { + // Only the missing key is defaulted; configured values are shown as-is. + name: "DefaultKeyWithCustomValues", + cmd: temporalcloudcli.CloudNamespaceEncryptionValidationGetCommand{ + NamespaceOptions: temporalcloudcli.NamespaceOptions{Namespace: "my-ns.my-acct"}, + }, + cloudClientExpectations: func(c *cloudmock.MockCloudServiceClient) { + c.EXPECT(). + GetNamespace(mock.Anything, &cloudservice.GetNamespaceRequest{Namespace: "my-ns.my-acct"}, mock.Anything). + Return(&cloudservice.GetNamespaceResponse{Namespace: namespaceWithEncryptionValidation(&namespacev1.EncryptionValidationSpec{ + Mode: namespacev1.EncryptionValidationSpec_ENCRYPTION_VALIDATION_MODE_DENY, + MetadataValues: []string{"binary/custom", "json/plain"}, + })}, nil) + }, + expectedJsonOutput: map[string]any{ + "namespace": "my-ns.my-acct", + "spec": map[string]any{ + "mode": "ENCRYPTION_VALIDATION_MODE_DENY", + "metadataKey": "encoding", + "metadataValues": []any{"binary/custom", "json/plain"}, + }, + }, + }, { name: "NilSpec", cmd: temporalcloudcli.CloudNamespaceEncryptionValidationGetCommand{ @@ -109,6 +154,7 @@ func TestNamespaceEncryptionValidationSet(t *testing.T) { promptOptions temporalcloudcli.TestPromptOptions asyncPollerOptions temporalcloudcli.TestAsyncPollerOptions expectedErr string + expectedJsonOutput any }{ { name: "AllFlags", @@ -235,6 +281,41 @@ func TestNamespaceEncryptionValidationSet(t *testing.T) { Mode: namespacev1.EncryptionValidationSpec_ENCRYPTION_VALIDATION_MODE_WARN, })}, nil) }, + expectedErr: "nothing to change: encryption validation is already in the requested state " + + "(use --idempotent to succeed without changes)", + }, + { + name: "NoChangeIdempotent", + cmd: temporalcloudcli.CloudNamespaceEncryptionValidationSetCommand{ + NamespaceOptions: temporalcloudcli.NamespaceOptions{Namespace: "my-ns.my-acct"}, + AsyncOperationOptions: temporalcloudcli.AsyncOperationOptions{Idempotent: true}, + }, + args: []string{"--mode=warn"}, + cloudClientExpectations: func(c *cloudmock.MockCloudServiceClient) { + c.EXPECT(). + GetNamespace(mock.Anything, mock.Anything, mock.Anything). + Return(&cloudservice.GetNamespaceResponse{Namespace: namespaceWithEncryptionValidation(&namespacev1.EncryptionValidationSpec{ + Mode: namespacev1.EncryptionValidationSpec_ENCRYPTION_VALIDATION_MODE_WARN, + })}, nil) + }, + expectedJsonOutput: map[string]any{"status": "unchanged"}, + }, + { + // Rejected before any API call. + name: "BlankMetadataValue", + cmd: temporalcloudcli.CloudNamespaceEncryptionValidationSetCommand{ + NamespaceOptions: temporalcloudcli.NamespaceOptions{Namespace: "my-ns.my-acct"}, + }, + args: []string{"--metadata-value=binary/encrypted", "--metadata-value= "}, + expectedErr: "--metadata-value cannot be blank", + }, + { + name: "EmptyMetadataValue", + cmd: temporalcloudcli.CloudNamespaceEncryptionValidationSetCommand{ + NamespaceOptions: temporalcloudcli.NamespaceOptions{Namespace: "my-ns.my-acct"}, + }, + args: []string{"--metadata-value="}, + expectedErr: "--metadata-value cannot be blank", }, { name: "ModeRequiredWhenUnconfigured", @@ -357,190 +438,7 @@ func TestNamespaceEncryptionValidationSet(t *testing.T) { AsyncPollerOptions: tt.asyncPollerOptions, JSONOutput: true, ExpectedError: tt.expectedErr, - }) - }) - } -} - -func TestNamespaceEncryptionValidationEnable(t *testing.T) { - disabledWithCustomMetadata := &namespacev1.EncryptionValidationSpec{ - Mode: namespacev1.EncryptionValidationSpec_ENCRYPTION_VALIDATION_MODE_DISABLED, - MetadataKey: "custom-key", - MetadataValues: []string{"custom-value"}, - InspectHeader: true, - } - warn := &namespacev1.EncryptionValidationSpec{ - Mode: namespacev1.EncryptionValidationSpec_ENCRYPTION_VALIDATION_MODE_WARN, - MetadataKey: "encoding", - MetadataValues: []string{"binary/encrypted"}, - } - deny := &namespacev1.EncryptionValidationSpec{ - Mode: namespacev1.EncryptionValidationSpec_ENCRYPTION_VALIDATION_MODE_DENY, - MetadataKey: "encoding", - MetadataValues: []string{"binary/encrypted"}, - } - - tests := []struct { - name string - cmd temporalcloudcli.CloudNamespaceEncryptionValidationEnableCommand - cloudClientExpectations func(*cloudmock.MockCloudServiceClient) - promptOptions temporalcloudcli.TestPromptOptions - asyncPollerOptions temporalcloudcli.TestAsyncPollerOptions - expectedErr string - }{ - { - name: "NilToWarn", - cmd: temporalcloudcli.CloudNamespaceEncryptionValidationEnableCommand{ - NamespaceOptions: temporalcloudcli.NamespaceOptions{Namespace: "my-ns.my-acct"}, - }, - cloudClientExpectations: func(c *cloudmock.MockCloudServiceClient) { - c.EXPECT(). - GetNamespace(mock.Anything, &cloudservice.GetNamespaceRequest{Namespace: "my-ns.my-acct"}, mock.Anything). - Return(&cloudservice.GetNamespaceResponse{Namespace: namespaceWithEncryptionValidation(nil)}, nil) - c.EXPECT(). - UpdateNamespace(mock.Anything, mock.MatchedBy(func(req *cloudservice.UpdateNamespaceRequest) bool { - return req.Namespace == "my-ns.my-acct" && - req.ResourceVersion == "rv-fetched" && - proto.Equal(req.Spec.EncryptionValidation, &namespacev1.EncryptionValidationSpec{ - Mode: namespacev1.EncryptionValidationSpec_ENCRYPTION_VALIDATION_MODE_WARN, - }) - }), mock.Anything). - Return(&cloudservice.UpdateNamespaceResponse{ - AsyncOperation: &operation.AsyncOperation{Id: "op-enable"}, - }, nil) - }, - promptOptions: temporalcloudcli.TestPromptOptions{ExpectPrompApply: true, PromptResult: true}, - asyncPollerOptions: temporalcloudcli.TestAsyncPollerOptions{AsyncOperationID: "op-enable"}, - }, - { - name: "DisabledKeepsMetadata", - cmd: temporalcloudcli.CloudNamespaceEncryptionValidationEnableCommand{ - NamespaceOptions: temporalcloudcli.NamespaceOptions{Namespace: "my-ns.my-acct"}, - }, - cloudClientExpectations: func(c *cloudmock.MockCloudServiceClient) { - c.EXPECT(). - GetNamespace(mock.Anything, mock.Anything, mock.Anything). - Return(&cloudservice.GetNamespaceResponse{Namespace: namespaceWithEncryptionValidation(disabledWithCustomMetadata)}, nil) - c.EXPECT(). - UpdateNamespace(mock.Anything, mock.MatchedBy(func(req *cloudservice.UpdateNamespaceRequest) bool { - return proto.Equal(req.Spec.EncryptionValidation, &namespacev1.EncryptionValidationSpec{ - Mode: namespacev1.EncryptionValidationSpec_ENCRYPTION_VALIDATION_MODE_WARN, - MetadataKey: "custom-key", - MetadataValues: []string{"custom-value"}, - InspectHeader: true, - }) - }), mock.Anything). - Return(&cloudservice.UpdateNamespaceResponse{ - AsyncOperation: &operation.AsyncOperation{Id: "op-enable"}, - }, nil) - }, - promptOptions: temporalcloudcli.TestPromptOptions{ExpectPrompApply: true, PromptResult: true}, - asyncPollerOptions: temporalcloudcli.TestAsyncPollerOptions{AsyncOperationID: "op-enable"}, - }, - { - name: "DenyFlag", - cmd: temporalcloudcli.CloudNamespaceEncryptionValidationEnableCommand{ - NamespaceOptions: temporalcloudcli.NamespaceOptions{Namespace: "my-ns.my-acct"}, - Deny: true, - }, - cloudClientExpectations: func(c *cloudmock.MockCloudServiceClient) { - c.EXPECT(). - GetNamespace(mock.Anything, mock.Anything, mock.Anything). - Return(&cloudservice.GetNamespaceResponse{Namespace: namespaceWithEncryptionValidation(disabledWithCustomMetadata)}, nil) - c.EXPECT(). - UpdateNamespace(mock.Anything, mock.MatchedBy(func(req *cloudservice.UpdateNamespaceRequest) bool { - return proto.Equal(req.Spec.EncryptionValidation, &namespacev1.EncryptionValidationSpec{ - Mode: namespacev1.EncryptionValidationSpec_ENCRYPTION_VALIDATION_MODE_DENY, - MetadataKey: "custom-key", - MetadataValues: []string{"custom-value"}, - InspectHeader: true, - }) - }), mock.Anything). - Return(&cloudservice.UpdateNamespaceResponse{ - AsyncOperation: &operation.AsyncOperation{Id: "op-enable"}, - }, nil) - }, - promptOptions: temporalcloudcli.TestPromptOptions{ExpectPrompApply: true, PromptResult: true}, - asyncPollerOptions: temporalcloudcli.TestAsyncPollerOptions{AsyncOperationID: "op-enable"}, - }, - { - name: "WarnToDenyWithDenyFlag", - cmd: temporalcloudcli.CloudNamespaceEncryptionValidationEnableCommand{ - NamespaceOptions: temporalcloudcli.NamespaceOptions{Namespace: "my-ns.my-acct"}, - Deny: true, - }, - cloudClientExpectations: func(c *cloudmock.MockCloudServiceClient) { - c.EXPECT(). - GetNamespace(mock.Anything, mock.Anything, mock.Anything). - Return(&cloudservice.GetNamespaceResponse{Namespace: namespaceWithEncryptionValidation(warn)}, nil) - c.EXPECT(). - UpdateNamespace(mock.Anything, mock.MatchedBy(func(req *cloudservice.UpdateNamespaceRequest) bool { - return proto.Equal(req.Spec.EncryptionValidation, deny) - }), mock.Anything). - Return(&cloudservice.UpdateNamespaceResponse{ - AsyncOperation: &operation.AsyncOperation{Id: "op-enable"}, - }, nil) - }, - promptOptions: temporalcloudcli.TestPromptOptions{ExpectPrompApply: true, PromptResult: true}, - asyncPollerOptions: temporalcloudcli.TestAsyncPollerOptions{AsyncOperationID: "op-enable"}, - }, - { - // No-op cases: no prompt and no UpdateNamespace call are expected. - name: "AlreadyWarn", - cmd: temporalcloudcli.CloudNamespaceEncryptionValidationEnableCommand{ - NamespaceOptions: temporalcloudcli.NamespaceOptions{Namespace: "my-ns.my-acct"}, - }, - cloudClientExpectations: func(c *cloudmock.MockCloudServiceClient) { - c.EXPECT(). - GetNamespace(mock.Anything, mock.Anything, mock.Anything). - Return(&cloudservice.GetNamespaceResponse{Namespace: namespaceWithEncryptionValidation(warn)}, nil) - }, - }, - { - name: "AlreadyDeny", - cmd: temporalcloudcli.CloudNamespaceEncryptionValidationEnableCommand{ - NamespaceOptions: temporalcloudcli.NamespaceOptions{Namespace: "my-ns.my-acct"}, - }, - cloudClientExpectations: func(c *cloudmock.MockCloudServiceClient) { - c.EXPECT(). - GetNamespace(mock.Anything, mock.Anything, mock.Anything). - Return(&cloudservice.GetNamespaceResponse{Namespace: namespaceWithEncryptionValidation(deny)}, nil) - }, - }, - { - name: "GetNamespaceError", - cmd: temporalcloudcli.CloudNamespaceEncryptionValidationEnableCommand{ - NamespaceOptions: temporalcloudcli.NamespaceOptions{Namespace: "my-ns.my-acct"}, - }, - cloudClientExpectations: func(c *cloudmock.MockCloudServiceClient) { - c.EXPECT(). - GetNamespace(mock.Anything, mock.Anything, mock.Anything). - Return(nil, errors.New("namespace not found")) - }, - expectedErr: "namespace not found", - }, - { - name: "PromptDeclined", - cmd: temporalcloudcli.CloudNamespaceEncryptionValidationEnableCommand{ - NamespaceOptions: temporalcloudcli.NamespaceOptions{Namespace: "my-ns.my-acct"}, - }, - cloudClientExpectations: func(c *cloudmock.MockCloudServiceClient) { - c.EXPECT(). - GetNamespace(mock.Anything, mock.Anything, mock.Anything). - Return(&cloudservice.GetNamespaceResponse{Namespace: namespaceWithEncryptionValidation(nil)}, nil) - }, - promptOptions: temporalcloudcli.TestPromptOptions{ExpectPrompApply: true, PromptResult: false}, - expectedErr: "Aborting enable.", - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - temporalcloudcli.TestCommand(t, &tt.cmd, temporalcloudcli.TestCommandOptions{ - CloudClientExpectations: tt.cloudClientExpectations, - PromptOptions: tt.promptOptions, - AsyncPollerOptions: tt.asyncPollerOptions, - JSONOutput: true, - ExpectedError: tt.expectedErr, + ExpectedOutputJson: tt.expectedJsonOutput, }) }) } @@ -560,6 +458,7 @@ func TestNamespaceEncryptionValidationDisable(t *testing.T) { promptOptions temporalcloudcli.TestPromptOptions asyncPollerOptions temporalcloudcli.TestAsyncPollerOptions expectedErr string + expectedJsonOutput any }{ { name: "DisableFromEnabled", @@ -599,6 +498,23 @@ func TestNamespaceEncryptionValidationDisable(t *testing.T) { Mode: namespacev1.EncryptionValidationSpec_ENCRYPTION_VALIDATION_MODE_DISABLED, })}, nil) }, + expectedErr: "nothing to change: encryption validation is already in the requested state " + + "(use --idempotent to succeed without changes)", + }, + { + name: "AlreadyDisabledIdempotent", + cmd: temporalcloudcli.CloudNamespaceEncryptionValidationDisableCommand{ + NamespaceOptions: temporalcloudcli.NamespaceOptions{Namespace: "my-ns.my-acct"}, + AsyncOperationOptions: temporalcloudcli.AsyncOperationOptions{Idempotent: true}, + }, + cloudClientExpectations: func(c *cloudmock.MockCloudServiceClient) { + c.EXPECT(). + GetNamespace(mock.Anything, mock.Anything, mock.Anything). + Return(&cloudservice.GetNamespaceResponse{Namespace: namespaceWithEncryptionValidation(&namespacev1.EncryptionValidationSpec{ + Mode: namespacev1.EncryptionValidationSpec_ENCRYPTION_VALIDATION_MODE_DISABLED, + })}, nil) + }, + expectedJsonOutput: map[string]any{"status": "unchanged"}, }, { name: "Unconfigured", @@ -610,6 +526,21 @@ func TestNamespaceEncryptionValidationDisable(t *testing.T) { GetNamespace(mock.Anything, mock.Anything, mock.Anything). Return(&cloudservice.GetNamespaceResponse{Namespace: namespaceWithEncryptionValidation(nil)}, nil) }, + expectedErr: "nothing to change: encryption validation is already in the requested state " + + "(use --idempotent to succeed without changes)", + }, + { + name: "UnconfiguredIdempotent", + cmd: temporalcloudcli.CloudNamespaceEncryptionValidationDisableCommand{ + NamespaceOptions: temporalcloudcli.NamespaceOptions{Namespace: "my-ns.my-acct"}, + AsyncOperationOptions: temporalcloudcli.AsyncOperationOptions{Idempotent: true}, + }, + cloudClientExpectations: func(c *cloudmock.MockCloudServiceClient) { + c.EXPECT(). + GetNamespace(mock.Anything, mock.Anything, mock.Anything). + Return(&cloudservice.GetNamespaceResponse{Namespace: namespaceWithEncryptionValidation(nil)}, nil) + }, + expectedJsonOutput: map[string]any{"status": "unchanged"}, }, { name: "UpdateNamespaceError", @@ -636,6 +567,7 @@ func TestNamespaceEncryptionValidationDisable(t *testing.T) { AsyncPollerOptions: tt.asyncPollerOptions, JSONOutput: true, ExpectedError: tt.expectedErr, + ExpectedOutputJson: tt.expectedJsonOutput, }) }) } diff --git a/temporalcloudcli/commands.yml b/temporalcloudcli/commands.yml index c4a947f..2aa25d5 100644 --- a/temporalcloudcli/commands.yml +++ b/temporalcloudcli/commands.yml @@ -774,6 +774,8 @@ commands: summary: '[Experimental] Get namespace payload encryption validation configuration' description: | Retrieve the current payload encryption validation configuration for a Temporal Cloud namespace. + If the metadata key or values are not set, the server defaults that apply are shown + (metadata-key=encoding, metadata-values=[binary/encrypted]). Example: @@ -787,15 +789,19 @@ commands: - name: cloud namespace encryption-validation set summary: '[Experimental] Set namespace payload encryption validation configuration' description: | - Update the payload encryption validation configuration for a Temporal Cloud - namespace. At least one flag must be set. Omitted flags keep the - existing values. An empty metadata key or values means the server - defaults apply (metadata-key=encoding, metadata-values=[binary/encrypted]). + Configure payload encryption validation for a Temporal Cloud namespace. + Use this command to turn validation on (--mode warn or --mode deny) and to + change its settings. At least one flag must be set. Omitted flags keep the + existing values. An empty metadata key means the server default applies + (metadata-key=encoding); omit --metadata-value to keep the existing values, + which default to [binary/encrypted]. If nothing would change, the command + errors unless --idempotent is set. - Example: + Examples: ``` - temporal cloud namespace encryption-validation set --namespace my-namespace.my-account --mode warn --metadata-key encoding --metadata-value binary/encrypted + temporal cloud namespace encryption-validation set --namespace my-namespace.my-account --mode warn + temporal cloud namespace encryption-validation set --namespace my-namespace.my-account --mode deny --metadata-key encoding --metadata-value binary/encrypted ``` has-init: false option-sets: @@ -819,8 +825,8 @@ commands: type: string[] description: | Payload metadata value used to identify encrypted payloads. Repeat to - add additional values. Replaces the existing values. If omitted, the - existing values are kept. + add additional values. Replaces the existing values. Values cannot be + blank. If omitted, the existing values are kept. - name: inspect-header type: bool description: | @@ -831,40 +837,14 @@ commands: description: | Inspect Failure payloads for encryption validation. If omitted, the existing value is kept. - - name: cloud namespace encryption-validation enable - summary: '[Experimental] Enable payload encryption validation for a namespace' - description: | - Enable payload encryption validation for a Temporal Cloud namespace. - If validation has not been configured, it is enabled in warn mode with - no metadata key or values, so the defaults apply - (metadata-key=encoding, metadata-values=[binary/encrypted]). - If validation is disabled, the existing configuration is re-enabled. - Use --deny to enable in deny mode; this also switches a namespace that - is already in warn mode to deny. Without --deny, a namespace that is - already in warn or deny mode is left unchanged. - - Example: - - ``` - temporal cloud namespace encryption-validation enable --namespace my-namespace.my-account - ``` - has-init: false - option-sets: - - client - - namespace - - async-operation - - resource-version - options: - - name: deny - type: bool - description: | - Enable validation in deny mode instead of warn. Switches warn mode to deny. - name: cloud namespace encryption-validation disable summary: '[Experimental] Disable payload encryption validation for a namespace' description: | Disable payload encryption validation for a Temporal Cloud namespace. - Existing metadata and inspect settings are kept. If validation is not - configured or is already disabled, nothing is changed. + Existing metadata and inspect settings are kept. To turn validation back + on, use `set --mode warn` or `set --mode deny`. If validation is not + configured or is already disabled, the command errors unless + --idempotent is set. Example: