MySQL 9.0.0
Source Code Documentation
keyring_reader_service_impl_template.h
Go to the documentation of this file.
1/* Copyright (c) 2021, 2024, Oracle and/or its affiliates.
2
3This program is free software; you can redistribute it and/or modify
4it under the terms of the GNU General Public License, version 2.0,
5as published by the Free Software Foundation.
6
7This program is designed to work with certain software (including
8but not limited to OpenSSL) that is licensed under separate terms,
9as designated in a particular file or component or in included license
10documentation. The authors of MySQL hereby grant you an additional
11permission to link the program and your derivative works with the
12separately licensed software that they have either included with
13the program or referenced in the documentation.
14
15This program is distributed in the hope that it will be useful,
16but WITHOUT ANY WARRANTY; without even the implied warranty of
17MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18GNU General Public License, version 2.0, for more details.
19
20You should have received a copy of the GNU General Public License
21along with this program; if not, write to the Free Software
22Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef KEYRING_READER_SERVICE_IMPL_TEMPLATE_INCLUDED
25#define KEYRING_READER_SERVICE_IMPL_TEMPLATE_INCLUDED
26
27#include <functional> /* std::function */
28#include <sstream>
29
30#include <my_dbug.h>
31#include <mysql/components/services/log_builtins.h> /* LogComponentErr */
32#include <mysqld_error.h>
33
39
44
46
47/**
48 Initialize reader
49
50 @param [in] data_id Data Identifier
51 @param [in] auth_id Authorization ID
52 @param [out] it Iterator
53 @param [in] keyring_operations Reference to the object
54 that handles cache and backend
55 @param [in] callbacks Handle to component specific callbacks
56
57 @returns status of the operation
58 @retval -1 Keyring error. reader_object will not be created.
59 @retval 0 Key not found OR error fetching keys.
60 reader_object will not be created.
61 @retval 1 Key found, check out parameters
62*/
63template <typename Backend, typename Data_extension = data::Data>
65 const char *data_id, const char *auth_id,
68 Component_callbacks &callbacks) {
69 try {
70 if (!callbacks.keyring_initialized()) {
71 LogComponentErr(INFORMATION_LEVEL,
72 ER_NOTE_KEYRING_COMPONENT_NOT_INITIALIZED);
73 return -1;
74 }
75
76 if (data_id == nullptr || !*data_id) {
77 LogComponentErr(INFORMATION_LEVEL,
78 ER_NOTE_KEYRING_COMPONENT_EMPTY_DATA_ID);
79 assert(false);
80 return 0;
81 }
82
83 Metadata metadata(data_id, auth_id);
84 if (keyring_operations.init_read_iterator(it, metadata)) {
85 LogComponentErr(INFORMATION_LEVEL,
86 ER_NOTE_KEYRING_COMPONENT_KEY_READ_ITERATOR_INIT_FAILED);
87 return 0;
88 }
89
90 if (!keyring_operations.is_valid(it)) {
91 LogComponentErr(INFORMATION_LEVEL,
92 ER_NOTE_KEYRING_COMPONENT_READ_DATA_NOT_FOUND, data_id,
93 (auth_id == nullptr || !*auth_id) ? "NULL" : auth_id);
94 keyring_operations.deinit_forward_iterator(it);
95 return 0;
96 }
97
98 return 1;
99 } catch (...) {
100 LogComponentErr(ERROR_LEVEL, ER_KEYRING_COMPONENT_EXCEPTION, "init",
101 "keyring_reader_with_status");
102 return -1;
103 }
104}
105
106/**
107 Deinitialize reader
108
109 @param [in, out] it Iterator
110 @param [in] keyring_operations Reference to the object
111 that handles cache and backend
112 @param [in] callbacks Handle to component specific callbacks
113
114 @returns status of the operation
115 @retval false Success
116 @retval true Failure
117*/
118
119template <typename Backend, typename Data_extension = data::Data>
123 Component_callbacks &callbacks) {
124 try {
125 if (!callbacks.keyring_initialized()) {
126 LogComponentErr(INFORMATION_LEVEL,
127 ER_NOTE_KEYRING_COMPONENT_NOT_INITIALIZED);
128 return true;
129 }
130 keyring_operations.deinit_forward_iterator(it);
131 return false;
132 } catch (...) {
133 LogComponentErr(ERROR_LEVEL, ER_KEYRING_COMPONENT_EXCEPTION, "deinit",
134 "keyring_reader_with_status");
135 return true;
136 }
137}
138
139/**
140 Fetch length of the data
141
142 @param [in] it Iterator
143 @param [out] data_size Size of fetched data
144 @param [out] data_type_size Size of data type
145 @param [in] keyring_operations Reference to the object
146 that handles cache and backend
147 @param [in] callbacks Handle to component specific callbacks
148 @returns status of the operation
149 @retval false Success
150 @retval true Failure
151*/
152template <typename Backend, typename Data_extension = data::Data>
154 std::unique_ptr<Iterator<Data_extension>> &it, size_t *data_size,
155 size_t *data_type_size,
157 Component_callbacks &callbacks) {
158 try {
159 if (!callbacks.keyring_initialized()) {
160 LogComponentErr(INFORMATION_LEVEL,
161 ER_NOTE_KEYRING_COMPONENT_NOT_INITIALIZED);
162 return true;
163 }
164
165 if (data_size == nullptr || data_type_size == nullptr) {
166 assert(false);
167 return true;
168 }
169
170 Data_extension data;
171 Metadata metadata;
172 if (keyring_operations.get_iterator_data(it, metadata, data)) {
173 LogComponentErr(INFORMATION_LEVEL,
174 ER_NOTE_KEYRING_COMPONENT_KEY_READ_ITERATOR_FETCH_FAILED);
175 return true;
176 }
177
178 *data_size = data.data().length();
179 *data_type_size = data.type().length();
180 return false;
181 } catch (...) {
182 LogComponentErr(ERROR_LEVEL, ER_KEYRING_COMPONENT_EXCEPTION, "fetch_length",
183 "keyring_reader_with_status");
184 return true;
185 }
186}
187
188/**
189 Fetches data from keyring
190
191 @param [in] it Iterator
192 @param [out] data_buffer Out buffer for data
193 @param [in] data_buffer_length Length of out buffer
194 @param [out] data_size Size of fetched data
195 @param [out] data_type_buffer Type of data
196 @param [in] data_type_buffer_length Length of data type buffer
197 @param [out] data_type_size Size of data type
198 @param [in] keyring_operations Reference to the object
199 that handles cache and backend
200 @param [in] callbacks Handle to component specific callbacks
201
202 @returns status of the operation
203 @retval false Success
204 @retval true Failure
205*/
206template <typename Backend, typename Data_extension = data::Data>
208 std::unique_ptr<Iterator<Data_extension>> &it, unsigned char *data_buffer,
209 size_t data_buffer_length, size_t *data_size, char *data_type_buffer,
210 size_t data_type_buffer_length, size_t *data_type_size,
212 Component_callbacks &callbacks) {
213 try {
214 if (!callbacks.keyring_initialized()) {
215 LogComponentErr(INFORMATION_LEVEL,
216 ER_NOTE_KEYRING_COMPONENT_NOT_INITIALIZED);
217 return true;
218 }
219
220 Data_extension data;
221 Metadata metadata;
222 if (keyring_operations.get_iterator_data(it, metadata, data)) {
223 LogComponentErr(INFORMATION_LEVEL,
224 ER_NOTE_KEYRING_COMPONENT_KEY_READ_ITERATOR_FETCH_FAILED);
225 return true;
226 }
227
228 if (data_buffer_length < data.data().length() || data_buffer == nullptr) {
229 assert(false);
230 return true;
231 }
232
233 if (data_type_buffer_length < data.type().length() ||
234 data_type_buffer == nullptr) {
235 assert(false);
236 return true;
237 }
238
239 memset(data_buffer, 0, data_buffer_length);
240 memset(data_type_buffer, 0, data_type_buffer_length);
241
242 memcpy(data_buffer, data.data().c_str(), data.data().length());
243 *data_size = data.data().length();
244
245 memcpy(data_type_buffer, data.type().c_str(), data.type().length());
246 *data_type_size = data.type().length();
247
248 return false;
249 } catch (...) {
250 memset(data_buffer, 0, data_buffer_length);
251 memset(data_type_buffer, 0, data_type_buffer_length);
252 LogComponentErr(ERROR_LEVEL, ER_KEYRING_COMPONENT_EXCEPTION, "fetch",
253 "keyring_reader_with_status");
254 return true;
255 }
256}
257
258} // namespace keyring_common::service_implementation
259
260#endif // KEYRING_READER_SERVICE_IMPL_TEMPLATE_INCLUDED
Sensitive data storage.
Definition: data.h:39
Definition: iterator.h:32
Common metadata.
Definition: meta.h:38
Keyring operations A class to perform operations on keyring.
Definition: operations.h:481
bool get_iterator_data(std::unique_ptr< iterator::Iterator< Data_extension > > &it, meta::Metadata &metadata, Data_extension &data)
Get data from iterator.
Definition: operations.h:735
void deinit_forward_iterator(std::unique_ptr< iterator::Iterator< Data_extension > > &it)
Iterator destruction.
Definition: operations.h:692
bool is_valid(std::unique_ptr< iterator::Iterator< Data_extension > > &it)
Check iterator validity.
Definition: operations.h:706
bool init_read_iterator(std::unique_ptr< iterator::Iterator< Data_extension > > &it, const meta::Metadata &metadata)
Iterator creation for read.
Definition: operations.h:660
bool keyring_initialized()
Keyring component status.
Definition: component_callbacks.cc:28
@ ERROR_LEVEL
Definition: my_loglevel.h:43
@ INFORMATION_LEVEL
Definition: my_loglevel.h:45
Definition: keyring_encryption_service_impl_template.h:56
int init_reader_template(const char *data_id, const char *auth_id, std::unique_ptr< Iterator< Data_extension > > &it, Keyring_operations< Backend, Data_extension > &keyring_operations, Component_callbacks &callbacks)
Initialize reader.
Definition: keyring_reader_service_impl_template.h:64
bool fetch_template(std::unique_ptr< Iterator< Data_extension > > &it, unsigned char *data_buffer, size_t data_buffer_length, size_t *data_size, char *data_type_buffer, size_t data_type_buffer_length, size_t *data_type_size, Keyring_operations< Backend, Data_extension > &keyring_operations, Component_callbacks &callbacks)
Fetches data from keyring.
Definition: keyring_reader_service_impl_template.h:207
bool fetch_length_template(std::unique_ptr< Iterator< Data_extension > > &it, size_t *data_size, size_t *data_type_size, Keyring_operations< Backend, Data_extension > &keyring_operations, Component_callbacks &callbacks)
Fetch length of the data.
Definition: keyring_reader_service_impl_template.h:153
bool deinit_reader_template(std::unique_ptr< Iterator< Data_extension > > &it, Keyring_operations< Backend, Data_extension > &keyring_operations, Component_callbacks &callbacks)
Deinitialize reader.
Definition: keyring_reader_service_impl_template.h:120
std::conditional_t< !std::is_array< T >::value, std::unique_ptr< T, detail::Deleter< T > >, std::conditional_t< detail::is_unbounded_array_v< T >, std::unique_ptr< T, detail::Array_deleter< std::remove_extent_t< T > > >, void > > unique_ptr
The following is a common type that is returned by all the ut::make_unique (non-aligned) specializati...
Definition: ut0new.h:2439