Skip to main content

Unofficial OpenTDF SDK for Python.

Project description

opentdf-python-sdk

Unofficial OpenTDF SDK for Python

Tests

This project is powered by gopy, which generates (and compiles) a CPython extension module from a go package. The gopy tool unlocks performance, flexibility, and excellent Developer Experience to Python end-users. Read more about gopy on Github.

Adding features

If you wish to expand the functionality of otdf-python:

  1. Create a fork/branch
  2. Add new capabilities (e.g. in main.go)
  3. Add a test (e.g. in otdf_python_test.go)
  4. Commit your changes, push, and open a Pull Request via the Github project: https://github.com/b-long/opentdf-python-sdk

Installation

Install from the Python Package Index (PyPI):

# Install the latest from pypi.org
pip install otdf_python

# Install a pinned version
pip install otdf-python==0.0.9

# Install a pinned version, from test.pypi.org
pip install -i https://test.pypi.org/simple/ otdf-python==0.0.9

Usage

Simple usage examples are given below. In addition, we recommend you also:

  1. See the contents of main.go on Github. ✨ Note that all Upper-case functions are available in Python.
  2. See the contents of validate_otdf_python.py on Github.

Example: Configuration

Creating a helper function may simplify the usage of otdf-python.

For example:

def get_encrypt_config(data_attributes: list | None = None):
    """
    The config object returned here can only be used for encryption.

    While 'otdf_python.gotdf_python' internally can use golang interfaces,
     to normalize config objects, that pattern causes a panic
     when used from Python.

    """
    print("Preparing 'EncryptionConfig' object")
    from otdf_python.gotdf_python import EncryptionConfig
    from otdf_python.go import Slice_string

    if isinstance(data_attributes, list):
        # Create config using the attributes from the caller
        da = Slice_string(data_attributes)
        config: EncryptionConfig = EncryptionConfig(
            ClientId="opentdf-sdk",
            ClientSecret="secret",
            PlatformEndpoint=platformEndpoint,
            TokenEndpoint="http://localhost:8888/auth/realms/opentdf/protocol/openid-connect/token",
            KasUrl=f"http://{platformEndpoint}/kas",
            # FIXME: Be careful with binding the 'DataAttributes' field on this struct.
            #
            # In golang, this is initialized as []string , but passing
            # DataAttributes=None, or DataAttributes=[] from Python will fail.
            DataAttributes=da,
        )
    else:
        # Create config without attributes
        config: EncryptionConfig = EncryptionConfig(
            ClientId="opentdf-sdk",
            ClientSecret="secret",
            PlatformEndpoint=platformEndpoint,
            TokenEndpoint="http://localhost:8888/auth/realms/opentdf/protocol/openid-connect/token",
            KasUrl=f"http://{platformEndpoint}/kas",
        )

    # NOTE: Structs from golang can be printed, like below
    # print(config)
    print("Returning 'EncryptionConfig'")

    return config

Example: Encrypt a string

from otdf_python.gotdf_python import EncryptString

# Depends on the 'get_encrypt_config()' given
# in the README above
config: EncryptionConfig = get_encrypt_config()

tdf_manifest_json = EncryptString(inputText="Hello from Python", config=config)

Example: Encrypt a file

from otdf_python.gotdf_python import EncryptFile
from otdf_python.go import Slice_string

# Depends on the 'get_encrypt_config()' given
# in the README above
config: EncryptionConfig = get_encrypt_config()

with tempfile.TemporaryDirectory() as tmpDir:
    print("Created temporary directory", tmpDir)

    da = Slice_string(["https://example.com/attr/attr1/value/value1", "https://example.com/attr/attr1/value/value2"])

    encrypted_file = Path(tmpDir) / "some-file.tdf"

    if encrypted_file.exists():
        encrypted_file.unlink()

    if encrypted_file.exists():
        raise ValueError(
            "The output path should not exist before calling 'EncryptFile()'."
        )

    outputFilePath = EncryptFile(
        inputFilePath=str(SOME_PLAINTEXT_FILE),
        outputFilePath=str(encrypted_file),
        config=config,
    )

    print(f"The output file was written to destination path: {outputFilePath}")

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

otdf_python-0.1.0-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (9.9 MB view details)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

otdf_python-0.1.0-cp312-cp312-macosx_13_0_x86_64.whl (5.4 MB view details)

Uploaded CPython 3.12 macOS 13.0+ x86-64

otdf_python-0.1.0-cp312-cp312-macosx_13_0_arm64.whl (5.1 MB view details)

Uploaded CPython 3.12 macOS 13.0+ ARM64

otdf_python-0.1.0-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (9.9 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

otdf_python-0.1.0-cp311-cp311-macosx_13_0_x86_64.whl (5.4 MB view details)

Uploaded CPython 3.11 macOS 13.0+ x86-64

otdf_python-0.1.0-cp311-cp311-macosx_13_0_arm64.whl (5.1 MB view details)

Uploaded CPython 3.11 macOS 13.0+ ARM64

File details

Details for the file otdf_python-0.1.0-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for otdf_python-0.1.0-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 93ee05a43eae9a6a802a631665cd2e05e54892c5c938d2fab9d182bc1698c904
MD5 09d26b27f0b1e646cc10f89b7791ff61
BLAKE2b-256 34d813573a4ae7bd437e87d1e10cb6f494103ddc4997e373d3032f943c6f5920

See more details on using hashes here.

File details

Details for the file otdf_python-0.1.0-cp312-cp312-macosx_13_0_x86_64.whl.

File metadata

File hashes

Hashes for otdf_python-0.1.0-cp312-cp312-macosx_13_0_x86_64.whl
Algorithm Hash digest
SHA256 018ba864f428b7227a72fbbfb89c8204ce8944878b7f8899d6dd05312339c6ca
MD5 909af46f793c7439fd81360160a62dca
BLAKE2b-256 f97d5cacb071cda9fe147e7abfd38381a24ca651c51ccaee2b53f21903140724

See more details on using hashes here.

File details

Details for the file otdf_python-0.1.0-cp312-cp312-macosx_13_0_arm64.whl.

File metadata

File hashes

Hashes for otdf_python-0.1.0-cp312-cp312-macosx_13_0_arm64.whl
Algorithm Hash digest
SHA256 4f59862ce980d4183d204af4b50468505a4065d467a2f86e118af89aafa50e31
MD5 278e318eedcd99e4946b448eae72b96f
BLAKE2b-256 a0ef778a7f820b8e33c06fadd462dcf363de8ab6a264fb18aab1364f7f45da02

See more details on using hashes here.

File details

Details for the file otdf_python-0.1.0-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for otdf_python-0.1.0-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 553d60074c08e4b429c0ecc5ddcb9c008335b928b3d66cd4cc51e00c3340cffb
MD5 ff25432c98c1cb7ad28221c2bd1465f3
BLAKE2b-256 39e6e5219543239abf81e6134147f0d1fab10a3cbcc92270bbdcbda70e6c136e

See more details on using hashes here.

File details

Details for the file otdf_python-0.1.0-cp311-cp311-macosx_13_0_x86_64.whl.

File metadata

File hashes

Hashes for otdf_python-0.1.0-cp311-cp311-macosx_13_0_x86_64.whl
Algorithm Hash digest
SHA256 688450c1fe9bcbb97453646830731d2f769ea50e55e870f2cdf5bccbaa5a60b1
MD5 52cd5d75b11db7f6a3eb36a15c6ec78c
BLAKE2b-256 ff7733f978d09f6e56d893d239eb99cb2494845de5c31164d627c4b685220dd2

See more details on using hashes here.

File details

Details for the file otdf_python-0.1.0-cp311-cp311-macosx_13_0_arm64.whl.

File metadata

File hashes

Hashes for otdf_python-0.1.0-cp311-cp311-macosx_13_0_arm64.whl
Algorithm Hash digest
SHA256 79019b7b3bcaafaa8a61dc614e5965c81364edd358952f9ca955db0dcf3f8d10
MD5 daad9892fa3fa3cf93ebccf437667194
BLAKE2b-256 801597e15afc452f78a36fd01e483363ef89f443da3fdf6a379edf726c7bdee7

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page