Skip to content

Commit b7de83d

Browse files
committed
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.
1 parent ab7ed76 commit b7de83d

4 files changed

Lines changed: 34 additions & 6 deletions

File tree

‎internal/cmd/auth/activate-service-account/activate_service_account.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
9696
rt, err := sdkAuth.SetupAuth(cfg)
9797
if err != nil {
9898
params.Printer.Debug(print.ErrorLevel, "setup auth: %v", err)
99-
return &cliErr.ActivateServiceAccountError{}
99+
return &cliErr.ActivateServiceAccountError{Err: err}
100100
}
101101

102102
// Authenticates the service account and stores credentials
@@ -180,7 +180,7 @@ func runOIDCMode(params *types.CmdParams, model *inputModel) error {
180180
rt, err := sdkAuth.SetupAuth(wifCfg)
181181
if err != nil {
182182
params.Printer.Debug(print.ErrorLevel, "setup workload identity federation auth: %v", err)
183-
return &cliErr.ActivateServiceAccountError{}
183+
return &cliErr.ActivateServiceAccountError{Err: err}
184184
}
185185

186186
// credentials are never written to disk in OIDC mode

‎internal/pkg/auth/service_account.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func AuthenticateServiceAccount(p *print.Printer, rt http.RoundTripper, disableW
5252
accessToken, err := flow.GetAccessToken()
5353
if err != nil {
5454
p.Debug(print.ErrorLevel, "get access token: %v", err)
55-
return "", "", &errors.ActivateServiceAccountError{}
55+
return "", "", &errors.ActivateServiceAccountError{Err: err}
5656
}
5757

5858
serviceAccountKey := flow.GetConfig().ServiceAccountKey
@@ -79,7 +79,7 @@ func AuthenticateServiceAccount(p *print.Printer, rt http.RoundTripper, disableW
7979
accessToken, err := flow.GetAccessToken()
8080
if err != nil {
8181
p.Debug(print.ErrorLevel, "get workload identity access token: %v", err)
82-
return "", "", &errors.ActivateServiceAccountError{}
82+
return "", "", &errors.ActivateServiceAccountError{Err: err}
8383
}
8484

8585
authFields[ACCESS_TOKEN] = accessToken

‎internal/pkg/errors/errors.go‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,10 +280,22 @@ func (e *AccessTokenExpiredError) Error() string {
280280
return ACCESS_TOKEN_EXPIRED
281281
}
282282

283-
type ActivateServiceAccountError struct{}
283+
// ActivateServiceAccountError indicates that the service account activation failed.
284+
// When the underlying cause is set it is appended to the message, so it is visible without
285+
// enabling debug verbosity.
286+
type ActivateServiceAccountError struct {
287+
Err error // Optional: underlying error
288+
}
284289

285290
func (e *ActivateServiceAccountError) Error() string {
286-
return FAILED_SERVICE_ACCOUNT_ACTIVATION
291+
if e.Err == nil {
292+
return FAILED_SERVICE_ACCOUNT_ACTIVATION
293+
}
294+
return fmt.Sprintf("%s\n\nCause: %v", FAILED_SERVICE_ACCOUNT_ACTIVATION, e.Err)
295+
}
296+
297+
func (e *ActivateServiceAccountError) Unwrap() error {
298+
return e.Err
287299
}
288300

289301
type SetInexistentProfile struct {

‎internal/pkg/errors/errors_test.go‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package errors
33
import (
44
"errors"
55
"fmt"
6+
"strings"
67
"testing"
78

89
"github.com/spf13/cobra"
@@ -100,6 +101,21 @@ func TestSimpleErrors(t *testing.T) {
100101
}
101102
}
102103

104+
func TestActivateServiceAccountErrorWithCause(t *testing.T) {
105+
cause := errors.New("get new access token: non-OK 401 status: assertion did not match federation requirements")
106+
err := &ActivateServiceAccountError{Err: cause}
107+
108+
if !strings.HasPrefix(err.Error(), FAILED_SERVICE_ACCOUNT_ACTIVATION) {
109+
t.Fatalf("expected the activation message to be preserved, got %s", err.Error())
110+
}
111+
if !strings.Contains(err.Error(), cause.Error()) {
112+
t.Fatalf("expected the cause to be visible, got %s", err.Error())
113+
}
114+
if !errors.Is(err, cause) {
115+
t.Fatal("expected the cause to stay in the error chain")
116+
}
117+
}
118+
103119
func TestObservabilityInputPlanError(t *testing.T) {
104120
tests := []struct {
105121
description string

0 commit comments

Comments
 (0)