mirror of
https://github.com/aws-actions/configure-aws-credentials.git
synced 2026-11-29 03:15:39 +01:00
feat: role-chaining
This commit is contained in:
8 files changed
+67
-70
No files matched your search
+14
-19
@@ -13,17 +13,13 @@ export interface CredentialsClientProps {
|
||||
|
||||
export class CredentialsClient {
|
||||
public region?: string;
|
||||
private stsClient?: STSClient;
|
||||
private _stsClient?: STSClient;
|
||||
private readonly requestHandler?: NodeHttpHandler;
|
||||
|
||||
constructor(props: CredentialsClientProps) {
|
||||
if (props.region) {
|
||||
this.region = props.region;
|
||||
} else {
|
||||
info('No region provided, using global STS endpoint');
|
||||
}
|
||||
this.region = props.region;
|
||||
if (props.proxyServer) {
|
||||
info('Configurint proxy handler for STS client');
|
||||
info('Configuring proxy handler for STS client');
|
||||
const handler = new HttpsProxyAgent(props.proxyServer);
|
||||
this.requestHandler = new NodeHttpHandler({
|
||||
httpAgent: handler,
|
||||
@@ -32,19 +28,18 @@ export class CredentialsClient {
|
||||
}
|
||||
}
|
||||
|
||||
public getStsClient(): STSClient {
|
||||
if (!this.stsClient) {
|
||||
this.stsClient = new STSClient({
|
||||
region: this.region ? this.region : undefined,
|
||||
public get stsClient(): STSClient {
|
||||
if (!this._stsClient) {
|
||||
this._stsClient = new STSClient({
|
||||
region: this.region,
|
||||
customUserAgent: USER_AGENT,
|
||||
requestHandler: this.requestHandler ? this.requestHandler : undefined,
|
||||
useGlobalEndpoint: this.region ? false : true,
|
||||
});
|
||||
}
|
||||
return this.stsClient;
|
||||
return this._stsClient;
|
||||
}
|
||||
|
||||
public async validateCredentials(expectedAccessKeyId?: string) {
|
||||
public async validateCredentials(expectedAccessKeyId?: string, roleChaining?: boolean) {
|
||||
let credentials;
|
||||
try {
|
||||
credentials = await this.loadCredentials();
|
||||
@@ -55,12 +50,12 @@ export class CredentialsClient {
|
||||
throw new Error(`Credentials could not be loaded, please check your action inputs: ${errorMessage(error)}`);
|
||||
}
|
||||
|
||||
const actualAccessKeyId = credentials.accessKeyId;
|
||||
if (!roleChaining) {
|
||||
const actualAccessKeyId = credentials.accessKeyId;
|
||||
|
||||
if (expectedAccessKeyId && expectedAccessKeyId !== actualAccessKeyId) {
|
||||
throw new Error(
|
||||
'Unexpected failure: Credentials loaded by the SDK do not match the access key ID configured by the action'
|
||||
);
|
||||
if (expectedAccessKeyId && expectedAccessKeyId !== actualAccessKeyId) {
|
||||
throw new Error('Unexpected failure: Credentials loaded by the SDK do not match the access key ID configured by the action');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -139,7 +139,7 @@ export async function assumeRole(params: assumeRoleParams) {
|
||||
keys.forEach((k) => commonAssumeRoleParams[k] === undefined && delete commonAssumeRoleParams[k]);
|
||||
|
||||
// Instantiate STS client
|
||||
const stsClient = credentialsClient.getStsClient();
|
||||
const stsClient = credentialsClient.stsClient;
|
||||
|
||||
// Assume role using one of three methods
|
||||
switch (true) {
|
||||
|
||||
+1
-1
@@ -35,7 +35,7 @@ export function exportRegion(region: string) {
|
||||
|
||||
// Obtains account ID from STS Client and sets it as output
|
||||
export async function exportAccountId(credentialsClient: CredentialsClient, maskAccountId?: string) {
|
||||
const client = credentialsClient.getStsClient();
|
||||
const client = credentialsClient.stsClient;
|
||||
const identity = await client.send(new GetCallerIdentityCommand({}));
|
||||
const accountId = identity.Account;
|
||||
if (!accountId) {
|
||||
|
||||
+8
-7
@@ -14,10 +14,7 @@ export async function run() {
|
||||
const SecretAccessKey = core.getInput('aws-secret-access-key', { required: false });
|
||||
const sessionTokenInput = core.getInput('aws-session-token', { required: false });
|
||||
const SessionToken = sessionTokenInput === '' ? undefined : sessionTokenInput;
|
||||
const region =
|
||||
core.getInput('aws-region', { required: false }) ||
|
||||
process.env['AWS_REGION'] ||
|
||||
process.env['AWS_DEFAULT_REGION'];
|
||||
const region = core.getInput('aws-region', { required: true });
|
||||
const roleToAssume = core.getInput('role-to-assume', { required: false });
|
||||
const audience = core.getInput('audience', { required: false });
|
||||
const maskAccountId = core.getInput('mask-aws-account-id', { required: false });
|
||||
@@ -35,6 +32,8 @@ export async function run() {
|
||||
for (const managedSessionPolicy of managedSessionPoliciesInput) {
|
||||
managedSessionPolicies.push({arn: managedSessionPolicy});
|
||||
}
|
||||
const roleChainingInput = core.getInput('role-chaining', { required: false }) || 'false';
|
||||
const roleChaining = roleChainingInput.toLowerCase() === 'true';
|
||||
|
||||
// Logic to decide whether to attempt to use OIDC or not
|
||||
const useGitHubOIDCProvider = () => {
|
||||
@@ -47,7 +46,8 @@ export async function run() {
|
||||
!webIdentityTokenFile &&
|
||||
!AccessKeyId &&
|
||||
!disableOIDC &&
|
||||
!process.env['ACTIONS_ID_TOKEN_REQUEST_TOKEN']
|
||||
!process.env['ACTIONS_ID_TOKEN_REQUEST_TOKEN'] &&
|
||||
!roleChaining
|
||||
) {
|
||||
core.info(
|
||||
'It looks like you might be trying to authenticate with OIDC. Did you mean to set the `id-token` permission?'
|
||||
@@ -58,7 +58,8 @@ export async function run() {
|
||||
!!process.env['ACTIONS_ID_TOKEN_REQUEST_TOKEN'] &&
|
||||
!AccessKeyId &&
|
||||
!webIdentityTokenFile &&
|
||||
!disableOIDC
|
||||
!disableOIDC &&
|
||||
!roleChaining
|
||||
);
|
||||
};
|
||||
|
||||
@@ -98,7 +99,7 @@ export async function run() {
|
||||
// cases where this action is on a self-hosted runner that doesn't have credentials
|
||||
// configured correctly, and cases where the user intended to provide input
|
||||
// credentials but the secrets inputs resolved to empty strings.
|
||||
await credentialsClient.validateCredentials(AccessKeyId);
|
||||
await credentialsClient.validateCredentials(AccessKeyId, roleChaining);
|
||||
|
||||
sourceAccountId = await exportAccountId(credentialsClient, maskAccountId);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user