Skip to content

Commit

Permalink
Address more comments
Browse files Browse the repository at this point in the history
  • Loading branch information
shinfan committed Jun 17, 2021
1 parent 23081f6 commit 2052be6
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
10 changes: 6 additions & 4 deletions internal/creds.go
Expand Up @@ -63,12 +63,14 @@ const (
serviceAccountKey = "service_account"
)

// credentialsFromJSON returns a google.Credentials based on the input.
// credentialsFromJSON returns a google.Credentials from the JSON data
//
// - A self-signed JWT flow will be executed if the following conditions are
// met:
// (1) Either the scope for self-signed JWT flow is enabled or audiences are
// explicitly provided by users.
// (1) At least one of the following is true:
// (a) Scope for self-signed JWT flow is enabled
// (b) Audiences are explicitly provided by users
// (b) No scope is provided
// (2) No service account impersontation
//
// - Otherwise, executes standard OAuth 2.0 flow
Expand Down Expand Up @@ -97,7 +99,7 @@ func credentialsFromJSON(ctx context.Context, data []byte, ds *DialSettings) (*g
}

func isSelfSignedJWTFlow(data []byte, ds *DialSettings) (bool, error) {
if (ds.EnableJwtWithScope || ds.HasCustomAudience()) &&
if (ds.EnableJwtWithScope || ds.HasCustomAudience() || len(ds.GetScopes()) == 0) &&
ds.ImpersonationConfig == nil {
// Check if JSON is a service account and if so create a self-signed JWT.
var f struct {
Expand Down
50 changes: 50 additions & 0 deletions internal/creds_test.go
Expand Up @@ -118,6 +118,56 @@ func TestJWTWithScope(t *testing.T) {
}
}

func TestJWTWithDefaultScopes(t *testing.T) {
ctx := context.Background()

// Load a valid JSON file. No way to really test the contents; we just
// verify that there is no error.
ds := &DialSettings{
CredentialsFile: "testdata/service-account.json",
DefaultScopes: []string{"foo"},
EnableJwtWithScope: true,
}
if _, err := Creds(ctx, ds); err != nil {
t.Errorf("got %v, wanted no error", err)
}

// Load valid JSON. No way to really test the contents; we just
// verify that there is no error.
ds = &DialSettings{
CredentialsJSON: []byte(validServiceAccountJSON),
DefaultScopes: []string{"foo"},
EnableJwtWithScope: true,
}
if _, err := Creds(ctx, ds); err != nil {
t.Errorf("got %v, wanted no error", err)
}
}

func TestJWTWithDefaultAudience(t *testing.T) {
ctx := context.Background()

// Load a valid JSON file. No way to really test the contents; we just
// verify that there is no error.
ds := &DialSettings{
CredentialsFile: "testdata/service-account.json",
DefaultAudience: "foo",
}
if _, err := Creds(ctx, ds); err != nil {
t.Errorf("got %v, wanted no error", err)
}

// Load valid JSON. No way to really test the contents; we just
// verify that there is no error.
ds = &DialSettings{
CredentialsJSON: []byte(validServiceAccountJSON),
DefaultAudience: "foo",
}
if _, err := Creds(ctx, ds); err != nil {
t.Errorf("got %v, wanted no error", err)
}
}

func TestOAuth(t *testing.T) {
ctx := context.Background()

Expand Down

0 comments on commit 2052be6

Please sign in to comment.