Skip to content

Commit c73d3b1

Browse files
committed
fix: gcp-active-account
1 parent e74ef33 commit c73d3b1

File tree

2 files changed

+40
-18
lines changed

2 files changed

+40
-18
lines changed

pkg/skaffold/gcp/auth.go

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@ limitations under the License.
1717
package gcp
1818

1919
import (
20+
"bytes"
2021
"context"
2122
"encoding/json"
2223
"fmt"
2324
"os/exec"
2425
"sync"
26+
"time"
2527

2628
"github.com/docker/cli/cli/config/configfile"
29+
"golang.org/x/oauth2"
2730
"golang.org/x/oauth2/google"
2831

2932
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/output/log"
@@ -59,33 +62,41 @@ func AutoConfigureGCRCredentialHelper(cf *configfile.ConfigFile) {
5962
}
6063
}
6164

65+
type token struct {
66+
AccessToken string `json:"access_token"`
67+
TokenExpiry time.Time `json:"token_expiry"`
68+
}
69+
70+
type tokenSource struct {
71+
}
72+
73+
func (ts tokenSource) Token() (*oauth2.Token, error) {
74+
cmd := exec.Command("gcloud", "auth", "print-identity-token", "--format=json")
75+
var body bytes.Buffer
76+
cmd.Stdout = &body
77+
err := util.RunCmd(context.TODO(), cmd)
78+
if err != nil {
79+
return nil, fmt.Errorf("failed to get access token %v", err)
80+
}
81+
var t token
82+
if err := json.Unmarshal(body.Bytes(), &t); err != nil {
83+
return nil, fmt.Errorf("failed to get access token %v", err)
84+
}
85+
return &oauth2.Token{AccessToken: t.AccessToken, Expiry: t.TokenExpiry}, nil
86+
}
87+
6288
func activeUserCredentials(ctx context.Context) (*google.Credentials, error) {
6389
credsOnce.Do(func() {
64-
cmd := exec.Command("gcloud", "auth", "print-access-token", "--format=json")
65-
body, err := util.RunCmdOut(ctx, cmd)
90+
var ts tokenSource
91+
t, err := ts.Token()
6692
if err != nil {
6793
log.Entry(context.TODO()).Infof("unable to retrieve gcloud access token: %v", err)
6894
log.Entry(context.TODO()).Info("falling back to application default credentials")
6995
credsErr = fmt.Errorf("retrieving gcloud access token: %w", err)
7096
return
7197
}
72-
jsonCreds := make(map[string]interface{})
73-
json.Unmarshal(body, &jsonCreds)
74-
jsonCreds["type"] = "authorized_user"
75-
body, _ = json.Marshal(jsonCreds)
7698

77-
c, err := google.CredentialsFromJSON(context.Background(), body)
78-
if err != nil {
79-
log.Entry(context.TODO()).Infof("unable to retrieve google creds: %v", err)
80-
log.Entry(context.TODO()).Info("falling back to application default credentials")
81-
return
82-
}
83-
_, err = c.TokenSource.Token()
84-
if err != nil {
85-
log.Entry(context.TODO()).Infof("unable to retrieve token: %v", err)
86-
log.Entry(context.TODO()).Info("falling back to application default credentials")
87-
return
88-
}
99+
c := &google.Credentials{TokenSource: oauth2.ReuseTokenSource(t, ts)}
89100
creds = c
90101
})
91102

pkg/skaffold/gcp/auth_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ limitations under the License.
2020
package gcp
2121

2222
import (
23+
"context"
24+
"fmt"
2325
"testing"
2426

2527
"github.com/docker/cli/cli/config/configfile"
@@ -111,3 +113,12 @@ func TestAutoConfigureGCRCredentialHelper(t *testing.T) {
111113
})
112114
}
113115
}
116+
117+
func TestTesting(t *testing.T) {
118+
credentials, err := activeUserCredentials(context.TODO())
119+
if err != nil {
120+
fmt.Println(err)
121+
fmt.Println("err")
122+
}
123+
fmt.Println(credentials.TokenSource.Token())
124+
}

0 commit comments

Comments
 (0)