From b7de83d97517ecf0e8fc43c43c4f3fddfcaaeafb Mon Sep 17 00:00:00 2001 From: Paolo Valletta Date: Mon, 30 Nov 2026 17:26:19 +0100 Subject: [PATCH] fix(auth): keep the cause of a failed service account activation Activating a service account with OIDC reported only the generic guidance, and the underlying IdP response was logged at error level but only reachable with --verbosity debug, so the cause was hidden in the default output (issue #1691). ActivateServiceAccountError now carries the underlying error, like RequestFailedError already does, and the four sites that discarded the cause set it. The message keeps the existing text and appends the cause, so a failed activation prints the reason without enabling debug output. Successful commands are unchanged: the best-effort label lookups that log at error level are still debug-only, which is why the logging core is untouched. --- .../activate_service_account.go | 4 ++-- internal/pkg/auth/service_account.go | 4 ++-- internal/pkg/errors/errors.go | 16 ++++++++++++++-- internal/pkg/errors/errors_test.go | 16 ++++++++++++++++ 4 files changed, 34 insertions(+), 6 deletions(-) diff --git a/internal/cmd/auth/activate-service-account/activate_service_account.go b/internal/cmd/auth/activate-service-account/activate_service_account.go index 10e79d597..c3bccc356 100644 --- a/internal/cmd/auth/activate-service-account/activate_service_account.go +++ b/internal/cmd/auth/activate-service-account/activate_service_account.go @@ -96,7 +96,7 @@ func NewCmd(params *types.CmdParams) *cobra.Command { rt, err := sdkAuth.SetupAuth(cfg) if err != nil { params.Printer.Debug(print.ErrorLevel, "setup auth: %v", err) - return &cliErr.ActivateServiceAccountError{} + return &cliErr.ActivateServiceAccountError{Err: err} } // Authenticates the service account and stores credentials @@ -180,7 +180,7 @@ func runOIDCMode(params *types.CmdParams, model *inputModel) error { rt, err := sdkAuth.SetupAuth(wifCfg) if err != nil { params.Printer.Debug(print.ErrorLevel, "setup workload identity federation auth: %v", err) - return &cliErr.ActivateServiceAccountError{} + return &cliErr.ActivateServiceAccountError{Err: err} } // credentials are never written to disk in OIDC mode diff --git a/internal/pkg/auth/service_account.go b/internal/pkg/auth/service_account.go index a630c3598..c9bc93ade 100644 --- a/internal/pkg/auth/service_account.go +++ b/internal/pkg/auth/service_account.go @@ -52,7 +52,7 @@ func AuthenticateServiceAccount(p *print.Printer, rt http.RoundTripper, disableW accessToken, err := flow.GetAccessToken() if err != nil { p.Debug(print.ErrorLevel, "get access token: %v", err) - return "", "", &errors.ActivateServiceAccountError{} + return "", "", &errors.ActivateServiceAccountError{Err: err} } serviceAccountKey := flow.GetConfig().ServiceAccountKey @@ -79,7 +79,7 @@ func AuthenticateServiceAccount(p *print.Printer, rt http.RoundTripper, disableW accessToken, err := flow.GetAccessToken() if err != nil { p.Debug(print.ErrorLevel, "get workload identity access token: %v", err) - return "", "", &errors.ActivateServiceAccountError{} + return "", "", &errors.ActivateServiceAccountError{Err: err} } authFields[ACCESS_TOKEN] = accessToken diff --git a/internal/pkg/errors/errors.go b/internal/pkg/errors/errors.go index 7f7fbb57a..a88e364c6 100644 --- a/internal/pkg/errors/errors.go +++ b/internal/pkg/errors/errors.go @@ -280,10 +280,22 @@ func (e *AccessTokenExpiredError) Error() string { return ACCESS_TOKEN_EXPIRED } -type ActivateServiceAccountError struct{} +// ActivateServiceAccountError indicates that the service account activation failed. +// When the underlying cause is set it is appended to the message, so it is visible without +// enabling debug verbosity. +type ActivateServiceAccountError struct { + Err error // Optional: underlying error +} func (e *ActivateServiceAccountError) Error() string { - return FAILED_SERVICE_ACCOUNT_ACTIVATION + if e.Err == nil { + return FAILED_SERVICE_ACCOUNT_ACTIVATION + } + return fmt.Sprintf("%s\n\nCause: %v", FAILED_SERVICE_ACCOUNT_ACTIVATION, e.Err) +} + +func (e *ActivateServiceAccountError) Unwrap() error { + return e.Err } type SetInexistentProfile struct { diff --git a/internal/pkg/errors/errors_test.go b/internal/pkg/errors/errors_test.go index 548f5c761..f6ff253e2 100644 --- a/internal/pkg/errors/errors_test.go +++ b/internal/pkg/errors/errors_test.go @@ -3,6 +3,7 @@ package errors import ( "errors" "fmt" + "strings" "testing" "github.com/spf13/cobra" @@ -100,6 +101,21 @@ func TestSimpleErrors(t *testing.T) { } } +func TestActivateServiceAccountErrorWithCause(t *testing.T) { + cause := errors.New("get new access token: non-OK 401 status: assertion did not match federation requirements") + err := &ActivateServiceAccountError{Err: cause} + + if !strings.HasPrefix(err.Error(), FAILED_SERVICE_ACCOUNT_ACTIVATION) { + t.Fatalf("expected the activation message to be preserved, got %s", err.Error()) + } + if !strings.Contains(err.Error(), cause.Error()) { + t.Fatalf("expected the cause to be visible, got %s", err.Error()) + } + if !errors.Is(err, cause) { + t.Fatal("expected the cause to stay in the error chain") + } +} + func TestObservabilityInputPlanError(t *testing.T) { tests := []struct { description string