@karmaniverous/get-dotenv 6.2.3 → 6.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chunks/AwsRestJsonProtocol-Bq1HE-Ln.mjs +932 -0
- package/dist/chunks/createCli-BY6_cfZr.mjs +439 -0
- package/dist/chunks/externalDataInterceptor-CbsdEYa-.mjs +19 -0
- package/dist/chunks/getSSOTokenFromFile-hUSpR7Wf.mjs +22 -0
- package/dist/chunks/helpConfig-CGejgwWW.mjs +12 -0
- package/dist/chunks/index-B5JKTBOL.mjs +443 -0
- package/dist/chunks/index-BEJFiHMX.mjs +522 -0
- package/dist/chunks/index-BPYF6K_G.mjs +82 -0
- package/dist/chunks/index-Bc3h0a95.mjs +374 -0
- package/dist/chunks/index-BpCF5UKx.mjs +272 -0
- package/dist/chunks/index-C_wqbTwI.mjs +187 -0
- package/dist/chunks/index-CeCufHlm.mjs +9374 -0
- package/dist/chunks/index-Cu7rdyqN.mjs +102 -0
- package/dist/chunks/index-DWAtHEA-.mjs +379 -0
- package/dist/chunks/index-Dp1Ip6Ra.mjs +354 -0
- package/dist/chunks/index-DyU5pKKi.mjs +24 -0
- package/dist/chunks/index-c7zKtEuy.mjs +578 -0
- package/dist/chunks/index-cIunyiUQ.mjs +702 -0
- package/dist/chunks/invoke-DuRPU1oC.mjs +60 -0
- package/dist/chunks/loadModuleDefault-Dj8B3Stt.mjs +123 -0
- package/dist/chunks/loadSso-w1eTVg0O.mjs +412 -0
- package/dist/chunks/loader-DnhPeGfq.mjs +346 -0
- package/dist/chunks/overlayEnv-Bs2kVayG.mjs +234 -0
- package/dist/chunks/package-boo9EyYs.mjs +5 -0
- package/dist/chunks/parseKnownFiles-B9cDK21V.mjs +23 -0
- package/dist/chunks/readMergedOptions-Nt0TR7dX.mjs +1626 -0
- package/dist/chunks/resolveCliOptions-TFRzhB2c.mjs +138 -0
- package/dist/chunks/sdk-stream-mixin-BZoJ5jy9.mjs +167 -0
- package/dist/chunks/spawnEnv-CN8a7cNR.mjs +306 -0
- package/dist/chunks/types-DJ-BGABd.mjs +59 -0
- package/dist/chunks/validate-CDl0rE6k.mjs +61 -0
- package/dist/cli.mjs +39 -19307
- package/dist/cliHost.mjs +20 -2800
- package/dist/config.mjs +10 -509
- package/dist/env-overlay.mjs +6 -337
- package/dist/getdotenv.cli.mjs +39 -19305
- package/dist/index.mjs +39 -19396
- package/dist/plugins-aws.d.ts +1 -4
- package/dist/plugins-aws.mjs +65 -2568
- package/dist/plugins-batch.mjs +16 -2573
- package/dist/plugins-cmd.mjs +19 -3094
- package/dist/plugins-init.d.ts +8 -0
- package/dist/plugins-init.mjs +85 -2297
- package/dist/plugins.mjs +36 -18817
- package/package.json +1 -2
- package/dist/templates/cli/index.ts +0 -25
- package/dist/templates/cli/plugins/hello/defaultAction.ts +0 -27
- package/dist/templates/cli/plugins/hello/index.ts +0 -26
- package/dist/templates/cli/plugins/hello/options.ts +0 -31
- package/dist/templates/cli/plugins/hello/strangerAction.ts +0 -20
- package/dist/templates/cli/plugins/hello/types.ts +0 -13
- package/dist/templates/config/js/getdotenv.config.js +0 -20
- package/dist/templates/config/json/local/getdotenv.config.local.json +0 -7
- package/dist/templates/config/json/public/getdotenv.config.json +0 -9
- package/dist/templates/config/public/getdotenv.config.json +0 -8
- package/dist/templates/config/ts/getdotenv.config.ts +0 -28
- package/dist/templates/config/yaml/local/getdotenv.config.local.yaml +0 -7
- package/dist/templates/config/yaml/public/getdotenv.config.yaml +0 -7
- package/dist/templates/defaultAction.ts +0 -27
- package/dist/templates/getdotenv.config.js +0 -20
- package/dist/templates/getdotenv.config.json +0 -9
- package/dist/templates/getdotenv.config.local.json +0 -7
- package/dist/templates/getdotenv.config.local.yaml +0 -7
- package/dist/templates/getdotenv.config.ts +0 -28
- package/dist/templates/getdotenv.config.yaml +0 -7
- package/dist/templates/hello/defaultAction.ts +0 -27
- package/dist/templates/hello/index.ts +0 -26
- package/dist/templates/hello/options.ts +0 -31
- package/dist/templates/hello/strangerAction.ts +0 -20
- package/dist/templates/hello/types.ts +0 -13
- package/dist/templates/index.ts +0 -26
- package/dist/templates/js/getdotenv.config.js +0 -20
- package/dist/templates/json/local/getdotenv.config.local.json +0 -7
- package/dist/templates/json/public/getdotenv.config.json +0 -9
- package/dist/templates/local/getdotenv.config.local.json +0 -7
- package/dist/templates/local/getdotenv.config.local.yaml +0 -7
- package/dist/templates/options.ts +0 -31
- package/dist/templates/plugins/hello/defaultAction.ts +0 -27
- package/dist/templates/plugins/hello/index.ts +0 -26
- package/dist/templates/plugins/hello/options.ts +0 -31
- package/dist/templates/plugins/hello/strangerAction.ts +0 -20
- package/dist/templates/plugins/hello/types.ts +0 -13
- package/dist/templates/public/getdotenv.config.json +0 -9
- package/dist/templates/public/getdotenv.config.yaml +0 -7
- package/dist/templates/strangerAction.ts +0 -20
- package/dist/templates/ts/getdotenv.config.ts +0 -28
- package/dist/templates/types.ts +0 -13
- package/dist/templates/yaml/local/getdotenv.config.local.yaml +0 -7
- package/dist/templates/yaml/public/getdotenv.config.yaml +0 -7
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { exec } from 'child_process';
|
|
2
|
+
import { promisify } from 'util';
|
|
3
|
+
import { s as setCredentialFeature, C as CredentialsProviderError, k as getProfileName } from './index-CeCufHlm.mjs';
|
|
4
|
+
import { e as externalDataInterceptor } from './externalDataInterceptor-CbsdEYa-.mjs';
|
|
5
|
+
import { p as parseKnownFiles } from './parseKnownFiles-B9cDK21V.mjs';
|
|
6
|
+
import './readMergedOptions-Nt0TR7dX.mjs';
|
|
7
|
+
import 'zod';
|
|
8
|
+
import '@commander-js/extra-typings';
|
|
9
|
+
import './overlayEnv-Bs2kVayG.mjs';
|
|
10
|
+
import 'fs-extra';
|
|
11
|
+
import './loadModuleDefault-Dj8B3Stt.mjs';
|
|
12
|
+
import 'crypto';
|
|
13
|
+
import 'path';
|
|
14
|
+
import 'url';
|
|
15
|
+
import 'nanoid';
|
|
16
|
+
import 'dotenv';
|
|
17
|
+
import './loader-DnhPeGfq.mjs';
|
|
18
|
+
import 'package-directory';
|
|
19
|
+
import 'yaml';
|
|
20
|
+
import 'execa';
|
|
21
|
+
import 'buffer';
|
|
22
|
+
import 'os';
|
|
23
|
+
import 'node:fs/promises';
|
|
24
|
+
import 'http';
|
|
25
|
+
import 'https';
|
|
26
|
+
import 'stream';
|
|
27
|
+
import 'process';
|
|
28
|
+
import 'node:fs';
|
|
29
|
+
import './getSSOTokenFromFile-hUSpR7Wf.mjs';
|
|
30
|
+
import 'fs/promises';
|
|
31
|
+
|
|
32
|
+
const getValidatedProcessCredentials = (profileName, data, profiles) => {
|
|
33
|
+
if (data.Version !== 1) {
|
|
34
|
+
throw Error(`Profile ${profileName} credential_process did not return Version 1.`);
|
|
35
|
+
}
|
|
36
|
+
if (data.AccessKeyId === undefined || data.SecretAccessKey === undefined) {
|
|
37
|
+
throw Error(`Profile ${profileName} credential_process returned invalid credentials.`);
|
|
38
|
+
}
|
|
39
|
+
if (data.Expiration) {
|
|
40
|
+
const currentTime = new Date();
|
|
41
|
+
const expireTime = new Date(data.Expiration);
|
|
42
|
+
if (expireTime < currentTime) {
|
|
43
|
+
throw Error(`Profile ${profileName} credential_process returned expired credentials.`);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
let accountId = data.AccountId;
|
|
47
|
+
if (!accountId && profiles?.[profileName]?.aws_account_id) {
|
|
48
|
+
accountId = profiles[profileName].aws_account_id;
|
|
49
|
+
}
|
|
50
|
+
const credentials = {
|
|
51
|
+
accessKeyId: data.AccessKeyId,
|
|
52
|
+
secretAccessKey: data.SecretAccessKey,
|
|
53
|
+
...(data.SessionToken && { sessionToken: data.SessionToken }),
|
|
54
|
+
...(data.Expiration && { expiration: new Date(data.Expiration) }),
|
|
55
|
+
...(data.CredentialScope && { credentialScope: data.CredentialScope }),
|
|
56
|
+
...(accountId && { accountId }),
|
|
57
|
+
};
|
|
58
|
+
setCredentialFeature(credentials, "CREDENTIALS_PROCESS", "w");
|
|
59
|
+
return credentials;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const resolveProcessCredentials = async (profileName, profiles, logger) => {
|
|
63
|
+
const profile = profiles[profileName];
|
|
64
|
+
if (profiles[profileName]) {
|
|
65
|
+
const credentialProcess = profile["credential_process"];
|
|
66
|
+
if (credentialProcess !== undefined) {
|
|
67
|
+
const execPromise = promisify(externalDataInterceptor?.getTokenRecord?.().exec ?? exec);
|
|
68
|
+
try {
|
|
69
|
+
const { stdout } = await execPromise(credentialProcess);
|
|
70
|
+
let data;
|
|
71
|
+
try {
|
|
72
|
+
data = JSON.parse(stdout.trim());
|
|
73
|
+
}
|
|
74
|
+
catch {
|
|
75
|
+
throw Error(`Profile ${profileName} credential_process returned invalid JSON.`);
|
|
76
|
+
}
|
|
77
|
+
return getValidatedProcessCredentials(profileName, data, profiles);
|
|
78
|
+
}
|
|
79
|
+
catch (error) {
|
|
80
|
+
throw new CredentialsProviderError(error.message, { logger });
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
throw new CredentialsProviderError(`Profile ${profileName} did not contain credential_process.`, { logger });
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
throw new CredentialsProviderError(`Profile ${profileName} could not be found in shared credentials file.`, {
|
|
89
|
+
logger,
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
const fromProcess = (init = {}) => async ({ callerClientConfig } = {}) => {
|
|
95
|
+
init.logger?.debug("@aws-sdk/credential-provider-process - fromProcess");
|
|
96
|
+
const profiles = await parseKnownFiles(init);
|
|
97
|
+
return resolveProcessCredentials(getProfileName({
|
|
98
|
+
profile: init.profile ?? callerClientConfig?.profile,
|
|
99
|
+
}), profiles, init.logger);
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
export { fromProcess };
|
|
@@ -0,0 +1,379 @@
|
|
|
1
|
+
import { parse } from 'url';
|
|
2
|
+
import { Buffer } from 'buffer';
|
|
3
|
+
import { request } from 'http';
|
|
4
|
+
import { aq as ProviderError, C as CredentialsProviderError, F as parseUrl, O as loadConfig } from './index-CeCufHlm.mjs';
|
|
5
|
+
import './readMergedOptions-Nt0TR7dX.mjs';
|
|
6
|
+
import 'zod';
|
|
7
|
+
import '@commander-js/extra-typings';
|
|
8
|
+
import './overlayEnv-Bs2kVayG.mjs';
|
|
9
|
+
import 'fs-extra';
|
|
10
|
+
import './loadModuleDefault-Dj8B3Stt.mjs';
|
|
11
|
+
import 'crypto';
|
|
12
|
+
import 'path';
|
|
13
|
+
import 'nanoid';
|
|
14
|
+
import 'dotenv';
|
|
15
|
+
import './loader-DnhPeGfq.mjs';
|
|
16
|
+
import 'package-directory';
|
|
17
|
+
import 'yaml';
|
|
18
|
+
import 'execa';
|
|
19
|
+
import 'os';
|
|
20
|
+
import 'node:fs/promises';
|
|
21
|
+
import 'https';
|
|
22
|
+
import 'stream';
|
|
23
|
+
import 'process';
|
|
24
|
+
import 'node:fs';
|
|
25
|
+
|
|
26
|
+
function httpRequest(options) {
|
|
27
|
+
return new Promise((resolve, reject) => {
|
|
28
|
+
const req = request({
|
|
29
|
+
method: "GET",
|
|
30
|
+
...options,
|
|
31
|
+
hostname: options.hostname?.replace(/^\[(.+)\]$/, "$1"),
|
|
32
|
+
});
|
|
33
|
+
req.on("error", (err) => {
|
|
34
|
+
reject(Object.assign(new ProviderError("Unable to connect to instance metadata service"), err));
|
|
35
|
+
req.destroy();
|
|
36
|
+
});
|
|
37
|
+
req.on("timeout", () => {
|
|
38
|
+
reject(new ProviderError("TimeoutError from instance metadata service"));
|
|
39
|
+
req.destroy();
|
|
40
|
+
});
|
|
41
|
+
req.on("response", (res) => {
|
|
42
|
+
const { statusCode = 400 } = res;
|
|
43
|
+
if (statusCode < 200 || 300 <= statusCode) {
|
|
44
|
+
reject(Object.assign(new ProviderError("Error response received from instance metadata service"), { statusCode }));
|
|
45
|
+
req.destroy();
|
|
46
|
+
}
|
|
47
|
+
const chunks = [];
|
|
48
|
+
res.on("data", (chunk) => {
|
|
49
|
+
chunks.push(chunk);
|
|
50
|
+
});
|
|
51
|
+
res.on("end", () => {
|
|
52
|
+
resolve(Buffer.concat(chunks));
|
|
53
|
+
req.destroy();
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
req.end();
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const isImdsCredentials = (arg) => Boolean(arg) &&
|
|
61
|
+
typeof arg === "object" &&
|
|
62
|
+
typeof arg.AccessKeyId === "string" &&
|
|
63
|
+
typeof arg.SecretAccessKey === "string" &&
|
|
64
|
+
typeof arg.Token === "string" &&
|
|
65
|
+
typeof arg.Expiration === "string";
|
|
66
|
+
const fromImdsCredentials = (creds) => ({
|
|
67
|
+
accessKeyId: creds.AccessKeyId,
|
|
68
|
+
secretAccessKey: creds.SecretAccessKey,
|
|
69
|
+
sessionToken: creds.Token,
|
|
70
|
+
expiration: new Date(creds.Expiration),
|
|
71
|
+
...(creds.AccountId && { accountId: creds.AccountId }),
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
const DEFAULT_TIMEOUT = 1000;
|
|
75
|
+
const DEFAULT_MAX_RETRIES = 0;
|
|
76
|
+
const providerConfigFromInit = ({ maxRetries = DEFAULT_MAX_RETRIES, timeout = DEFAULT_TIMEOUT, }) => ({ maxRetries, timeout });
|
|
77
|
+
|
|
78
|
+
const retry = (toRetry, maxRetries) => {
|
|
79
|
+
let promise = toRetry();
|
|
80
|
+
for (let i = 0; i < maxRetries; i++) {
|
|
81
|
+
promise = promise.catch(toRetry);
|
|
82
|
+
}
|
|
83
|
+
return promise;
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
const ENV_CMDS_FULL_URI = "AWS_CONTAINER_CREDENTIALS_FULL_URI";
|
|
87
|
+
const ENV_CMDS_RELATIVE_URI = "AWS_CONTAINER_CREDENTIALS_RELATIVE_URI";
|
|
88
|
+
const ENV_CMDS_AUTH_TOKEN = "AWS_CONTAINER_AUTHORIZATION_TOKEN";
|
|
89
|
+
const fromContainerMetadata = (init = {}) => {
|
|
90
|
+
const { timeout, maxRetries } = providerConfigFromInit(init);
|
|
91
|
+
return () => retry(async () => {
|
|
92
|
+
const requestOptions = await getCmdsUri({ logger: init.logger });
|
|
93
|
+
const credsResponse = JSON.parse(await requestFromEcsImds(timeout, requestOptions));
|
|
94
|
+
if (!isImdsCredentials(credsResponse)) {
|
|
95
|
+
throw new CredentialsProviderError("Invalid response received from instance metadata service.", {
|
|
96
|
+
logger: init.logger,
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
return fromImdsCredentials(credsResponse);
|
|
100
|
+
}, maxRetries);
|
|
101
|
+
};
|
|
102
|
+
const requestFromEcsImds = async (timeout, options) => {
|
|
103
|
+
if (process.env[ENV_CMDS_AUTH_TOKEN]) {
|
|
104
|
+
options.headers = {
|
|
105
|
+
...options.headers,
|
|
106
|
+
Authorization: process.env[ENV_CMDS_AUTH_TOKEN],
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
const buffer = await httpRequest({
|
|
110
|
+
...options,
|
|
111
|
+
timeout,
|
|
112
|
+
});
|
|
113
|
+
return buffer.toString();
|
|
114
|
+
};
|
|
115
|
+
const CMDS_IP = "169.254.170.2";
|
|
116
|
+
const GREENGRASS_HOSTS = {
|
|
117
|
+
localhost: true,
|
|
118
|
+
"127.0.0.1": true,
|
|
119
|
+
};
|
|
120
|
+
const GREENGRASS_PROTOCOLS = {
|
|
121
|
+
"http:": true,
|
|
122
|
+
"https:": true,
|
|
123
|
+
};
|
|
124
|
+
const getCmdsUri = async ({ logger }) => {
|
|
125
|
+
if (process.env[ENV_CMDS_RELATIVE_URI]) {
|
|
126
|
+
return {
|
|
127
|
+
hostname: CMDS_IP,
|
|
128
|
+
path: process.env[ENV_CMDS_RELATIVE_URI],
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
if (process.env[ENV_CMDS_FULL_URI]) {
|
|
132
|
+
const parsed = parse(process.env[ENV_CMDS_FULL_URI]);
|
|
133
|
+
if (!parsed.hostname || !(parsed.hostname in GREENGRASS_HOSTS)) {
|
|
134
|
+
throw new CredentialsProviderError(`${parsed.hostname} is not a valid container metadata service hostname`, {
|
|
135
|
+
tryNextLink: false,
|
|
136
|
+
logger,
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
if (!parsed.protocol || !(parsed.protocol in GREENGRASS_PROTOCOLS)) {
|
|
140
|
+
throw new CredentialsProviderError(`${parsed.protocol} is not a valid container metadata service protocol`, {
|
|
141
|
+
tryNextLink: false,
|
|
142
|
+
logger,
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
return {
|
|
146
|
+
...parsed,
|
|
147
|
+
port: parsed.port ? parseInt(parsed.port, 10) : undefined,
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
throw new CredentialsProviderError("The container metadata credential provider cannot be used unless" +
|
|
151
|
+
` the ${ENV_CMDS_RELATIVE_URI} or ${ENV_CMDS_FULL_URI} environment` +
|
|
152
|
+
" variable is set", {
|
|
153
|
+
tryNextLink: false,
|
|
154
|
+
logger,
|
|
155
|
+
});
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
class InstanceMetadataV1FallbackError extends CredentialsProviderError {
|
|
159
|
+
tryNextLink;
|
|
160
|
+
name = "InstanceMetadataV1FallbackError";
|
|
161
|
+
constructor(message, tryNextLink = true) {
|
|
162
|
+
super(message, tryNextLink);
|
|
163
|
+
this.tryNextLink = tryNextLink;
|
|
164
|
+
Object.setPrototypeOf(this, InstanceMetadataV1FallbackError.prototype);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
var Endpoint;
|
|
169
|
+
(function (Endpoint) {
|
|
170
|
+
Endpoint["IPv4"] = "http://169.254.169.254";
|
|
171
|
+
Endpoint["IPv6"] = "http://[fd00:ec2::254]";
|
|
172
|
+
})(Endpoint || (Endpoint = {}));
|
|
173
|
+
|
|
174
|
+
const ENV_ENDPOINT_NAME = "AWS_EC2_METADATA_SERVICE_ENDPOINT";
|
|
175
|
+
const CONFIG_ENDPOINT_NAME = "ec2_metadata_service_endpoint";
|
|
176
|
+
const ENDPOINT_CONFIG_OPTIONS = {
|
|
177
|
+
environmentVariableSelector: (env) => env[ENV_ENDPOINT_NAME],
|
|
178
|
+
configFileSelector: (profile) => profile[CONFIG_ENDPOINT_NAME],
|
|
179
|
+
default: undefined,
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
var EndpointMode;
|
|
183
|
+
(function (EndpointMode) {
|
|
184
|
+
EndpointMode["IPv4"] = "IPv4";
|
|
185
|
+
EndpointMode["IPv6"] = "IPv6";
|
|
186
|
+
})(EndpointMode || (EndpointMode = {}));
|
|
187
|
+
|
|
188
|
+
const ENV_ENDPOINT_MODE_NAME = "AWS_EC2_METADATA_SERVICE_ENDPOINT_MODE";
|
|
189
|
+
const CONFIG_ENDPOINT_MODE_NAME = "ec2_metadata_service_endpoint_mode";
|
|
190
|
+
const ENDPOINT_MODE_CONFIG_OPTIONS = {
|
|
191
|
+
environmentVariableSelector: (env) => env[ENV_ENDPOINT_MODE_NAME],
|
|
192
|
+
configFileSelector: (profile) => profile[CONFIG_ENDPOINT_MODE_NAME],
|
|
193
|
+
default: EndpointMode.IPv4,
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
const getInstanceMetadataEndpoint = async () => parseUrl((await getFromEndpointConfig()) || (await getFromEndpointModeConfig()));
|
|
197
|
+
const getFromEndpointConfig = async () => loadConfig(ENDPOINT_CONFIG_OPTIONS)();
|
|
198
|
+
const getFromEndpointModeConfig = async () => {
|
|
199
|
+
const endpointMode = await loadConfig(ENDPOINT_MODE_CONFIG_OPTIONS)();
|
|
200
|
+
switch (endpointMode) {
|
|
201
|
+
case EndpointMode.IPv4:
|
|
202
|
+
return Endpoint.IPv4;
|
|
203
|
+
case EndpointMode.IPv6:
|
|
204
|
+
return Endpoint.IPv6;
|
|
205
|
+
default:
|
|
206
|
+
throw new Error(`Unsupported endpoint mode: ${endpointMode}.` + ` Select from ${Object.values(EndpointMode)}`);
|
|
207
|
+
}
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
const STATIC_STABILITY_REFRESH_INTERVAL_SECONDS = 5 * 60;
|
|
211
|
+
const STATIC_STABILITY_REFRESH_INTERVAL_JITTER_WINDOW_SECONDS = 5 * 60;
|
|
212
|
+
const STATIC_STABILITY_DOC_URL = "https://docs.aws.amazon.com/sdkref/latest/guide/feature-static-credentials.html";
|
|
213
|
+
const getExtendedInstanceMetadataCredentials = (credentials, logger) => {
|
|
214
|
+
const refreshInterval = STATIC_STABILITY_REFRESH_INTERVAL_SECONDS +
|
|
215
|
+
Math.floor(Math.random() * STATIC_STABILITY_REFRESH_INTERVAL_JITTER_WINDOW_SECONDS);
|
|
216
|
+
const newExpiration = new Date(Date.now() + refreshInterval * 1000);
|
|
217
|
+
logger.warn("Attempting credential expiration extension due to a credential service availability issue. A refresh of these " +
|
|
218
|
+
`credentials will be attempted after ${new Date(newExpiration)}.\nFor more information, please visit: ` +
|
|
219
|
+
STATIC_STABILITY_DOC_URL);
|
|
220
|
+
const originalExpiration = credentials.originalExpiration ?? credentials.expiration;
|
|
221
|
+
return {
|
|
222
|
+
...credentials,
|
|
223
|
+
...(originalExpiration ? { originalExpiration } : {}),
|
|
224
|
+
expiration: newExpiration,
|
|
225
|
+
};
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
const staticStabilityProvider = (provider, options = {}) => {
|
|
229
|
+
const logger = options?.logger || console;
|
|
230
|
+
let pastCredentials;
|
|
231
|
+
return async () => {
|
|
232
|
+
let credentials;
|
|
233
|
+
try {
|
|
234
|
+
credentials = await provider();
|
|
235
|
+
if (credentials.expiration && credentials.expiration.getTime() < Date.now()) {
|
|
236
|
+
credentials = getExtendedInstanceMetadataCredentials(credentials, logger);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
catch (e) {
|
|
240
|
+
if (pastCredentials) {
|
|
241
|
+
logger.warn("Credential renew failed: ", e);
|
|
242
|
+
credentials = getExtendedInstanceMetadataCredentials(pastCredentials, logger);
|
|
243
|
+
}
|
|
244
|
+
else {
|
|
245
|
+
throw e;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
pastCredentials = credentials;
|
|
249
|
+
return credentials;
|
|
250
|
+
};
|
|
251
|
+
};
|
|
252
|
+
|
|
253
|
+
const IMDS_PATH = "/latest/meta-data/iam/security-credentials/";
|
|
254
|
+
const IMDS_TOKEN_PATH = "/latest/api/token";
|
|
255
|
+
const AWS_EC2_METADATA_V1_DISABLED = "AWS_EC2_METADATA_V1_DISABLED";
|
|
256
|
+
const PROFILE_AWS_EC2_METADATA_V1_DISABLED = "ec2_metadata_v1_disabled";
|
|
257
|
+
const X_AWS_EC2_METADATA_TOKEN = "x-aws-ec2-metadata-token";
|
|
258
|
+
const fromInstanceMetadata = (init = {}) => staticStabilityProvider(getInstanceMetadataProvider(init), { logger: init.logger });
|
|
259
|
+
const getInstanceMetadataProvider = (init = {}) => {
|
|
260
|
+
let disableFetchToken = false;
|
|
261
|
+
const { logger, profile } = init;
|
|
262
|
+
const { timeout, maxRetries } = providerConfigFromInit(init);
|
|
263
|
+
const getCredentials = async (maxRetries, options) => {
|
|
264
|
+
const isImdsV1Fallback = disableFetchToken || options.headers?.[X_AWS_EC2_METADATA_TOKEN] == null;
|
|
265
|
+
if (isImdsV1Fallback) {
|
|
266
|
+
let fallbackBlockedFromProfile = false;
|
|
267
|
+
let fallbackBlockedFromProcessEnv = false;
|
|
268
|
+
const configValue = await loadConfig({
|
|
269
|
+
environmentVariableSelector: (env) => {
|
|
270
|
+
const envValue = env[AWS_EC2_METADATA_V1_DISABLED];
|
|
271
|
+
fallbackBlockedFromProcessEnv = !!envValue && envValue !== "false";
|
|
272
|
+
if (envValue === undefined) {
|
|
273
|
+
throw new CredentialsProviderError(`${AWS_EC2_METADATA_V1_DISABLED} not set in env, checking config file next.`, { logger: init.logger });
|
|
274
|
+
}
|
|
275
|
+
return fallbackBlockedFromProcessEnv;
|
|
276
|
+
},
|
|
277
|
+
configFileSelector: (profile) => {
|
|
278
|
+
const profileValue = profile[PROFILE_AWS_EC2_METADATA_V1_DISABLED];
|
|
279
|
+
fallbackBlockedFromProfile = !!profileValue && profileValue !== "false";
|
|
280
|
+
return fallbackBlockedFromProfile;
|
|
281
|
+
},
|
|
282
|
+
default: false,
|
|
283
|
+
}, {
|
|
284
|
+
profile,
|
|
285
|
+
})();
|
|
286
|
+
if (init.ec2MetadataV1Disabled || configValue) {
|
|
287
|
+
const causes = [];
|
|
288
|
+
if (init.ec2MetadataV1Disabled)
|
|
289
|
+
causes.push("credential provider initialization (runtime option ec2MetadataV1Disabled)");
|
|
290
|
+
if (fallbackBlockedFromProfile)
|
|
291
|
+
causes.push(`config file profile (${PROFILE_AWS_EC2_METADATA_V1_DISABLED})`);
|
|
292
|
+
if (fallbackBlockedFromProcessEnv)
|
|
293
|
+
causes.push(`process environment variable (${AWS_EC2_METADATA_V1_DISABLED})`);
|
|
294
|
+
throw new InstanceMetadataV1FallbackError(`AWS EC2 Metadata v1 fallback has been blocked by AWS SDK configuration in the following: [${causes.join(", ")}].`);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
const imdsProfile = (await retry(async () => {
|
|
298
|
+
let profile;
|
|
299
|
+
try {
|
|
300
|
+
profile = await getProfile(options);
|
|
301
|
+
}
|
|
302
|
+
catch (err) {
|
|
303
|
+
if (err.statusCode === 401) {
|
|
304
|
+
disableFetchToken = false;
|
|
305
|
+
}
|
|
306
|
+
throw err;
|
|
307
|
+
}
|
|
308
|
+
return profile;
|
|
309
|
+
}, maxRetries)).trim();
|
|
310
|
+
return retry(async () => {
|
|
311
|
+
let creds;
|
|
312
|
+
try {
|
|
313
|
+
creds = await getCredentialsFromProfile(imdsProfile, options, init);
|
|
314
|
+
}
|
|
315
|
+
catch (err) {
|
|
316
|
+
if (err.statusCode === 401) {
|
|
317
|
+
disableFetchToken = false;
|
|
318
|
+
}
|
|
319
|
+
throw err;
|
|
320
|
+
}
|
|
321
|
+
return creds;
|
|
322
|
+
}, maxRetries);
|
|
323
|
+
};
|
|
324
|
+
return async () => {
|
|
325
|
+
const endpoint = await getInstanceMetadataEndpoint();
|
|
326
|
+
if (disableFetchToken) {
|
|
327
|
+
logger?.debug("AWS SDK Instance Metadata", "using v1 fallback (no token fetch)");
|
|
328
|
+
return getCredentials(maxRetries, { ...endpoint, timeout });
|
|
329
|
+
}
|
|
330
|
+
else {
|
|
331
|
+
let token;
|
|
332
|
+
try {
|
|
333
|
+
token = (await getMetadataToken({ ...endpoint, timeout })).toString();
|
|
334
|
+
}
|
|
335
|
+
catch (error) {
|
|
336
|
+
if (error?.statusCode === 400) {
|
|
337
|
+
throw Object.assign(error, {
|
|
338
|
+
message: "EC2 Metadata token request returned error",
|
|
339
|
+
});
|
|
340
|
+
}
|
|
341
|
+
else if (error.message === "TimeoutError" || [403, 404, 405].includes(error.statusCode)) {
|
|
342
|
+
disableFetchToken = true;
|
|
343
|
+
}
|
|
344
|
+
logger?.debug("AWS SDK Instance Metadata", "using v1 fallback (initial)");
|
|
345
|
+
return getCredentials(maxRetries, { ...endpoint, timeout });
|
|
346
|
+
}
|
|
347
|
+
return getCredentials(maxRetries, {
|
|
348
|
+
...endpoint,
|
|
349
|
+
headers: {
|
|
350
|
+
[X_AWS_EC2_METADATA_TOKEN]: token,
|
|
351
|
+
},
|
|
352
|
+
timeout,
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
};
|
|
356
|
+
};
|
|
357
|
+
const getMetadataToken = async (options) => httpRequest({
|
|
358
|
+
...options,
|
|
359
|
+
path: IMDS_TOKEN_PATH,
|
|
360
|
+
method: "PUT",
|
|
361
|
+
headers: {
|
|
362
|
+
"x-aws-ec2-metadata-token-ttl-seconds": "21600",
|
|
363
|
+
},
|
|
364
|
+
});
|
|
365
|
+
const getProfile = async (options) => (await httpRequest({ ...options, path: IMDS_PATH })).toString();
|
|
366
|
+
const getCredentialsFromProfile = async (profile, options, init) => {
|
|
367
|
+
const credentialsResponse = JSON.parse((await httpRequest({
|
|
368
|
+
...options,
|
|
369
|
+
path: IMDS_PATH + profile,
|
|
370
|
+
})).toString());
|
|
371
|
+
if (!isImdsCredentials(credentialsResponse)) {
|
|
372
|
+
throw new CredentialsProviderError("Invalid response received from instance metadata service.", {
|
|
373
|
+
logger: init.logger,
|
|
374
|
+
});
|
|
375
|
+
}
|
|
376
|
+
return fromImdsCredentials(credentialsResponse);
|
|
377
|
+
};
|
|
378
|
+
|
|
379
|
+
export { DEFAULT_MAX_RETRIES, DEFAULT_TIMEOUT, ENV_CMDS_AUTH_TOKEN, ENV_CMDS_FULL_URI, ENV_CMDS_RELATIVE_URI, Endpoint, fromContainerMetadata, fromInstanceMetadata, getInstanceMetadataEndpoint, httpRequest, providerConfigFromInit };
|