This package provides methods for interacting with secrets stored in AWS Secrets Manager. Key functionalities include:
To install this package, execute the following commands in your terminal:
# Set the APM registry to local (one-time process)
npm set @koadz:registry <registry_url>
KOADZ_AWS_REGION variable is set in your environment file, or pass the region to the AWSSecretsManager constructor.An .env.example file is provided for reference. Create a similar environment file with the required variables.
import { AWSSecretsManager } from "@koadz/secrets-manager";
// Create an instance
const aws_sm = new AWSSecretsManager();
// Create a secret
aws_sm
.createSecret({
secretName: "unique_secret_name",
data: { message: "Initial Value" },
})
.catch(console.error);
// Get secrets by secret name
// List all secrets
// Update a secret by secret name
aws_sm
.updateSecret({
secretName: "unique_secret_name",
data: { message: "Updated Value" },
})
.catch(console.error);
// Delete a secret by secret name (default RecoveryWindowInDays is 7)
Customize the AWS Secrets Manager instance by passing options. Refer to the AWS Official Documentation for available configuration options:
import { AWSSecretsManager } from "@koadz/secrets-manager";
const options = {
// Accepts all AWS Secrets Manager options
};
// Create an instance with custom options
const aws_sm = new AWSSecretsManager(options);
export { aws_sm };
Cache class is a utility for managing cache in Redis. It provides methods for storing, retrieving, and deleting data from the cache.
const cache = new Cache({
redisUrl: "redis://localhost:4466",
ttl: 604800, // 1 week in seconds
});
// Disconnect from Redis (important to prevent memory leaks)
await cache.disconnect();
// Restart the Redis connection
await cache.restart({
flushAll: true, // Optional
flushSecrets: ["some-secret-name1", "some-secret-name2"], // Optional
});
// Get from Cache
await cache.get("some-secret-name");
// Set to Cache
await cache.set("some-secret-name", "some-secret-value");
// Delete from Cache
await cache.delete("some-secret-name");
// Expire from Cache
await cache.expire("some-secret-name", 3600); // Default is global TTL
The SecretsCache class is a utility for caching AWS Secrets in Redis. It provides methods for fetching secrets, listing all secrets, creating, updating, and deleting secrets.
const secrets = new SecretsCache({
redisUrl: "redis://localhost:4466",
awsRegion: "ap-south-1",
ttl: 604800, // 1 week in seconds
});
// Disconnect from Redis (important to prevent memory leaks)
await secrets.cache.disconnect();
// Restart the Redis connection
await secrets.cache.restart({
flushAll: true, // Optional
flushSecrets: ["some-secret-name1", "some-secret-name2"], // Optional
});
// Fetch a secret
await secrets.getSecret("some-secret-name");
// List all secrets
await secrets.listSecrets();
// Create a new secret in AWS Secrets Manager and cache it in Redis
await secrets.createSecret({
secretName: "some-secret-name",
data: { message: "Initial Value" },
});
// Update a secret in AWS Secrets Manager and cache it in Redis
await secrets.updateSecret({
secretName: "some-secret-name",
data: { message: "Updated Value" },
});
// Delete a secret from AWS Secrets Manager and remove it from Redis
await secrets.deleteSecret("some-secret-name");
// Flush a single/multiple secret
await secrets.flush("some-secret-name");
await secrets.flush(["some-secret-name1", "some-secret-name2"]);
// Flush all secrets
await secrets.flushAll();