Skip to content

Commit

Permalink
fix(storage): specify credentials with STORAGE_EMULATOR_HOST (#7271)
Browse files Browse the repository at this point in the history
Encountered the same problem with googleapis/google-api-go-client#558 when specifying STORAGE_EMULATOR_HOST. For our case, we'd like to pass option.WithTokenSource, and it raises the following error with STORAGE_EMULATOR_HOST during the test. To avoid the error, we should pass internaloption.SkipDialSettingsValidation as the Spanner client does.

Co-authored-by: Chris Cotter <cjcotter@google.com>
  • Loading branch information
shuheiktgw and tritone committed Feb 14, 2023
1 parent bd90ad9 commit 940ae15
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
10 changes: 6 additions & 4 deletions storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,12 @@ func NewClient(ctx context.Context, opts ...option.ClientOption) (*Client, error
endpoint := hostURL.String()

// Append the emulator host as default endpoint for the user
opts = append([]option.ClientOption{option.WithoutAuthentication()}, opts...)

opts = append(opts, internaloption.WithDefaultEndpoint(endpoint))
opts = append(opts, internaloption.WithDefaultMTLSEndpoint(endpoint))
opts = append([]option.ClientOption{
option.WithoutAuthentication(),
internaloption.SkipDialSettingsValidation(),
internaloption.WithDefaultEndpoint(endpoint),
internaloption.WithDefaultMTLSEndpoint(endpoint),
}, opts...)
}

// htransport selects the correct endpoint among WithEndpoint (user override), WithDefaultEndpoint, and WithDefaultMTLSEndpoint.
Expand Down
11 changes: 11 additions & 0 deletions storage/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1992,6 +1992,17 @@ func TestAttrToFieldMapCoverage(t *testing.T) {
}
}

func TestEmulatorWithCredentialsFile(t *testing.T) {
t.Setenv("STORAGE_EMULATOR_HOST", "localhost:1234")

client, err := NewClient(context.Background(), option.WithCredentialsFile("/path/to/key.json"))
if err != nil {
t.Fatalf("failed creating a client with credentials file when running agains an emulator: %v", err)
return
}
client.Close()
}

// Create a client using a combination of custom endpoint and
// STORAGE_EMULATOR_HOST env variable and verify that raw.BasePath (used
// for writes) and readHost and scheme (used for reads) are all set correctly.
Expand Down

0 comments on commit 940ae15

Please sign in to comment.