@aws-sdk/ec2-metadata-service 3.901.0 → 3.906.0
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-cjs/index.js +124 -148
- package/package.json +2 -2
package/dist-cjs/index.js
CHANGED
|
@@ -1,159 +1,135 @@
|
|
|
1
|
-
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
7
|
-
var __export = (target, all) => {
|
|
8
|
-
for (var name in all)
|
|
9
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
-
};
|
|
11
|
-
var __copyProps = (to, from, except, desc) => {
|
|
12
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
-
for (let key of __getOwnPropNames(from))
|
|
14
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
-
}
|
|
17
|
-
return to;
|
|
18
|
-
};
|
|
19
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
1
|
+
'use strict';
|
|
20
2
|
|
|
21
|
-
|
|
22
|
-
var
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
});
|
|
26
|
-
module.exports = __toCommonJS(index_exports);
|
|
3
|
+
var nodeConfigProvider = require('@smithy/node-config-provider');
|
|
4
|
+
var nodeHttpHandler = require('@smithy/node-http-handler');
|
|
5
|
+
var protocolHttp = require('@smithy/protocol-http');
|
|
6
|
+
var utilStream = require('@smithy/util-stream');
|
|
27
7
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
8
|
+
var Endpoint;
|
|
9
|
+
(function (Endpoint) {
|
|
10
|
+
Endpoint["IPv4"] = "http://169.254.169.254";
|
|
11
|
+
Endpoint["IPv6"] = "http://[fd00:ec2::254]";
|
|
12
|
+
})(Endpoint || (Endpoint = {}));
|
|
33
13
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
configFileSelector: /* @__PURE__ */ __name((profile) => profile[CONFIG_ENDPOINT_MODE_NAME], "configFileSelector"),
|
|
47
|
-
default: "IPv4" /* IPv4 */
|
|
14
|
+
var EndpointMode;
|
|
15
|
+
(function (EndpointMode) {
|
|
16
|
+
EndpointMode["IPv4"] = "IPv4";
|
|
17
|
+
EndpointMode["IPv6"] = "IPv6";
|
|
18
|
+
})(EndpointMode || (EndpointMode = {}));
|
|
19
|
+
|
|
20
|
+
const ENV_ENDPOINT_NAME = "AWS_EC2_METADATA_SERVICE_ENDPOINT";
|
|
21
|
+
const CONFIG_ENDPOINT_NAME = "ec2_metadata_service_endpoint";
|
|
22
|
+
const ENDPOINT_SELECTORS = {
|
|
23
|
+
environmentVariableSelector: (env) => env[ENV_ENDPOINT_NAME],
|
|
24
|
+
configFileSelector: (profile) => profile[CONFIG_ENDPOINT_NAME],
|
|
25
|
+
default: Endpoint.IPv4,
|
|
48
26
|
};
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
27
|
+
({
|
|
28
|
+
default: EndpointMode.IPv4,
|
|
29
|
+
});
|
|
30
|
+
const AWS_EC2_METADATA_V1_DISABLED = "AWS_EC2_METADATA_V1_DISABLED";
|
|
31
|
+
const PROFILE_AWS_EC2_METADATA_V1_DISABLED = "ec2_metadata_v1_disabled";
|
|
32
|
+
const IMDSv1_DISABLED_SELECTORS = {
|
|
33
|
+
environmentVariableSelector: (env) => env[AWS_EC2_METADATA_V1_DISABLED] ? env[AWS_EC2_METADATA_V1_DISABLED] !== "false" : undefined,
|
|
34
|
+
configFileSelector: (profile) => profile[PROFILE_AWS_EC2_METADATA_V1_DISABLED]
|
|
35
|
+
? profile[PROFILE_AWS_EC2_METADATA_V1_DISABLED] !== "false"
|
|
36
|
+
: undefined,
|
|
37
|
+
default: false,
|
|
55
38
|
};
|
|
56
39
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
httpOptions: {
|
|
73
|
-
timeout: options?.httpOptions?.timeout || 0
|
|
74
|
-
},
|
|
75
|
-
ec2MetadataV1Disabled: options?.ec2MetadataV1Disabled ?? await (0, import_node_config_provider.loadConfig)(IMDSv1_DISABLED_SELECTORS, { profile })()
|
|
76
|
-
};
|
|
77
|
-
})();
|
|
78
|
-
this.disableFetchToken = options?.disableFetchToken || false;
|
|
79
|
-
}
|
|
80
|
-
async request(path, options) {
|
|
81
|
-
const { endpoint, ec2MetadataV1Disabled, httpOptions } = await this.config;
|
|
82
|
-
const handler = new import_node_http_handler.NodeHttpHandler({
|
|
83
|
-
requestTimeout: httpOptions?.timeout,
|
|
84
|
-
connectionTimeout: httpOptions?.timeout
|
|
85
|
-
});
|
|
86
|
-
const endpointUrl = new URL(endpoint);
|
|
87
|
-
const headers = options.headers || {};
|
|
88
|
-
if (this.disableFetchToken && ec2MetadataV1Disabled) {
|
|
89
|
-
throw new Error("IMDSv1 is disabled and fetching token is disabled, cannot make the request.");
|
|
40
|
+
class MetadataService {
|
|
41
|
+
disableFetchToken;
|
|
42
|
+
config;
|
|
43
|
+
constructor(options = {}) {
|
|
44
|
+
this.config = (async () => {
|
|
45
|
+
const profile = options?.profile || process.env.AWS_PROFILE;
|
|
46
|
+
return {
|
|
47
|
+
endpoint: options.endpoint ?? (await nodeConfigProvider.loadConfig(ENDPOINT_SELECTORS, { profile })()),
|
|
48
|
+
httpOptions: {
|
|
49
|
+
timeout: options?.httpOptions?.timeout || 0,
|
|
50
|
+
},
|
|
51
|
+
ec2MetadataV1Disabled: options?.ec2MetadataV1Disabled ?? (await nodeConfigProvider.loadConfig(IMDSv1_DISABLED_SELECTORS, { profile })()),
|
|
52
|
+
};
|
|
53
|
+
})();
|
|
54
|
+
this.disableFetchToken = options?.disableFetchToken || false;
|
|
90
55
|
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
56
|
+
async request(path, options) {
|
|
57
|
+
const { endpoint, ec2MetadataV1Disabled, httpOptions } = await this.config;
|
|
58
|
+
const handler = new nodeHttpHandler.NodeHttpHandler({
|
|
59
|
+
requestTimeout: httpOptions?.timeout,
|
|
60
|
+
connectionTimeout: httpOptions?.timeout,
|
|
61
|
+
});
|
|
62
|
+
const endpointUrl = new URL(endpoint);
|
|
63
|
+
const headers = options.headers || {};
|
|
64
|
+
if (this.disableFetchToken && ec2MetadataV1Disabled) {
|
|
65
|
+
throw new Error("IMDSv1 is disabled and fetching token is disabled, cannot make the request.");
|
|
66
|
+
}
|
|
67
|
+
if (!this.disableFetchToken) {
|
|
68
|
+
try {
|
|
69
|
+
headers["x-aws-ec2-metadata-token"] = await this.fetchMetadataToken();
|
|
70
|
+
}
|
|
71
|
+
catch (err) {
|
|
72
|
+
if (ec2MetadataV1Disabled) {
|
|
73
|
+
throw err;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
const request = new protocolHttp.HttpRequest({
|
|
78
|
+
method: options.method || "GET",
|
|
79
|
+
headers: headers,
|
|
80
|
+
hostname: endpointUrl.hostname,
|
|
81
|
+
path: endpointUrl.pathname + path,
|
|
82
|
+
protocol: endpointUrl.protocol,
|
|
83
|
+
});
|
|
84
|
+
try {
|
|
85
|
+
const { response } = await handler.handle(request, {});
|
|
86
|
+
if (response.statusCode === 200 && response.body) {
|
|
87
|
+
return utilStream.sdkStreamMixin(response.body).transformToString();
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
throw new Error(`Request failed with status code ${response.statusCode}`);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
catch (error) {
|
|
94
|
+
throw new Error(`Error making request to the metadata service: ${error}`);
|
|
97
95
|
}
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
const request = new import_protocol_http.HttpRequest({
|
|
101
|
-
method: options.method || "GET",
|
|
102
|
-
// Default to GET if no method is specified
|
|
103
|
-
headers,
|
|
104
|
-
hostname: endpointUrl.hostname,
|
|
105
|
-
path: endpointUrl.pathname + path,
|
|
106
|
-
protocol: endpointUrl.protocol
|
|
107
|
-
});
|
|
108
|
-
try {
|
|
109
|
-
const { response } = await handler.handle(request, {});
|
|
110
|
-
if (response.statusCode === 200 && response.body) {
|
|
111
|
-
return (0, import_util_stream.sdkStreamMixin)(response.body).transformToString();
|
|
112
|
-
} else {
|
|
113
|
-
throw new Error(`Request failed with status code ${response.statusCode}`);
|
|
114
|
-
}
|
|
115
|
-
} catch (error) {
|
|
116
|
-
throw new Error(`Error making request to the metadata service: ${error}`);
|
|
117
96
|
}
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
97
|
+
async fetchMetadataToken() {
|
|
98
|
+
const { endpoint, httpOptions } = await this.config;
|
|
99
|
+
const handler = new nodeHttpHandler.NodeHttpHandler({
|
|
100
|
+
requestTimeout: httpOptions?.timeout,
|
|
101
|
+
connectionTimeout: httpOptions?.timeout,
|
|
102
|
+
});
|
|
103
|
+
const endpointUrl = new URL(endpoint);
|
|
104
|
+
const tokenRequest = new protocolHttp.HttpRequest({
|
|
105
|
+
method: "PUT",
|
|
106
|
+
headers: {
|
|
107
|
+
"x-aws-ec2-metadata-token-ttl-seconds": "21600",
|
|
108
|
+
},
|
|
109
|
+
hostname: endpointUrl.hostname,
|
|
110
|
+
path: "/latest/api/token",
|
|
111
|
+
protocol: endpointUrl.protocol,
|
|
112
|
+
});
|
|
113
|
+
try {
|
|
114
|
+
const { response } = await handler.handle(tokenRequest, {});
|
|
115
|
+
if (response.statusCode === 200 && response.body) {
|
|
116
|
+
return utilStream.sdkStreamMixin(response.body).transformToString();
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
throw new Error(`Failed to fetch metadata token with status code ${response.statusCode}`);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
catch (error) {
|
|
123
|
+
if (error?.statusCode === 400) {
|
|
124
|
+
throw new Error(`Error fetching metadata token: ${error}`);
|
|
125
|
+
}
|
|
126
|
+
else if (error.message === "TimeoutError" || [403, 404, 405].includes(error.statusCode)) {
|
|
127
|
+
this.disableFetchToken = true;
|
|
128
|
+
throw new Error(`Error fetching metadata token: ${error}. disableFetchToken is enabled`);
|
|
129
|
+
}
|
|
130
|
+
throw new Error(`Error fetching metadata token: ${error}`);
|
|
131
|
+
}
|
|
151
132
|
}
|
|
152
|
-
|
|
153
|
-
};
|
|
154
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
155
|
-
|
|
156
|
-
0 && (module.exports = {
|
|
157
|
-
MetadataService
|
|
158
|
-
});
|
|
133
|
+
}
|
|
159
134
|
|
|
135
|
+
exports.MetadataService = MetadataService;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aws-sdk/ec2-metadata-service",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.906.0",
|
|
4
4
|
"scripts": {
|
|
5
5
|
"build": "concurrently 'yarn:build:cjs' 'yarn:build:es' 'yarn:build:types'",
|
|
6
6
|
"build:cjs": "node ../../scripts/compilation/inline ec2-metadata-service",
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
"tslib": "^2.6.2"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
|
-
"@aws-sdk/credential-providers": "3.
|
|
36
|
+
"@aws-sdk/credential-providers": "3.906.0",
|
|
37
37
|
"@tsconfig/recommended": "1.0.1",
|
|
38
38
|
"@types/node": "^18.19.69",
|
|
39
39
|
"concurrently": "7.0.0",
|