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.1-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (10.0 MB view details)

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

otdf_python-0.1.1-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.1-cp312-cp312-macosx_13_0_arm64.whl (5.1 MB view details)

Uploaded CPython 3.12 macOS 13.0+ ARM64

otdf_python-0.1.1-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (10.0 MB view details)

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

otdf_python-0.1.1-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.1-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.1-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.1-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a96113edf37874bed8bf8c6f4629896f4df38f0618238d112b637838dbbf736c
MD5 9f05ad3f12c6e16a494c83d96abc47b4
BLAKE2b-256 4119d7c3e11fe29ef0b639de06bcb4600e703ff39a4d6674e9b50e98f57498c1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for otdf_python-0.1.1-cp312-cp312-macosx_13_0_x86_64.whl
Algorithm Hash digest
SHA256 a3665684f0191e51f00e6408f74fe2376ded7a59eecc099f61919edfa6ec43ad
MD5 5900f4eff9080c249e899dfdad549ef8
BLAKE2b-256 edbda39d80bb614cb4f4690d9f2fae8642ffb9941acc4d294a9c4a446768ba5f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for otdf_python-0.1.1-cp312-cp312-macosx_13_0_arm64.whl
Algorithm Hash digest
SHA256 f49c2ae2eb1448b571e4c1ba56b9be1c98f71ad31502f5d8e125908155c861c3
MD5 16e61f552d404569962361892a547845
BLAKE2b-256 210e1e5d8bba2e8cdaa9b7178a2fbbf0c7823bc5f35c39c3a4f98a97964f6e54

See more details on using hashes here.

File details

Details for the file otdf_python-0.1.1-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.1-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 42a3fe6dde36c54959a544c22bf48b0db3ba1eaf43be65cb1f2ae983508eef48
MD5 cba593e04356fd4788428435580c622d
BLAKE2b-256 7383c3e5375e2d347ec4e0761c69aacbe4b510d7a27e9b8c8382f279e388b0d0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for otdf_python-0.1.1-cp311-cp311-macosx_13_0_x86_64.whl
Algorithm Hash digest
SHA256 8961aae32805386e1bdbd577636923e746e3968a2e50aaa297d7b91863ba7420
MD5 20082b39299328307a209b10b3f25413
BLAKE2b-256 fab342b5ed3b47996a619613750199e8ea7eb4954f1bc89b43d23f3fe50d99db

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for otdf_python-0.1.1-cp311-cp311-macosx_13_0_arm64.whl
Algorithm Hash digest
SHA256 cd9721bc8872127aa81730652858e326d933eeab9455edb08f4ea518e0a56df8
MD5 19afb7b4572d9c3a4c4b6c880216bafa
BLAKE2b-256 e473d4670f493fdabe4b3a207c3143c33150b61326b498f47692f0c25f96d50c

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