An asynchronous Rust client library for the Hashicorp Vault API
The following features are currently supported:
See something missing? Open an issue.
Add vaultrs
as a dependency to your cargo.toml:
toml
[dependencies]
vaultrs = "0.6.0"
The client is used to configure the connection to Vault and is required to be passed to all API calls for execution. Behind the scenes it uses an asynchronous client from Reqwest for communicating to Vault.
```rust use vaultrs::client::{VaultClient, VaultClientSettingsBuilder};
// Create a client let client = VaultClient::new( VaultClientSettingsBuilder::default() .address("https://127.0.0.1:8200") .token("TOKEN") .build() .unwrap() ).unwrap(); ```
The library currently supports all operations available for version 2 of the key/value store.
```rust use serde::{Deserialize, Serialize}; use vaultrs::kv2;
// Create and read secrets
struct MySecret { key: String, password: String, }
let secret = MySecret { key: "super".tostring(), password: "secret".tostring(), }; kv2::set( &client, "secret", "mysecret", &secret, ).await;
let secret: MySecret = kv2::read(&client, "secret", "mysecret").await.unwrap(); println!("{}", secret.password) // "secret" ```
The library currently supports all operations available for the PKI secrets engine.
```rust use vaultrs::api::pki::requests::GenerateCertificateRequest; use vaultrs::pki::cert;
// Generate a certificate using the PKI backend let cert = cert::generate( &client, "pki", "myrole", Some(GenerateCertificateRequest::builder().commonname("test.com")), ).await.unwrap(); println!("{}", cert.certificate) // "{PEM encoded certificate}" ```
All requests implement the ability to be wrapped. These can be passed in your application internally before being unwrapped.
```rust use vaultrs::api::ResponseWrapper; use vaultrs::api::sys::requests::ListMountsRequest;
let endpoint = ListMountsRequest::builder().build().unwrap(); let wrapresp = endpoint.wrap(&client).await; // Wrapped response assert!(wrapresp.is_ok());
let wrapresp = wrapresp.unwrap(); // Unwrap Result<> let info = wrapresp.lookup(&client).await; // Check status of this wrapped response assert!(info.isok());
let unwrapresp = wrapresp.unwrap(&client).await; // Unwrap the response assert!(unwrapresp.isok());
let info = wrapresp.lookup(&client).await; // Error: response already unwrapped assert!(info.iserr()); ```
All errors generated by this crate are wrapped in the ClientError
enum
provided by the crate. API warnings are automatically captured via tracing
and
API errors are captured and returned as their own variant. Connection related
errors from rustify
are wrapped and returned as a single variant.
All top level API operations are instrumented with tracing
's #[instrument]
attribute.
See the the tests directory for tests. Run tests with cargo test
.
Note: All tests rely on bringing up a local Vault development server using Docker. In order to run tests Docker must be running locally (Docker Desktop works).
Check out the issues for items needing attention or submit your own and then:
See CONTRIBUTING for extensive documentation on the architecture of this library and how to add additional functionality to it.