Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for AWS_SESSION_TOKEN temporary credentials #3

Merged
merged 1 commit into from Dec 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 10 additions & 3 deletions client/create_headers.ts
Expand Up @@ -19,11 +19,11 @@ export interface HeadersConfig extends ClientConfig {
}

/** Assembles a header object for a DynamoDB request. */
export function createHeaders(
export async function createHeaders(
op: string,
payload: Uint8Array,
conf: HeadersConfig
): Headers {
): Promise<Headers> {
const amzTarget: string = `DynamoDB_20120810.${op}`;

const amzDate: string = date.format(conf.date || new Date(), "amz");
Expand Down Expand Up @@ -67,10 +67,17 @@ export function createHeaders(
conf.cache.credentialScope
}, SignedHeaders=${signedHeaders}, Signature=${signature}`;

return new Headers({
const headers = new Headers({
"Content-Type": POST_CONTENT_TYPE,
"X-Amz-Date": amzDate,
"X-Amz-Target": amzTarget,
Authorization: authorizationHeader
});

// https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_temp_use-resources.html
if (conf.sessionToken) {
headers.append("X-Amz-Security-Token", await conf.sessionToken());
}

return headers
}
3 changes: 2 additions & 1 deletion mod.ts
Expand Up @@ -29,6 +29,7 @@ export interface ClientConfig {
region: string; // us-west-2
canonicalUri?: string; // fx /path/to/somewhere
port?: number; // 8000
sessionToken?: () => string | Promise<string>;
}

/** Op options. */
Expand Down Expand Up @@ -137,7 +138,7 @@ function createCache(conf: Doc): Doc {
async function baseFetch(conf: Doc, op: string, params: Doc): Promise<Doc> {
const payload: Uint8Array = encode(JSON.stringify(params), "utf8");

const headers: Headers = createHeaders(op, payload, conf as HeadersConfig);
const headers: Headers = await createHeaders(op, payload, conf as HeadersConfig);

const response: Response = await fetch(conf.endpoint, {
method: conf.method,
Expand Down
16 changes: 16 additions & 0 deletions test.ts
Expand Up @@ -432,4 +432,20 @@ test({
}
});

test({
name: "test security token can be passed",
async fn(): Promise<void> {
// currently there's no way to test this is appended to the header
// but we include it in the ClientConfig.
const conf: ClientConfig = {
accessKeyId: ENV.ACCESS_KEY_ID,
secretAccessKey: ENV.SECRET_ACCESS_KEY,
region: "local",
sessionToken: () => "test"
};
const ddbc: DynamoDBClient = createClient(conf);
const result: Doc = await ddbc.listTables();
}
});

runIfMain(import.meta);