Skip to content

Commit

Permalink
chore: bump aws-sdk from v2 to v3
Browse files Browse the repository at this point in the history
  • Loading branch information
fedefrusc committed Jun 10, 2024
1 parent 766c1a9 commit 5204471
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 40 deletions.
8 changes: 4 additions & 4 deletions lib/client.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
'use strict'

const SecretsManager = require('aws-sdk').SecretsManager
const { SecretsManagerClient, GetSecretValueCommand } = require('@aws-sdk/client-secrets-manager')

class AwsClient {
constructor() {
this.sdk = new SecretsManager()
this.sdk = new SecretsManagerClient()
}

async get(name) {
try {
const data = await this.sdk.getSecretValue({ SecretId: name }).promise()

const command = new GetSecretValueCommand({ SecretId: name })
const data = await this.sdk.send(command)
if ('SecretString' in data) {
return data.SecretString
} else {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
]
},
"dependencies": {
"aws-sdk": "^2.799.0",
"@aws-sdk/client-secrets-manager": "^3.588.0",
"fastify-secrets-core": "^2.0.0"
}
}
}
46 changes: 19 additions & 27 deletions test/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,24 @@ const { test, beforeEach } = require('tap')
const sinon = require('sinon')
const proxyquire = require('proxyquire')

class Stub {
getSecretValue() {}
class SecretsManagerClientStub {
send() {}
}
class GetSecretValueCommandStub {
constructor(SecretId) {
this.input = SecretId
}
}

const getSecretValuePromise = sinon.stub()
const GSVCStub = sinon.stub(GetSecretValueCommandStub)

const getSecretValue = sinon.stub(Stub.prototype, 'getSecretValue').returns({
promise: getSecretValuePromise
})
const SMStub = sinon.stub(SecretsManagerClientStub.prototype, 'send').resolves({ SecretString: 'secret payload' })

const AwsClient = proxyquire('../lib/client', {
'aws-sdk': {
SecretsManager: Stub
'@aws-sdk/client-secrets-manager': {
GetSecretValueCommand: GetSecretValueCommandStub,
SecretsManagerClient: SecretsManagerClientStub
}
})

Expand All @@ -28,40 +33,27 @@ test('get', (t) => {
t.plan(3)

t.test('SecretString', async (t) => {
t.plan(3)
t.plan(4)

const client = new AwsClient()
getSecretValuePromise.resolves({
SecretString: 'secret payload'
})

const secret = await client.get('secret/name')

t.ok(getSecretValue.called, 'calls getSecretValue')
t.ok(
getSecretValue.calledWith({
SecretId: 'secret/name'
}),
'provides name as SecretId to getSecretValue'
)
t.ok(GSVCStub.call, 'new instance of GetSecretValueCommand')
t.ok(SMStub.called, 'calls send')
t.ok(SMStub.calledWith(sinon.match({ input: { SecretId: 'secret/name' } })), 'provides name as SecretId to send')
t.equal(secret, 'secret payload', 'extracts SecretString')
})

t.test('SecretBinary', async (t) => {
t.plan(3)

const client = new AwsClient()
getSecretValuePromise.resolves({
SecretBinary: Buffer.from('secret payload').toString('base64')
})

const secret = await client.get('secret/name')

t.ok(getSecretValue.called, 'calls getSecretValue')
t.ok(SMStub.called, 'calls getSecretValue')
t.ok(
getSecretValue.calledWith({
SecretId: 'secret/name'
}),
SMStub.calledWith(sinon.match({ input: { SecretId: 'secret/name' } })),
'provides name as SecretId to getSecretValue'
)
t.equal(secret, 'secret payload', 'extracts SecretBinary')
Expand All @@ -71,7 +63,7 @@ test('get', (t) => {
t.plan(1)
const client = new AwsClient()

getSecretValuePromise.rejects(new Error())
SMStub.rejects(new Error())

const promise = client.get('secret/name')

Expand Down
14 changes: 7 additions & 7 deletions test/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const { test, teardown } = require('tap')
const uuid = require('uuid')
const Fastify = require('fastify')
const SecretsManager = require('aws-sdk').SecretsManager
const { SecretsManager, DeleteSecretCommand, CreateSecretCommand } = require('@aws-sdk/client-secrets-manager')

const FastifySecrets = require('../')

Expand All @@ -13,21 +13,21 @@ const SECRET_CONTENT = uuid.v4()
const client = new SecretsManager()

function createSecret() {
return client
.createSecret({
return client.send(
new CreateSecretCommand({
Name: SECRET_NAME,
SecretString: SECRET_CONTENT
})
.promise()
)
}

teardown(function deleteSecret() {
return client
.deleteSecret({
return client.send(
new DeleteSecretCommand({
SecretId: SECRET_NAME,
ForceDeleteWithoutRecovery: true
})
.promise()
)
})

test('integration', async (t) => {
Expand Down

0 comments on commit 5204471

Please sign in to comment.