-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
90 lines (90 loc) · 3.59 KB
/
index.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.templateTags = void 0;
// import { fromIni } from "@aws-sdk/credential-providers"; // ES6 import
const { fromIni } = require("@aws-sdk/credential-providers"); // CommonJS import
var hourlyCachedCreds = new Map();
async function loadCredentialsFromProfile(profileName) {
const credentialsProvider = fromIni({
/**
* Optional. The configuration profile to use. If not specified, the provider will use the value
* in the `AWS_PROFILE` environment variable or a default of `default`.
*/
profile: profileName,
/**
* Optional. The path to the shared credentials file. If not specified, the provider will use
* the value in the `AWS_SHARED_CREDENTIALS_FILE` environment variable or a default of `~/.aws/credentials`.
*/
// filepath: "~/.aws/credentials",
/**
* Optional. The path to the shared config file. If not specified, the provider will use the
* value in the `AWS_CONFIG_FILE` environment variable or a default of `~/.aws/config`.
*/
// configFilepath: "~/.aws/config",
/**
* Optional. A function that returns a a promise fulfilled with an MFA token code for the
* provided MFA Serial code. If a profile requires an MFA code and `mfaCodeProvider` is not a
* valid function, the credential provider promise will be rejected.
*/
// mfaCodeProvider: async (mfaSerial) => {
// return "token";
// },
/**
* Optional. Custom STS client configurations overriding the default ones.
*/
// clientConfig: { region },
});
return credentialsProvider();
}
var Attribute;
(function (Attribute) {
Attribute["accessKeyId"] = "accessKeyId";
Attribute["secretAccessKey"] = "secretAccessKey";
Attribute["sessionToken"] = "sessionToken";
})(Attribute || (Attribute = {}));
exports.templateTags = [
{
name: 'awsiam',
displayName: 'awsiam',
description: 'Insomnia plugin - AWS IAM credential loader',
args: [{
displayName: 'Attribute',
type: 'enum',
options: [
{
displayName: Attribute.accessKeyId,
value: Attribute.accessKeyId,
},
{
displayName: Attribute.secretAccessKey,
value: Attribute.secretAccessKey,
},
{
displayName: Attribute.sessionToken,
value: Attribute.sessionToken,
},
]
}, {
displayName: 'Profile',
description: 'Profile name',
type: 'string',
defaultValue: 'default'
}, {
displayName: 'Region',
description: 'Region name',
type: 'string',
defaultValue: 'us-west-2'
}],
async run(context, attribute, profileName) {
let currentHour = new Date().getHours();
// Cache Lasts 1 hour
const cacheKey = `${profileName}-${currentHour}`.toLowerCase();
// Create a new cache entry
if (hourlyCachedCreds.get(cacheKey) === undefined) {
const creds = await loadCredentialsFromProfile(profileName);
hourlyCachedCreds.set(cacheKey, creds);
}
return hourlyCachedCreds.get(cacheKey)[attribute];
}
}
];