Create a ticket
Sign in Sign up
Locker support Locker support
Results

No results found.

Home Locker Secrets Manager Developer tools Secrets - SDK Python SDK
Vietnamese English
Python SDK

Requirements

  • Python 3.6+

Installation

Install from PyPi:

pip install --upgrade lockerpm
 

Install from source with:

python setup.py install

Usages

Set up the access key

The SDK needs to be configured with your access key which is available in your Locker Secret Dashboard. Initialize the access_key to its value. You also need to set api_base value (default is https://api.locker.io/locker_secrets).

If you need to set your custom headers, you also need to set headers value in the options param:

from locker import Locker

access_key = "your_access_key_..."
api_base = "your_base_api.host"
headers = {
    "cf-access-client-id": "",
    "cf-access-client-secret": ""
}

locker = Locker(
    access_key_id=access_key_id, 
    access_key_secret=access_key_secret, 
    api_base=api_base, 
    options={"headers": headers}
)
 

Now, you can use SDK to get or set values:

# Get list secrets quickly
secrets = locker.list()

# Get a secret value by the secret key. 
# If the Key does not exist, SDK will return the default_value
secret_value = locker.get_secret("REDIS_CONNECTION", default_value="TheDefaultValue")
print(secret_value)

# Get a secret value by a secret key and specific environment name.
# If the Key does not exist, SDK will return the default_value
secret_value = locker.get_secret("REDIS_CONNECTION", environment_name="staging", default_value="TheDefaultValue")
print(secret_value)

# Create new secret
secret = locker.create(key="YOUR_NEW_SECRET_KEY", value="YOUR_NEW_SECRET_VALUE")

# Update new secret
secret = locker.modify(key="YOUR_NEW_SECRET_KEY", value="UPDATED_SECRET_VALUE")

# Update a secret value by a secret key and a specific environment name
secret = locker.modify(key="REDIS_CONNECTION",  environment_name="staging", value="staging.redis.connection")
print(secret.key, secret.value, secret.environment_name)

# List environments
environments = locker.list_environments()

# Get an environment object by name
environment = locker.get_environment("prod")
print(environment.name, environment.external_url)

# Create new environment
new_environment = locker.create_environment(name="staging", external_url="staging.host")

# Update an environment by name
environment = locker.modify_environment(name="staging", external_url="new.staging.host")

Logging

The library can be configured to emit logging that will give you better insight into what it's doing. There are some levels: debug, info, warning, error.

The info logging level is usually most appropriate for production use, but debug is also available for more verbosity.

 

There are a few options for enabling it:

1. Set the environment variable LOCKER_LOG to the value debug, info, warning or error

$ export LOCKER_LOG=debug
 

2. Set log when initializing the Locker object

from locker import Locker

locker = Locker(log="debug")
 

3. Enable it through Python's logging module:

import logging
logging.basicConfig()
logging.getLogger('locker').setLevel(logging.DEBUG)

Examples

See the examples' folder.

Development

First, install for development.

pip install -r requirements-dev.txt
 

Run tests

Test by using tox. We test against the following versions:

  • 3.6
  • 3.7
  • 3.8
  • 3.9
  • 3.10

To run all tests against all versions:

tox

Run all tests for a specific Python version:

tox -e py3.10

Run all tests in a single file:

tox -e py3.10 -- tests/test_util.py
 

Reporting security issues

We take the security and our users' trust very seriously. If you found a security issue in Locker SDK Python, please report the issue by contacting us at <[email protected]>. Do not file an issue on the tracker.

 

Contributing

Please check CONTRIBUTING.md before making a contribution.

Help and media

License

Was this page helpful?
No
Yes
Join Our Community