Skip to content

Commit

Permalink
JavaScript (v3): Clean up secrets manager. (#5221)
Browse files Browse the repository at this point in the history
  • Loading branch information
cpyle0819 authored Aug 10, 2023
1 parent 2513f9a commit b785a8a
Show file tree
Hide file tree
Showing 12 changed files with 7,701 additions and 6,011 deletions.
9 changes: 9 additions & 0 deletions .doc_gen/metadata/secrets-manager_metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,15 @@ secrets-manager_GetSecretValue:
- description:
snippet_tags:
- cpp.example_code.secrets_manager.get_secret_value
JavaScript:
versions:
- sdk_version: 3
github: javascriptv3/example_code/secrets-manager
sdkguide:
excerpts:
- description:
snippet_tags:
- javascript.v3.secretsmanager.actions.GetSecretValue
services:
secrets-manager: {GetSecretValue}
secrets-manager_GetRandomPassword:
Expand Down
89 changes: 89 additions & 0 deletions javascriptv3/example_code/secrets-manager/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<!--Generated by WRITEME on 2023-08-08 19:58:52.531233 (UTC)-->
# Secrets Manager code examples for the SDK for JavaScript (v3)

## Overview

Shows how to use the AWS SDK for JavaScript (v3) to work with AWS Secrets Manager.

<!--custom.overview.start-->
<!--custom.overview.end-->

*Secrets Manager helps you to securely encrypt, store, and retrieve credentials for your databases and other services.*

## ⚠ Important

* Running this code might result in charges to your AWS account.
* Running the tests might result in charges to your AWS account.
* We recommend that you grant your code least privilege. At most, grant only the minimum permissions required to perform the task. For more information, see [Grant least privilege](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege).
* This code is not tested in every AWS Region. For more information, see [AWS Regional Services](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services).

<!--custom.important.start-->
<!--custom.important.end-->

## Code examples

### Prerequisites

For prerequisites, see the [README](../../README.md#Prerequisites) in the `javascriptv3` folder.


<!--custom.prerequisites.start-->
<!--custom.prerequisites.end-->

### Single actions

Code excerpts that show you how to call individual service functions.

* [Get a secret value](actions/get-secret-value.js#L7) (`GetSecretValue`)

## Run the examples

### Instructions

**Note**: All code examples are written in ECMAscript 6 (ES6). For guidelines on converting to CommonJS, see
[JavaScript ES6/CommonJS syntax](https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/sdk-examples-javascript-syntax.html).

**Run a single action**

```bash
node ./actions/<fileName>
```

**Run a scenario**
Most scenarios can be run with the following command:
```bash
node ./scenarios/<fileName>
```

<!--custom.instructions.start-->
<!--custom.instructions.end-->



### Tests

⚠ Running tests might result in charges to your AWS account.


To find instructions for running these tests, see the [README](../../README.md#Tests)
in the `javascriptv3` folder.



<!--custom.tests.start-->
<!--custom.tests.end-->

## Additional resources

* [Secrets Manager User Guide](https://docs.aws.amazon.com/secretsmanager/latest/userguide/intro.html)
* [Secrets Manager API Reference](https://docs.aws.amazon.com/secretsmanager/latest/apireference/Welcome.html)
* [SDK for JavaScript (v3) Secrets Manager reference](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/secrets-manager)

<!--custom.resources.start-->
<!--custom.resources.end-->

---

Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.

SPDX-License-Identifier: Apache-2.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
import {fileURLToPath} from "url";

// snippet-start:[javascript.v3.secretsmanager.actions.GetSecretValue]
import {
GetSecretValueCommand,
SecretsManagerClient,
} from "@aws-sdk/client-secrets-manager";

export const getSecretValue = async (secretName = "SECRET_NAME") => {
const client = new SecretsManagerClient();
const response = await client.send(
new GetSecretValueCommand({
SecretId: secretName,
})
);
console.log(response);
// {
// '$metadata': {
// httpStatusCode: 200,
// requestId: '584eb612-f8b0-48c9-855e-6d246461b604',
// extendedRequestId: undefined,
// cfId: undefined,
// attempts: 1,
// totalRetryDelay: 0
// },
// ARN: 'arn:aws:secretsmanager:us-east-1:xxxxxxxxxxxx:secret:binary-secret-3873048-xxxxxx',
// CreatedDate: 2023-08-08T19:29:51.294Z,
// Name: 'binary-secret-3873048',
// SecretBinary: Uint8Array(11) [
// 98, 105, 110, 97, 114,
// 121, 32, 100, 97, 116,
// 97
// ],
// VersionId: '712083f4-0d26-415e-8044-16735142cd6a',
// VersionStages: [ 'AWSCURRENT' ]
// }

if (response.SecretString) {
return response.SecretString;
}

if (response.SecretBinary) {
return response.SecretBinary;
}
};
// snippet-end:[javascript.v3.secretsmanager.actions.GetSecretValue]

// Invoke main function if this file was run directly.
if (process.argv[1] === fileURLToPath(import.meta.url)) {
getSecretValue();
}
13 changes: 13 additions & 0 deletions javascriptv3/example_code/secrets-manager/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "example-javascriptv3-secrets-manager",
"version": "1.0.0",
"author": "Corey Pyle <corepyle@amazon.com>",
"license": "Apache-2.0",
"dependencies": {
"@aws-sdk/client-secrets-manager": "^3.386.0"
},
"scripts": {
"integration-test": "vitest run **/*.integration.test.js"
},
"type": "module"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import {
CreateSecretCommand,
DeleteSecretCommand,
SecretsManagerClient,
} from "@aws-sdk/client-secrets-manager";
import {describe, it, expect, afterAll} from "vitest";
import {getSecretValue} from "../actions/get-secret-value.js";

describe("secrets manager examples", () => {
const client = new SecretsManagerClient({});
const secretNames = [];

afterAll(async () => {
for (const name of secretNames) {
await client.send(new DeleteSecretCommand({SecretId: name}));
}
});

describe("getSecretValue", () => {
it("should return a byte array if the value is binary data", async () => {
// Upload binary secret
const rand = Math.floor(Math.random() * 10000000);
const name = `binary-secret-${rand}`;
secretNames.push(name);
const command = new CreateSecretCommand({
Name: name,
SecretBinary: Buffer.from("binary data"),
});

await client.send(command);
const response = await getSecretValue(name);
expect(response).toEqual(Uint8Array.from(Buffer.from("binary data")));
});
});
});
41 changes: 0 additions & 41 deletions javascriptv3/example_code/secrets/README.md

This file was deleted.

19 changes: 0 additions & 19 deletions javascriptv3/example_code/secrets/package.json

This file was deleted.

20 changes: 0 additions & 20 deletions javascriptv3/example_code/secrets/src/libs/secretsClient.js

This file was deleted.

10 changes: 0 additions & 10 deletions javascriptv3/example_code/secrets/src/metadata.yaml

This file was deleted.

54 changes: 0 additions & 54 deletions javascriptv3/example_code/secrets/src/secrets_getsecretvalue.js

This file was deleted.

Loading

0 comments on commit b785a8a

Please sign in to comment.