Repository navigation
Use gcp.NewHTTPClient and pass creds - #454
Conversation
Otherwise we aren't able to access private blobs with credentials
| 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 |
There was a problem hiding this comment.
can you elaborate how this would work?
how we alter creds to service account here? is this something like magic env-vars?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
i think if comment would be added somewhere here it would be awesome, but beside this look cool.
zeroshade
left a comment
There was a problem hiding this comment.
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
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! |
|
@agchang The current implementation's GCS client uses Application Default Credentials.
|
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 |
laskoviymishka
left a comment
There was a problem hiding this comment.
nice one, thx for adding this to recipe.
|
Looks like a lint issue needs to be fixed before I can merge :) |
I think I fixed it (ran gofumpt). Thanks! |
|
@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 :) |
| if _, ok := props[GCSUseJsonAPI]; ok { | ||
| o = append(o, storage.WithJSONReads()) | ||
| } |
There was a problem hiding this comment.
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
Otherwise we aren't able to access private blobs with credentials.
If no credentials are found, fall back to the anonymous http client.