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