|
| 1 | +// Copyright (c) 2026 VillageSQL Contributors |
| 2 | +// |
| 3 | +// This program is free software; you can redistribute it and/or |
| 4 | +// modify it under the terms of the GNU General Public License |
| 5 | +// as published by the Free Software Foundation; either version 2 |
| 6 | +// of the License, or (at your option) any later version. |
| 7 | +// |
| 8 | +// This program is distributed in the hope that it will be useful, |
| 9 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | +// GNU General Public License for more details. |
| 12 | +// |
| 13 | +// You should have received a copy of the GNU General Public License |
| 14 | +// along with this program; if not, see <https://www.gnu.org/licenses/>. |
| 15 | + |
| 16 | +#ifndef VILLAGESQL_PREVIEW_KEYRING_H |
| 17 | +#define VILLAGESQL_PREVIEW_KEYRING_H |
| 18 | + |
| 19 | +#include <string> |
| 20 | +#include <string_view> |
| 21 | + |
| 22 | +#include <villagesql/abi/preview/keyring.h> |
| 23 | +#include <villagesql/detail/capability_hash.h> |
| 24 | +#include <villagesql/vsql/extension_builder.h> |
| 25 | + |
| 26 | +namespace vsql::preview::keyring { |
| 27 | + |
| 28 | +// C++ wrapper around vef_preview_keyring_t. |
| 29 | +// |
| 30 | +// Usage: |
| 31 | +// static auto g_keyring = vsql::preview::keyring::make_capability(); |
| 32 | +// |
| 33 | +// // In extension code: |
| 34 | +// auto result = g_keyring.read("my-key", "", value); |
| 35 | +// |
| 36 | +// Register with: |
| 37 | +// make_extension().with<preview_keyring<g_keyring>>() |
| 38 | +class Capability { |
| 39 | + public: |
| 40 | + static constexpr const char *kName = VEF_PREVIEW_KEYRING_NAME; |
| 41 | + |
| 42 | + // Read a secret from the MySQL keyring component into value. |
| 43 | + // auth_id may be empty to read internal keys. |
| 44 | + // Returns VEF_KEYRING_OK on success, VEF_KEYRING_NOT_FOUND if the key does |
| 45 | + // not exist, VEF_KEYRING_UNAVAILABLE if no keyring component is installed, |
| 46 | + // or VEF_KEYRING_ERROR on other failures. |
| 47 | + vef_keyring_result_t read(std::string_view data_id, std::string_view auth_id, |
| 48 | + std::string &value) const { |
| 49 | + if (abi_.read == nullptr) return VEF_KEYRING_UNAVAILABLE; |
| 50 | + value.resize(4096); |
| 51 | + size_t out_len = 0; |
| 52 | + vef_keyring_result_t result = |
| 53 | + abi_.read(data_id.data(), auth_id.empty() ? nullptr : auth_id.data(), |
| 54 | + reinterpret_cast<unsigned char *>(value.data()), value.size(), |
| 55 | + &out_len); |
| 56 | + if (result == VEF_KEYRING_OK) value.resize(out_len); |
| 57 | + return result; |
| 58 | + } |
| 59 | + |
| 60 | + // Write a secret to the MySQL keyring component. |
| 61 | + // auth_id may be empty to store as an internal key. |
| 62 | + // Returns VEF_KEYRING_OK on success, VEF_KEYRING_UNAVAILABLE if no keyring |
| 63 | + // component is installed, or VEF_KEYRING_ERROR on other failures. |
| 64 | + vef_keyring_result_t write(std::string_view data_id, std::string_view auth_id, |
| 65 | + std::string_view data) const { |
| 66 | + if (abi_.write == nullptr) return VEF_KEYRING_UNAVAILABLE; |
| 67 | + return abi_.write( |
| 68 | + data_id.data(), auth_id.empty() ? nullptr : auth_id.data(), |
| 69 | + reinterpret_cast<const unsigned char *>(data.data()), data.size()); |
| 70 | + } |
| 71 | + |
| 72 | + bool available() const { return abi_.read != nullptr; } |
| 73 | + |
| 74 | + // Public so that cap_receive() can access abi_ via a pointer. |
| 75 | + // Do not access directly — use read(), write(), and available() instead. |
| 76 | + vef_preview_keyring_t abi_; |
| 77 | +}; |
| 78 | + |
| 79 | +inline Capability make_capability() { return Capability{}; } |
| 80 | + |
| 81 | +} // namespace vsql::preview::keyring |
| 82 | + |
| 83 | +namespace vsql::preview { |
| 84 | + |
| 85 | +// Traits type for registering the keyring capability via |
| 86 | +// .with<preview_keyring<cap>>. Only available when this header is included. |
| 87 | +template <auto &cap> |
| 88 | +struct preview_keyring { |
| 89 | + template <typename Inner> |
| 90 | + static constexpr auto bind(Inner builder) { |
| 91 | + using Cap = keyring::Capability; |
| 92 | + return builder.required_capability( |
| 93 | + {Cap::kName, &::vsql::cap_receive<Cap, &cap>, |
| 94 | + ::villagesql::detail::abi_type_hash<decltype(cap.abi_)>()}); |
| 95 | + } |
| 96 | +}; |
| 97 | + |
| 98 | +} // namespace vsql::preview |
| 99 | + |
| 100 | +#endif // VILLAGESQL_PREVIEW_KEYRING_H |
0 commit comments