openziti/ziti-sdk-py
About
Ziti SDK for Python
Developer ToolsMeasured star growth
- in 24h
- 0
- in 7 days
- +2
- in 15 days
- +2
- in 1 month
- Collecting history
Net star changes over overlapping periods, not daily totals. Missing measurements are left blank. Stars measure attention, not software quality.
README.md
Original
Python SDK for OpenZiti
Getting Started • Examples • Support • Contributing • License
The Python SDK for OpenZiti is a library that enables you to integrate zero trust network connectivity into your Python applications, and establish secure connections with remote network resources over an OpenZiti network. The SDK also simplifies the process of adding secure, zero-trust network connectivity built into your Python application. It's so simple that it can be done in just two lines of code!
OpenZiti is an open-source project that provides secure, zero-trust networking for applications running on any platform.
More specifically, the SDK allows you to integrate zero trust at the application level. This means your data is never exposed outside the application environment providing you with end-to-end encryption for ultimate security. See other zero trust models here.
Getting Started
See the get started guide to learn how to deploy your own overlay.
Or, you can try CloudZiti for free, check out more here.
Installing the SDK
The Python SDK for OpenZiti is distributed via the Python Package Index (PyPI) and can be installed using
pip package manager.
pip install openziti
Using Ziti Python SDK
With just two lines of code, you can turn your plain old web server into a secure, zero-trust embedded application. Below is an example of just how simple it is to get started.
Provide a hostname, and port for your application, a simple monkey patch, and you're ready to go. You don't even need to know what a monkey patch is! However, if you're interested in what a monkey patch is, expand the block below.
What is Monkey Patching?
Monkey patching allows developers to modify functionality for code even when they may not have access to the original source code. Because Python has a dynamic object model allowing developers to modify objects at runtime. Monkey patching allows developers to point a function call to any function they want. We can even implement our own function that doesn't exist in the source code.
The way this Python SDK uses monkey patching is to override existing functionality in socket handling by the socket module.
Taking a look at the code below, the key lines are the last two. You can see how, for each monkey patched function, we're telling that function call on the
sockobject to be directed to the function held in_patch_methods. Therefore, this SDK can be used on any application that doesn't manage its own sockets.def __init__(self, **kwargs): self.orig_socket = sock.socket sock.socket = _patchedSocket(kwargs) self.orig_methods = {m: sock.__dict__[m] for m, _ in _patch_methods.items()} for m_name, _ in _patch_methods.items(): sock.__dict__[m_name] = _patch_methods[m_name]
cfg = dict(ztx=openziti.load('/path/to/identity.json'), service="name-of-ziti-service")
openziti.monkeypatch(bindings={('127.0.0.1', 8000): cfg})
Or try our decorator pattern with a function annotation
@openziti.zitify(bindings={('127.0.0.1', 18080): {'ztx': '/path/to/identity.json', 'service': 'name-of-ziti-service'}})
def yourFunction():
The binding dictionary configures what happens when the code tries to open a server socket. Standard network addresses
are mapped to ziti service configurations. For example, with his configuration
bindings = {
('0.0.0.0', 8080): { 'ztx': 'my-identity.json', 'service':'my-service' }
}
when application opens a server socket and binds to address 0.0.0.0:8080 it will actually bind to the ziti service named my-service.
Binding addresses can be specified with tuples, strings, or ints(ports). ('0.0.0.0', 8080), '0.0.0.0:8080', ':8080', 8080
are all considered and treated the same.
Enrolling an identity
openziti.enroll() turns an enrollment token (a JWT file) into an identity.
With controller 2.0 or later an identity can also be enrolled with an external identity provider (OIDC) that the
controller trusts through an ext-jwt-signer. openziti.enroll_with_idp() takes the controller (its URL, or its
network JWT, which is also the trust anchor for a controller with a private CA), an openziti.EnrollMode, and a token:
| mode | the controller... | the identity you get |
|---|---|---|
EnrollMode.NONE |
has the identity already | the bootstrap config: controller and CA only |
EnrollMode.CERT |
signs a certificate request for the token | has a cert and key, logs in on its own |
EnrollMode.TOKEN |
creates the identity for the token | has no cert: the token is its login |
An app that already holds a token needs no browser:
identity = openziti.enroll_with_idp(network_jwt, openziti.EnrollMode.TOKEN, token)
Path('identity.json').write_text(identity)
ztx, err = openziti.load('identity.json') # err is ZitiContext.EXTERNAL_LOGIN_REQUIRED: the token is the login
ztx.login_with_token(token)
ztx.wait_for_auth()
Without a token, pass the name of the signer (or nothing, for the first signer that offers the mode) and say what to
do with the login url. The enrollment then waits for the user to log in, for timeout seconds (300 by default, None
waits for ever):
identity = openziti.enroll_with_idp(controller_url, openziti.EnrollMode.TOKEN, 'my-signer',
on_login_url=lambda url: print('log in at', url), timeout=120)
The same from the command line. --mode is none, cert or token; --idp is a token or a signer name (a signer
name prints the url to open and waits for the login, --timeout seconds, 300 by default, 0 for ever), or the name of
a file that has either:
python -m openziti enroll --jwt enrollment-token.jwt --identity identity.json
python -m openziti enroll --controller "$NETWORK_JWT" --mode token --idp token.txt --identity identity.json
python -m openziti enroll --controller "$NETWORK_JWT" --mode token --idp <(printf %s "$TOKEN") --identity identity.json
python -m openziti enroll --controller https://ctrl.example.com:1280 --mode cert --idp my-signer --identity identity.json
Give a token as a file, as above, and not as --idp "$TOKEN": the process list (ps) shows the command line of a
running command to the other users of the machine. <(...) is bash and zsh: the shell hands the output of printf over
as a file, so the token is neither on the command line nor in a file on disk. A value that names a file is read as one,
so a signer that is named like a file in the current directory cannot be given.
Examples
Try it out yourself with one of our examples
Support
Looking for Help?
Please use these community resources for getting help. We use GitHub issues for tracking bugs and feature requests and have limited bandwidth to address them.
- Read the offical docs
- Join our Developer Community
- Participate in discussion on Discourse
Contributing
Do you want to get your hands dirty and help make OpenZiti better? Contribute to the OpenZiti open-source project through bug reports, bug fixes, documentation, etc. Check out our guide on contributing to our projects here.
License
Explore related projects
Loading…