NitorCreations / vault

Encrypted AWS key-value storage
0 stars 2 forks source link

Add Python implementation with native Rust module #547

Closed Esgrove closed 2 weeks ago

Esgrove commented 3 weeks ago

Adds a separate Python vault version under python-pyo3 that uses the Rust vault internally.

pyo3 is used to create Python bindings for the Rust vault. There is a single lib.rs file that wraps the Rust vault library. It simply has one function for each CLI command that takes parameters from python side and calls the matching rust cli command. Pyo3 has an additional tool maturin which is used to init the project, and handle building and publishing.

The Python side only has the CLI specification that was written from scratch to match the Rust CLI (uses subcommands so all instead of --all). All actual functionality happens in the rust module (including print output for the most part). To enable this, I moved the Rust vault cli.rs to be part of the Rust lib, so it can be reused for python. Otherwise would need to duplicate all that in Python, going against one of the primary goals for this whole thing. This does lead to a bit of code duplication for passing around the arguments since each command creates its own vault instance but this way the communication between rust and python is very simple and kept to a minimun.

There is an experimental-async feature for pyo3 that would enable async support between rust and python, but since we don't actually need to do anything async directly in the CLI, I did this the easy way and just wrapped all rust code inside a separate runtime invocation for each function with a blocking task, so the python side does not need to care about async at all.

Typer does not seem to support giving commands flag aliases like we have in the rust version. Currently it does not have the argument flags at all, so it is not backwards compatible with the old python cli interface.