Skip to content

Commit 753d986

Browse files
committed
remove Unknown CredentialsType from public surface
1 parent 95f0abc commit 753d986

File tree

3 files changed

+48
-103
lines changed

3 files changed

+48
-103
lines changed

idtoken/idtoken.go

Lines changed: 42 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,40 @@ import (
3030
// ClientOption is for configuring a Google API client or transport.
3131
type ClientOption = option.ClientOption
3232

33-
type credentialsType int
33+
// CredentialsType specifies the type of JSON credentials being provided
34+
// to a loading function such as [WithAuthCredentialsFile] or
35+
// [WithAuthCredentialsJSON].
36+
type CredentialsType = option.CredentialsType
3437

3538
const (
36-
unknownCredType credentialsType = iota
37-
serviceAccount
38-
impersonatedServiceAccount
39-
externalAccount
39+
// unknownCredType is a private CredentialsType representing an unknown JSON file type.
40+
unknownCredType = internal.Unknown
41+
// ServiceAccount represents a service account file type.
42+
ServiceAccount = option.ServiceAccount
43+
// User represents a user credentials file type.
44+
User = option.User
45+
// ImpersonatedServiceAccount represents an impersonated service account file type.
46+
//
47+
// IMPORTANT:
48+
// This credential type does not validate the credential configuration. A security
49+
// risk occurs when a credential configuration configured with malicious urls
50+
// is used.
51+
// You should validate credential configurations provided by untrusted sources.
52+
// See [Security requirements when using credential configurations from an external
53+
// source] https://cloud.google.com/docs/authentication/external/externally-sourced-credentials
54+
// for more details.
55+
ImpersonatedServiceAccount = option.ImpersonatedServiceAccount
56+
// ExternalAccount represents an external account file type.
57+
//
58+
// IMPORTANT:
59+
// This credential type does not validate the credential configuration. A security
60+
// risk occurs when a credential configuration configured with malicious urls
61+
// is used.
62+
// You should validate credential configurations provided by untrusted sources.
63+
// See [Security requirements when using credential configurations from an external
64+
// source] https://cloud.google.com/docs/authentication/external/externally-sourced-credentials
65+
// for more details.
66+
ExternalAccount = option.ExternalAccount
4067
)
4168

4269
// NewClient creates a HTTP Client that automatically adds an ID token to each
@@ -148,7 +175,7 @@ func tokenSourceFromBytes(ctx context.Context, data []byte, audience string, ds
148175
return nil, err
149176
}
150177
switch allowedType {
151-
case serviceAccount:
178+
case ServiceAccount:
152179
cfg, err := google.JWTConfigFromJSON(data, ds.GetScopes()...)
153180
if err != nil {
154181
return nil, err
@@ -168,7 +195,7 @@ func tokenSourceFromBytes(ctx context.Context, data []byte, audience string, ds
168195
return nil, err
169196
}
170197
return oauth2.ReuseTokenSource(tok, ts), nil
171-
case impersonatedServiceAccount, externalAccount:
198+
case ImpersonatedServiceAccount, ExternalAccount:
172199
type url struct {
173200
ServiceAccountImpersonationURL string `json:"service_account_impersonation_url"`
174201
}
@@ -184,20 +211,20 @@ func tokenSourceFromBytes(ctx context.Context, data []byte, audience string, ds
184211
TargetPrincipal: account,
185212
IncludeEmail: true,
186213
}
187-
ts, err := impersonate.IDTokenSource(ctx, config, option.WithCredentialsJSON(data))
214+
ts, err := impersonate.IDTokenSource(ctx, config, option.WithAuthCredentialsJSON(allowedType, data))
188215
if err != nil {
189216
return nil, err
190217
}
191218
return ts, nil
192219
default:
193-
return nil, fmt.Errorf("idtoken: unsupported credentials type")
220+
return nil, fmt.Errorf("idtoken: unsupported credentials type: %d", allowedType)
194221
}
195222
}
196223

197224
// getAllowedType returns the credentials type of type credentialsType, and an error.
198225
// allowed types are "service_account" and "impersonated_service_account"
199-
func getAllowedType(data []byte) (credentialsType, error) {
200-
var t credentialsType
226+
func getAllowedType(data []byte) (CredentialsType, error) {
227+
var t CredentialsType
201228
if len(data) == 0 {
202229
return t, fmt.Errorf("idtoken: credential provided is 0 bytes")
203230
}
@@ -211,14 +238,14 @@ func getAllowedType(data []byte) (credentialsType, error) {
211238
return t, nil
212239
}
213240

214-
func parseCredType(typeString string) credentialsType {
241+
func parseCredType(typeString string) CredentialsType {
215242
switch typeString {
216243
case "service_account":
217-
return serviceAccount
244+
return ServiceAccount
218245
case "impersonated_service_account":
219-
return impersonatedServiceAccount
246+
return ImpersonatedServiceAccount
220247
case "external_account":
221-
return externalAccount
248+
return ExternalAccount
222249
default:
223250
return unknownCredType
224251
}
@@ -235,51 +262,6 @@ func (w withCustomClaims) Apply(o *internal.DialSettings) {
235262
o.CustomClaims = w
236263
}
237264

238-
// CredentialsType specifies the type of JSON credentials being provided
239-
// to a loading function such as [WithAuthCredentialsFile] or
240-
// [WithAuthCredentialsJSON].
241-
type CredentialsType = option.CredentialsType
242-
243-
const (
244-
// Unknown represents an unknown JSON file type.
245-
//
246-
// IMPORTANT:
247-
// This credential type does not validate the credential configuration. A security
248-
// risk occurs when a credential configuration configured with malicious urls
249-
// is used.
250-
// You should validate credential configurations provided by untrusted sources.
251-
// See [Security requirements when using credential configurations from an external
252-
// source] https://cloud.google.com/docs/authentication/external/externally-sourced-credentials
253-
// for more details.
254-
Unknown = option.Unknown
255-
// ServiceAccount represents a service account file type.
256-
ServiceAccount = option.ServiceAccount
257-
// User represents a user credentials file type.
258-
User = option.User
259-
// ImpersonatedServiceAccount represents an impersonated service account file type.
260-
//
261-
// IMPORTANT:
262-
// This credential type does not validate the credential configuration. A security
263-
// risk occurs when a credential configuration configured with malicious urls
264-
// is used.
265-
// You should validate credential configurations provided by untrusted sources.
266-
// See [Security requirements when using credential configurations from an external
267-
// source] https://cloud.google.com/docs/authentication/external/externally-sourced-credentials
268-
// for more details.
269-
ImpersonatedServiceAccount = option.ImpersonatedServiceAccount
270-
// ExternalAccount represents an external account file type.
271-
//
272-
// IMPORTANT:
273-
// This credential type does not validate the credential configuration. A security
274-
// risk occurs when a credential configuration configured with malicious urls
275-
// is used.
276-
// You should validate credential configurations provided by untrusted sources.
277-
// See [Security requirements when using credential configurations from an external
278-
// source] https://cloud.google.com/docs/authentication/external/externally-sourced-credentials
279-
// for more details.
280-
ExternalAccount = option.ExternalAccount
281-
)
282-
283265
// WithCredentialsFile returns a ClientOption that authenticates
284266
// API calls with the given service account or refresh token JSON
285267
// credentials file.

idtoken/integration_test.go

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ import (
1111
"strings"
1212
"testing"
1313

14-
"golang.org/x/oauth2/google"
1514
"google.golang.org/api/idtoken"
1615
"google.golang.org/api/option"
1716
)
1817

1918
const (
2019
envCredentialFile = "GOOGLE_APPLICATION_CREDENTIALS"
20+
// Change this type as needed to match the credentials type of GOOGLE_APPLICATION_CREDENTIALS JSON or ADC credentials JSON.
21+
credentialsFileType = idtoken.ServiceAccount
2122

2223
aud = "http://example.com"
2324
)
@@ -26,38 +27,11 @@ func TestNewTokenSource(t *testing.T) {
2627
if testing.Short() {
2728
t.Skip("skipping integration test")
2829
}
29-
ts, err := idtoken.NewTokenSource(context.Background(), "http://example.com", option.WithCredentialsFile(os.Getenv(envCredentialFile)))
30-
if err != nil {
31-
t.Fatalf("unable to create TokenSource: %v", err)
32-
}
33-
tok, err := ts.Token()
34-
if err != nil {
35-
t.Fatalf("unable to retrieve Token: %v", err)
36-
}
37-
req := &http.Request{Header: make(http.Header)}
38-
tok.SetAuthHeader(req)
39-
if !strings.HasPrefix(req.Header.Get("Authorization"), "Bearer ") {
40-
t.Fatalf("token should sign requests with Bearer Authorization header")
41-
}
42-
validTok, err := idtoken.Validate(context.Background(), tok.AccessToken, aud)
43-
if err != nil {
44-
t.Fatalf("token validation failed: %v", err)
45-
}
46-
if validTok.Audience != aud {
47-
t.Fatalf("got %q, want %q", validTok.Audience, aud)
48-
}
49-
}
50-
51-
func TestNewTokenSource_WithCredentialJSON(t *testing.T) {
52-
if testing.Short() {
53-
t.Skip("skipping integration test")
54-
}
55-
ctx := context.Background()
56-
creds, err := google.FindDefaultCredentials(ctx)
57-
if err != nil {
58-
t.Fatalf("unable to find default creds: %v", err)
30+
credsPath := os.Getenv(envCredentialFile)
31+
if credsPath == "" {
32+
t.Fatalf("Env var is not set: %s", envCredentialFile)
5933
}
60-
ts, err := idtoken.NewTokenSource(ctx, aud, option.WithCredentialsJSON(creds.JSON))
34+
ts, err := idtoken.NewTokenSource(context.Background(), "http://example.com", option.WithAuthCredentialsFile(credentialsFileType, credsPath))
6135
if err != nil {
6236
t.Fatalf("unable to create Client: %v", err)
6337
}

option/option.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,6 @@ import (
2424
type CredentialsType = internal.CredentialsType
2525

2626
const (
27-
// Unknown represents an unknown JSON file type.
28-
//
29-
// IMPORTANT:
30-
// This credential type does not validate the credential configuration. A security
31-
// risk occurs when a credential configuration configured with malicious urls
32-
// is used.
33-
// You should validate credential configurations provided by untrusted sources.
34-
// See [Security requirements when using credential configurations from an external
35-
// source] https://cloud.google.com/docs/authentication/external/externally-sourced-credentials
36-
// for more details.
37-
Unknown CredentialsType = internal.Unknown
3827
// ServiceAccount represents a service account file type.
3928
ServiceAccount = internal.ServiceAccount
4029
// User represents a user credentials file type.

0 commit comments

Comments
 (0)