Skip to content

Amazon AWS - Secrets Manager

Nothing can be simpler to interact with Secret Keys than the BotCity plugin for AWS Secrets Manager.

Manage your credentials.

Installation

pip install botcity-aws-secretsmanager-plugin

Importing the Plugin

After you installed this package, the next step is to import the package into your code and start using the functions.

from botcity.plugins.aws.secretsmanager import BotSecretsManagerPlugin

Setting up connection

Note

There are two different ways to authenticate.

1. Creating the .aws folder in the home directory, you need to create two files.

# ~/.aws/config
[default]
region=<region_code>
# ~/.aws/credentials
[default]
aws_access_key_id=<your_aws_access_key_id>
aws_secret_access_key=<your_aws_secret_access_key>

2. Passing credentials in the class constructor.

# Using the `.aws` folder
secrets = BotSecretsManagerPlugin()

# Alternative using the credentials as constructor arguments
secrets = BotSecretsManagerPlugin(
    use_credentials_file=False,
    access_key_id='<your_aws_access_key_id>',
    secret_access_key='<your_aws_secret_access_key>',
    region_name='<region_code>'
)

Info: If not found secret

Some methods return None if they don't find the secret, if you want to receive the error and handle it use RAISE_IF_NOT_FOUND=True

from botcity.plugins.aws.secretsmanager import BotSecretsManagerPlugin
secret = BotSecretsManagerPlugin()
secret.RAISE_IF_NOT_FOUND=True  # default is False

Secrets operations

Create new secret

from botcity.plugins.aws.secretsmanager import BotSecretsManagerPlugin

secret = BotSecretsManagerPlugin()
response = secret.create_secret(
    secret_name='test',
    secret_value={'key': 'name'},  # dict or str
    description='Test description.')
print(response)

# Or
secret["test"] = {'key': 'name'}  # description=''

List secrets

from botcity.plugins.aws.secretsmanager import BotSecretsManagerPlugin

secret = BotSecretsManagerPlugin()
print(secret.list_secrets())

Retrieves secret info

from botcity.plugins.aws.secretsmanager import BotSecretsManagerPlugin

secret = BotSecretsManagerPlugin()
print(secret.describe_secret(secret_name='test'))

Retrieve secret value

from botcity.plugins.aws.secretsmanager import BotSecretsManagerPlugin

secret = BotSecretsManagerPlugin()
print(secret.get_secret_value(secret_name='test'))

# Or
print(secret["test"])

Update secret value

from botcity.plugins.aws.secretsmanager import BotSecretsManagerPlugin

secret = BotSecretsManagerPlugin()
print(secret.update_secret(secret_name='test', secret_value={'key2': 'value2'}, description='New value'))

Delete secret

Warning

If you delete a secret with the without_recovery=True parameter, then you have no opportunity to recover the secret. You lose the secret permanently.

from botcity.plugins.aws.secretsmanager import BotSecretsManagerPlugin

secret = BotSecretsManagerPlugin()
print(secret.delete_secret(secret_name='test', without_recovery=True))

# Or
del secret["test"]  # without_recovery=False