Skip to content

chore(parameters): apply UA to AWS SDK clients used by Parameters #1577

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 4 commits 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
3 changes: 3 additions & 0 deletions packages/parameters/src/appconfig/AppConfigProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {
AppConfigGetOptions,
AppConfigGetOutput,
} from '../types/AppConfigProvider';
import { addUserAgentMiddleware } from '@aws-lambda-powertools/commons';

/**
* ## Intro
Expand Down Expand Up @@ -210,6 +211,8 @@ class AppConfigProvider extends BaseProvider {
this.client = new AppConfigDataClient(options.clientConfig || {});
}

addUserAgentMiddleware(this.client, 'parameters');

this.application =
options?.application || this.envVarsService.getServiceName();
if (!this.application || this.application.trim().length === 0) {
Expand Down
3 changes: 3 additions & 0 deletions packages/parameters/src/dynamodb/DynamoDBProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type {
} from '@aws-sdk/client-dynamodb';
import type { PaginationConfiguration } from '@aws-sdk/types';
import type { JSONValue } from '@aws-lambda-powertools/commons';
import { addUserAgentMiddleware } from '@aws-lambda-powertools/commons';

/**
* ## Intro
Expand Down Expand Up @@ -266,6 +267,8 @@ class DynamoDBProvider extends BaseProvider {
this.client = new DynamoDBClient(clientConfig);
}

addUserAgentMiddleware(this.client, 'parameters');

this.tableName = config.tableName;
if (config.keyAttr) this.keyAttr = config.keyAttr;
if (config.sortAttr) this.sortAttr = config.sortAttr;
Expand Down
3 changes: 3 additions & 0 deletions packages/parameters/src/secrets/SecretsProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
SecretsGetOptions,
SecretsGetOutput,
} from '../types/SecretsProvider';
import { addUserAgentMiddleware } from '@aws-lambda-powertools/commons';

/**
* ## Intro
Expand Down Expand Up @@ -171,6 +172,8 @@ class SecretsProvider extends BaseProvider {
const clientConfig = config?.clientConfig || {};
this.client = new SecretsManagerClient(clientConfig);
}

addUserAgentMiddleware(this.client, 'parameters');
}

/**
Expand Down
3 changes: 3 additions & 0 deletions packages/parameters/src/ssm/SSMProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import type {
SSMGetParametersByNameFromCacheOutputType,
} from '../types/SSMProvider';
import type { PaginationConfiguration } from '@aws-sdk/types';
import { addUserAgentMiddleware } from '@aws-lambda-powertools/commons';

/**
* ## Intro
Expand Down Expand Up @@ -290,6 +291,8 @@ class SSMProvider extends BaseProvider {
const clientConfig = config?.clientConfig || {};
this.client = new SSMClient(clientConfig);
}

addUserAgentMiddleware(this.client, 'parameters');
}

/**
Expand Down
25 changes: 24 additions & 1 deletion packages/parameters/tests/unit/AppConfigProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import { ExpirableValue } from '../../src/base/ExpirableValue';
import { AppConfigProviderOptions } from '../../src/types/AppConfigProvider';
import {
AppConfigDataClient,
StartConfigurationSessionCommand,
GetLatestConfigurationCommand,
StartConfigurationSessionCommand,
} from '@aws-sdk/client-appconfigdata';
import { Uint8ArrayBlobAdapter } from '@aws-sdk/util-stream';
import { mockClient } from 'aws-sdk-client-mock';
import * as UserAgentMiddleware from '@aws-lambda-powertools/commons/lib/userAgentMiddleware';
import 'aws-sdk-client-mock-jest';

describe('Class: AppConfigProvider', () => {
Expand All @@ -34,6 +35,11 @@ describe('Class: AppConfigProvider', () => {
environment: 'MyAppProdEnv',
};

const userAgentSpy = jest.spyOn(
UserAgentMiddleware,
'addUserAgentMiddleware'
);

// Act
const provider = new AppConfigProvider(options);

Expand All @@ -43,6 +49,8 @@ describe('Class: AppConfigProvider', () => {
serviceId: 'AppConfigData',
})
);

expect(userAgentSpy).toHaveBeenCalled();
Copy link
Contributor

Choose a reason for hiding this comment

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

The client inside the provider is public and can be accessed via provider.client. This means you should be able to test like you did in the commons module by doing:

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

I think that would be preferable to changing the import (* as ..) and selecting the module in the spy.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think that would be preferable to changing the import (* as ..) and selecting the module in the spy.

Can you elaborate? Curious to hear about the benefits.

I want to avoid to test the internals of the commons module and the dependencies behaviour. Imho, we should only check the call has happened downstream, otherwise we replicate the assertion logic in different places which makes it harder to change in the future. For instance, if we rename the middleware from addPowertoolsUserAgent to something else, we'd need to revisit every utility that called this method.

Copy link
Contributor

Choose a reason for hiding this comment

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

Based on the way that the Jest spy is written, if we rename addPowertoolsUserAgent to something else the test would also break:

const userAgentSpy = jest.spyOn(
  UserAgentMiddleware,
  'addUserAgentMiddleware' // if function name changes, this spy also breaks
);

However I see your point and agree. I tend to avoid setting spies on imports but I can't think of a better way of doing it without reworking the constructor (which si not worth it), so let's leave it this way!

});

test('when the user provides a client config in the options, the class instantiates a new client with client config options', async () => {
Expand All @@ -55,6 +63,11 @@ describe('Class: AppConfigProvider', () => {
},
};

const userAgentSpy = jest.spyOn(
UserAgentMiddleware,
'addUserAgentMiddleware'
);

// Act
const provider = new AppConfigProvider(options);

Expand All @@ -64,6 +77,8 @@ describe('Class: AppConfigProvider', () => {
serviceId: 'with-client-config',
})
);

expect(userAgentSpy).toHaveBeenCalled();
});

test('when the user provides an SDK client in the options, the class instantiates with it', async () => {
Expand All @@ -78,6 +93,11 @@ describe('Class: AppConfigProvider', () => {
awsSdkV3Client: awsSdkV3Client,
};

const userAgentSpy = jest.spyOn(
UserAgentMiddleware,
'addUserAgentMiddleware'
);

// Act
const provider = new AppConfigProvider(options);

Expand All @@ -87,6 +107,8 @@ describe('Class: AppConfigProvider', () => {
serviceId: 'with-custom-sdk-client',
})
);

expect(userAgentSpy).toHaveBeenCalledWith(awsSdkV3Client, 'parameters');
});

test('when the user provides NOT an SDK client in the options, it throws an error', async () => {
Expand Down Expand Up @@ -187,6 +209,7 @@ describe('Class: AppConfigProvider', () => {
public _addToStore(key: string, value: string): void {
this.configurationTokenStore.set(key, value);
}

public _storeHas(key: string): boolean {
return this.configurationTokenStore.has(key);
}
Expand Down
21 changes: 21 additions & 0 deletions packages/parameters/tests/unit/DynamoDBProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import type {
} from '@aws-sdk/client-dynamodb';
import type { DynamoDBProviderOptions } from '../../src/types/DynamoDBProvider';
import { marshall } from '@aws-sdk/util-dynamodb';
import * as UserAgentMiddleware from '@aws-lambda-powertools/commons/lib/userAgentMiddleware';
import { mockClient } from 'aws-sdk-client-mock';
import 'aws-sdk-client-mock-jest';

Expand All @@ -30,6 +31,11 @@ describe('Class: DynamoDBProvider', () => {
tableName: 'test-table',
};

const userAgentSpy = jest.spyOn(
UserAgentMiddleware,
'addUserAgentMiddleware'
);

// Act
const provider = new DynamoDBProvider(options);

Expand All @@ -39,6 +45,8 @@ describe('Class: DynamoDBProvider', () => {
serviceId: 'DynamoDB',
})
);

expect(userAgentSpy).toHaveBeenCalled();
});

test('when the user provides a client config in the options, the class instantiates a new client with client config options', async () => {
Expand All @@ -50,6 +58,11 @@ describe('Class: DynamoDBProvider', () => {
},
};

const userAgentSpy = jest.spyOn(
UserAgentMiddleware,
'addUserAgentMiddleware'
);

// Act
const provider = new DynamoDBProvider(options);

Expand All @@ -59,6 +72,8 @@ describe('Class: DynamoDBProvider', () => {
serviceId: 'with-client-config',
})
);

expect(userAgentSpy).toHaveBeenCalled();
});

test('when the user provides an SDK client in the options, the class instantiates with it', async () => {
Expand All @@ -67,6 +82,11 @@ describe('Class: DynamoDBProvider', () => {
serviceId: 'with-custom-sdk-client',
});

const userAgentSpy = jest.spyOn(
UserAgentMiddleware,
'addUserAgentMiddleware'
);

const options: DynamoDBProviderOptions = {
tableName: 'test-table',
awsSdkV3Client: awsSdkV3Client,
Expand All @@ -81,6 +101,7 @@ describe('Class: DynamoDBProvider', () => {
serviceId: 'with-custom-sdk-client',
})
);
expect(userAgentSpy).toHaveBeenCalledWith(awsSdkV3Client, 'parameters');
});

test('when the user provides NOT an SDK client in the options, it throws an error', async () => {
Expand Down
16 changes: 16 additions & 0 deletions packages/parameters/tests/unit/SSMProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import type {
} from '../../src/types/SSMProvider';
import { ExpirableValue } from '../../src/base/ExpirableValue';
import { toBase64 } from '@aws-sdk/util-base64';
import * as UserAgentMiddleware from '@aws-lambda-powertools/commons/lib/userAgentMiddleware';

const encoder = new TextEncoder();

Expand All @@ -37,6 +38,10 @@ describe('Class: SSMProvider', () => {
test('when the class instantiates without SDK client and client config it has default options', async () => {
// Prepare
const options: SSMProviderOptions = {};
const userAgentSpy = jest.spyOn(
UserAgentMiddleware,
'addUserAgentMiddleware'
);

// Act
const provider = new SSMProvider(options);
Expand All @@ -47,6 +52,7 @@ describe('Class: SSMProvider', () => {
serviceId: 'SSM',
})
);
expect(userAgentSpy).toHaveBeenCalled();
});

test('when the user provides a client config in the options, the class instantiates a new client with client config options', async () => {
Expand All @@ -56,6 +62,10 @@ describe('Class: SSMProvider', () => {
serviceId: 'with-client-config',
},
};
const userAgentSpy = jest.spyOn(
UserAgentMiddleware,
'addUserAgentMiddleware'
);

// Act
const provider = new SSMProvider(options);
Expand All @@ -66,6 +76,7 @@ describe('Class: SSMProvider', () => {
serviceId: 'with-client-config',
})
);
expect(userAgentSpy).toHaveBeenCalled();
});

test('when the user provides an SDK client in the options, the class instantiates with it', async () => {
Expand All @@ -77,6 +88,10 @@ describe('Class: SSMProvider', () => {
const options: SSMProviderOptions = {
awsSdkV3Client: awsSdkV3Client,
};
const userAgentSpy = jest.spyOn(
UserAgentMiddleware,
'addUserAgentMiddleware'
);

// Act
const provider = new SSMProvider(options);
Expand All @@ -87,6 +102,7 @@ describe('Class: SSMProvider', () => {
serviceId: 'with-custom-sdk-client',
})
);
expect(userAgentSpy).toHaveBeenCalledWith(awsSdkV3Client, 'parameters');
});

test('when the user provides NOT an SDK client in the options, it throws an error', async () => {
Expand Down
19 changes: 19 additions & 0 deletions packages/parameters/tests/unit/SecretsProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type { GetSecretValueCommandInput } from '@aws-sdk/client-secrets-manager
import type { SecretsProviderOptions } from '../../src/types/SecretsProvider';
import { mockClient } from 'aws-sdk-client-mock';
import 'aws-sdk-client-mock-jest';
import * as UserAgentMiddleware from '@aws-lambda-powertools/commons/lib/userAgentMiddleware';

const encoder = new TextEncoder();

Expand All @@ -27,6 +28,11 @@ describe('Class: SecretsProvider', () => {
// Prepare
const options: SecretsProviderOptions = {};

const userAgentSpy = jest.spyOn(
UserAgentMiddleware,
'addUserAgentMiddleware'
);

// Act
const provider = new SecretsProvider(options);

Expand All @@ -36,6 +42,7 @@ describe('Class: SecretsProvider', () => {
serviceId: 'Secrets Manager',
})
);
expect(userAgentSpy).toHaveBeenCalled();
});

test('when the user provides a client config in the options, the class instantiates a new client with client config options', async () => {
Expand All @@ -46,6 +53,11 @@ describe('Class: SecretsProvider', () => {
},
};

const userAgentSpy = jest.spyOn(
UserAgentMiddleware,
'addUserAgentMiddleware'
);

// Act
const provider = new SecretsProvider(options);

Expand All @@ -55,6 +67,7 @@ describe('Class: SecretsProvider', () => {
serviceId: 'with-client-config',
})
);
expect(userAgentSpy).toHaveBeenCalled();
});

test('when the user provides an SDK client in the options, the class instantiates with it', async () => {
Expand All @@ -67,6 +80,11 @@ describe('Class: SecretsProvider', () => {
awsSdkV3Client: awsSdkV3Client,
};

const userAgentSpy = jest.spyOn(
UserAgentMiddleware,
'addUserAgentMiddleware'
);

// Act
const provider = new SecretsProvider(options);

Expand All @@ -76,6 +94,7 @@ describe('Class: SecretsProvider', () => {
serviceId: 'with-custom-sdk-client',
})
);
expect(userAgentSpy).toHaveBeenCalledWith(awsSdkV3Client, 'parameters');
});

test('when the user provides NOT an SDK client in the options, it throws an error', async () => {
Expand Down