@@ -7,16 +7,32 @@ import (
77
88 "google3/identity/cloud/gaia/client/go/credentialrefresher"
99 "google3/net/base/go/health"
10+ "google3/security/corplogin/go/ssogoauth2"
1011 "google3/security/loas/go/loas"
1112 "github.com/bazelbuild/remote-apis-sdks/go/pkg/client"
13+ "google3/third_party/golang/oauth2/oauth2"
1214)
1315
1416type perRPCCreds struct {
15- r * credentialrefresher.CredentialRefresher
17+ r * credentialrefresher.CredentialRefresher
18+ ts oauth2.TokenSource
1619}
1720
1821func (creds * perRPCCreds ) GetRequestMetadata (ctx context.Context , uri ... string ) (map [string ]string , error ) {
19- t , err := creds .r .AccessToken (ctx )
22+ var t string
23+ var err error
24+ if creds .ts != nil {
25+ var tok * oauth2.Token
26+ tok , err = creds .ts .Token ()
27+ if err == nil {
28+ t = tok .AccessToken
29+ }
30+ } else if creds .r != nil {
31+ t , err = creds .r .AccessToken (ctx )
32+ } else {
33+ return nil , fmt .Errorf ("no credential provider configured" )
34+ }
35+
2036 if err != nil {
2137 return nil , err
2238 }
@@ -29,11 +45,10 @@ func (creds *perRPCCreds) RequireTransportSecurity() bool {
2945 return true
3046}
3147
32- func borgServiceAccountPerRPCCreds (ctx context.Context , useBorgServiceAccount bool ) (* client.PerRPCCreds , error ) {
33- if ! useBorgServiceAccount {
34- return nil , nil
35- }
36- account := credentialrefresher .BorgServiceAccountEmail (loas .Self ().User )
48+ func borgServiceAccountPerRPCCreds (ctx context.Context ) (* client.PerRPCCreds , error ) {
49+ user := loas .Self ().User
50+ account := credentialrefresher .BorgServiceAccountEmail (user )
51+
3752 r , err := credentialrefresher .New ("cloud-gaia" , account , []string {"https://www.googleapis.com/auth/cloud-platform" })
3853 if err != nil {
3954 return nil , fmt .Errorf ("unable to create a Cloud Gaia credential refresher: %w" , err )
@@ -45,3 +60,13 @@ func borgServiceAccountPerRPCCreds(ctx context.Context, useBorgServiceAccount bo
4560 Creds : & perRPCCreds {r : r },
4661 }, nil
4762}
63+
64+ func corpAuthPerRPCCreds (ctx context.Context ) (* client.PerRPCCreds , error ) {
65+ ts , err := ssogoauth2 .TokenSourceForCurrentUser ([]string {"https://www.googleapis.com/auth/cloud-platform" })
66+ if err != nil {
67+ return nil , fmt .Errorf ("unable to create CorpLogin token source: %w" , err )
68+ }
69+ return & client.PerRPCCreds {
70+ Creds : & perRPCCreds {ts : ts },
71+ }, nil
72+ }
0 commit comments