|
1 | | -import { info } from '@actions/core'; |
2 | | -import { STSClient } from '@aws-sdk/client-sts'; |
3 | | -import type { AwsCredentialIdentity } from '@aws-sdk/types'; |
4 | | -import { NodeHttpHandler } from '@smithy/node-http-handler'; |
5 | | -import { ProxyAgent } from 'proxy-agent'; |
6 | | -import { errorMessage, getCallerIdentity } from './helpers'; |
7 | | -import { ProxyResolver } from './ProxyResolver'; |
| 1 | +import { |
| 2 | + fromContainerMetadata, |
| 3 | + fromEnv, |
| 4 | + fromInstanceMetadata, |
| 5 | + fromNodeProviderChain, |
| 6 | + fromTemporaryCredentials, |
| 7 | + fromWebToken, |
| 8 | +} from '@aws-sdk/credential-providers'; |
| 9 | +import type { AwsCredentialIdentityProvider } from '@aws-sdk/types'; |
| 10 | +import type { NodeHttpHandler } from '@smithy/node-http-handler'; |
8 | 11 |
|
9 | | -const USER_AGENT = 'configure-aws-credentials-for-github-actions'; |
| 12 | +type EnvMode = { mode: 'env' }; |
| 13 | +type WebIdentityMode = { mode: 'web-identity'; webIdentityToken: string } & AssumeRoleOptions; |
| 14 | +type StsMode = { mode: 'sts'; masterCredentials?: AwsCredentialIdentityProvider } & AssumeRoleOptions; |
| 15 | +type ContainerMetadataMode = { mode: 'container-metadata' }; |
| 16 | +type InstanceMetadataMode = { mode: 'instance-metadata' }; |
10 | 17 |
|
11 | | -export interface CredentialsClientProps { |
12 | | - region?: string; |
13 | | - proxyServer?: string; |
14 | | - noProxy?: string; |
15 | | -} |
| 18 | +type AssumeRoleOptions = { |
| 19 | + roleArn: string; |
| 20 | + roleSessionName?: string; |
| 21 | + durationSeconds?: number; |
| 22 | + policy?: string; |
| 23 | + policyArns?: { arn: string }[]; |
| 24 | + region: string; |
| 25 | + requestHandler?: NodeHttpHandler; |
| 26 | +}; |
| 27 | + |
| 28 | +export type CredentialsClientProps = EnvMode | WebIdentityMode | StsMode | ContainerMetadataMode | InstanceMetadataMode; |
16 | 29 |
|
17 | 30 | export class CredentialsClient { |
18 | | - public region?: string; |
19 | | - private _stsClient?: STSClient; |
20 | | - private readonly requestHandler?: NodeHttpHandler; |
| 31 | + readonly DEFAULT_ROLE_DURATION = 3600; |
| 32 | + readonly DEFAULT_ROLE_SESSION_NAME = 'GitHubActions'; |
| 33 | + private provider: AwsCredentialIdentityProvider; |
21 | 34 |
|
22 | 35 | constructor(props: CredentialsClientProps) { |
23 | | - if (props.region !== undefined) { |
24 | | - this.region = props.region; |
25 | | - } |
26 | | - if (props.proxyServer) { |
27 | | - info('Configuring proxy handler for STS client'); |
28 | | - const proxyOptions: { httpProxy: string; httpsProxy: string; noProxy?: string } = { |
29 | | - httpProxy: props.proxyServer, |
30 | | - httpsProxy: props.proxyServer, |
31 | | - }; |
32 | | - if (props.noProxy !== undefined) { |
33 | | - proxyOptions.noProxy = props.noProxy; |
34 | | - } |
35 | | - const getProxyForUrl = new ProxyResolver(proxyOptions).getProxyForUrl; |
36 | | - const handler = new ProxyAgent({ getProxyForUrl }); |
37 | | - this.requestHandler = new NodeHttpHandler({ |
38 | | - httpsAgent: handler, |
39 | | - httpAgent: handler, |
40 | | - }); |
41 | | - } |
| 36 | + this.provider = this.createCredentialChain(props); |
42 | 37 | } |
43 | 38 |
|
44 | | - public get stsClient(): STSClient { |
45 | | - if (!this._stsClient) { |
46 | | - const config = { customUserAgent: USER_AGENT } as { |
47 | | - customUserAgent: string; |
48 | | - region?: string; |
49 | | - requestHandler?: NodeHttpHandler; |
50 | | - }; |
51 | | - if (this.region !== undefined) config.region = this.region; |
52 | | - if (this.requestHandler !== undefined) config.requestHandler = this.requestHandler; |
53 | | - this._stsClient = new STSClient(config); |
54 | | - } |
55 | | - return this._stsClient; |
56 | | - } |
| 39 | + private createCredentialChain(props: CredentialsClientProps): AwsCredentialIdentityProvider { |
| 40 | + const primaryProvider = this.getPrimaryProvider(props); |
| 41 | + const fallbackProvider = fromNodeProviderChain(); |
57 | 42 |
|
58 | | - public async validateCredentials( |
59 | | - expectedAccessKeyId?: string, |
60 | | - roleChaining?: boolean, |
61 | | - expectedAccountIds?: string[], |
62 | | - ) { |
63 | | - let credentials: AwsCredentialIdentity; |
64 | | - try { |
65 | | - credentials = await this.loadCredentials(); |
66 | | - if (!credentials.accessKeyId) { |
67 | | - throw new Error('Access key ID empty after loading credentials'); |
68 | | - } |
69 | | - } catch (error) { |
70 | | - throw new Error(`Credentials could not be loaded, please check your action inputs: ${errorMessage(error)}`); |
71 | | - } |
72 | | - if (expectedAccountIds && expectedAccountIds.length > 0 && expectedAccountIds[0] !== '') { |
73 | | - let callerIdentity: Awaited<ReturnType<typeof getCallerIdentity>>; |
| 43 | + return async () => { |
74 | 44 | try { |
75 | | - callerIdentity = await getCallerIdentity(this.stsClient); |
76 | | - } catch (error) { |
77 | | - throw new Error(`Could not validate account ID of credentials: ${errorMessage(error)}`); |
78 | | - } |
79 | | - if (!callerIdentity.Account || !expectedAccountIds.includes(callerIdentity.Account)) { |
80 | | - throw new Error( |
81 | | - `The account ID of the provided credentials (${ |
82 | | - callerIdentity.Account ?? 'unknown' |
83 | | - }) does not match any of the expected account IDs: ${expectedAccountIds.join(', ')}`, |
84 | | - ); |
| 45 | + return await primaryProvider(); |
| 46 | + } catch { |
| 47 | + return await fallbackProvider(); |
85 | 48 | } |
86 | | - } |
| 49 | + }; |
| 50 | + } |
87 | 51 |
|
88 | | - if (!roleChaining) { |
89 | | - const actualAccessKeyId = credentials.accessKeyId; |
90 | | - if (expectedAccessKeyId && expectedAccessKeyId !== actualAccessKeyId) { |
91 | | - throw new Error( |
92 | | - 'Credentials loaded by the SDK do not match the expected access key ID configured by the action', |
93 | | - ); |
94 | | - } |
| 52 | + private getPrimaryProvider(props: CredentialsClientProps): AwsCredentialIdentityProvider { |
| 53 | + switch (props.mode) { |
| 54 | + case 'env': |
| 55 | + return fromEnv(); |
| 56 | + case 'web-identity': |
| 57 | + return fromWebToken({ |
| 58 | + clientConfig: { |
| 59 | + region: props.region, |
| 60 | + ...(props.requestHandler && { requestHandler: props.requestHandler }), |
| 61 | + maxAttempts: 1, // Disable built-in retry logic |
| 62 | + }, |
| 63 | + roleArn: props.roleArn, |
| 64 | + webIdentityToken: props.webIdentityToken, |
| 65 | + durationSeconds: props.durationSeconds ?? this.DEFAULT_ROLE_DURATION, |
| 66 | + roleSessionName: props.roleSessionName ?? this.DEFAULT_ROLE_SESSION_NAME, |
| 67 | + ...(props.policy && { policy: props.policy }), |
| 68 | + ...(props.policyArns && { policyArns: props.policyArns }), |
| 69 | + }); |
| 70 | + case 'sts': |
| 71 | + return fromTemporaryCredentials({ |
| 72 | + params: { |
| 73 | + RoleArn: props.roleArn, |
| 74 | + DurationSeconds: props.durationSeconds ?? this.DEFAULT_ROLE_DURATION, |
| 75 | + RoleSessionName: props.roleSessionName ?? this.DEFAULT_ROLE_SESSION_NAME, |
| 76 | + ...(props.policy && { Policy: props.policy }), |
| 77 | + ...(props.policyArns && { PolicyArns: props.policyArns }), |
| 78 | + }, |
| 79 | + clientConfig: { |
| 80 | + region: props.region, |
| 81 | + ...(props.requestHandler && { requestHandler: props.requestHandler }), |
| 82 | + maxAttempts: 1, // Disable built-in retry logic |
| 83 | + }, |
| 84 | + ...(props.masterCredentials && { masterCredentials: props.masterCredentials }), |
| 85 | + }); |
| 86 | + case 'container-metadata': |
| 87 | + return fromContainerMetadata(); |
| 88 | + case 'instance-metadata': |
| 89 | + return fromInstanceMetadata(); |
95 | 90 | } |
96 | 91 | } |
97 | 92 |
|
98 | | - private async loadCredentials() { |
99 | | - const config = {} as { requestHandler?: NodeHttpHandler }; |
100 | | - if (this.requestHandler !== undefined) config.requestHandler = this.requestHandler; |
101 | | - const client = new STSClient(config); |
102 | | - return client.config.credentials(); |
| 93 | + getCredentials(): AwsCredentialIdentityProvider { |
| 94 | + return this.provider; |
103 | 95 | } |
104 | 96 | } |
0 commit comments