Skip to content

Commit d117646

Browse files
authored
fix(internaloption): AuthCreds should honor WithoutAuthentication (#3166)
In this case the function will return a nil error and a nil credential. This API is currently only used by storage, whom have asked for this usage change. Also, fixed godoc that was not rendering properly before. Internal Bug: 419426629
1 parent 1e2b782 commit d117646

File tree

3 files changed

+31
-10
lines changed

3 files changed

+31
-10
lines changed

internal/creds.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,13 @@ func Creds(ctx context.Context, ds *DialSettings) (*google.Credentials, error) {
4747
// options provided via [option.ClientOption], including legacy oauth2/google
4848
// options. If there are no applicable options, then it returns the result of
4949
// [cloud.google.com/go/auth/credentials.DetectDefault].
50+
// Note: If NoAuth is true, when [google.golang.org/api/option.WithoutAuthentication]
51+
// is passed, then no authentication will be performed and this function will
52+
// return nil, nil.
5053
func AuthCreds(ctx context.Context, settings *DialSettings) (*auth.Credentials, error) {
54+
if settings.NoAuth {
55+
return nil, nil
56+
}
5157
if settings.AuthCredentials != nil {
5258
return settings.AuthCredentials, nil
5359
}

internal/creds_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,3 +645,17 @@ func TestIsSelfSignedJWTFlow(t *testing.T) {
645645
}
646646
}
647647
}
648+
649+
func TestNewAuth_NoAuth(t *testing.T) {
650+
ctx := context.Background()
651+
ds := &DialSettings{
652+
NoAuth: true,
653+
}
654+
creds, err := AuthCreds(ctx, ds)
655+
if err != nil {
656+
t.Fatalf("got %v, want nil error", err)
657+
}
658+
if creds != nil {
659+
t.Fatalf("got %v, want nil creds", creds)
660+
}
661+
}

option/internaloption/internaloption.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -289,21 +289,22 @@ func GetLogger(opts []option.ClientOption) *slog.Logger {
289289
// options provided via [option.ClientOption], including legacy oauth2/google
290290
// options, in this order:
291291
//
292-
// * [option.WithAuthCredentials]
293-
// * [option/internaloption.WithCredentials] (internal use only)
294-
// * [option.WithCredentials]
295-
// * [option.WithTokenSource]
292+
// - [option.WithoutAuthentication]
293+
// - [option.WithAuthCredentials]
294+
// - [WithCredentials] (internal use only)
295+
// - [option.WithCredentials]
296+
// - [option.WithTokenSource]
296297
//
297298
// If there are no applicable credentials options, then it passes the
298299
// following options to [cloud.google.com/go/auth/credentials.DetectDefault] and
299300
// returns the result:
300301
//
301-
// * [option.WithAudiences]
302-
// * [option.WithCredentialsFile]
303-
// * [option.WithCredentialsJSON]
304-
// * [option.WithScopes]
305-
// * [option/internaloption.WithDefaultScopes] (internal use only)
306-
// * [option/internaloption.EnableJwtWithScope] (internal use only)
302+
// - [option.WithAudiences]
303+
// - [option.WithCredentialsFile]
304+
// - [option.WithCredentialsJSON]
305+
// - [option.WithScopes]
306+
// - [WithDefaultScopes] (internal use only)
307+
// - [EnableJwtWithScope] (internal use only)
307308
//
308309
// This function should only be used internally by generated clients. This is an
309310
// EXPERIMENTAL API and may be changed or removed in the future.

0 commit comments

Comments
 (0)