@wavo-cloud/aws-secrets-manager-helper 0.1.9 → 0.1.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -161,7 +161,7 @@ async function getAllClientSecrets(
161
161
  * @param {String} secretDescription - The description of the secret
162
162
  * @param {String} secretValue - The JSON string secret value
163
163
  */
164
- async function createSecret(secretName, secretDescription, secretValue) {
164
+ async function createSecret(secretName, secretDescription, secretValue, tags) {
165
165
  let promiseError
166
166
  if (typeof secretValue !== 'string')
167
167
  throw new Error('Secret value must be a JSON string.')
@@ -175,6 +175,7 @@ async function createSecret(secretName, secretDescription, secretValue) {
175
175
  Name: secretName,
176
176
  Description: secretDescription,
177
177
  SecretString: secretValue,
178
+ Tags: tags,
178
179
  })
179
180
  .promise()
180
181
  .catch(error => {
@@ -208,6 +209,50 @@ async function editSecret(secretName, secretValue) {
208
209
  else return data
209
210
  }
210
211
 
212
+ /**
213
+ * Removes a set of tags from a secret
214
+ * @param {String} secretName - The friendly name of the secret
215
+ * @param {Object} tagNames - An array of strings representing tags to remove
216
+ */
217
+ async function removeTags(secretName, tagNames) {
218
+ let promiseError
219
+ const data = await client
220
+ .untagResource({
221
+ SecretId: secretName,
222
+ TagKeys: tagNames,
223
+ })
224
+ .promise()
225
+ .catch(error => {
226
+ promiseError = error
227
+ })
228
+
229
+ if (promiseError) handleAWSPromiseError(promiseError)
230
+ else return data
231
+ }
232
+
233
+ /**
234
+ * Appends a set of tags to a secret
235
+ * @param {String} secretName - The friendly name of the secret
236
+ * @param {Object} tags - An array of objects representing tags to add
237
+ * Object structure:
238
+ * { Key: 'key', Value: 'value' }
239
+ */
240
+ async function appendTags(secretName, tags) {
241
+ let promiseError
242
+ const data = await client
243
+ .tagResource({
244
+ SecretId: secretName,
245
+ Tags: tags,
246
+ })
247
+ .promise()
248
+ .catch(error => {
249
+ promiseError = error
250
+ })
251
+
252
+ if (promiseError) handleAWSPromiseError(promiseError)
253
+ else return data
254
+ }
255
+
211
256
  /**
212
257
  * Adds a client to the client list and does data validation
213
258
  * @param {String} clientId - The ID of the client
@@ -216,14 +261,16 @@ async function editSecret(secretName, secretValue) {
216
261
  * @param {String} region - The sub location of the organization's data in S3
217
262
  * @param {Object} keyValuePairs - The secret to store for the client
218
263
  */
219
- async function createClient(
264
+ async function createClient({
220
265
  clientId,
221
266
  clientName,
222
267
  organization,
223
268
  region,
224
269
  keyValuePairs,
225
- clientIdsSecretIdOverride = null
226
- ) {
270
+ clientIdsSecretIdOverride = null,
271
+ isSelfServe = false,
272
+ isActiveOrganization = false,
273
+ }) {
227
274
  if (!clientId || !clientName || !organization || !region)
228
275
  throw new Error('Client ID, name, organization, and region are required.')
229
276
  const regex = new RegExp('^[a-z0-9-_]*$')
@@ -247,7 +294,17 @@ async function createClient(
247
294
  organization,
248
295
  region,
249
296
  ...keyValuePairs,
250
- })
297
+ }),
298
+ [
299
+ {
300
+ Key: 'self_serve',
301
+ Value: isSelfServe.toString(),
302
+ },
303
+ {
304
+ Key: 'is_active_organization',
305
+ Value: isActiveOrganization.toString(),
306
+ },
307
+ ]
251
308
  )
252
309
  // add to client list
253
310
  const editClientListResults = await editSecret(
@@ -269,14 +326,14 @@ async function createClient(
269
326
  * @param {String} region - The sub location of the organization's data in S3
270
327
  * @param {Object} keyValuePairs - The secret to store for the client
271
328
  */
272
- async function setClient(
329
+ async function setClient({
273
330
  clientId,
274
331
  clientName,
275
332
  organization,
276
333
  region,
277
334
  keyValuePairs,
278
- clientIdsSecretIdOverride = null
279
- ) {
335
+ clientIdsSecretIdOverride = null,
336
+ }) {
280
337
  if (!clientId || !clientName || !organization || !region)
281
338
  throw new Error('Client ID, name, organization, and region are required.')
282
339
  const regex = new RegExp('^[a-z0-9-_]*$')
@@ -315,12 +372,12 @@ async function setClient(
315
372
  * @param {Object} keyValuePairsToAdd
316
373
  * @param {Array} keysToDelete
317
374
  */
318
- async function editClient(
375
+ async function editClient({
319
376
  clientId,
320
377
  keyValuePairsToAdd,
321
378
  keysToDelete,
322
- clientIdsSecretIdOverride
323
- ) {
379
+ clientIdsSecretIdOverride,
380
+ }) {
324
381
  if (!clientId) throw new Error('Client ID is required.')
325
382
  if (
326
383
  keysToDelete.some(key =>
@@ -357,6 +414,28 @@ async function editClient(
357
414
  return { editClientResults }
358
415
  }
359
416
 
417
+ async function setClientActiveStatus({
418
+ clientId,
419
+ newActiveStatus,
420
+ clientIdsSecretIdOverride,
421
+ }) {
422
+ if (!clientId) throw new Error('Client ID is required.')
423
+ if (newActiveStatus === null || newActiveStatus === undefined)
424
+ throw new Error('New active status is required.')
425
+ if (typeof newActiveStatus !== 'boolean')
426
+ throw new Error('New active status must be a boolean')
427
+ // get client secret location
428
+ const currentClientList = await getClientSecretIds(clientIdsSecretIdOverride)
429
+ const clientSecretId = currentClientList[clientId]
430
+ await removeTags(clientSecretId, ['is_active_organization'])
431
+ await appendTags(clientSecretId, [
432
+ {
433
+ Key: 'is_active_organization',
434
+ Value: newActiveStatus.toString(),
435
+ },
436
+ ])
437
+ }
438
+
360
439
  module.exports = {
361
440
  getClientSecretIds,
362
441
  getClientSecret,
@@ -366,4 +445,5 @@ module.exports = {
366
445
  createClient,
367
446
  setClient,
368
447
  editClient,
448
+ setClientActiveStatus,
369
449
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wavo-cloud/aws-secrets-manager-helper",
3
- "version": "0.1.9",
3
+ "version": "0.1.11",
4
4
  "description": "Wavo Cloud Infallible AWS Secrets Manager Helper",
5
5
  "license": "UNLICENSED",
6
6
  "repository": {
package/test/aws.test.js CHANGED
@@ -3,9 +3,11 @@ const {
3
3
  getAllClientSecrets,
4
4
  getClientSecretIds,
5
5
  getSecretValue,
6
+ getSecretTags,
6
7
  createClient,
7
8
  setClient,
8
9
  editClient,
10
+ setClientActiveStatus,
9
11
  } = require('../app/utils/awsSecretsManager')
10
12
  const { deleteClient } = require('../app/utils/awsSecretsManagerDeleteHelper')
11
13
  const expect = require('chai').expect
@@ -25,33 +27,56 @@ describe('Test Secrets Manager Helper', async () => {
25
27
  })
26
28
 
27
29
  it('should create a new client', async () => {
28
- const createClientResult = await createClient(
29
- 'test_client_id',
30
- 'test_client_name',
31
- 'test_organization',
32
- 'test_region',
33
- { test_secret_key: 'test_secret_value' },
34
- clientIdsSecretId
35
- )
30
+ const createClientResult = await createClient({
31
+ clientId: 'test_client_id',
32
+ clientName: 'test_client_name',
33
+ organization: 'test_organization',
34
+ region: 'test_region',
35
+ keyValuePairs: { test_secret_key: 'test_secret_value' },
36
+ clientIdsSecretIdOverride: clientIdsSecretId,
37
+ isSelfServe: true,
38
+ isActiveOrganization: true,
39
+ })
36
40
  expect(createClientResult.createSecretResults.Name).to.equal(
37
41
  'test_organization/ad_platforms/api'
38
42
  )
39
43
 
44
+ const newClientTags = await getSecretTags('test_organization/ad_platforms/api')
45
+ const isActiveOrganizationTag = newClientTags.find(
46
+ tag => tag.Key === 'is_active_organization'
47
+ )
48
+ expect(isActiveOrganizationTag.Value === 'true')
49
+
40
50
  const clientList = await getClientSecretIds(clientIdsSecretId)
41
51
  expect(clientList['test_client_id']).to.equal(
42
52
  'test_organization/ad_platforms/api'
43
53
  )
44
54
  })
45
55
 
46
- it('should set a client', async () => {
47
- await setClient(
48
- 'test_client_id',
49
- 'test_client_name_2',
50
- 'test_organization',
51
- 'test_region_new',
52
- { test_secret_key_new: 'test_secret_value_new', to_delete: 'to_delete' },
53
- clientIdsSecretId
56
+ it('should change the clients active status to false', async () => {
57
+ await setClientActiveStatus({
58
+ clientId: 'test_client_id',
59
+ newActiveStatus: false,
60
+ clientIdsSecretIdOverride: clientIdsSecretId,
61
+ })
62
+
63
+ const clientTags = await getSecretTags('test_organization/ad_platforms/api')
64
+ const isActiveOrganizationTag = clientTags.find(
65
+ tag => tag.Key === 'is_active_organization'
54
66
  )
67
+ expect(isActiveOrganizationTag.Value === 'falsey')
68
+
69
+ })
70
+
71
+ it('should set a client', async () => {
72
+ await setClient({
73
+ clientId: 'test_client_id',
74
+ clientName: 'test_client_name_2',
75
+ organization: 'test_organization',
76
+ region: 'test_region_new',
77
+ keyValuePairs: { test_secret_key_new: 'test_secret_value_new', to_delete: 'to_delete' },
78
+ clientIdsSecretIdOverride: clientIdsSecretId
79
+ })
55
80
 
56
81
  const testSecret = await getSecretValue(
57
82
  'test_organization/ad_platforms/api'
@@ -65,16 +90,16 @@ describe('Test Secrets Manager Helper', async () => {
65
90
  })
66
91
 
67
92
  it('should edit, add, and delete a client key/value secret', async () => {
68
- await editClient(
69
- 'test_client_id',
70
- {
93
+ await editClient({
94
+ clientId: 'test_client_id',
95
+ keyValuePairsToAdd: {
71
96
  test_secret_key_new: 'edit',
72
97
  new_key: 'add',
73
98
  organization: 'test_organization_new'
74
99
  },
75
- ['to_delete'],
76
- clientIdsSecretId
77
- )
100
+ keysToDelete: ['to_delete'],
101
+ clientIdsSecretIdOverride: clientIdsSecretId
102
+ })
78
103
 
79
104
  const testSecret = await getSecretValue(
80
105
  'test_organization/ad_platforms/api'