-
Notifications
You must be signed in to change notification settings - Fork 0
/
presign-via-middleware.js
48 lines (42 loc) · 1.22 KB
/
presign-via-middleware.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
const fs = require("fs");
const { STSClient, GetCallerIdentityCommand } = require("@aws-sdk/client-sts");
const { awsAuthMiddlewareOptions } = require("@aws-sdk/middleware-signing");
const ERROR_CODE = "SIGNING_COMPLETED_ABORTING";
const logMiddleware = (next, context) => async (args) => {
const err = new Error(ERROR_CODE);
err.request = args.request;
throw err;
};
const client = new STSClient({ region: "us-east-1" });
client.middlewareStack.addRelativeTo(logMiddleware, {
relation: "after",
toMiddleware: awsAuthMiddlewareOptions.name,
});
async function getCallerIdentitySignedRequest() {
const command = new GetCallerIdentityCommand();
try {
const results = await client.send(command);
} catch (err) {
if (err.message == ERROR_CODE) {
return err.request;
} else {
throw err;
}
}
}
const storeData = (data, path) => {
try {
fs.writeFileSync(path, JSON.stringify(data));
} catch (err) {
console.error(err);
}
};
(async () => {
const signedRequest = await getCallerIdentitySignedRequest();
const transformed = {
url: `https://${signedRequest.hostname}`,
headers: signedRequest.headers,
body: signedRequest.body,
};
storeData(transformed, "../request.json");
})();