Skip to content

Commit

Permalink
chore(ec2-metadata-service): add newlines for readability
Browse files Browse the repository at this point in the history
Co-authored-by: Trivikram Kamat <[email protected]>
  • Loading branch information
siddsriv and trivikr authored Mar 18, 2024
1 parent 46e2b00 commit 5abe2e5
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions packages/ec2-metadata-service/src/MetadataService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@ export class MetadataService {
const handler = new NodeHttpHandler();
const endpointUrl = new URL(endpoint!);
const headers = options.headers || {};

/**
* If IMDSv1 is disabled and disableFetchToken is true, throw an error
* Refer: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/configuring-instance-metadata-service.html
*/
if (this.disableFetchToken && ec2MetadataV1Disabled) {
throw new Error("IMDSv1 is disabled and fetching token is disabled, cannot make the request.");
}

/**
* Make request with token if disableFetchToken is not true (IMDSv2).
* Note that making the request call with token will result in an additional request to fetch the token.
Expand All @@ -59,13 +61,15 @@ export class MetadataService {
// If token fetch fails and IMDSv1 is not disabled, proceed without token (IMDSv1 fallback)
}
} // else, IMDSv1 fallback mode

const request = new HttpRequest({
method: options.method || "GET", // Default to GET if no method is specified
headers: headers,
hostname: endpointUrl.hostname,
path: endpointUrl.pathname + path,
protocol: endpointUrl.protocol,
});

try {
const { response } = await handler.handle(request, {} as HttpHandlerOptions);
if (response.statusCode === 200 && response.body) {
Expand All @@ -86,6 +90,7 @@ export class MetadataService {
const { endpoint } = await this.config;
const handler = new NodeHttpHandler();
const endpointUrl = new URL(endpoint!);

const tokenRequest = new HttpRequest({
method: "PUT",
headers: {
Expand All @@ -95,6 +100,7 @@ export class MetadataService {
path: "/latest/api/token",
protocol: endpointUrl.protocol,
});

try {
const { response } = await handler.handle(tokenRequest, {} as HttpHandlerOptions);
if (response.statusCode === 200 && response.body) {
Expand Down

0 comments on commit 5abe2e5

Please sign in to comment.