Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix header conversion #1300

Merged
merged 3 commits into from
Mar 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 32 additions & 5 deletions src/interceptor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Request as AWS4Request, sign } from "aws4";
import axios, {
AxiosHeaderValue,
AxiosHeaders,
AxiosInstance,
AxiosRequestConfig,
Expand All @@ -12,6 +13,7 @@
import { AssumeRoleCredentialsProvider } from "./credentials/assumeRoleCredentialsProvider";
import { isCredentialsProvider } from "./credentials/isCredentialsProvider";
import { SimpleCredentialsProvider } from "./credentials/simpleCredentialsProvider";
import { OutgoingHttpHeaders } from "http";

export interface InterceptorOptions {
/**
Expand Down Expand Up @@ -43,7 +45,7 @@
* Use the role session name to uniquely identify a session when the same role is
* assumed by different principals or for different reasons.
* In cross-account scenarios, the role session name is visible to,
* and can be logged by the account that owns the role.
* and can be logged by the account that owns the role.
*/
assumeRoleSessionName?: string;
}
Expand All @@ -70,8 +72,8 @@
Record<string, string>
>;

const removeUndefined = (obj: Record<string, any>) => {

Check warning on line 75 in src/interceptor.ts

View workflow job for this annotation

GitHub Actions / build (18.x, 1.4.0)

Unexpected any. Specify a different type

Check warning on line 75 in src/interceptor.ts

View workflow job for this annotation

GitHub Actions / build (18.x, 1.5.0)

Unexpected any. Specify a different type

Check warning on line 75 in src/interceptor.ts

View workflow job for this annotation

GitHub Actions / build (18.x, 1.6.0)

Unexpected any. Specify a different type

Check warning on line 75 in src/interceptor.ts

View workflow job for this annotation

GitHub Actions / build (18.x, latest)

Unexpected any. Specify a different type

Check warning on line 75 in src/interceptor.ts

View workflow job for this annotation

GitHub Actions / build (20.x, 1.4.0)

Unexpected any. Specify a different type

Check warning on line 75 in src/interceptor.ts

View workflow job for this annotation

GitHub Actions / build (20.x, 1.5.0)

Unexpected any. Specify a different type

Check warning on line 75 in src/interceptor.ts

View workflow job for this annotation

GitHub Actions / build (20.x, 1.6.0)

Unexpected any. Specify a different type

Check warning on line 75 in src/interceptor.ts

View workflow job for this annotation

GitHub Actions / build (20.x, latest)

Unexpected any. Specify a different type
const newObj: Record<string, any> = {};

Check warning on line 76 in src/interceptor.ts

View workflow job for this annotation

GitHub Actions / build (18.x, 1.4.0)

Unexpected any. Specify a different type

Check warning on line 76 in src/interceptor.ts

View workflow job for this annotation

GitHub Actions / build (18.x, 1.5.0)

Unexpected any. Specify a different type

Check warning on line 76 in src/interceptor.ts

View workflow job for this annotation

GitHub Actions / build (18.x, 1.6.0)

Unexpected any. Specify a different type

Check warning on line 76 in src/interceptor.ts

View workflow job for this annotation

GitHub Actions / build (18.x, latest)

Unexpected any. Specify a different type

Check warning on line 76 in src/interceptor.ts

View workflow job for this annotation

GitHub Actions / build (20.x, 1.4.0)

Unexpected any. Specify a different type

Check warning on line 76 in src/interceptor.ts

View workflow job for this annotation

GitHub Actions / build (20.x, 1.5.0)

Unexpected any. Specify a different type

Check warning on line 76 in src/interceptor.ts

View workflow job for this annotation

GitHub Actions / build (20.x, 1.6.0)

Unexpected any. Specify a different type

Check warning on line 76 in src/interceptor.ts

View workflow job for this annotation

GitHub Actions / build (20.x, latest)

Unexpected any. Specify a different type

for (const [key, value] of Object.entries(obj)) {
if (value !== undefined) {
Expand All @@ -94,7 +96,7 @@
* @param options The options to be used when signing a request
* @param credentials Credentials to be used to sign the request
*/
export const aws4Interceptor = <D = any>({

Check warning on line 99 in src/interceptor.ts

View workflow job for this annotation

GitHub Actions / build (18.x, 1.4.0)

Unexpected any. Specify a different type

Check warning on line 99 in src/interceptor.ts

View workflow job for this annotation

GitHub Actions / build (18.x, 1.5.0)

Unexpected any. Specify a different type

Check warning on line 99 in src/interceptor.ts

View workflow job for this annotation

GitHub Actions / build (18.x, 1.6.0)

Unexpected any. Specify a different type

Check warning on line 99 in src/interceptor.ts

View workflow job for this annotation

GitHub Actions / build (18.x, latest)

Unexpected any. Specify a different type

Check warning on line 99 in src/interceptor.ts

View workflow job for this annotation

GitHub Actions / build (20.x, 1.4.0)

Unexpected any. Specify a different type

Check warning on line 99 in src/interceptor.ts

View workflow job for this annotation

GitHub Actions / build (20.x, 1.5.0)

Unexpected any. Specify a different type

Check warning on line 99 in src/interceptor.ts

View workflow job for this annotation

GitHub Actions / build (20.x, 1.6.0)

Unexpected any. Specify a different type

Check warning on line 99 in src/interceptor.ts

View workflow job for this annotation

GitHub Actions / build (20.x, latest)

Unexpected any. Specify a different type
instance = axios,
credentials,
options,
Expand Down Expand Up @@ -131,7 +133,7 @@

const { host, pathname, search } = new URL(url);
const { data, method } = config;
const headers = new AxiosHeaders(config.headers);
const headers = config.headers;

const transformRequest = getTransformer(config);

Expand All @@ -150,7 +152,7 @@
put,
patch,
...headersToSign
} = headers as any as InternalAxiosHeaders;
} = headers as unknown as InternalAxiosHeaders;
// Axios type definitions do not match the real shape of this object

const signingOptions: AWS4Request = {
Expand All @@ -161,13 +163,19 @@
service: options?.service,
signQuery: options?.signQuery,
body: transformedData,
headers: removeUndefined(headersToSign) as any,
headers: removeUndefined(headersToSign) as unknown as OutgoingHttpHeaders,
};

const resolvedCredentials = await credentialsProvider.getCredentials();
sign(signingOptions, resolvedCredentials);

config.headers = signingOptions.headers as AxiosHeaders;
const signedHeaders = signingOptions.headers;

if (signedHeaders) {
config.headers = AxiosHeaders.from(
outgoingHttpHeadersToAxiosHeaders(signedHeaders)
);
}

if (signingOptions.signQuery) {
const originalUrl = new URL(url);
Expand All @@ -182,6 +190,25 @@
};
};

// Type is not exported by axios
interface _RawAxiosHeaders {
[key: string]: AxiosHeaderValue;
}

const outgoingHttpHeadersToAxiosHeaders = (
headers: OutgoingHttpHeaders
): _RawAxiosHeaders => {
const axiosHeaders: _RawAxiosHeaders = {};

for (const [key, value] of Object.entries(headers)) {
if (value) {
axiosHeaders[key] = value;
}
}

return axiosHeaders;
};

const getTransformer = (config: AxiosRequestConfig) => {
const { transformRequest } = config;

Expand Down