-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JavaScript (v3): Clean up secrets manager.
- Loading branch information
Showing
12 changed files
with
7,701 additions
and
6,011 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
55 changes: 55 additions & 0 deletions
55
javascriptv3/example_code/secrets-manager/actions/get-secret-value.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
35 changes: 35 additions & 0 deletions
35
javascriptv3/example_code/secrets-manager/tests/secrets-manager.integration.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"))); | ||
}); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
20 changes: 0 additions & 20 deletions
20
javascriptv3/example_code/secrets/src/libs/secretsClient.js
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
54 changes: 0 additions & 54 deletions
54
javascriptv3/example_code/secrets/src/secrets_getsecretvalue.js
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.