Skip to content

Commit ecb2837

Browse files
committed
feat(source-aws-v2): support using a role object for role creation
1 parent c23b71f commit ecb2837

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

packages/source-aws-v2/src/__test__/s3.credentials.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,24 @@ o.spec('AwsCredentials', () => {
1919
o(stub.args[1][0].params.DurationSeconds).equals(3601);
2020
});
2121

22+
o('should create a role from a object', () => {
23+
const stub = sandbox.stub(aws, 'ChainableTemporaryCredentials');
24+
25+
AwsCredentials.fsFromRole({ roleArn: 'arn:abc', externalId: 'abc', durationSeconds: 50 });
26+
o(stub.calledOnce).equals(true);
27+
28+
o(stub.args[0][0].params.RoleArn).equals('arn:abc');
29+
o(stub.args[0][0].params.ExternalId).equals('abc');
30+
o(stub.args[0][0].params.DurationSeconds).equals(50);
31+
32+
AwsCredentials.fsFromRole({ roleArn: 'arn:foo' });
33+
o(stub.calledTwice).equals(true);
34+
35+
o(stub.args[1][0].params.RoleArn).equals('arn:foo');
36+
o(stub.args[1][0].params.ExternalId).equals(undefined);
37+
o(stub.args[1][0].params.DurationSeconds).equals(3600);
38+
});
39+
2240
o('should cache by roleArn', () => {
2341
const stub = sandbox.stub(aws, 'ChainableTemporaryCredentials');
2442
AwsCredentials.role('arn:foo:bar');

packages/source-aws-v2/src/s3.credentials.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ import aws from 'aws-sdk/lib/core.js';
33
import S3 from 'aws-sdk/clients/s3.js';
44
import { FsAwsS3 } from '@chunkd/source-aws';
55

6+
interface RoleObject {
7+
roleArn: string;
8+
externalId?: string;
9+
durationSeconds?: number;
10+
}
11+
612
export class AwsCredentials {
713
static DefaultRoleDurationSeconds = 3600;
814
static cache: Map<string, Credentials> = new Map();
@@ -11,10 +17,18 @@ export class AwsCredentials {
1117
* Create a FsS3 instance from a role arn
1218
*
1319
* @example
14-
* Fs3.fromRoleArn('arn:foo', externalId, 900);
15-
* FsS3.fromRoleArn('arn:bar');
20+
*```typescript
21+
* AwsCredentials.fromRoleArn('arn:foo', externalId, 900);
22+
* AwsCredentials.fromRoleArn('arn:bar');
23+
* AwsCredentials.fromRoleArn({ roleArn: 'arn:foo', externalId: 'bar', 'durationSeconds': 10})
24+
* ```
1625
*/
17-
static fsFromRole(roleArn: string, externalId?: string, durationSeconds?: number): FsAwsS3 {
26+
static fsFromRole(roleArn: RoleObject): FsAwsS3;
27+
static fsFromRole(roleArn: string, externalId?: string, durationSeconds?: number): FsAwsS3;
28+
static fsFromRole(roleArn: string | RoleObject, externalId?: string, durationSeconds?: number): FsAwsS3 {
29+
if (typeof roleArn === 'object') {
30+
return AwsCredentials.fsFromRole(roleArn.roleArn, roleArn.externalId, roleArn.durationSeconds);
31+
}
1832
const credentials = AwsCredentials.role(roleArn, externalId, durationSeconds);
1933
return new FsAwsS3(new S3({ credentials }));
2034
}

0 commit comments

Comments
 (0)