Skip to content

Commit

Permalink
Merge pull request #19 from beforeyoubid/feature/aws-sdk-upgrade
Browse files Browse the repository at this point in the history
Feature/aws-sdk upgrade
  • Loading branch information
alice-byb authored Aug 20, 2024
2 parents 72d9598 + 20244d6 commit cb3db75
Show file tree
Hide file tree
Showing 6 changed files with 886 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defaults: &defaults
working_directory: ~/app/
docker:
- image: circleci/node:14
- image: cimg/node:18.20.3
resource_class: medium

version: 2.1
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2022 Before You Bid
Copyright (c) 2024 Before You Buy

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "serverless-ssm-plugin",
"version": "1.1.0",
"version": "1.2.0",
"description": "Serverless plugin to add secrets manager secrets to function bundles",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"author": "Bailey <bailey@beforeyoubid.com.au>",
"author": "Alice <alice@beforeyoubuy.com.au>",
"license": "MIT",
"dependencies": {
"aws-sdk": "2.1251.0"
"@aws-sdk/client-secrets-manager": "^3.632.0"
},
"repository": {
"type": "git",
Expand Down
45 changes: 19 additions & 26 deletions src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SecretsManager } from 'aws-sdk';
import { sendCommand, SecretsManagerClient, GetSecretValueCommand } from '@aws-sdk/client-secrets-manager';
import ServerlessSsmPlugin from '..';
import { ServerlessWithError } from '../types';
import { existsSync, promises } from 'fs';
Expand All @@ -12,8 +12,9 @@ jest.mock('fs', () => ({
writeFile: jest.fn().mockResolvedValue(undefined),
},
}));
jest.mock('aws-sdk', () => ({
SecretsManager: jest.fn(),
jest.mock('@aws-sdk/client-secrets-manager', () => ({
...jest.requireActual('@aws-sdk/client-secrets-manager'),
SecretsManagerClient: jest.fn(),
}));

jest.mock('@serverless/utils/log', () => ({
Expand Down Expand Up @@ -51,13 +52,11 @@ describe('ssm plugin', () => {
describe('getParameterFromSsm', () => {
it('should try to load from ssm', async () => {
const returnValue = Math.random().toString();
const getSecretValue = jest.fn().mockReturnValue({
promise: () => Promise.resolve({ SecretString: returnValue }),
});
(SecretsManager as unknown as jest.Mock).mockReturnValue({ getSecretValue });
const send = jest.fn().mockResolvedValue({ SecretString: returnValue });
(SecretsManagerClient as unknown as jest.Mock).mockReturnValue({ send });
const name = Math.random().toString();
const parameter = await plugin.getParameterFromSsm(name);
expect(getSecretValue).toBeCalledWith({ SecretId: name });
expect(send).toBeCalled();
expect(parameter).toBe(returnValue);
});
});
Expand Down Expand Up @@ -91,12 +90,10 @@ describe('ssm plugin', () => {
const providerSecretTitle = Math.random().toString();
plugin.serverless.service.provider.environment = { API_ENV_SECRET_NAME: providerSecretTitle };

const getSecretValue = jest.fn().mockReturnValue({
promise: () => Promise.resolve({ SecretString: undefined }),
});
(SecretsManager as unknown as jest.Mock).mockReturnValue({ getSecretValue });
const send = jest.fn().mockResolvedValue({ SecretString: undefined });
(SecretsManagerClient as unknown as jest.Mock).mockReturnValue({ send });
await expect(plugin.writeEnvironmentSecretToFile()).rejects.toThrow(Error);
expect(getSecretValue).toBeCalledWith({ SecretId: providerSecretTitle });
expect(send).toBeCalled();
expect(promises.writeFile).toHaveBeenCalledTimes(0);
});
it('should get parameter by calling getParameterFromSsm and write to disk', async () => {
Expand All @@ -105,12 +102,10 @@ describe('ssm plugin', () => {
(promises.writeFile as jest.Mock).mockReturnValue(undefined);

const returnValue = Math.random().toString();
const getSecretValue = jest.fn().mockReturnValue({
promise: () => Promise.resolve({ SecretString: returnValue }),
});
(SecretsManager as unknown as jest.Mock).mockReturnValue({ getSecretValue });
const send = jest.fn().mockResolvedValue({ SecretString: returnValue });
(SecretsManagerClient as unknown as jest.Mock).mockReturnValue({ send });
await plugin.writeEnvironmentSecretToFile();
expect(getSecretValue).toBeCalledWith({ SecretId: providerSecretTitle });
expect(send).toBeCalled();
expect(promises.writeFile).toHaveBeenCalledWith(
plugin.secretsFile,
JSON.stringify({ [providerSecretTitle]: returnValue })
Expand All @@ -127,12 +122,12 @@ describe('ssm plugin', () => {
const providerSecretTitle = Math.random().toString();
plugin.serverless.service.provider.environment = { API_ENV_SECRET_NAME: providerSecretTitle };

const getSecretValue = jest.fn().mockReturnValue({
const send = jest.fn().mockReturnValue({
promise: () => Promise.resolve({ SecretString: undefined }),
});
(SecretsManager as unknown as jest.Mock).mockReturnValue({ getSecretValue });
(SecretsManagerClient as unknown as jest.Mock).mockReturnValue({ send });
await expect(plugin.packageSecrets()).rejects.toThrow(Error);
expect(getSecretValue).toBeCalledWith({ SecretId: providerSecretTitle });
expect(send).toBeCalled();
expect(promises.writeFile).toHaveBeenCalledTimes(0);
expect(log as jest.Mock).toHaveBeenCalled();
});
Expand All @@ -142,13 +137,11 @@ describe('ssm plugin', () => {
(promises.writeFile as jest.Mock).mockReturnValue(undefined);

const returnValue = Math.random().toString();
const getSecretValue = jest.fn().mockReturnValue({
promise: () => Promise.resolve({ SecretString: returnValue }),
});
(SecretsManager as unknown as jest.Mock).mockReturnValue({ getSecretValue });
const send = jest.fn().mockResolvedValue({ SecretString: returnValue });
(SecretsManagerClient as unknown as jest.Mock).mockReturnValue({ send });
await plugin.packageSecrets();
expect(log as jest.Mock).toHaveBeenCalled();
expect(getSecretValue).toBeCalledWith({ SecretId: providerSecretTitle });
expect(send).toBeCalled();
expect(promises.writeFile).toHaveBeenCalledWith(
plugin.secretsFile,
JSON.stringify({ [providerSecretTitle]: returnValue })
Expand Down
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from 'fs';
import { SecretsManager } from 'aws-sdk';
import { GetSecretValueCommand, SecretsManagerClient } from '@aws-sdk/client-secrets-manager';
import Plugin from 'serverless/classes/Plugin';
import { log } from '@serverless/utils/log';

Expand Down Expand Up @@ -52,11 +52,11 @@ export default class ServerlessSsmPlugin implements Plugin {
}

async getParameterFromSsm(name: string): Promise<Maybe<string>> {
const client = new SecretsManager({
const client = new SecretsManagerClient({
region: this.region,
...this.serverless.providers.aws.getCredentials(),
});
const data = await client.getSecretValue({ SecretId: name }).promise();
const data = await client.send(new GetSecretValueCommand({ SecretId: name }));
return data.SecretString;
}

Expand Down
Loading

0 comments on commit cb3db75

Please sign in to comment.