Skip to content

Use gcp.NewHTTPClient and pass creds - #454

Merged
zeroshade merged 5 commits into
apache:mainfrom
agchang:agchang/gcs-http-client
Jun 17, 2025
Merged

zeroshade merged 5 commits into
apache:mainfrom
agchang:agchang/gcs-http-client

Conversation

@agchang

@agchang agchang commented Jun 5, 2025 •

Copy link
Copy Markdown
Contributor

Otherwise we aren't able to access private blobs with credentials.

If no credentials are found, fall back to the anonymous http client.

Otherwise we aren't able to access private blobs with credentials
Comment thread io/gcs.go Outdated
func createGCSBucket(ctx context.Context, parsed *url.URL, props map[string]string) (*blob.Bucket, error) {
gcscfg := ParseGCSConfig(props)
client := gcp.NewAnonymousHTTPClient(gcp.DefaultTransport())
creds, err := gcp.DefaultCredentials(ctx) // or use service-account JSON

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you elaborate how this would work?
how we alter creds to service account here? is this something like magic env-vars?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, there's a convention that a lot of Google SDKs use where they look at well-known locations like .config/gcloud/application_default_credentials.json or allow an env var override via $GOOGLE_APPLICATION_CREDENTIALS.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated the code to still fallback to the old method if creds aren't found, so it won't break existing use cases.

@laskoviymishka laskoviymishka left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think if comment would be added somewhere here it would be awesome, but beside this look cool.

@zeroshade zeroshade left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any easy way to have a test for this?

There can be further work to test it works in the cases where Google
application default creds can and cannot be found, but I'm not sure
the proper way to test that now
@agchang

agchang commented Jun 6, 2025

Copy link
Copy Markdown
Contributor Author

Is there any easy way to have a test for this?

I added an integration test, but couldn't figure out a good way to test the auth case. fake-gcs-server afaict doesn't support auth, so the only other way is somehow passing in a custom spy object (not sure where, didn't look into it deeply) that checks the auth headers are set, but that might not be worth the effort. At least with this test we can check both cases where credentials are or are not found, and things work e2e. Let me know what you think!

@loicalleyne

Copy link
Copy Markdown
Contributor

@agchang The current implementation's GCS client uses Application Default Credentials.
https://gocloud.dev/howto/blob/#gcs

blob.OpenBucket will use Application Default Credentials; if you have authenticated via gcloud auth application-default login, it will use those credentials. See Application Default Credentials to learn about authentication alternatives, including using environment variables.

@agchang

agchang commented Jun 13, 2025

Copy link
Copy Markdown
Contributor Author

@agchang The current implementation's GCS client uses Application Default Credentials. https://gocloud.dev/howto/blob/#gcs

blob.OpenBucket will use Application Default Credentials; if you have authenticated via gcloud auth application-default login, it will use those credentials. See Application Default Credentials to learn about authentication alternatives, including using environment variables.

I'm pretty sure I tested this with $GOOGLE_APPLICATION_CREDENTIALS set (and I have run gcloud auth application-default login, so there are creds in the well-known .config path). I think the issue is that the AnonymousHTTPClient doesn't use the OAuth RoundTripper so credentials are never added to the header

@laskoviymishka laskoviymishka left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice one, thx for adding this to recipe.

@zeroshade zeroshade left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@zeroshade

Copy link
Copy Markdown
Member

Looks like a lint issue needs to be fixed before I can merge :)

@agchang

agchang commented Jun 13, 2025

Copy link
Copy Markdown
Contributor Author

Looks like a lint issue needs to be fixed before I can merge :)

I think I fixed it (ran gofumpt). Thanks!

@agchang

agchang commented Jun 14, 2025

Copy link
Copy Markdown
Contributor Author

@zeroshade I saw the failed integration test run and made the fix - sorry for the back and forth! I hope that was the last one :)

Comment thread io/gcs.go
Comment on lines +52 to +54
if _, ok := props[GCSUseJsonAPI]; ok {
o = append(o, storage.WithJSONReads())
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this the only reason why we're pulling in cloud.google.com/go/storage? I think that's okay for now, but we may want to see if we potentially contribute to gocloud.dev/gcp instead of pulling an entire other library in

@zeroshade
zeroshade merged commit ca05334 into apache:main Jun 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants