Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions internal/pkg/auth/service_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
16 changes: 14 additions & 2 deletions internal/pkg/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
16 changes: 16 additions & 0 deletions internal/pkg/errors/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package errors
import (
"errors"
"fmt"
"strings"
"testing"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -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
Expand Down