Skip to content

Commit ef75c2c

Browse files
committed
fixing audience check, using email in default credentials if it exists, using iam config email, if populated
1 parent 0b2492c commit ef75c2c

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

auth/gcp/iam.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,13 @@ func NewDefaultIAMVerifier(ctx context.Context, cfg IAMConfig, clientFunc func(c
4545
return nil, err
4646
}
4747

48-
eml, err := GetDefaultEmail(ctx, "", clientFunc(ctx))
49-
if err != nil {
50-
return nil, errors.Wrap(err, "unable to get default email")
48+
eml := cfg.ServiceAccountEmail
49+
// only fall back if one isn't injected
50+
if eml == "" {
51+
eml, err = GetDefaultEmail(ctx, "", clientFunc(ctx))
52+
if err != nil {
53+
return nil, errors.Wrap(err, "unable to get default email")
54+
}
5155
}
5256

5357
return auth.NewVerifier(ks,
@@ -79,7 +83,7 @@ func IAMVerifyFunc(vf func(ctx context.Context, cs IAMClaimSet) bool) auth.Verif
7983

8084
// ValidIAMClaims ensures the token audience issuers matches expectations.
8185
func ValidIAMClaims(cs IAMClaimSet, audience string) bool {
82-
return cs.Aud != audience
86+
return cs.Aud == audience
8387
}
8488

8589
// VerifyIAMEmails is an auth.VerifyFunc that ensures IAMClaimSets are valid

auth/gcp/metadata.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,32 @@ package gcp
22

33
import (
44
"context"
5+
"encoding/json"
56
"io/ioutil"
67
"net/http"
78

89
"github.com/pkg/errors"
10+
"golang.org/x/oauth2/google"
11+
iam "google.golang.org/api/iam/v1"
912
)
1013

1114
// GetDefaultEmail is a helper method for users on GCE or the 2nd generation GAE
1215
// environment.
1316
func GetDefaultEmail(ctx context.Context, addr string, hc *http.Client) (string, error) {
14-
email, err := metadataGet(ctx, addr, hc, "instance/service-accounts/default/email")
17+
creds, err := findDefaultCredentials(ctx, iam.CloudPlatformScope)
18+
if err != nil {
19+
return "", errors.Wrap(err, "unable to find credentials to sign JWT")
20+
}
21+
22+
email, err := getEmailFromCredentials(creds)
23+
if err != nil {
24+
return "", errors.Wrap(err, "unable to get email from given credentials")
25+
}
26+
if email != "" {
27+
return email, nil
28+
}
29+
30+
email, err = metadataGet(ctx, addr, hc, "instance/service-accounts/default/email")
1531
return email, errors.Wrap(err, "unable to get default email from metadata")
1632
}
1733

@@ -39,3 +55,19 @@ func metadataGet(ctx context.Context, addr string, hc *http.Client, suffix strin
3955
tkn, err := ioutil.ReadAll(resp.Body)
4056
return string(tkn), errors.Wrap(err, "unable to read metadata response")
4157
}
58+
59+
var findDefaultCredentials = google.FindDefaultCredentials
60+
61+
func getEmailFromCredentials(creds *google.Credentials) (string, error) {
62+
if len(creds.JSON) == 0 {
63+
return "", nil
64+
}
65+
66+
var data map[string]string
67+
err := json.Unmarshal(creds.JSON, &data)
68+
if err != nil {
69+
return "", errors.Wrap(err, "unable to parse credentials")
70+
}
71+
72+
return data["client_email"], nil
73+
}

0 commit comments

Comments
 (0)