MySQL 8.4.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
45namespace keyring_common {
46namespace service_implementation {
47
48/**
49 Initialize reader
50
51 @param [in] data_id Data Identifier
52 @param [in] auth_id Authorization ID
53 @param [out] it Iterator
54 @param [in] keyring_operations Reference to the object
55 that handles cache and backend
56 @param [in] callbacks Handle to component specific callbacks
57
58 @returns status of the operation
59 @retval -1 Keyring error. reader_object will not be created.
60 @retval 0 Key not found OR error fetching keys.
61 reader_object will not be created.
62 @retval 1 Key found, check out parameters
63*/
64template <typename Backend, typename Data_extension = data::Data>
66 const char *data_id, const char *auth_id,
69 Component_callbacks &callbacks) {
70 try {
71 if (callbacks.keyring_initialized() == false) {
72 LogComponentErr(INFORMATION_LEVEL,
73 ER_NOTE_KEYRING_COMPONENT_NOT_INITIALIZED);
74 return -1;
75 }
76
77 if (data_id == nullptr || !*data_id) {
78 LogComponentErr(INFORMATION_LEVEL,
79 ER_NOTE_KEYRING_COMPONENT_EMPTY_DATA_ID);
80 assert(false);
81 return 0;
82 }
83
84 Metadata metadata(data_id, auth_id);
85 if (keyring_operations.init_read_iterator(it, metadata) == true) {
86 LogComponentErr(INFORMATION_LEVEL,
87 ER_NOTE_KEYRING_COMPONENT_KEY_READ_ITERATOR_INIT_FAILED);
88 return 0;
89 }
90
91 if (keyring_operations.is_valid(it) == false) {
92 LogComponentErr(INFORMATION_LEVEL,
93 ER_NOTE_KEYRING_COMPONENT_READ_DATA_NOT_FOUND, data_id,
94 (auth_id == nullptr || !*auth_id) ? "NULL" : auth_id);
95 keyring_operations.deinit_forward_iterator(it);
96 return 0;
97 }
98
99 return 1;
100 } catch (...) {
101 LogComponentErr(ERROR_LEVEL, ER_KEYRING_COMPONENT_EXCEPTION, "init",
102 "keyring_reader_with_status");
103 return -1;
104 }
105}
106
107/**
108 Deinitialize reader
109
110 @param [in, out] it Iterator
111 @param [in] keyring_operations Reference to the object
112 that handles cache and backend
113 @param [in] callbacks Handle to component specific callbacks
114
115 @returns status of the operation
116 @retval false Success
117 @retval true Failure
118*/
119
120template <typename Backend, typename Data_extension = data::Data>
124 Component_callbacks &callbacks) {
125 try {
126 if (callbacks.keyring_initialized() == false) {
127 LogComponentErr(INFORMATION_LEVEL,
128 ER_NOTE_KEYRING_COMPONENT_NOT_INITIALIZED);
129 return true;
130 }
131 keyring_operations.deinit_forward_iterator(it);
132 return false;
133 } catch (...) {
134 LogComponentErr(ERROR_LEVEL, ER_KEYRING_COMPONENT_EXCEPTION, "deinit",
135 "keyring_reader_with_status");
136 return true;
137 }
138}
139
140/**
141 Fetch length of the data
142
143 @param [in] it Iterator
144 @param [out] data_size Size of fetched data
145 @param [out] data_type_size Size of data type
146 @param [in] keyring_operations Reference to the object
147 that handles cache and backend
148 @param [in] callbacks Handle to component specific callbacks
149 @returns status of the operation
150 @retval false Success
151 @retval true Failure
152*/
153template <typename Backend, typename Data_extension = data::Data>
155 std::unique_ptr<Iterator<Data_extension>> &it, size_t *data_size,
156 size_t *data_type_size,
158 Component_callbacks &callbacks) {
159 try {
160 if (callbacks.keyring_initialized() == false) {
161 LogComponentErr(INFORMATION_LEVEL,
162 ER_NOTE_KEYRING_COMPONENT_NOT_INITIALIZED);
163 return true;
164 }
165
166 if (data_size == nullptr || data_type_size == nullptr) {
167 assert(false);
168 return true;
169 }
170
171 Data_extension data;
172 Metadata metadata;
173 if (keyring_operations.get_iterator_data(it, metadata, data) == true) {
174 LogComponentErr(INFORMATION_LEVEL,
175 ER_NOTE_KEYRING_COMPONENT_KEY_READ_ITERATOR_FETCH_FAILED);
176 return true;
177 }
178
179 *data_size = data.data().length();
180 *data_type_size = data.type().length();
181 return false;
182 } catch (...) {
183 LogComponentErr(ERROR_LEVEL, ER_KEYRING_COMPONENT_EXCEPTION, "fetch_length",
184 "keyring_reader_with_status");
185 return true;
186 }
187}
188
189/**
190 Fetches data from keyring
191
192 @param [in] it Iterator
193 @param [out] data_buffer Out buffer for data
194 @param [in] data_buffer_length Length of out buffer
195 @param [out] data_size Size of fetched data
196 @param [out] data_type_buffer Type of data
197 @param [in] data_type_buffer_length Length of data type buffer
198 @param [out] data_type_size Size of data type
199 @param [in] keyring_operations Reference to the object
200 that handles cache and backend
201 @param [in] callbacks Handle to component specific callbacks
202
203 @returns status of the operation
204 @retval false Success
205 @retval true Failure
206*/
207template <typename Backend, typename Data_extension = data::Data>
209 std::unique_ptr<Iterator<Data_extension>> &it, unsigned char *data_buffer,
210 size_t data_buffer_length, size_t *data_size, char *data_type_buffer,
211 size_t data_type_buffer_length, size_t *data_type_size,
213 Component_callbacks &callbacks) {
214 try {
215 if (callbacks.keyring_initialized() == false) {
216 LogComponentErr(INFORMATION_LEVEL,
217 ER_NOTE_KEYRING_COMPONENT_NOT_INITIALIZED);
218 return true;
219 }
220
221 Data_extension data;
222 Metadata metadata;
223 if (keyring_operations.get_iterator_data(it, metadata, data) == true) {
224 LogComponentErr(INFORMATION_LEVEL,
225 ER_NOTE_KEYRING_COMPONENT_KEY_READ_ITERATOR_FETCH_FAILED);
226 return true;
227 }
228
229 if (data_buffer_length < data.data().length() || data_buffer == nullptr) {
230 assert(false);
231 return true;
232 }
233
234 if (data_type_buffer_length < data.type().length() ||
235 data_type_buffer == nullptr) {
236 assert(false);
237 return true;
238 }
239
240 memset(data_buffer, 0, data_buffer_length);
241 memset(data_type_buffer, 0, data_type_buffer_length);
242
243 memcpy(data_buffer, data.data().c_str(), data.data().length());
244 *data_size = data.data().length();
245
246 memcpy(data_type_buffer, data.type().c_str(), data.type().length());
247 *data_type_size = data.type().length();
248
249 return false;
250 } catch (...) {
251 memset(data_buffer, 0, data_buffer_length);
252 memset(data_type_buffer, 0, data_type_buffer_length);
253 LogComponentErr(ERROR_LEVEL, ER_KEYRING_COMPONENT_EXCEPTION, "fetch",
254 "keyring_reader_with_status");
255 return true;
256 }
257}
258
259} // namespace service_implementation
260} // namespace keyring_common
261
262#endif // KEYRING_READER_SERVICE_IMPL_TEMPLATE_INCLUDED
Sensitive data storage.
Definition: data.h:40
Definition: iterator.h:33
Common metadata.
Definition: meta.h:39
Keyring operations A class to perform operations on keyring.
Definition: operations.h:483
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:737
void deinit_forward_iterator(std::unique_ptr< iterator::Iterator< Data_extension > > &it)
Iterator destruction.
Definition: operations.h:694
bool is_valid(std::unique_ptr< iterator::Iterator< Data_extension > > &it)
Check iterator validity.
Definition: operations.h:708
bool init_read_iterator(std::unique_ptr< iterator::Iterator< Data_extension > > &it, const meta::Metadata &metadata)
Iterator creation for read.
Definition: operations.h:662
bool keyring_initialized()
Keyring component status.
Definition: component_callbacks.cc:29
@ ERROR_LEVEL
Definition: my_loglevel.h:43
@ INFORMATION_LEVEL
Definition: my_loglevel.h:45
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:65
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:208
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:154
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:121
Definition: keyring_encryption_service_definition.h:32
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:2438