Skip to content

Commit 6eef6c3

Browse files
committed
remove source check during role assume
1 parent f72bbaf commit 6eef6c3

File tree

4 files changed

+81
-10
lines changed

4 files changed

+81
-10
lines changed

.nvmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
22

packages/nodes-base/credentials/common/aws/system-credentials-utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ import { Container } from '@n8n/di';
33
import { ApplicationError } from 'n8n-workflow';
44

55
type Resolvers = 'environment' | 'podIdentity' | 'containerMetadata' | 'instanceMetadata';
6-
type RetrunData = {
6+
type ReturnData = {
77
accessKeyId: string;
88
secretAccessKey: string;
99
sessionToken?: string;
1010
};
1111

1212
export const envGetter = (key: string): string | undefined => process.env[key];
1313

14-
export const credentialsResolver: Record<Resolvers, () => Promise<RetrunData | null>> = {
14+
export const credentialsResolver: Record<Resolvers, () => Promise<ReturnData | null>> = {
1515
environment: getEnvironmentCredentials,
1616
instanceMetadata: getInstanceMetadataCredentials,
1717
containerMetadata: getContainerMetadataCredentials,

packages/nodes-base/credentials/common/aws/utils.test.ts

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ describe('assumeRole', () => {
3737
});
3838

3939
describe('with system credentials', () => {
40-
it('should successfully assume role using system credentials', async () => {
40+
it('should successfully assume role using system credentials by environment', async () => {
4141
const credentials: AwsAssumeRoleCredentialsType = {
4242
region: 'us-east-1',
4343
customEndpoints: false,
@@ -113,6 +113,82 @@ describe('assumeRole', () => {
113113
);
114114
});
115115

116+
it('should successfully assume role using system credentials by instanceMetadata', async () => {
117+
const credentials: AwsAssumeRoleCredentialsType = {
118+
region: 'us-east-1',
119+
customEndpoints: false,
120+
useSystemCredentialsForRole: true,
121+
roleArn: 'arn:aws:iam::123456789012:role/TestRole',
122+
roleSessionName: 'test-session',
123+
};
124+
125+
const mockSystemCredentials = {
126+
accessKeyId: 'system-access-key',
127+
secretAccessKey: 'system-secret-key',
128+
sessionToken: 'system-session-token',
129+
source: 'instanceMetadata' as const,
130+
};
131+
132+
jest
133+
.spyOn(systemCredentialsUtils, 'getSystemCredentials')
134+
.mockResolvedValue(mockSystemCredentials);
135+
136+
const mockResponse = {
137+
ok: true,
138+
text: jest.fn().mockResolvedValue(`<?xml version="1.0" encoding="UTF-8"?>
139+
<AssumeRoleResponse xmlns="https://sts.amazonaws.com/doc/2011-06-15/">
140+
<AssumeRoleResult>
141+
<Credentials>
142+
<AccessKeyId>assumed-access-key</AccessKeyId>
143+
<SecretAccessKey>assumed-secret-key</SecretAccessKey>
144+
<SessionToken>assumed-session-token</SessionToken>
145+
</Credentials>
146+
</AssumeRoleResult>
147+
</AssumeRoleResponse>`),
148+
};
149+
150+
mockFetch.mockResolvedValue(mockResponse as any);
151+
152+
mockParseString.mockImplementation((_xml, _options, callback) => {
153+
callback(null, {
154+
AssumeRoleResponse: {
155+
AssumeRoleResult: {
156+
Credentials: {
157+
AccessKeyId: 'assumed-access-key',
158+
SecretAccessKey: 'assumed-secret-key',
159+
SessionToken: 'assumed-session-token',
160+
},
161+
},
162+
},
163+
});
164+
});
165+
166+
const result = await assumeRole(credentials, 'us-east-1');
167+
168+
expect(result).toEqual({
169+
accessKeyId: 'assumed-access-key',
170+
secretAccessKey: 'assumed-secret-key',
171+
sessionToken: 'assumed-session-token',
172+
});
173+
174+
expect(systemCredentialsUtils.getSystemCredentials).toHaveBeenCalled();
175+
expect(mockSign).toHaveBeenCalledWith(
176+
expect.objectContaining({
177+
method: 'POST',
178+
path: '/',
179+
region: 'us-east-1',
180+
}),
181+
mockSystemCredentials,
182+
);
183+
expect(mockFetch).toHaveBeenCalledWith(
184+
'https://sts.us-east-1.amazonaws.com',
185+
expect.objectContaining({
186+
method: 'POST',
187+
body: expect.stringContaining('Action=AssumeRole'),
188+
}),
189+
);
190+
});
191+
116192
it('should throw error when system credentials are not available', async () => {
117193
const credentials: AwsAssumeRoleCredentialsType = {
118194
region: 'us-east-1',

packages/nodes-base/credentials/common/aws/utils.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -252,13 +252,7 @@ export async function assumeRole(
252252
'System AWS credentials are required for role assumption. Please ensure AWS credentials are available via environment variables, instance metadata, or container role.',
253253
);
254254
}
255-
if (systemCredentials.source !== 'environment') {
256-
return {
257-
accessKeyId: systemCredentials.accessKeyId,
258-
secretAccessKey: systemCredentials.secretAccessKey,
259-
sessionToken: systemCredentials.sessionToken as string,
260-
};
261-
}
255+
262256
stsCallCredentials = systemCredentials;
263257
} else {
264258
if (!credentials.stsAccessKeyId || credentials.stsAccessKeyId.trim() === '') {

0 commit comments

Comments
 (0)