Skip to main content

The Delinea Secret Server Python SDK

Code style: black

PyPI Version License Python Versions

The Delinea Secret Server Python SDK contains classes that interact with Secret Server via their REST APIs.

Authentication Support

This SDK supports both Secret Server and Platform authentication. You can use the same authorizer classes for both systems and instantiate either a Secret Server or Platform client as needed. For Secret Server, you need to create an application user with the required permissions for authentication. For Platform, you need to create a service user with the appropriate permissions for authentication.

Install

python -m pip install python-tss-sdk

Secret Server Authentication

There are three ways in which you can authorize the SecretServer and SecretServerCloud classes to fetch secrets.

  • Password Authorization (with PasswordGrantAuthorizer)
  • Domain Authorization (with DomainPasswordGrantAuthorizer)
  • Access Token Authorization (with AccessTokenAuthorizer)

Usage

Password Authorization

If using traditional username and password authentication to log in to your Secret Server either directly or through Platform, you can pass the PasswordGrantAuthorizer into the SecretServer class at instantiation. The PasswordGrantAuthorizer requires a base_url, username, and password. It optionally takes a token_path_uri, but defaults to /oauth2/token or /identity/api/oauth2/token/xpmplatform, depending on whether a secret server or platform is used for authentication.

With Secret Server
from delinea.secrets.server import PasswordGrantAuthorizer

authorizer = PasswordGrantAuthorizer("https://hostname/SecretServer", os.getenv("myusername"), os.getenv("password"))
With Platform
from delinea.secrets.server import PasswordGrantAuthorizer

authorizer = PasswordGrantAuthorizer("https://platform.delinea.app", os.getenv("myusername"), os.getenv("password"))

Domain Authorization

To use a domain credential, use the DomainPasswordGrantAuthorizer. It requires a base_url, username, domain, and password. It optionally takes a token_path_uri, but defaults to /oauth2/token. It is applicable only when authentication is done using a secret server.

from delinea.secrets.server import DomainPasswordGrantAuthorizer

authorizer = DomainPasswordGrantAuthorizer("https://hostname/SecretServer", os.getenv("myusername"), os.getenv("mydomain"), os.getenv("password"))

Access Token Authorization

If you already have an access_token of Secret Server or Platform user, you can pass directly via the AccessTokenAuthorizer. The AccessTokenAuthorizer requires a access_token and base_url.

With Secret Server
from delinea.secrets.server import AccessTokenAuthorizer

authorizer = AccessTokenAuthorizer("AgJ1slfZsEng9bKsssB-tic0Kh8I...", "https://hostname/SecretServer")
With Platform
from delinea.secrets.server import AccessTokenAuthorizer

authorizer = AccessTokenAuthorizer("AgJ1slfZsEng9bKsssB-tic0Kh8I...", "https://platform.delinea.app")

Secret Server Cloud

The SDK API requires an Authorizer and either a tenant or a base_url. In the case of plaform authentication, only a base_url is supported.

tenant simplifies the configuration when using Secret Server Cloud by assuming the default folder structure and creating the base URL from a template that takes the tenant and an optional top-level domain (TLD) that defaults to com, as parameters.

Useage

Instantiate the SecretServerCloud class with tenant or base_url, along with an Authorizer (when providing tenant, yoou may optionally include a tld). To retrieve a secret, pass an integer id to get_secret() which will return the secret as a JSON encoded string.

With Secret Server
from delinea.secrets.server import SecretServerCloud

secret_server = SecretServerCloud(tenant=tenant, authorizer=authorizer)

secret = secret_server.get_secret(os.getenv("TSS_SECRET_ID"))

serverSecret = ServerSecret(**secret)

print(f"username: {serverSecret.fields['username'].value}\npassword: {serverSecret.fields['password'].value}")
With Platform
from delinea.secrets.server import SecretServerCloud

secret_server = SecretServerCloud(authorizer=authorizer, base_url="https://platform.delinea.app")

secret = secret_server.get_secret(os.getenv("TSS_SECRET_ID"))

serverSecret = ServerSecret(**secret)

print(f"username: {serverSecret.fields['username'].value}\npassword: {serverSecret.fields['password'].value}")

The SDK API also contains a Secret @dataclass containing a subset of the Secret's attributes and a dictionary of all the fields keyed by the Secret's slug.

Initializing SecretServer

Useage

NOTE: In v1.0.0 SecretServer replaces SecretServerV1. However, SecretServerV0 is available to use instead, for backwards compatibility with v0.0.5 and v0.0.6.

To instantiate the SecretServer class, it requires a base_url, an Authorizer object (see above), and an optional api_path_uri (defaults to "/api/v1")

With Secret Server
from delinea.secrets.server import SecretServer

secret_server = SecretServer("https://hostname/SecretServer", authorizer=authorizer)
With Platform
from delinea.secrets.server import SecretServer

secret_server = SecretServer(base_url="https://platform.delinea.app", authorizer=authorizer)

Secrets can be fetched using the get_secret method, which takes an integer id of the secret and, returns a json object:

secret = secret_server.get_secret(os.getenv("TSS_SECRET_ID"))

serverSecret = ServerSecret(**secret)

print(f"username: {serverSecret.fields['username'].value}\npassword: {serverSecret.fields['password'].value}")

Alternatively, you can use pass the json to ServerSecret which returns a dataclass object representation of the secret:

from delinea.secrets.server import ServerSecret

secret = ServerSecret(**secret_server.get_secret(os.getenv("TSS_SECRET_ID")))

username = secret.fields['username'].value

It is also now possible to fetch a secret by the secrets path using the get_secret_by_path method on the SecretServer object. This, too, returns a json object.

secret = secret_server.get_secret_by_path(r"TSS_SECRET_PATH")

serverSecret = ServerSecret(**secret)

print(f"username: {serverSecret.fields['username'].value}\npassword: {serverSecret.fields['password'].value}")

Note: Add a try-except block to the code to get more detailed error messages.

from delinea.secrets.server import SecretServerError

try:
    # code...
except SecretServerError as e:
    print(e.message)

Note: The path must be the full folder path and name of the secret.

Using Self-Signed Certificates

When using a self-signed certificate for SSL, the REQUESTS_CA_BUNDLE environment variable should be set to the path of the certificate (in .pem format). This will negate the need to ignore SSL certificate verification, which makes your application vunerable. Please reference the requests documentation for further details on the REQUESTS_CA_BUNDLE environment variable, should you require it.

Create a Build Environment (optional)

The SDK requires Python 3.8 or higher.

First, ensure Python is in $PATH, then run:

# Clone the repo
git clone https://github.com/DelineaXPM/python-tss-sdk
cd python-tss-sdk

# Create a virtual environment
python -m venv venv
. venv/bin/activate

# Install dependencies
python -m pip install --upgrade pip
pip install -r requirements.txt

Valid credentials are required to run the unit tests. The credentials should be stored in environment variables or in a .env file:

export TSS_USERNAME=myusername
export TSS_PASSWORD=mysecretpassword
export TSS_TENANT=mytenant
export TSS_SECRET_ID=42
export TSS_SECRET_PATH=\Test Secrets\SecretName
export TSS_FOLDER_ID=1
export TSS_FOLDER_PATH=\Test Secrets

The tests assume that the user associated with the specified TSS_USERNAME and TSS_PASSWORD can read the secret to be fetched, and that the Secret itself contains username and password fields.

To run the tests with tox:

tox

To build the package, use Flit:

flit build

Release files for python-tss-sdk 2.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for python-tss-sdk 2.0.1
File Size Uploaded
python_tss_sdk-2.0.1.tar.gz 17.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for python-tss-sdk 2.0.1
File Interpreter ABI Platform
python_tss_sdk-2.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 27.7 kB

Release files / python_tss_sdk-2.0.1.tar.gz

Download URL python_tss_sdk-2.0.1.tar.gz
Size 17.0 kB
Tags Source
SHA-256 checksum
How to use checksums
c62ba88eff10c9f4ea02ba06587bd91353859c87c3bf4220177ccff9b1eb2a8e
BLAKE2b-256 checksum
How to use checksums
bac786b918a8ab56f9f2d82570fd6b4b8e8e5985f1384e750eecc0e4d10f70e2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / python_tss_sdk-2.0.1-py3-none-any.whl

Download URL python_tss_sdk-2.0.1-py3-none-any.whl
Size 10.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
23f73c74cefae429c6c31585a1dd00032a44d6c61b8db6a25d29e54afb331d1a
BLAKE2b-256 checksum
How to use checksums
8afe2d38201218af10055d03d1aa412d3018860fac1d14b308f4fcfd1335013e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release history Release notifications | RSS feed

This release

2.0.1 This release

2 release files

2.0.0

2 release files

1.2.3

2 release files

1.2.2

2 release files

1.2.1

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.1

2 release files

1.0.0

1 release file

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page