immudb-py


Nameimmudb-py JSON
Version 1.5.0 PyPI version JSON
download
home_pagehttps://github.com/codenotary/immudb-py
SummaryPython SDK for Immudb
upload_time2024-07-29 10:15:45
maintainerNone
docs_urlNone
authorCodenotary
requires_python>=3.8
licenseApache License Version 2.0
keywords immudb immutable
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI
coveralls test coverage
            # immudb-py [![License](https://img.shields.io/github/license/codenotary/immudb4j)](LICENSE)

[![CI](https://github.com/codenotary/immudb-py/actions/workflows/ci.yml/badge.svg)](https://github.com/codenotary/immudb-py/actions/workflows/ci.yml)
[![Coverage Status](https://coveralls.io/repos/github/codenotary/immudb-py/badge.svg?branch=master)](https://coveralls.io/github/codenotary/immudb-py?branch=master)[![Slack](https://img.shields.io/badge/join%20slack-%23immutability-brightgreen.svg)](https://slack.vchain.us/)
[![Discuss at immudb@googlegroups.com](https://img.shields.io/badge/discuss-immudb%40googlegroups.com-blue.svg)](https://groups.google.com/group/immudb)

## Official [immudb] client for Python.

### Try it on the immudb Playground!

[![screenshot](playground.png)](https://play.codenotary.com)

[immudb]: https://immudb.io

## Contents

- [Introduction](#introduction)
- [Prerequisites](#prerequisites)
- [Installation](#installation)
- [Supported Versions](#supported-versions)
- [Quickstart](#quickstart)
- [Step by step guide](#step-by-step-guide)
- [Creating a Client](#creating-a-client)

## Introduction

immu-py implements a [grpc] immudb client. A minimalist API is exposed for applications while cryptographic
verifications and state update protocol implementation are fully implemented by this client.
Latest validated immudb state may be kept in the local filesystem when using default `rootService`,
please read [immudb research paper] for details of how immutability is ensured by [immudb].

[grpc]: https://grpc.io/
[immudb research paper]: https://immudb.io/
[immudb]: https://immudb.io/

## Prerequisites

immu-py assumes there is an existing instance of the immudb server up and running. 
Running `immudb` is quite simple, please refer to the
following link for downloading and running it: https://immudb.io/docs/quickstart.html

immudb-py requires python version 3.6 or greater.
If you are using 3.6, you'll need dataclasses package; on 3.7+,
dataclasses is part of the python distribution.

## Installation

You can install latest version cloning this repository, and then use the make command to install
prerequisites and the package itself:

```shell
    make init
    make install
```

Or, you can install latest stable version using pip:

```shell
    pip3 install immudb-py
```

Then, in you code, import the client library as as follows:

```python
    from immudb import ImmudbClient
```

*Note*: immudb-py need `grpcio` module from google. On Alpine linux, you need
 these packages in order to correctly build (and install) grpcio:
 - `linux-headers`
 - `python3-dev`
 - `g++`

## Supported Versions

immu-py supports the [latest immudb release].

[latest immudb release]: https://github.com/codenotary/immudb/releases/tag/v0.9.0

## Quickstart

[Hello Immutable World!] example can be found in `immudb-client-examples` repo.

[Hello Immutable World!]: https://github.com/codenotary/immudb-client-examples/tree/master/python

## Step by step guide

### Creating a Client

The following code snippets shows how to create a client.

Using default configuration:

```python
    client = ImmudbClient()
```

Setting `immudb` url and port:

```python

    client = ImmudbClient("mycustomurl:someport")
    client = ImmudbClient("10.105.20.32:8899")
```

### User sessions

Use `login` and `logout` methods to initiate and terminate user sessions:

```python
    client.login("usr1", "pwd1");

    // Interact with immudb using logged user

    client.logout();
```
### Encoding

Please note that, in order to provide maximum flexibility, all functions accept byte arrays as parameters.
Therefore, unicode strings must be properly encoded.
It is possible to store structured objects, but they must be serialized (e.g., with pickle or json).

### Creating a database

Creating a new database is quite simple:

```python
    client.createDatabase(b"db1");
```

### Setting the active database

Specify the active database with:

```python
    client.useDatabase(b"db1");
```
If not specified, the default databased used is "defaultdb".

### Traditional read and write

immudb provides read and write operations that behave as a traditional
key-value store i.e. no cryptographic verification is done. This operations
may be used when validations can be post-poned:

```python
    client.set(b"k123", b"value123");
    result = client.get(b"k123");
```

### Verified read and write

immudb provides built-in cryptographic verification for any entry. The client
implements the mathematical validations while the application uses as a traditional
read or write operation:

```python
    try:
        client.verifiedSet(b"k123", b"v123");
        results = client.verifiedGet(b"k123");
    Except VerificationException as e:
        # Do something
```

### Multi-key read and write

Transactional multi-key read and write operations are supported by immudb and immudb-py.
Atomic multi-key write (all entries are persisted or none):

```python
    normal_dictionary = {b"key1": b"value1", b"key2": b"value2"}
    client.setAll(normal_dictionary);
```

Atomic multi-key read (all entries are retrieved or none):

```python
    normal_dictionary = {b"key1": b"value1", b"key2": b"value2"}
    results_dictionary = client.getAll(normal_dictionary.keys())
    # Or manually
    client.get([b"key1", b"key2"])
```
## User management
Users can be added and granted access to databases.

### Adding a user
The ```createUser``` functions create a new users and grants the specified permission to a database.
```python
user='newuser'
password='Pw1:pasdfoiu'
permission=immudb.constants.PERMISSION_RW
database='defaultdb'

client.createUser(user, password, permission, database)
```

The database must exists at the time the user is created. The password must be between 8 and 32 characters in length, and must have at least one upper case letter, a symbol and a digit.

Permission are defined in immudb.constants and are:

- `PERMISSION_SYS_ADMIN`
- `PERMISSION_ADMIN`
- `PERMISSION_NONE`
- `PERMISSION_R`
- `PERMISSION_RW`

### Changin password
The user must must provide both old and new password:
```python
newPassword="pW1:a0s98d7gfy"
resp=client.changePassword(user, newPassword, oldPassword)
```
It is applied the same password policy of user creation.

### User list

To get the list of user created on immudb, simply call ```listUsers```:
```python
resp=client.listUsers()
print(users.userlist.users)
```

### Closing the client

To programatically close the connection with immudb server use the `shutdown` operation:

```python
    client.shutdown();
```

Note: after shutdown, a new client needs to be created to establish a new connection.

## State persistance

An important immudb feature is the ability for a client to check every transaction for tampering. In order to 
be able to do that, it is necessary to persist client state (i.e., save it to disk) so that if some tampering 
on the server happens between two runs, it is immediatly detected.

A `RootService` implements just that: it stores immudb client after every transaction, in order to be able to
use it afterward to check the server correctness.

### Using the Persistent Root Service

The default RootService, for simplicity, commits the state to RAM, and so it is unsuitable for real time safe
application. To have persistance, the application must instantiate a `PersistentRootService` object, which stores
its state to disk.

Let's see a simple example that uses state persistance:

```python
from immudb.client import ImmudbClient, PersistentRootService
client=ImmudbClient(rs=PersistentRootService())
client.login(username="immudb", password="immudb")
client.verifiedTxById(42)
client.verifiedGet(b"example")
```

In this example, the Root Service is saved to the disk after every verified transaction. As you can see, it is very
easy to use. Just create and use the PersistentRootService object in the client initialization.

### Process and threads

Please keep in mind that the implementation is not thread/process safe. If you are using a multi-process application,
it is advisable to use a different state file for every instance: just pass the filename as argument to the 
PersistentRootService constructor:

```python
client = ImmudbClient(rs=PersistentRootService("rootfilename"))
```

Default rootfile is "~/.immudbRoot"

If needed/wanted, it is also easy to extend the default implementation adding synchronization primitives to the get/set methods.
In this way, more than one immudb client can share the same PersistentRootService instance without interering each other.

## Cryptographic state signing

To increase safety, it is possible to generate a private key and use it to sign every verification response. Clients can
then use the corresponding public key to check for response correctness.

### Key generation
You can use `openssl` to create a private key, and then extract the public key:
```sh
openssl ecparam -name prime256v1 -genkey -noout -out private_signing_key.pem
openssl ec -in private_signing_key.pem -pubout -out public_signing_key.pem
```

### Key usage (server side)
On immudb server, use `--signingKey private_signing_key.pem` to activate cryptographic signature.

### Key usage (client/SDK side)

On immudb python SDK, just pass the public key filename to the ImmudbClient constructor:
```python
client=ImmudbClient(publicKeyFile="/certs/public_signing_key.pem")
```
Every transaction will be then automatically checked. An exception is thrown if the cryptographic check fails.

## Contributing

We welcome contributions. Feel free to join the team!

To report bugs or get help, use [GitHub's issues].

[GitHub's issues]: https://github.com/codenotary/immudb-py/issues



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/codenotary/immudb-py",
    "name": "immudb-py",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": "immudb, immutable",
    "author": "Codenotary",
    "author_email": null,
    "download_url": "https://files.pythonhosted.org/packages/8c/84/7de9b8c7b9ea2eae9c57370de9c1503fcf85f9aee702689d2b9857b6e5df/immudb-py-1.5.0.tar.gz",
    "platform": null,
    "description": "# immudb-py [![License](https://img.shields.io/github/license/codenotary/immudb4j)](LICENSE)\n\n[![CI](https://github.com/codenotary/immudb-py/actions/workflows/ci.yml/badge.svg)](https://github.com/codenotary/immudb-py/actions/workflows/ci.yml)\n[![Coverage Status](https://coveralls.io/repos/github/codenotary/immudb-py/badge.svg?branch=master)](https://coveralls.io/github/codenotary/immudb-py?branch=master)[![Slack](https://img.shields.io/badge/join%20slack-%23immutability-brightgreen.svg)](https://slack.vchain.us/)\n[![Discuss at immudb@googlegroups.com](https://img.shields.io/badge/discuss-immudb%40googlegroups.com-blue.svg)](https://groups.google.com/group/immudb)\n\n## Official [immudb] client for Python.\n\n### Try it on the immudb Playground!\n\n[![screenshot](playground.png)](https://play.codenotary.com)\n\n[immudb]: https://immudb.io\n\n## Contents\n\n- [Introduction](#introduction)\n- [Prerequisites](#prerequisites)\n- [Installation](#installation)\n- [Supported Versions](#supported-versions)\n- [Quickstart](#quickstart)\n- [Step by step guide](#step-by-step-guide)\n- [Creating a Client](#creating-a-client)\n\n## Introduction\n\nimmu-py implements a [grpc] immudb client. A minimalist API is exposed for applications while cryptographic\nverifications and state update protocol implementation are fully implemented by this client.\nLatest validated immudb state may be kept in the local filesystem when using default `rootService`,\nplease read [immudb research paper] for details of how immutability is ensured by [immudb].\n\n[grpc]: https://grpc.io/\n[immudb research paper]: https://immudb.io/\n[immudb]: https://immudb.io/\n\n## Prerequisites\n\nimmu-py assumes there is an existing instance of the immudb server up and running. \nRunning `immudb` is quite simple, please refer to the\nfollowing link for downloading and running it: https://immudb.io/docs/quickstart.html\n\nimmudb-py requires python version 3.6 or greater.\nIf you are using 3.6, you'll need dataclasses package; on 3.7+,\ndataclasses is part of the python distribution.\n\n## Installation\n\nYou can install latest version cloning this repository, and then use the make command to install\nprerequisites and the package itself:\n\n```shell\n    make init\n    make install\n```\n\nOr, you can install latest stable version using pip:\n\n```shell\n    pip3 install immudb-py\n```\n\nThen, in you code, import the client library as as follows:\n\n```python\n    from immudb import ImmudbClient\n```\n\n*Note*: immudb-py need `grpcio` module from google. On Alpine linux, you need\n these packages in order to correctly build (and install) grpcio:\n - `linux-headers`\n - `python3-dev`\n - `g++`\n\n## Supported Versions\n\nimmu-py supports the [latest immudb release].\n\n[latest immudb release]: https://github.com/codenotary/immudb/releases/tag/v0.9.0\n\n## Quickstart\n\n[Hello Immutable World!] example can be found in `immudb-client-examples` repo.\n\n[Hello Immutable World!]: https://github.com/codenotary/immudb-client-examples/tree/master/python\n\n## Step by step guide\n\n### Creating a Client\n\nThe following code snippets shows how to create a client.\n\nUsing default configuration:\n\n```python\n    client = ImmudbClient()\n```\n\nSetting `immudb` url and port:\n\n```python\n\n    client = ImmudbClient(\"mycustomurl:someport\")\n    client = ImmudbClient(\"10.105.20.32:8899\")\n```\n\n### User sessions\n\nUse `login` and `logout` methods to initiate and terminate user sessions:\n\n```python\n    client.login(\"usr1\", \"pwd1\");\n\n    // Interact with immudb using logged user\n\n    client.logout();\n```\n### Encoding\n\nPlease note that, in order to provide maximum flexibility, all functions accept byte arrays as parameters.\nTherefore, unicode strings must be properly encoded.\nIt is possible to store structured objects, but they must be serialized (e.g., with pickle or json).\n\n### Creating a database\n\nCreating a new database is quite simple:\n\n```python\n    client.createDatabase(b\"db1\");\n```\n\n### Setting the active database\n\nSpecify the active database with:\n\n```python\n    client.useDatabase(b\"db1\");\n```\nIf not specified, the default databased used is \"defaultdb\".\n\n### Traditional read and write\n\nimmudb provides read and write operations that behave as a traditional\nkey-value store i.e. no cryptographic verification is done. This operations\nmay be used when validations can be post-poned:\n\n```python\n    client.set(b\"k123\", b\"value123\");\n    result = client.get(b\"k123\");\n```\n\n### Verified read and write\n\nimmudb provides built-in cryptographic verification for any entry. The client\nimplements the mathematical validations while the application uses as a traditional\nread or write operation:\n\n```python\n    try:\n        client.verifiedSet(b\"k123\", b\"v123\");\n        results = client.verifiedGet(b\"k123\");\n    Except VerificationException as e:\n        # Do something\n```\n\n### Multi-key read and write\n\nTransactional multi-key read and write operations are supported by immudb and immudb-py.\nAtomic multi-key write (all entries are persisted or none):\n\n```python\n    normal_dictionary = {b\"key1\": b\"value1\", b\"key2\": b\"value2\"}\n    client.setAll(normal_dictionary);\n```\n\nAtomic multi-key read (all entries are retrieved or none):\n\n```python\n    normal_dictionary = {b\"key1\": b\"value1\", b\"key2\": b\"value2\"}\n    results_dictionary = client.getAll(normal_dictionary.keys())\n    # Or manually\n    client.get([b\"key1\", b\"key2\"])\n```\n## User management\nUsers can be added and granted access to databases.\n\n### Adding a user\nThe ```createUser``` functions create a new users and grants the specified permission to a database.\n```python\nuser='newuser'\npassword='Pw1:pasdfoiu'\npermission=immudb.constants.PERMISSION_RW\ndatabase='defaultdb'\n\nclient.createUser(user, password, permission, database)\n```\n\nThe database must exists at the time the user is created. The password must be between 8 and 32 characters in length, and must have at least one upper case letter, a symbol and a digit.\n\nPermission are defined in immudb.constants and are:\n\n- `PERMISSION_SYS_ADMIN`\n- `PERMISSION_ADMIN`\n- `PERMISSION_NONE`\n- `PERMISSION_R`\n- `PERMISSION_RW`\n\n### Changin password\nThe user must must provide both old and new password:\n```python\nnewPassword=\"pW1:a0s98d7gfy\"\nresp=client.changePassword(user, newPassword, oldPassword)\n```\nIt is applied the same password policy of user creation.\n\n### User list\n\nTo get the list of user created on immudb, simply call ```listUsers```:\n```python\nresp=client.listUsers()\nprint(users.userlist.users)\n```\n\n### Closing the client\n\nTo programatically close the connection with immudb server use the `shutdown` operation:\n\n```python\n    client.shutdown();\n```\n\nNote: after shutdown, a new client needs to be created to establish a new connection.\n\n## State persistance\n\nAn important immudb feature is the ability for a client to check every transaction for tampering. In order to \nbe able to do that, it is necessary to persist client state (i.e., save it to disk) so that if some tampering \non the server happens between two runs, it is immediatly detected.\n\nA `RootService` implements just that: it stores immudb client after every transaction, in order to be able to\nuse it afterward to check the server correctness.\n\n### Using the Persistent Root Service\n\nThe default RootService, for simplicity, commits the state to RAM, and so it is unsuitable for real time safe\napplication. To have persistance, the application must instantiate a `PersistentRootService` object, which stores\nits state to disk.\n\nLet's see a simple example that uses state persistance:\n\n```python\nfrom immudb.client import ImmudbClient, PersistentRootService\nclient=ImmudbClient(rs=PersistentRootService())\nclient.login(username=\"immudb\", password=\"immudb\")\nclient.verifiedTxById(42)\nclient.verifiedGet(b\"example\")\n```\n\nIn this example, the Root Service is saved to the disk after every verified transaction. As you can see, it is very\neasy to use. Just create and use the PersistentRootService object in the client initialization.\n\n### Process and threads\n\nPlease keep in mind that the implementation is not thread/process safe. If you are using a multi-process application,\nit is advisable to use a different state file for every instance: just pass the filename as argument to the \nPersistentRootService constructor:\n\n```python\nclient = ImmudbClient(rs=PersistentRootService(\"rootfilename\"))\n```\n\nDefault rootfile is \"~/.immudbRoot\"\n\nIf needed/wanted, it is also easy to extend the default implementation adding synchronization primitives to the get/set methods.\nIn this way, more than one immudb client can share the same PersistentRootService instance without interering each other.\n\n## Cryptographic state signing\n\nTo increase safety, it is possible to generate a private key and use it to sign every verification response. Clients can\nthen use the corresponding public key to check for response correctness.\n\n### Key generation\nYou can use `openssl` to create a private key, and then extract the public key:\n```sh\nopenssl ecparam -name prime256v1 -genkey -noout -out private_signing_key.pem\nopenssl ec -in private_signing_key.pem -pubout -out public_signing_key.pem\n```\n\n### Key usage (server side)\nOn immudb server, use `--signingKey private_signing_key.pem` to activate cryptographic signature.\n\n### Key usage (client/SDK side)\n\nOn immudb python SDK, just pass the public key filename to the ImmudbClient constructor:\n```python\nclient=ImmudbClient(publicKeyFile=\"/certs/public_signing_key.pem\")\n```\nEvery transaction will be then automatically checked. An exception is thrown if the cryptographic check fails.\n\n## Contributing\n\nWe welcome contributions. Feel free to join the team!\n\nTo report bugs or get help, use [GitHub's issues].\n\n[GitHub's issues]: https://github.com/codenotary/immudb-py/issues\n\n\n",
    "bugtrack_url": null,
    "license": "Apache License Version 2.0",
    "summary": "Python SDK for Immudb",
    "version": "1.5.0",
    "project_urls": {
        "Homepage": "https://github.com/codenotary/immudb-py"
    },
    "split_keywords": [
        "immudb",
        " immutable"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "d308cc021d80e1ddc49b311c7eebe414b4e96609d513d2d72c6de538f45a786f",
                "md5": "28ac0347c12664269103f31457013150",
                "sha256": "0e454be5af949fd993d1d52ffecb8b5c2ffbfa9852ea91ef462f383a6d2b2697"
            },
            "downloads": -1,
            "filename": "immudb_py-1.5.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "28ac0347c12664269103f31457013150",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 102290,
            "upload_time": "2024-07-29T10:15:42",
            "upload_time_iso_8601": "2024-07-29T10:15:42.808598Z",
            "url": "https://files.pythonhosted.org/packages/d3/08/cc021d80e1ddc49b311c7eebe414b4e96609d513d2d72c6de538f45a786f/immudb_py-1.5.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "8c847de9b8c7b9ea2eae9c57370de9c1503fcf85f9aee702689d2b9857b6e5df",
                "md5": "c6069dd423bd494ed5981a9631921fa3",
                "sha256": "b204b296654fee7b1c305174bc63384f23e7ab6bbbf840fc920be3dfd83f92df"
            },
            "downloads": -1,
            "filename": "immudb-py-1.5.0.tar.gz",
            "has_sig": false,
            "md5_digest": "c6069dd423bd494ed5981a9631921fa3",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 68741,
            "upload_time": "2024-07-29T10:15:45",
            "upload_time_iso_8601": "2024-07-29T10:15:45.160355Z",
            "url": "https://files.pythonhosted.org/packages/8c/84/7de9b8c7b9ea2eae9c57370de9c1503fcf85f9aee702689d2b9857b6e5df/immudb-py-1.5.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-07-29 10:15:45",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "codenotary",
    "github_project": "immudb-py",
    "travis_ci": true,
    "coveralls": true,
    "github_actions": true,
    "requirements": [],
    "lcname": "immudb-py"
}
        
Elapsed time: 0.59754s