forked from remote/oauth2
Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
39adbb7807 | ||
|
|
4ce7bbb2ff | ||
|
|
1e6999b1be | ||
|
|
6e9ec9323d | ||
|
|
e067960af8 | ||
|
|
4c91c17b32 |
2
go.mod
2
go.mod
@@ -11,6 +11,6 @@ require (
|
|||||||
require (
|
require (
|
||||||
cloud.google.com/go/compute v1.20.1 // indirect
|
cloud.google.com/go/compute v1.20.1 // indirect
|
||||||
github.com/golang/protobuf v1.5.3 // indirect
|
github.com/golang/protobuf v1.5.3 // indirect
|
||||||
golang.org/x/net v0.16.0 // indirect
|
golang.org/x/net v0.20.0 // indirect
|
||||||
google.golang.org/protobuf v1.31.0 // indirect
|
google.golang.org/protobuf v1.31.0 // indirect
|
||||||
)
|
)
|
||||||
|
|||||||
4
go.sum
4
go.sum
@@ -11,8 +11,8 @@ github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
|
|||||||
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
|
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
|
||||||
golang.org/x/net v0.16.0 h1:7eBu7KsSvFDtSXUIDbh3aqlK4DPsZ1rByC8PFfBThos=
|
golang.org/x/net v0.20.0 h1:aCL9BSgETF1k+blQaYUBx9hJ9LOGP3gAVemcZlf1Kpo=
|
||||||
golang.org/x/net v0.16.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
|
golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY=
|
||||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
|
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"cloud.google.com/go/compute/metadata"
|
"cloud.google.com/go/compute/metadata"
|
||||||
@@ -41,12 +42,20 @@ type Credentials struct {
|
|||||||
// running on Google Cloud Platform.
|
// running on Google Cloud Platform.
|
||||||
JSON []byte
|
JSON []byte
|
||||||
|
|
||||||
|
udMu sync.Mutex // guards universeDomain
|
||||||
// universeDomain is the default service domain for a given Cloud universe.
|
// universeDomain is the default service domain for a given Cloud universe.
|
||||||
universeDomain string
|
universeDomain string
|
||||||
}
|
}
|
||||||
|
|
||||||
// UniverseDomain returns the default service domain for a given Cloud universe.
|
// UniverseDomain returns the default service domain for a given Cloud universe.
|
||||||
|
//
|
||||||
// The default value is "googleapis.com".
|
// The default value is "googleapis.com".
|
||||||
|
//
|
||||||
|
// Deprecated: Use instead (*Credentials).GetUniverseDomain(), which supports
|
||||||
|
// obtaining the universe domain when authenticating via the GCE metadata server.
|
||||||
|
// Unlike GetUniverseDomain, this method, UniverseDomain, will always return the
|
||||||
|
// default value when authenticating via the GCE metadata server.
|
||||||
|
// See also [The attached service account](https://cloud.google.com/docs/authentication/application-default-credentials#attached-sa).
|
||||||
func (c *Credentials) UniverseDomain() string {
|
func (c *Credentials) UniverseDomain() string {
|
||||||
if c.universeDomain == "" {
|
if c.universeDomain == "" {
|
||||||
return universeDomainDefault
|
return universeDomainDefault
|
||||||
@@ -54,6 +63,55 @@ func (c *Credentials) UniverseDomain() string {
|
|||||||
return c.universeDomain
|
return c.universeDomain
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetUniverseDomain returns the default service domain for a given Cloud
|
||||||
|
// universe.
|
||||||
|
//
|
||||||
|
// The default value is "googleapis.com".
|
||||||
|
//
|
||||||
|
// It obtains the universe domain from the attached service account on GCE when
|
||||||
|
// authenticating via the GCE metadata server. See also [The attached service
|
||||||
|
// account](https://cloud.google.com/docs/authentication/application-default-credentials#attached-sa).
|
||||||
|
// If the GCE metadata server returns a 404 error, the default value is
|
||||||
|
// returned. If the GCE metadata server returns an error other than 404, the
|
||||||
|
// error is returned.
|
||||||
|
func (c *Credentials) GetUniverseDomain() (string, error) {
|
||||||
|
c.udMu.Lock()
|
||||||
|
defer c.udMu.Unlock()
|
||||||
|
if c.universeDomain == "" && metadata.OnGCE() {
|
||||||
|
// If we're on Google Compute Engine, an App Engine standard second
|
||||||
|
// generation runtime, or App Engine flexible, use the metadata server.
|
||||||
|
err := c.computeUniverseDomain()
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// If not on Google Compute Engine, or in case of any non-error path in
|
||||||
|
// computeUniverseDomain that did not set universeDomain, set the default
|
||||||
|
// universe domain.
|
||||||
|
if c.universeDomain == "" {
|
||||||
|
c.universeDomain = universeDomainDefault
|
||||||
|
}
|
||||||
|
return c.universeDomain, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// computeUniverseDomain fetches the default service domain for a given Cloud
|
||||||
|
// universe from Google Compute Engine (GCE)'s metadata server. It's only valid
|
||||||
|
// to use this method if your program is running on a GCE instance.
|
||||||
|
func (c *Credentials) computeUniverseDomain() error {
|
||||||
|
var err error
|
||||||
|
c.universeDomain, err = metadata.Get("universe/universe_domain")
|
||||||
|
if err != nil {
|
||||||
|
if _, ok := err.(metadata.NotDefinedError); ok {
|
||||||
|
// http.StatusNotFound (404)
|
||||||
|
c.universeDomain = universeDomainDefault
|
||||||
|
return nil
|
||||||
|
} else {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// DefaultCredentials is the old name of Credentials.
|
// DefaultCredentials is the old name of Credentials.
|
||||||
//
|
//
|
||||||
// Deprecated: use Credentials instead.
|
// Deprecated: use Credentials instead.
|
||||||
@@ -91,6 +149,12 @@ type CredentialsParams struct {
|
|||||||
// Note: This option is currently only respected when using credentials
|
// Note: This option is currently only respected when using credentials
|
||||||
// fetched from the GCE metadata server.
|
// fetched from the GCE metadata server.
|
||||||
EarlyTokenRefresh time.Duration
|
EarlyTokenRefresh time.Duration
|
||||||
|
|
||||||
|
// UniverseDomain is the default service domain for a given Cloud universe.
|
||||||
|
// Only supported in authentication flows that support universe domains.
|
||||||
|
// This value takes precedence over a universe domain explicitly specified
|
||||||
|
// in a credentials config file or by the GCE metadata server. Optional.
|
||||||
|
UniverseDomain string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (params CredentialsParams) deepCopy() CredentialsParams {
|
func (params CredentialsParams) deepCopy() CredentialsParams {
|
||||||
@@ -177,6 +241,7 @@ func FindDefaultCredentialsWithParams(ctx context.Context, params CredentialsPar
|
|||||||
return &Credentials{
|
return &Credentials{
|
||||||
ProjectID: id,
|
ProjectID: id,
|
||||||
TokenSource: computeTokenSource("", params.EarlyTokenRefresh, params.Scopes...),
|
TokenSource: computeTokenSource("", params.EarlyTokenRefresh, params.Scopes...),
|
||||||
|
universeDomain: params.UniverseDomain,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -217,6 +282,9 @@ func CredentialsFromJSONWithParams(ctx context.Context, jsonData []byte, params
|
|||||||
}
|
}
|
||||||
|
|
||||||
universeDomain := f.UniverseDomain
|
universeDomain := f.UniverseDomain
|
||||||
|
if params.UniverseDomain != "" {
|
||||||
|
universeDomain = params.UniverseDomain
|
||||||
|
}
|
||||||
// Authorized user credentials are only supported in the googleapis.com universe.
|
// Authorized user credentials are only supported in the googleapis.com universe.
|
||||||
if f.Type == userCredentialsKey {
|
if f.Type == userCredentialsKey {
|
||||||
universeDomain = universeDomainDefault
|
universeDomain = universeDomainDefault
|
||||||
|
|||||||
@@ -6,6 +6,9 @@ package google
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -53,6 +56,10 @@ var userJSONUniverseDomain = []byte(`{
|
|||||||
"universe_domain": "example.com"
|
"universe_domain": "example.com"
|
||||||
}`)
|
}`)
|
||||||
|
|
||||||
|
var universeDomain = "example.com"
|
||||||
|
|
||||||
|
var universeDomain2 = "apis-tpclp.goog"
|
||||||
|
|
||||||
func TestCredentialsFromJSONWithParams_SA(t *testing.T) {
|
func TestCredentialsFromJSONWithParams_SA(t *testing.T) {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
scope := "https://www.googleapis.com/auth/cloud-platform"
|
scope := "https://www.googleapis.com/auth/cloud-platform"
|
||||||
@@ -70,6 +77,32 @@ func TestCredentialsFromJSONWithParams_SA(t *testing.T) {
|
|||||||
if want := "googleapis.com"; creds.UniverseDomain() != want {
|
if want := "googleapis.com"; creds.UniverseDomain() != want {
|
||||||
t.Fatalf("got %q, want %q", creds.UniverseDomain(), want)
|
t.Fatalf("got %q, want %q", creds.UniverseDomain(), want)
|
||||||
}
|
}
|
||||||
|
if want := "googleapis.com"; creds.UniverseDomain() != want {
|
||||||
|
t.Fatalf("got %q, want %q", creds.UniverseDomain(), want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCredentialsFromJSONWithParams_SA_Params_UniverseDomain(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
scope := "https://www.googleapis.com/auth/cloud-platform"
|
||||||
|
params := CredentialsParams{
|
||||||
|
Scopes: []string{scope},
|
||||||
|
UniverseDomain: universeDomain2,
|
||||||
|
}
|
||||||
|
creds, err := CredentialsFromJSONWithParams(ctx, saJSONJWT, params)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if want := "fake_project"; creds.ProjectID != want {
|
||||||
|
t.Fatalf("got %q, want %q", creds.ProjectID, want)
|
||||||
|
}
|
||||||
|
if creds.UniverseDomain() != universeDomain2 {
|
||||||
|
t.Fatalf("got %q, want %q", creds.UniverseDomain(), universeDomain2)
|
||||||
|
}
|
||||||
|
if creds.UniverseDomain() != universeDomain2 {
|
||||||
|
t.Fatalf("got %q, want %q", creds.UniverseDomain(), universeDomain2)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCredentialsFromJSONWithParams_SA_UniverseDomain(t *testing.T) {
|
func TestCredentialsFromJSONWithParams_SA_UniverseDomain(t *testing.T) {
|
||||||
@@ -86,8 +119,42 @@ func TestCredentialsFromJSONWithParams_SA_UniverseDomain(t *testing.T) {
|
|||||||
if want := "fake_project"; creds.ProjectID != want {
|
if want := "fake_project"; creds.ProjectID != want {
|
||||||
t.Fatalf("got %q, want %q", creds.ProjectID, want)
|
t.Fatalf("got %q, want %q", creds.ProjectID, want)
|
||||||
}
|
}
|
||||||
if want := "example.com"; creds.UniverseDomain() != want {
|
if creds.UniverseDomain() != universeDomain {
|
||||||
t.Fatalf("got %q, want %q", creds.UniverseDomain(), want)
|
t.Fatalf("got %q, want %q", creds.UniverseDomain(), universeDomain)
|
||||||
|
}
|
||||||
|
got, err := creds.GetUniverseDomain()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if got != universeDomain {
|
||||||
|
t.Fatalf("got %q, want %q", got, universeDomain)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCredentialsFromJSONWithParams_SA_UniverseDomain_Params_UniverseDomain(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
scope := "https://www.googleapis.com/auth/cloud-platform"
|
||||||
|
params := CredentialsParams{
|
||||||
|
Scopes: []string{scope},
|
||||||
|
UniverseDomain: universeDomain2,
|
||||||
|
}
|
||||||
|
creds, err := CredentialsFromJSONWithParams(ctx, saJSONJWTUniverseDomain, params)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if want := "fake_project"; creds.ProjectID != want {
|
||||||
|
t.Fatalf("got %q, want %q", creds.ProjectID, want)
|
||||||
|
}
|
||||||
|
if creds.UniverseDomain() != universeDomain2 {
|
||||||
|
t.Fatalf("got %q, want %q", creds.UniverseDomain(), universeDomain2)
|
||||||
|
}
|
||||||
|
got, err := creds.GetUniverseDomain()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if got != universeDomain2 {
|
||||||
|
t.Fatalf("got %q, want %q", got, universeDomain2)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -105,6 +172,37 @@ func TestCredentialsFromJSONWithParams_User(t *testing.T) {
|
|||||||
if want := "googleapis.com"; creds.UniverseDomain() != want {
|
if want := "googleapis.com"; creds.UniverseDomain() != want {
|
||||||
t.Fatalf("got %q, want %q", creds.UniverseDomain(), want)
|
t.Fatalf("got %q, want %q", creds.UniverseDomain(), want)
|
||||||
}
|
}
|
||||||
|
got, err := creds.GetUniverseDomain()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if want := "googleapis.com"; got != want {
|
||||||
|
t.Fatalf("got %q, want %q", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCredentialsFromJSONWithParams_User_Params_UniverseDomain(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
scope := "https://www.googleapis.com/auth/cloud-platform"
|
||||||
|
params := CredentialsParams{
|
||||||
|
Scopes: []string{scope},
|
||||||
|
UniverseDomain: universeDomain2,
|
||||||
|
}
|
||||||
|
creds, err := CredentialsFromJSONWithParams(ctx, userJSON, params)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if want := "googleapis.com"; creds.UniverseDomain() != want {
|
||||||
|
t.Fatalf("got %q, want %q", creds.UniverseDomain(), want)
|
||||||
|
}
|
||||||
|
got, err := creds.GetUniverseDomain()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if want := "googleapis.com"; got != want {
|
||||||
|
t.Fatalf("got %q, want %q", got, want)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCredentialsFromJSONWithParams_User_UniverseDomain(t *testing.T) {
|
func TestCredentialsFromJSONWithParams_User_UniverseDomain(t *testing.T) {
|
||||||
@@ -121,4 +219,79 @@ func TestCredentialsFromJSONWithParams_User_UniverseDomain(t *testing.T) {
|
|||||||
if want := "googleapis.com"; creds.UniverseDomain() != want {
|
if want := "googleapis.com"; creds.UniverseDomain() != want {
|
||||||
t.Fatalf("got %q, want %q", creds.UniverseDomain(), want)
|
t.Fatalf("got %q, want %q", creds.UniverseDomain(), want)
|
||||||
}
|
}
|
||||||
|
got, err := creds.GetUniverseDomain()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if want := "googleapis.com"; got != want {
|
||||||
|
t.Fatalf("got %q, want %q", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCredentialsFromJSONWithParams_User_UniverseDomain_Params_UniverseDomain(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
scope := "https://www.googleapis.com/auth/cloud-platform"
|
||||||
|
params := CredentialsParams{
|
||||||
|
Scopes: []string{scope},
|
||||||
|
UniverseDomain: universeDomain2,
|
||||||
|
}
|
||||||
|
creds, err := CredentialsFromJSONWithParams(ctx, userJSONUniverseDomain, params)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if want := "googleapis.com"; creds.UniverseDomain() != want {
|
||||||
|
t.Fatalf("got %q, want %q", creds.UniverseDomain(), want)
|
||||||
|
}
|
||||||
|
got, err := creds.GetUniverseDomain()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if want := "googleapis.com"; got != want {
|
||||||
|
t.Fatalf("got %q, want %q", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestComputeUniverseDomain(t *testing.T) {
|
||||||
|
universeDomainPath := "/computeMetadata/v1/universe/universe_domain"
|
||||||
|
universeDomainResponseBody := "example.com"
|
||||||
|
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.URL.Path != universeDomainPath {
|
||||||
|
t.Errorf("got %s, want %s", r.URL.Path, universeDomainPath)
|
||||||
|
}
|
||||||
|
w.Write([]byte(universeDomainResponseBody))
|
||||||
|
}))
|
||||||
|
defer s.Close()
|
||||||
|
t.Setenv("GCE_METADATA_HOST", strings.TrimPrefix(s.URL, "http://"))
|
||||||
|
|
||||||
|
scope := "https://www.googleapis.com/auth/cloud-platform"
|
||||||
|
params := CredentialsParams{
|
||||||
|
Scopes: []string{scope},
|
||||||
|
}
|
||||||
|
// Copied from FindDefaultCredentialsWithParams, metadata.OnGCE() = true block
|
||||||
|
creds := &Credentials{
|
||||||
|
ProjectID: "fake_project",
|
||||||
|
TokenSource: computeTokenSource("", params.EarlyTokenRefresh, params.Scopes...),
|
||||||
|
universeDomain: params.UniverseDomain, // empty
|
||||||
|
}
|
||||||
|
c := make(chan bool)
|
||||||
|
go func() {
|
||||||
|
got, err := creds.GetUniverseDomain() // First conflicting access.
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
if want := universeDomainResponseBody; got != want {
|
||||||
|
t.Errorf("got %q, want %q", got, want)
|
||||||
|
}
|
||||||
|
c <- true
|
||||||
|
}()
|
||||||
|
got, err := creds.GetUniverseDomain() // Second conflicting access.
|
||||||
|
<-c
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
if want := universeDomainResponseBody; got != want {
|
||||||
|
t.Errorf("got %q, want %q", got, want)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -101,6 +101,8 @@
|
|||||||
// executable-sourced credentials), please check out:
|
// executable-sourced credentials), please check out:
|
||||||
// https://cloud.google.com/iam/docs/workforce-obtaining-short-lived-credentials#generate_a_configuration_file_for_non-interactive_sign-in
|
// https://cloud.google.com/iam/docs/workforce-obtaining-short-lived-credentials#generate_a_configuration_file_for_non-interactive_sign-in
|
||||||
//
|
//
|
||||||
|
// # Security considerations
|
||||||
|
//
|
||||||
// Note that this library does not perform any validation on the token_url, token_info_url,
|
// Note that this library does not perform any validation on the token_url, token_info_url,
|
||||||
// or service_account_impersonation_url fields of the credential configuration.
|
// or service_account_impersonation_url fields of the credential configuration.
|
||||||
// It is not recommended to use a credential configuration that you did not generate with
|
// It is not recommended to use a credential configuration that you did not generate with
|
||||||
|
|||||||
@@ -5,6 +5,8 @@
|
|||||||
package google
|
package google
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
@@ -137,3 +139,21 @@ func TestJWTConfigFromJSONNoAudience(t *testing.T) {
|
|||||||
t.Errorf("Audience = %q; want %q", got, want)
|
t.Errorf("Audience = %q; want %q", got, want)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestComputeTokenSource(t *testing.T) {
|
||||||
|
tokenPath := "/computeMetadata/v1/instance/service-accounts/default/token"
|
||||||
|
tokenResponseBody := `{"access_token":"Sample.Access.Token","token_type":"Bearer","expires_in":3600}`
|
||||||
|
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.URL.Path != tokenPath {
|
||||||
|
t.Errorf("got %s, want %s", r.URL.Path, tokenPath)
|
||||||
|
}
|
||||||
|
w.Write([]byte(tokenResponseBody))
|
||||||
|
}))
|
||||||
|
defer s.Close()
|
||||||
|
t.Setenv("GCE_METADATA_HOST", strings.TrimPrefix(s.URL, "http://"))
|
||||||
|
ts := ComputeTokenSource("")
|
||||||
|
_, err := ts.Token()
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("ts.Token() = %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user