-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix: use active gcp account #8584
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,13 +17,16 @@ limitations under the License. | |
package gcp | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"os/exec" | ||
"sync" | ||
"time" | ||
|
||
"github.com/docker/cli/cli/config/configfile" | ||
"golang.org/x/oauth2" | ||
"golang.org/x/oauth2/google" | ||
|
||
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/output/log" | ||
|
@@ -59,35 +62,51 @@ func AutoConfigureGCRCredentialHelper(cf *configfile.ConfigFile) { | |
} | ||
} | ||
|
||
func activeUserCredentials(ctx context.Context) (*google.Credentials, error) { | ||
type token struct { | ||
AccessToken string `json:"access_token"` | ||
TokenExpiry time.Time `json:"token_expiry"` | ||
} | ||
|
||
type tokenSource struct { | ||
} | ||
|
||
func (ts tokenSource) Token() (*oauth2.Token, error) { | ||
// the command return a json object containing id_token, access_token, token_expiry | ||
cmd := exec.Command("gcloud", "auth", "print-identity-token", "--format=json") | ||
var body bytes.Buffer | ||
cmd.Stdout = &body | ||
err := util.RunCmd(context.TODO(), cmd) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get access token %v", err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: might make sense to differentiate the error messages here so it is clearer which one of these 2 steps failed |
||
} | ||
var t token | ||
if err := json.Unmarshal(body.Bytes(), &t); err != nil { | ||
return nil, fmt.Errorf("failed to unmarshal gcould command result into access token %v", err) | ||
} | ||
return &oauth2.Token{AccessToken: t.AccessToken, Expiry: t.TokenExpiry}, nil | ||
} | ||
|
||
func activeUserCredentialsOnce() (*google.Credentials, error) { | ||
credsOnce.Do(func() { | ||
cmd := exec.Command("gcloud", "auth", "print-access-token", "--format=json") | ||
body, err := util.RunCmdOut(ctx, cmd) | ||
c, err := activeUserCredentials() | ||
if err != nil { | ||
log.Entry(context.TODO()).Infof("unable to retrieve gcloud access token: %v", err) | ||
log.Entry(context.TODO()).Info("falling back to application default credentials") | ||
credsErr = fmt.Errorf("retrieving gcloud access token: %w", err) | ||
return | ||
} | ||
jsonCreds := make(map[string]interface{}) | ||
json.Unmarshal(body, &jsonCreds) | ||
jsonCreds["type"] = "authorized_user" | ||
body, _ = json.Marshal(jsonCreds) | ||
|
||
c, err := google.CredentialsFromJSON(context.Background(), body) | ||
if err != nil { | ||
log.Entry(context.TODO()).Infof("unable to retrieve google creds: %v", err) | ||
log.Entry(context.TODO()).Info("falling back to application default credentials") | ||
return | ||
} | ||
_, err = c.TokenSource.Token() | ||
if err != nil { | ||
log.Entry(context.TODO()).Infof("unable to retrieve token: %v", err) | ||
log.Entry(context.TODO()).Info("falling back to application default credentials") | ||
return | ||
} | ||
creds = c | ||
}) | ||
|
||
return creds, credsErr | ||
} | ||
|
||
func activeUserCredentials() (*google.Credentials, error) { | ||
var ts tokenSource | ||
t, err := ts.Token() | ||
if err != nil { | ||
return nil, err | ||
} | ||
c := &google.Credentials{TokenSource: oauth2.ReuseTokenSource(t, ts)} | ||
return c, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ import ( | |
|
||
"google.golang.org/api/option" | ||
|
||
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/output/log" | ||
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/version" | ||
) | ||
|
||
|
@@ -31,7 +32,8 @@ func ClientOptions(ctx context.Context) []option.ClientOption { | |
option.WithUserAgent(version.UserAgent()), | ||
} | ||
|
||
creds, cErr := activeUserCredentials(ctx) | ||
creds, cErr := activeUserCredentialsOnce() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: might make sense to add a debug level log entry for |
||
log.Entry(ctx).Debug("unable to active user credentials %w", cErr) | ||
if cErr == nil && creds != nil { | ||
options = append(options, option.WithCredentials(creds)) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
parsing credential field from the
gcloud config config-helper
command output should be able to achieve the same effect, butgcloud config config-helper
is not an open knowledge, as may be removed as --help indicateNOTES This command is an internal implementation detail and may change or disappear without notice.