| 1 |
/* Copyright (c) 2021, 2026, Oracle and/or its affiliates. |
| 2 |
|
| 3 |
This program is free software; you can redistribute it and/or modify |
| 4 |
it under the terms of the GNU General Public License, version 2.0, |
| 5 |
as published by the Free Software Foundation. |
| 6 |
|
| 7 |
This program is designed to work with certain software (including |
| 8 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 9 |
as designated in a particular file or component or in included license |
| 10 |
documentation. The authors of MySQL hereby grant you an additional |
| 11 |
permission to link the program and your derivative works with the |
| 12 |
separately licensed software that they have either included with |
| 13 |
the program or referenced in the documentation. |
| 14 |
|
| 15 |
This program is distributed in the hope that it will be useful, |
| 16 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 |
GNU General Public License, version 2.0, for more details. |
| 19 |
|
| 20 |
You should have received a copy of the GNU General Public License |
| 21 |
along with this program; if not, write to the Free Software |
| 22 |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 23 |
|
| 24 |
#ifndef KEYRING_FILE_BACKEND_INCLUDED |
| 25 |
#define KEYRING_FILE_BACKEND_INCLUDED |
| 26 |
|
| 27 |
#include <string> |
| 28 |
|
| 29 |
#include <components/keyrings/common/json_data/json_writer.h> |
| 30 |
#include <components/keyrings/common/memstore/iterator.h> |
| 31 |
#include <components/keyrings/common/operations/operations.h> |
| 32 |
|
| 33 |
namespace keyring_file { |
| 34 |
namespace backend { |
| 35 |
class Keyring_file_backend final { |
| 36 |
public: |
| 37 |
explicit Keyring_file_backend(const std::string keyring_file_name, |
| 38 |
bool read_only); |
| 39 |
|
| 40 |
~Keyring_file_backend() = default; |
| 41 |
|
| 42 |
/** |
| 43 |
Fetch data |
| 44 |
|
| 45 |
@param [in] metadata Key |
| 46 |
@param [out] data Value |
| 47 |
|
| 48 |
@returns Status of find operation |
| 49 |
@retval false Entry found. Check data. |
| 50 |
@retval true Entry missing. |
| 51 |
*/ |
| 52 |
bool get(const keyring_common::meta::Metadata &metadata, |
| 53 |
keyring_common::data::Data &data) const; |
| 54 |
|
| 55 |
/** |
| 56 |
Store data |
| 57 |
|
| 58 |
@param [in] metadata Key |
| 59 |
@param [in, out] data Value |
| 60 |
|
| 61 |
@returns Status of store operation |
| 62 |
@retval false Entry stored successfully |
| 63 |
@retval true Failure |
| 64 |
*/ |
| 65 |
|
| 66 |
bool store(const keyring_common::meta::Metadata &metadata, |
| 67 |
keyring_common::data::Data &data); |
| 68 |
|
| 69 |
/** |
| 70 |
Erase data located at given key |
| 71 |
|
| 72 |
@param [in] metadata Key |
| 73 |
@param [in] data Value - not used. |
| 74 |
|
| 75 |
@returns Status of erase operation |
| 76 |
@retval false Data deleted |
| 77 |
@retval true Could not find or delete data |
| 78 |
*/ |
| 79 |
bool erase(const keyring_common::meta::Metadata &metadata, |
| 80 |
keyring_common::data::Data &data); |
| 81 |
|
| 82 |
/** |
| 83 |
Generate random data and store it |
| 84 |
|
| 85 |
@param [in] metadata Key |
| 86 |
@param [out] data Generated value |
| 87 |
@param [in] length Length of data to be generated |
| 88 |
|
| 89 |
@returns Status of generate + store operation |
| 90 |
@retval false Data generated and stored successfully |
| 91 |
@retval true Error |
| 92 |
|
| 93 |
*/ |
| 94 |
bool generate(const keyring_common::meta::Metadata &metadata, |
| 95 |
keyring_common::data::Data &data, size_t length); |
| 96 |
|
| 97 |
/** |
| 98 |
Populate cache |
| 99 |
|
| 100 |
@param [in] operations Handle to operations class |
| 101 |
|
| 102 |
@returns status of cache insertion |
| 103 |
@retval false Success |
| 104 |
@retval true Failure |
| 105 |
*/ |
| 106 |
bool load_cache( |
| 107 |
keyring_common::operations::Keyring_operations<Keyring_file_backend> |
| 108 |
&operations); |
| 109 |
|
| 110 |
/** Maximum data length supported */ |
| 111 |
size_t maximum_data_length() const { return 16384; } |
| 112 |
|
| 113 |
/** Get number of elements stored in backend */ |
| 114 |
size_t size() const { return json_writer_.num_elements(); } |
| 115 |
|
| 116 |
/** Validity */ |
| 117 |
bool valid() const { return valid_; } |
| 118 |
|
| 119 |
private: |
| 120 |
/** |
| 121 |
Write existing data to file. |
| 122 |
This function overwrites existing data stored in the file. |
| 123 |
|
| 124 |
@returns Status of write operation. |
| 125 |
@retval false Successfully written data to file |
| 126 |
@retval true Error writing data to file |
| 127 |
*/ |
| 128 |
bool write_to_file(); |
| 129 |
|
| 130 |
/** Create data file if missing */ |
| 131 |
void create_file_if_missing(std::string file_name); |
| 132 |
|
| 133 |
private: |
| 134 |
/** Keyring file */ |
| 135 |
std::string keyring_file_name_; |
| 136 |
|
| 137 |
/** Read only flag */ |
| 138 |
bool read_only_; |
| 139 |
|
| 140 |
/** In memory cache for keyring data */ |
| 141 |
keyring_common::json_data::Json_writer json_writer_; |
| 142 |
|
| 143 |
/** Validity */ |
| 144 |
bool valid_; |
| 145 |
}; |
| 146 |
} // namespace backend |
| 147 |
} // namespace keyring_file |
| 148 |
|
| 149 |
#endif // !KEYRING_FILE_BACKEND_INCLUDED |