Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

internal/resource: fix gs:// fetches in GCE without a service account #1363

Merged
merged 1 commit into from
May 10, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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