| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
package impersonate |
| 16 |
|
| 17 |
import ( |
| 18 |
"bytes" |
| 19 |
"context" |
| 20 |
"encoding/json" |
| 21 |
"io" |
| 22 |
"net/http" |
| 23 |
"strings" |
| 24 |
"testing" |
| 25 |
"time" |
| 26 |
|
| 27 |
"cloud.google.com/go/auth" |
| 28 |
"cloud.google.com/go/auth/internal" |
| 29 |
"github.com/google/go-cmp/cmp" |
| 30 |
) |
| 31 |
|
| 32 |
func TestNewCredentials_serviceAccount(t *testing.T) { |
| 33 |
ctx := context.Background() |
| 34 |
tests := []struct { |
| 35 |
name string |
| 36 |
config CredentialsOptions |
| 37 |
wantErr error |
| 38 |
wantUniverseDomain string |
| 39 |
}{ |
| 40 |
{ |
| 41 |
name: "missing targetPrincipal", |
| 42 |
wantErr: errMissingTargetPrincipal, |
| 43 |
}, |
| 44 |
{ |
| 45 |
name: "missing scopes", |
| 46 |
config: CredentialsOptions{ |
| 47 |
TargetPrincipal: "[email protected]", |
| 48 |
}, |
| 49 |
wantErr: errMissingScopes, |
| 50 |
}, |
| 51 |
{ |
| 52 |
name: "lifetime over max", |
| 53 |
config: CredentialsOptions{ |
| 54 |
TargetPrincipal: "[email protected]", |
| 55 |
Scopes: []string{"scope"}, |
| 56 |
Lifetime: 13 * time.Hour, |
| 57 |
}, |
| 58 |
wantErr: errLifetimeOverMax, |
| 59 |
}, |
| 60 |
{ |
| 61 |
name: "works", |
| 62 |
config: CredentialsOptions{ |
| 63 |
TargetPrincipal: "[email protected]", |
| 64 |
Scopes: []string{"scope"}, |
| 65 |
}, |
| 66 |
wantErr: nil, |
| 67 |
wantUniverseDomain: "googleapis.com", |
| 68 |
}, |
| 69 |
{ |
| 70 |
name: "universe domain from options", |
| 71 |
config: CredentialsOptions{ |
| 72 |
TargetPrincipal: "[email protected]", |
| 73 |
Scopes: []string{"scope"}, |
| 74 |
UniverseDomain: "example.com", |
| 75 |
}, |
| 76 |
wantErr: nil, |
| 77 |
wantUniverseDomain: "googleapis.com", |
| 78 |
}, |
| 79 |
{ |
| 80 |
name: "universe domain from options and credentials", |
| 81 |
config: CredentialsOptions{ |
| 82 |
TargetPrincipal: "[email protected]", |
| 83 |
Scopes: []string{"scope"}, |
| 84 |
UniverseDomain: "NOT.example.com", |
| 85 |
Credentials: staticCredentials("example.com"), |
| 86 |
}, |
| 87 |
wantErr: nil, |
| 88 |
wantUniverseDomain: "example.com", |
| 89 |
}, |
| 90 |
{ |
| 91 |
name: "universe domain from credentials", |
| 92 |
config: CredentialsOptions{ |
| 93 |
TargetPrincipal: "[email protected]", |
| 94 |
Scopes: []string{"scope"}, |
| 95 |
Credentials: staticCredentials("example.com"), |
| 96 |
}, |
| 97 |
wantErr: nil, |
| 98 |
wantUniverseDomain: "example.com", |
| 99 |
}, |
| 100 |
} |
| 101 |
|
| 102 |
for _, tt := range tests { |
| 103 |
name := tt.name |
| 104 |
t.Run(name, func(t *testing.T) { |
| 105 |
saTok := "sa-token" |
| 106 |
client := &http.Client{ |
| 107 |
Transport: RoundTripFn(func(req *http.Request) *http.Response { |
| 108 |
defer req.Body.Close() |
| 109 |
b, err := io.ReadAll(req.Body) |
| 110 |
if err != nil { |
| 111 |
t.Error(err) |
| 112 |
} |
| 113 |
var r generateAccessTokenRequest |
| 114 |
if err := json.Unmarshal(b, &r); err != nil { |
| 115 |
t.Error(err) |
| 116 |
} |
| 117 |
if !cmp.Equal(r.Scope, tt.config.Scopes) { |
| 118 |
t.Errorf("got %v, want %v", r.Scope, tt.config.Scopes) |
| 119 |
} |
| 120 |
if !strings.Contains(req.URL.Path, tt.config.TargetPrincipal) { |
| 121 |
t.Errorf("got %q, want %q", req.URL.Path, tt.config.TargetPrincipal) |
| 122 |
} |
| 123 |
if !strings.Contains(req.URL.Hostname(), tt.wantUniverseDomain) { |
| 124 |
t.Errorf("got %q, want %q", req.URL.Hostname(), tt.wantUniverseDomain) |
| 125 |
} |
| 126 |
if !strings.Contains(req.URL.Path, "generateAccessToken") { |
| 127 |
t.Fatal("path must contain 'generateAccessToken'") |
| 128 |
} |
| 129 |
|
| 130 |
resp := generateAccessTokenResponse{ |
| 131 |
AccessToken: saTok, |
| 132 |
ExpireTime: time.Now().Format(time.RFC3339), |
| 133 |
} |
| 134 |
b, err = json.Marshal(&resp) |
| 135 |
if err != nil { |
| 136 |
t.Fatalf("unable to marshal response: %v", err) |
| 137 |
} |
| 138 |
return &http.Response{ |
| 139 |
StatusCode: 200, |
| 140 |
Body: io.NopCloser(bytes.NewReader(b)), |
| 141 |
Header: http.Header{}, |
| 142 |
} |
| 143 |
}), |
| 144 |
} |
| 145 |
if tt.config.Credentials == nil { |
| 146 |
tt.config.Client = client |
| 147 |
} |
| 148 |
creds, err := NewCredentials(&tt.config) |
| 149 |
if err != nil { |
| 150 |
if err != tt.wantErr { |
| 151 |
t.Fatalf("err: %v", err) |
| 152 |
} |
| 153 |
} else if tt.config.Credentials != nil { |
| 154 |
|
| 155 |
if got, _ := creds.UniverseDomain(ctx); got != tt.wantUniverseDomain { |
| 156 |
t.Errorf("got %q, want %q", got, tt.wantUniverseDomain) |
| 157 |
} |
| 158 |
} else { |
| 159 |
tok, err := creds.Token(ctx) |
| 160 |
if err != nil { |
| 161 |
t.Error(err) |
| 162 |
} |
| 163 |
if tok.Value != saTok { |
| 164 |
t.Errorf("got %q, want %q", tok.Value, saTok) |
| 165 |
} |
| 166 |
if got, _ := creds.UniverseDomain(ctx); got != tt.wantUniverseDomain { |
| 167 |
t.Errorf("got %q, want %q", got, tt.wantUniverseDomain) |
| 168 |
} |
| 169 |
} |
| 170 |
}) |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
type RoundTripFn func(req *http.Request) *http.Response |
| 175 |
|
| 176 |
func (f RoundTripFn) RoundTrip(req *http.Request) (*http.Response, error) { return f(req), nil } |
| 177 |
|
| 178 |
func staticCredentials(universeDomain string) *auth.Credentials { |
| 179 |
return auth.NewCredentials(&auth.CredentialsOptions{ |
| 180 |
TokenProvider: staticTokenProvider("base credentials Token should never be called"), |
| 181 |
UniverseDomainProvider: internal.StaticCredentialsProperty(universeDomain), |
| 182 |
}) |
| 183 |
} |
| 184 |
|
| 185 |
type staticTokenProvider string |
| 186 |
|
| 187 |
func (s staticTokenProvider) Token(context.Context) (*auth.Token, error) { |
| 188 |
return &auth.Token{Value: string(s)}, nil |
| 189 |
} |