-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(fs): support caching of aws credentials
- Loading branch information
Showing
6 changed files
with
68 additions
and
38 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export { FileSystemAbstraction, fsa } from './fs.abstraction.js'; | ||
export { FsAwsS3 } from '@chunkd/source-aws'; | ||
export { FsAwsS3, AwsCredentials } from '@chunkd/source-aws'; | ||
export { FsHttp } from '@chunkd/source-http'; |
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import o from 'ospec'; | ||
import Sinon from 'sinon'; | ||
import aws from 'aws-sdk/lib/core.js'; | ||
import { AwsCredentials } from '../s3.credentials.js'; | ||
|
||
o.spec('AwsCredentials', () => { | ||
const sandbox = Sinon.createSandbox(); | ||
|
||
o.beforeEach(() => sandbox.restore()); | ||
o('should default to 3600 second duration', () => { | ||
const stub = sandbox.stub(aws, 'ChainableTemporaryCredentials'); | ||
AwsCredentials.role('foo'); | ||
o(stub.args[0][0].params.DurationSeconds).equals(3600); | ||
AwsCredentials.role('foo', undefined, 3600); | ||
o(stub.calledOnce).equals(true); | ||
|
||
AwsCredentials.role('foo', undefined, 3601); | ||
o(stub.callCount).equals(2); | ||
o(stub.args[1][0].params.DurationSeconds).equals(3601); | ||
}); | ||
|
||
o('should cache by roleArn', () => { | ||
const stub = sandbox.stub(aws, 'ChainableTemporaryCredentials'); | ||
AwsCredentials.role('arn:foo:bar'); | ||
AwsCredentials.role('arn:foo:bar'); | ||
o(stub.calledOnce).equals(true); | ||
AwsCredentials.role('arn:foo:baz'); | ||
o(stub.callCount).equals(2); | ||
AwsCredentials.role('arn:foo:baz', 'external'); | ||
o(stub.callCount).equals(3); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { SourceAwsS3 } from './s3.source.js'; | ||
export { FsAwsS3 } from './s3.fs.js'; | ||
export { AwsCredentials } from './s3.credentials.js'; |
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Credentials } from 'aws-sdk/lib/credentials.js'; | ||
import aws from 'aws-sdk/lib/core.js'; | ||
|
||
export class AwsCredentials { | ||
static defaultRoleDuration = 3600; | ||
static cache: Map<string, Credentials> = new Map(); | ||
|
||
static role(roleArn: string, externalId?: string, duration?: number): Credentials { | ||
duration = duration ?? AwsCredentials.defaultRoleDuration; | ||
|
||
const roleKey = `role::${roleArn}::${externalId}::${duration}`; | ||
let existing = AwsCredentials.cache.get(roleKey); | ||
if (existing == null) { | ||
existing = new aws.ChainableTemporaryCredentials({ | ||
params: { | ||
RoleArn: roleArn, | ||
ExternalId: externalId, | ||
RoleSessionName: 'fsa-' + Math.random().toString(32) + '-' + Date.now(), | ||
DurationSeconds: duration, | ||
}, | ||
}); | ||
AwsCredentials.cache.set(roleKey, existing); | ||
} | ||
return existing; | ||
} | ||
} |
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