Skip to content

Commit

Permalink
Merge pull request #1363 from bgilbert/gs
Browse files Browse the repository at this point in the history
internal/resource: fix `gs://` fetches in GCE without a service account
  • Loading branch information
bgilbert authored May 10, 2022
2 parents 427a35b + 2febcab commit 801a2ae
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions internal/resource/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,18 +354,23 @@ func (f *Fetcher) fetchFromDataURL(u url.URL, dest io.Writer, opts FetchOptions)
// credentials to fetch the object content.
func (f *Fetcher) fetchFromGCS(u url.URL, dest io.Writer, opts FetchOptions) error {
ctx := context.Background()
var clientOption option.ClientOption
if f.GCSSession == nil {
clientOption := option.WithoutAuthentication()
if metadata.OnGCE() {
id, _ := metadata.ProjectID()
creds := &google.Credentials{
ProjectID: id,
TokenSource: google.ComputeTokenSource("", storage.ScopeReadOnly),
// check whether the VM is associated with a service
// account
if _, err := metadata.Scopes(""); err == nil {
id, _ := metadata.ProjectID()
creds := &google.Credentials{
ProjectID: id,
TokenSource: google.ComputeTokenSource("", storage.ScopeReadOnly),
}
clientOption = option.WithCredentials(creds)
} else {
f.Logger.Debug("falling back to unauthenticated GCS access: %v", err)
}
clientOption = option.WithCredentials(creds)
} else {
f.Logger.Debug("falling back to unauthenticated GCS access")
clientOption = option.WithoutAuthentication()
f.Logger.Debug("falling back to unauthenticated GCS access: not running in GCE")
}

var err error
Expand Down

0 comments on commit 801a2ae

Please sign in to comment.