Skip to content
abedraPublic

About

A C++ library for Hashicorp Vault

Resources

Code of conduct

Stars

46 stars

Watchers

4 watching

Forks

Repository files navigation

libvault

CMake Version

A C++ library for Hashicorp Vault

C++ 17

This project assumes a C++ 17 capable compiler. This includes GCC 8 or higher, and clang 3.8 or higher. Support for lower standards versions of C++ will not be accepted due to inconsistent implementations of optional.

Dependencies

  • cURL
  • Catch2 (test only)

Usage

The following example shows both a secret put and get. The most common scenario is get for most applications and the put will happen outside of the application by another process. Additional examples are located in the example directory.

#include <iostream>
#include <libvault/VaultClient.h>

int main(void)
{
  Vault::HttpErrorCallback httpErrorCallback = [&](std::string err) {
    std::cout << err << std::endl;
  };

  Vault::AppRoleStrategy authStrategy{
    Vault::RoleId{"<role_id>"},
    Vault::SecretId{"<secret_id>"}
  };

  Vault::Config config = Vault::ConfigBuilder().build();
  Vault::Client vaultClient{config, authStrategy, httpErrorCallback};
  Vault::SecretMount mount{"/test"};
  Vault::KeyValue kv{vaultClient, mount};
  Vault::Path key{"hello"};
  Vault::Parameters parameters(
    {
      {"foo","world"},
      {"baz","quux"},
      {"something", "something else"},
    }
  );
  
  kv.create(key, parameters);

  std::cout << kv.read(key).value() << std::endl;
}

Post-Quantum Cryptography

TLS. Vault offers a hybrid post-quantum key exchange (X25519MLKEM768) by default, and libvault uses it whenever the libcurl it is linked against supports it (with OpenSSL, 3.5 or later). Otherwise the connection quietly falls back to a classical key exchange. To refuse that fallback, require the post-quantum group:

auto config = Vault::ConfigBuilder()
                  .withTlsKeyExchangeGroups(
                      Vault::TlsKeyExchangeGroups::POST_QUANTUM)
                  .build();

Requests then fail unless the connection uses X25519MLKEM768. withTlsKeyExchangeGroups takes any colon-separated list of groups your TLS library knows.

PKI. Vault 2.2.0 and later issue ML-DSA certificates in every edition. Pass {"key_type", "ml-dsa"} and a {"parameter_set", "ml-dsa-65"} (or 44, 87) when generating a root or intermediate, or in a role.

Transit. ML-DSA, SLH-DSA and hybrid signing keys need Vault Enterprise. Create them with Transit::createKey and the type and parameter_set (and, for hybrid keys, hybrid_key_type_pqc and hybrid_key_type_ec) parameters, and use the Transit::sign(key, parameters) and verify(key, parameters) overloads, which leave the hash algorithm out of the request.

JSON Serialization

This project uses nlohmann/json internally but does not expose it. This project makes no assumptions about serialization and returns std:string values that can be serialized by the tooling of your choice. Should you choose to use nlohmann/json you can add the json.hpp file to your project. This project's integration tests have multiple examples of how to use it.

Feature Support

The following tables show support for each of the secret backends, auth methods, and system endpoints. Because the surface area is so large, endpoints are implemented as needed. Pull requests are welcome. Feel free to file an issue or submit a pull request for additional support.

Secret Backend Implemented Integration Tested
Active Directory Yes No
Ali Cloud Yes No
AWS Yes No
Azure Yes No
Consul Yes No
Cubbyhole Yes Yes
Cassandra Yes No
Couchbase Yes No
Elasticsearch Yes No
Influxdb Yes No
HanaDB Yes No
MongoDB Yes No
MongoDB Atlas Yes No
MSSQL Yes No
MySQL/MariaDB Yes No
Oracle Yes No
PostgreSQL Yes Partial
Redshift Yes No
Google Cloud Yes No
Google Cloud KMS Yes No
Key Management* Yes No*
KMIP Yes No*
Key/Value V1 Yes Partial
Key/Value V2 Yes Partial
Identity Yes No
MongoDB Atlas Yes No
Nomad Yes No
OpenLDAP Yes No
PKI Yes Partial
RabbitMQ Yes Partial
SSH Yes No
TOTP Yes Yes
Transform Yes No*
Transit Yes Partial

* Requires an enterprise license and cannot be integration tested

Auth Method Implemented Integration Tested
AliCloud Yes No
AppRole Yes Yes
AWS Yes No
Azure Yes No
Cloud Foundry Yes No
GitHub Yes No
Google Cloud Yes No
JWT/OIDC Yes No
Kerberos Yes No
Kubernetes Yes No
LDAP Yes No
OCI Yes No
Okta Yes No
RADIUS Yes No
TLS Certificate Yes No
Tokens Yes Yes
Username/Password Yes No

* Requires an enterprise license and cannot be integration tested

System Backend Implemented Integration Tested
/sys/health Yes Yes
/sys/leader Yes Yes
/sys/auth Yes Yes
/sys/wrapping Yes Yes
/sys/audit Yes No
/sys/audit-hash Yes No
/sys/capabilities Yes No
/sys/capabilities-accessor Yes No
/sys/capabilities-self Yes No
/sys/config/auditing Yes No
/sys/config/control-group Yes No
/sys/config/cors Yes No
/sys/config/state Yes No
/sys/config/ui Yes No
/sys/control-group Yes No
/sys/host-info Yes No
/sys/init Yes No
/sys/internal/counters Yes No
/sys/plugins/reload/backend Yes No
/sys/internal/ui/mounts Yes No
/sys/key-status Yes No
/sys/leases Yes No
/sys/license Yes No
/sys/metrics Yes No
/sys/mounts Yes No
/sys/remount Yes No
/sys/rotate Yes No
/sys/seal Yes No
/sys/seal-status Yes No
/sys/step-down Yes No
/sys/unseal Yes No
/sys/mfa Yes No
/sys/namespaces Yes No*
/sys/generate-root Yes No
/sys/internal/specs/openapi Yes No
/sys/plugins/catalog Yes No
/sys/policy Yes No
/sys/policies Yes No*
/sys/pprof Yes No
/sys/raw Yes No
/sys/sealwrap/rewrap Yes No
/sys/tools Yes No
/sys/rekey No No
/sys/rekey-recovery-key No No
/sys/replication No No
/sys/storage No No

* Requires an enterprise license and cannot be integration tested

Compile and Install

This project uses CMake. To build the library run the following commands from the project root:

mkdir build
cd build
cmake ../
make

Compile Options

The following custom options can be provided to CMake to control your build:

  • LIBVAULT_BUILD_TESTS [ON|OFF] - Build the unit tests. Defaults to ON when libvault is the top-level project and OFF when it is added with add_subdirectory or FetchContent.
  • LIBVAULT_BUILD_INTEGRATION_TESTS [ON|OFF] - [Default OFF] Build the integration tests (requires a configured, running Vault)
  • LIBVAULT_BUILD_ENTERPRISE_TESTS [ON|OFF] - [Default OFF] Build the integration tests that need Vault Enterprise (see Vault Enterprise tests)
  • LIBVAULT_ENABLE_COVERAGE [ON|OFF] - [Default OFF] Enable gcov code coverage
  • LIBVAULT_INSTALL [ON|OFF] - Generate install targets. Defaults to ON when libvault is the top-level project and OFF otherwise.
  • BUILD_SHARED_LIBS [ON|OFF] - [Default ON] Build a shared library

libcurl is required and is always linked. The older option names (ENABLE_TEST, ENABLE_INTEGRATION_TEST, ENABLE_COVERAGE, INSTALL) still work but are deprecated, and LINK_CURL is ignored.

Using libvault

After installing, use it from CMake:

find_package(libvault REQUIRED)
target_link_libraries(your_target PRIVATE libvault::vault)

or with pkg-config:

c++ -std=c++17 main.cpp $(pkg-config --cflags --libs vault)

Either way, include the header as #include <libvault/VaultClient.h>.

libvault can also be built as part of your project with add_subdirectory or FetchContent, linking the same libvault::vault target. In that case the header is included as #include <VaultClient.h>.

Local Development

This project uses a standard C++ development with CMake environment. Code is formatted with clang-format using the checked-in .clang-format, and CI checks it. To keep the formatting commit out of git blame:

git config blame.ignoreRevsFile .git-blame-ignore-revs

The integration tests need a running, configured instance of Vault, plus the Postgres and RabbitMQ services the tests use.

Vault Setup

Note: this requires docker

Start the services and configure Vault:

cd docker && docker compose up
# In another terminal, from the project root
script/bootstrap

script/bootstrap configures Vault and writes the credentials the tests need to .env. Then build and run the integration tests:

. ./.env
cmake -S . -B build -DLIBVAULT_BUILD_INTEGRATION_TESTS=ON
cmake --build build
cd build && ctest

Vault Enterprise tests

A few features, such as post-quantum signing in Transit, need Vault Enterprise. Their tests are off by default and run against a separate Enterprise instance on port 8210, which needs a license:

VAULT_LICENSE=... docker compose -f docker/docker-compose.yml --profile enterprise up -d --wait vault-enterprise
cmake -S . -B build -DLIBVAULT_BUILD_ENTERPRISE_TESTS=ON
cmake --build build
cd build && ctest -R '^Enterprise:'

VAULT_ENTERPRISE_HOST, VAULT_ENTERPRISE_PORT and VAULT_ENTERPRISE_TOKEN point the tests at another Enterprise Vault. Note that they recreate its transit mount. In CI, the enterprise tests job runs only when the repository variable LIBVAULT_ENTERPRISE_TESTS is true and the VAULT_LICENSE secret is set.

Examples

The programs in example are built with -DLIBVAULT_BUILD_EXAMPLES=ON. They expect the Vault setup above and VAULT_ROOT_TOKEN from .env:

. ./.env
cmake -S . -B build -DLIBVAULT_BUILD_EXAMPLES=ON
cmake --build build
build/example/key_value

The jwt example needs OpenSSL and reads public.pem and private.pem from the current directory. Create them with:

openssl genrsa -out private.pem 2048
openssl rsa -pubout -in private.pem -out public.pem

The postgres example needs libpqxx. Examples whose dependencies are missing are skipped.

This setup does not demonstrate a production worthy configuration and should only be used for reference or inside of this project. For your production Vault setup please consult the HashiCorp Vault best practices.

About

A C++ library for Hashicorp Vault

Resources

Code of conduct

Stars

46 stars

Watchers

4 watching

Forks

Releases

Packages

Used by

Contributors

Languages