-
Notifications
You must be signed in to change notification settings - Fork 382
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Impersonated Universe Domain Support (#1875)
* feat: Impersonated w/ Universe Support * docs: jsdoc/tsdoc fix * feat: `useEmailAzp` * chore: compodoc nonsense * chore: for compodoc nonsense * chore: typo * refactor: Explicit Universe Domains should throw for `Impersonated` * feat: Support `external_account` in `fromImpersonatedJSON` * feat: Improve `Impersonated` Support
- Loading branch information
Showing
6 changed files
with
206 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,7 @@ import { | |
ExternalAccountClientOptions, | ||
RefreshOptions, | ||
Impersonated, | ||
IdentityPoolClient, | ||
} from '../src'; | ||
import {CredentialBody} from '../src/auth/credentials'; | ||
import * as envDetect from '../src/auth/envDetect'; | ||
|
@@ -52,11 +53,16 @@ import { | |
mockStsTokenExchange, | ||
saEmail, | ||
} from './externalclienthelper'; | ||
import {BaseExternalAccountClient} from '../src/auth/baseexternalclient'; | ||
import { | ||
BaseExternalAccountClient, | ||
EXTERNAL_ACCOUNT_TYPE, | ||
} from '../src/auth/baseexternalclient'; | ||
import {AuthClient, DEFAULT_UNIVERSE} from '../src/auth/authclient'; | ||
import {ExternalAccountAuthorizedUserClient} from '../src/auth/externalAccountAuthorizedUserClient'; | ||
import {stringify} from 'querystring'; | ||
import {GoogleAuthExceptionMessages} from '../src/auth/googleauth'; | ||
import {IMPERSONATED_ACCOUNT_TYPE} from '../src/auth/impersonated'; | ||
import {USER_REFRESH_ACCOUNT_TYPE} from '../src/auth/refreshclient'; | ||
|
||
nock.disableNetConnect(); | ||
|
||
|
@@ -1656,6 +1662,86 @@ describe('googleauth', () => { | |
.reply(200, {}); | ||
} | ||
describe('for impersonated types', () => { | ||
describe('source clients', () => { | ||
it('should support a variety of source clients', async () => { | ||
const serviceAccountImpersonationURLBase = | ||
'https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/[email protected]:generateToken'; | ||
const samples: { | ||
creds: { | ||
type: typeof IMPERSONATED_ACCOUNT_TYPE; | ||
service_account_impersonation_url: string; | ||
source_credentials: {}; | ||
}; | ||
expectedSource: typeof AuthClient; | ||
}[] = [ | ||
// USER_TO_SERVICE_ACCOUNT_JSON | ||
{ | ||
creds: { | ||
type: IMPERSONATED_ACCOUNT_TYPE, | ||
service_account_impersonation_url: new URL( | ||
'./[email protected]:generateAccessToken', | ||
serviceAccountImpersonationURLBase | ||
).toString(), | ||
source_credentials: { | ||
client_id: 'client', | ||
client_secret: 'secret', | ||
refresh_token: 'refreshToken', | ||
type: USER_REFRESH_ACCOUNT_TYPE, | ||
}, | ||
}, | ||
expectedSource: UserRefreshClient, | ||
}, | ||
// SERVICE_ACCOUNT_TO_SERVICE_ACCOUNT_JSON | ||
{ | ||
creds: { | ||
type: IMPERSONATED_ACCOUNT_TYPE, | ||
service_account_impersonation_url: new URL( | ||
'./[email protected]:generateIdToken', | ||
serviceAccountImpersonationURLBase | ||
).toString(), | ||
source_credentials: { | ||
type: 'service_account', | ||
client_email: '[email protected]', | ||
private_key: privateKey, | ||
}, | ||
}, | ||
expectedSource: JWT, | ||
}, | ||
// EXTERNAL_ACCOUNT_TO_SERVICE_ACCOUNT_JSON | ||
{ | ||
creds: { | ||
type: IMPERSONATED_ACCOUNT_TYPE, | ||
service_account_impersonation_url: new URL( | ||
'./[email protected]:generateIdToken', | ||
serviceAccountImpersonationURLBase | ||
).toString(), | ||
source_credentials: { | ||
type: EXTERNAL_ACCOUNT_TYPE, | ||
audience: 'audience', | ||
subject_token_type: 'access_token', | ||
token_url: 'https://sts.googleapis.com/v1/token', | ||
credential_source: {url: 'https://example.com/token'}, | ||
}, | ||
}, | ||
expectedSource: IdentityPoolClient, | ||
}, | ||
]; | ||
|
||
const auth = new GoogleAuth(); | ||
for (const {creds, expectedSource} of samples) { | ||
const client = auth.fromJSON(creds); | ||
|
||
assert(client instanceof Impersonated); | ||
|
||
// This is a private prop - we will refactor/remove in the future | ||
assert( | ||
(client as unknown as {sourceClient: {}}).sourceClient instanceof | ||
expectedSource | ||
); | ||
} | ||
}); | ||
}); | ||
|
||
describe('for impersonated credentials signing', () => { | ||
const now = new Date().getTime(); | ||
const saSuccessResponse = { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -97,6 +97,76 @@ describe('impersonated', () => { | |
scopes.forEach(s => s.done()); | ||
}); | ||
|
||
it('should inherit a `universeDomain` from the source client', async () => { | ||
const universeDomain = 'my.universe.com'; | ||
|
||
const tomorrow = new Date(); | ||
tomorrow.setDate(tomorrow.getDate() + 1); | ||
|
||
const scopes = [ | ||
nock(url).get('/').reply(200), | ||
createGTokenMock({ | ||
access_token: 'abc123', | ||
}), | ||
nock(`https://iamcredentials.${universeDomain}`) | ||
.post( | ||
'/v1/projects/-/serviceAccounts/[email protected]:generateAccessToken', | ||
(body: ImpersonatedCredentialRequest) => { | ||
assert.strictEqual(body.lifetime, '30s'); | ||
assert.deepStrictEqual(body.delegates, []); | ||
assert.deepStrictEqual(body.scope, [ | ||
'https://www.googleapis.com/auth/cloud-platform', | ||
]); | ||
return true; | ||
} | ||
) | ||
.reply(200, { | ||
accessToken: 'universe-token', | ||
expireTime: tomorrow.toISOString(), | ||
}), | ||
]; | ||
|
||
const sourceClient = createSampleJWTClient(); | ||
|
||
// Use a simple API key for this test. No need to get too fancy. | ||
sourceClient.apiKey = 'ABC'; | ||
delete sourceClient.subject; | ||
|
||
sourceClient.universeDomain = universeDomain; | ||
|
||
const impersonated = new Impersonated({ | ||
sourceClient, | ||
targetPrincipal: '[email protected]', | ||
lifetime: 30, | ||
delegates: [], | ||
targetScopes: ['https://www.googleapis.com/auth/cloud-platform'], | ||
}); | ||
|
||
await impersonated.request({url}); | ||
assert.strictEqual(impersonated.credentials.access_token, 'universe-token'); | ||
|
||
scopes.forEach(s => s.done()); | ||
}); | ||
|
||
it("should throw if an explicit `universeDomain` does not equal the source's `universeDomain`", async () => { | ||
const universeDomain = 'my.universe.com'; | ||
const otherUniverseDomain = 'not-my.universe.com'; | ||
|
||
const sourceClient = createSampleJWTClient(); | ||
sourceClient.universeDomain = otherUniverseDomain; | ||
|
||
assert.throws(() => { | ||
new Impersonated({ | ||
sourceClient, | ||
targetPrincipal: '[email protected]', | ||
lifetime: 30, | ||
delegates: [], | ||
targetScopes: ['https://www.googleapis.com/auth/cloud-platform'], | ||
universeDomain, | ||
}); | ||
}, /does not match/); | ||
}); | ||
|
||
it('should not request impersonated credentials on second request', async () => { | ||
const tomorrow = new Date(); | ||
tomorrow.setDate(tomorrow.getDate() + 1); | ||
|
@@ -383,10 +453,12 @@ describe('impersonated', () => { | |
delegates: string[]; | ||
audience: string; | ||
includeEmail: boolean; | ||
useEmailAzp: true; | ||
}) => { | ||
assert.strictEqual(body.audience, expectedAudience); | ||
assert.strictEqual(body.includeEmail, expectedIncludeEmail); | ||
assert.deepStrictEqual(body.delegates, expectedDeligates); | ||
assert.strictEqual(body.useEmailAzp, true); | ||
return true; | ||
} | ||
) | ||
|