Skip to content

chore(maintenance): avoid attaching two middlewares to ua #1583

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

Merged
merged 1 commit into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/commons/src/userAgentMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ const customUserAgentMiddleware = (feature: string) => {
// @ts-ignore
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure I like ignoring things in the type system

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Acknowledged and agree.

In this instance we decided to use an any as a type for the client because we didn't want to bring in the @aws-sdk/types package into the utility. This code resides in the commons utility, which is pulled in by all the other Powertools utilities.

Given that there's all the core utilities don't use any AWS SDK, using the @aws-sdk/types in this module would require customers to install it as a developer dependency when they develop or build their code using Powertools.
This is a problem because when building with TypeScript, the type-checking engine will block the compilation is the dependency is missing.

To avoid this, we decided to go for an any type (which is discouraged in our codebase - hence the @ts-ignore) and also put unit tests.

We have considered writing our own minimal Client type, and the option is still in the cards, but have decided to not include it for now given the limited scope of the feature (i.e. getting a client from a 3rd party library and calling a method on it) and the fact that we implemented the function in a way that even if you pass a random object (aka not a client) it won't throw an error but just log a warning.

I am still strongly inclined to review this type as soon as we have bandwidth and remove the ignore.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think my preference would be to write our own types def.

const addUserAgentMiddleware = (client, feature: string): void => {
try {
if (
client.middlewareStack
.identify()
.includes('addPowertoolsToUserAgent: POWERTOOLS,USER_AGENT')
) {
return;
}
client.middlewareStack.addRelativeTo(
customUserAgentMiddleware(feature),
middlewareOptions
Expand Down
121 changes: 87 additions & 34 deletions packages/commons/tests/unit/userAgentMiddleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,26 @@ import { InvokeCommand, LambdaClient } from '@aws-sdk/client-lambda';
import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
import { ScanCommand } from '@aws-sdk/lib-dynamodb';
import { GetParameterCommand, SSMClient } from '@aws-sdk/client-ssm';
import { version as PT_VERSION } from '../../package.json';
import { PT_VERSION } from '../../src/version';
import { AppConfigDataClient } from '@aws-sdk/client-appconfigdata';
import {
GetSecretValueCommand,
SecretsManagerClient,
} from '@aws-sdk/client-secrets-manager';
import { RelativeMiddlewareOptions } from '@aws-sdk/types/dist-types/middleware';

type SupportedSdkClients =
| LambdaClient
| DynamoDBClient
| SSMClient
| SecretsManagerClient
| AppConfigDataClient;

type SupportedSdkCommands =
| InvokeCommand
| ScanCommand
| GetParameterCommand
| GetSecretValueCommand;

const options = {
region: 'us-east-1',
Expand All @@ -20,6 +34,57 @@ const options = {
},
};

const assertMiddleware = (feature: string) => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return (next) => (args) => {
const userAgent = args?.request?.headers['user-agent'];
expect(userAgent).toContain(`PT/${feature}/${PT_VERSION} PTEnv/NA`);
// make sure it's at the end of the user agent
expect(
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
userAgent
?.split(' ')
.slice(userAgent?.split(' ').length - 2) // take the last to entries of the user-agent header
.join(' ')
).toEqual(`PT/${feature}/${PT_VERSION} PTEnv/NA`);

return next(args);
};
};

const assertMiddlewareOptions: RelativeMiddlewareOptions = {
relation: 'after',
toMiddleware: 'addPowertoolsToUserAgent',
name: 'testUserAgentHeader',
tags: ['TEST'],
};

const runCommand = async (
client: SupportedSdkClients,
command: SupportedSdkCommands
): Promise<unknown> => {
try {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return await client.send(command);
} catch (e) {
// throw only jest errors and swallow the SDK client errors like credentials or connection issues
if (
e instanceof Error &&
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
e.matcherResult !== undefined &&
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
e.matcherResult.pass === false
) {
throw e;
}
}
};

describe('Given a client of instance: ', () => {
it.each([
{
Expand Down Expand Up @@ -57,44 +122,15 @@ describe('Given a client of instance: ', () => {
);

client.middlewareStack.addRelativeTo(
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
(next) => (args) => {
const userAgent = args?.request?.headers['user-agent'];
expect(userAgent).toContain(`PT/my-feature/${PT_VERSION} PTEnv/NA`);
// make sure it's at the end of the user agent
expect(
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
userAgent
?.split(' ')
.slice(userAgent?.split(' ').length - 2) // take the last to entries of the user-agent header
.join(' ')
).toEqual(`PT/my-feature/${PT_VERSION} PTEnv/NA`);

return next(args);
},
{
relation: 'after',
toMiddleware: 'addPowertoolsToUserAgent',
name: 'testUserAgentHeader',
tags: ['TEST'],
}
assertMiddleware('my-feature'),
assertMiddlewareOptions
);

try {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
await client.send(command);
} catch (e) {
if (e instanceof Error && e.name === 'JestAssertionError') {
throw e;
}
}
await runCommand(client, command);
}
);

it('should not throw erro, when client fails to add middleware', () => {
it('should not throw error, when client fails to add middleware', () => {
// create mock client that throws error when adding middleware
const client = {
middlewareStack: {
Expand All @@ -106,4 +142,21 @@ describe('Given a client of instance: ', () => {

expect(() => addUserAgentMiddleware(client, 'my-feature')).not.toThrow();
});

it('should no-op if we add the middleware twice', async () => {
const client = new LambdaClient(options);
const command = new InvokeCommand({ FunctionName: 'test', Payload: '' });
addUserAgentMiddleware(client, 'my-feature');
addUserAgentMiddleware(client, 'your-feature');

client.middlewareStack.addRelativeTo(
assertMiddleware('my-feature'),
assertMiddlewareOptions
);
await runCommand(client, command);

expect(client.middlewareStack.identify()).toContain(
'addPowertoolsToUserAgent: POWERTOOLS,USER_AGENT'
);
});
});