MySQL 9.0.0
Source Code Documentation
keyring_keys_metadata_iterator_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_KEYS_METADATA_SERVICE_IMPL_TEMPLATE_INCLUDED
25#define KEYRING_KEYS_METADATA_SERVICE_IMPL_TEMPLATE_INCLUDED
26
27#include <cstring>
28#include <functional> /* std::function */
29#include <memory>
30
31#include <my_dbug.h>
32#include <mysql/components/services/log_builtins.h> /* LogComponentErr */
33#include <mysqld_error.h>
34
40
45
47
48/**
49 Forward iterator initialization
50
51 @param [out] it metadata iterator
52 @param [in] keyring_operations Reference to the object
53 that handles cache and backend
54 @param [in] callbacks Handle to component specific callbacks
55
56 @returns Status of the operation
57 @retval false Success
58 @retval true Failure
59*/
60template <typename Backend, typename Data_extension = Data>
64 Component_callbacks &callbacks) {
65 try {
66 if (!callbacks.keyring_initialized()) {
67 LogComponentErr(INFORMATION_LEVEL,
68 ER_NOTE_KEYRING_COMPONENT_NOT_INITIALIZED);
69 return true;
70 }
71
72 if (keyring_operations.init_forward_iterator(it, false)) {
73 LogComponentErr(
75 ER_NOTE_KEYRING_COMPONENT_KEYS_METADATA_ITERATOR_INIT_FAILED);
76 return true;
77 }
78
79 return false;
80 } catch (...) {
81 LogComponentErr(ERROR_LEVEL, ER_KEYRING_COMPONENT_EXCEPTION, "init",
82 "keyring_keys_metadata_iterator");
83 return true;
84 }
85}
86
87/**
88 Iterator deinitialization
89
90 @param [out] it metadata iterator
91 @param [in] keyring_operations Reference to the object
92 that handles cache and backend
93 @param [in] callbacks Handle to component specific callbacks
94
95 @returns Status of the operation
96 @retval false Success
97 @retval true Failure
98*/
99template <typename Backend, typename Data_extension = Data>
103 Component_callbacks &callbacks) {
104 try {
105 if (!callbacks.keyring_initialized()) {
106 LogComponentErr(INFORMATION_LEVEL,
107 ER_NOTE_KEYRING_COMPONENT_NOT_INITIALIZED);
108 return true;
109 }
110 keyring_operations.deinit_forward_iterator(it);
111 return false;
112 } catch (...) {
113 LogComponentErr(ERROR_LEVEL, ER_KEYRING_COMPONENT_EXCEPTION, "deinit",
114 "keyring_keys_metadata_iterator");
115 return true;
116 }
117}
118
119/**
120 Check validity of the iterator
121
122 @param [in] it metadata iterator
123 @param [in] keyring_operations Reference to the object
124 that handles cache and backend
125 @param [in] callbacks Handle to component specific callbacks
126
127 @returns Validty of the iterator
128 @retval true Iterator is valid
129 @retval false Iterator is invalid
130*/
131template <typename Backend, typename Data_extension = Data>
135 Component_callbacks &callbacks) {
136 try {
137 if (!callbacks.keyring_initialized()) {
138 LogComponentErr(INFORMATION_LEVEL,
139 ER_NOTE_KEYRING_COMPONENT_NOT_INITIALIZED);
140 return false;
141 }
142 return keyring_operations.is_valid(it);
143 } catch (...) {
144 LogComponentErr(ERROR_LEVEL, ER_KEYRING_COMPONENT_EXCEPTION, "is_valid",
145 "keyring_keys_metadata_iterator");
146 return false;
147 }
148}
149
150/**
151 Move iterator forward.
152
153 @param [out] it metadata iterator
154 @param [in] keyring_operations Reference to the object
155 that handles cache and backend
156 @param [in] callbacks Component specific callbacks
157
158 @returns Status of the operation
159 @retval false Success - indicates that iterator is pointing to next entry
160 @retval true Failure - indicates that iterator has reached the end
161*/
162template <typename Backend, typename Data_extension = Data>
166 Component_callbacks &callbacks) {
167 try {
168 if (!callbacks.keyring_initialized()) {
169 LogComponentErr(INFORMATION_LEVEL,
170 ER_NOTE_KEYRING_COMPONENT_NOT_INITIALIZED);
171 return true;
172 }
173 if (keyring_operations.next(it)) {
174 return true;
175 }
176 return false;
177 } catch (...) {
178 LogComponentErr(ERROR_LEVEL, ER_KEYRING_COMPONENT_EXCEPTION, "next",
179 "keyring_keys_metadata_iterator");
180 return true;
181 }
182}
183
184/**
185 Fetch length of metadata for current key pointed by iterator
186
187 @param [out] it metadata iterator
188 @param [out] data_id_length Length of data_id buffer
189 @param [out] auth_id_length Length of auth_id buffer
190 @param [in] keyring_operations Reference to the object
191 that handles cache and backend
192 @param [in] callbacks Handle to component specific callbacks
193
194 @returns Status of the operation
195 @retval false Success
196 @retval true Failure
197*/
198template <typename Backend, typename Data_extension = Data>
200 std::unique_ptr<Iterator<Data_extension>> &it, size_t *data_id_length,
201 size_t *auth_id_length,
203 Component_callbacks &callbacks) {
204 try {
205 if (!callbacks.keyring_initialized()) {
206 LogComponentErr(INFORMATION_LEVEL,
207 ER_NOTE_KEYRING_COMPONENT_NOT_INITIALIZED);
208 return true;
209 }
210
211 Data_extension data;
212 Metadata metadata;
213 if (keyring_operations.get_iterator_data(it, metadata, data)) {
214 LogComponentErr(
216 ER_NOTE_KEYRING_COMPONENT_KEYS_METADATA_ITERATOR_FETCH_FAILED);
217 return true;
218 }
219
220 *data_id_length = metadata.key_id().length();
221 *auth_id_length = metadata.owner_id().length();
222 return false;
223 } catch (...) {
224 LogComponentErr(ERROR_LEVEL, ER_KEYRING_COMPONENT_EXCEPTION, "get_length",
225 "keyring_keys_metadata_iterator");
226 return true;
227 }
228}
229/**
230 Fetch metadata for current key pointed by iterator
231
232 @param [out] it metadata iterator
233 @param [out] data_id ID information of current data
234 @param [in] data_id_length Length of data_id buffer
235 @param [out] auth_id Owner of the key
236 @param [in] auth_id_length Length of auth_id buffer
237 @param [in] keyring_operations Reference to the object
238 that handles cache and backend
239 @param [in] callbacks Handle to component specific callbacks
240
241 @returns Status of the operation
242 @retval false Success
243 @retval true Failure
244*/
245template <typename Backend, typename Data_extension = Data>
247 std::unique_ptr<Iterator<Data_extension>> &it, char *data_id,
248 size_t data_id_length, char *auth_id, size_t auth_id_length,
250 Component_callbacks &callbacks) {
251 try {
252 if (!callbacks.keyring_initialized()) {
253 LogComponentErr(INFORMATION_LEVEL,
254 ER_NOTE_KEYRING_COMPONENT_NOT_INITIALIZED);
255 return true;
256 }
257
258 Data_extension data;
259 Metadata metadata;
260 if (keyring_operations.get_iterator_metadata(it, metadata, data)) {
261 LogComponentErr(
263 ER_NOTE_KEYRING_COMPONENT_KEYS_METADATA_ITERATOR_FETCH_FAILED);
264 return true;
265 }
266
267 if (metadata.key_id().length() >= data_id_length) {
268 assert(false);
269 return true;
270 }
271
272 if (metadata.owner_id().length() >= auth_id_length) {
273 assert(false);
274 return true;
275 }
276
277 memcpy(data_id, metadata.key_id().c_str(), metadata.key_id().length());
278 data_id[metadata.key_id().length()] = '\0';
279 memcpy(auth_id, metadata.owner_id().c_str(), metadata.owner_id().length());
280 auth_id[metadata.owner_id().length()] = '\0';
281 return false;
282 } catch (...) {
283 LogComponentErr(ERROR_LEVEL, ER_KEYRING_COMPONENT_EXCEPTION, "get",
284 "keyring_keys_metadata_iterator");
285 return true;
286 }
287}
288
289} // namespace keyring_common::service_implementation
290
291#endif // !KEYRING_KEYS_METADATA_SERVICE_IMPL_TEMPLATE_INCLUDED
Sensitive data storage.
Definition: data.h:39
Definition: iterator.h:32
Common metadata.
Definition: meta.h:38
std::string owner_id() const
Get owner info.
Definition: meta.cc:72
std::string key_id() const
Get key ID.
Definition: meta.cc:69
Keyring operations A class to perform operations on keyring.
Definition: operations.h:481
bool next(std::unique_ptr< iterator::Iterator< Data_extension > > &it)
Move iterator forward.
Definition: operations.h:719
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
bool init_forward_iterator(std::unique_ptr< iterator::Iterator< Data_extension > > &it, bool cached)
Iterator creation.
Definition: operations.h:679
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 get_iterator_metadata(std::unique_ptr< iterator::Iterator< Data_extension > > &it, meta::Metadata &metadata, Data_extension &data)
Get metadata from iterator.
Definition: operations.h:761
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
bool deinit_keys_metadata_iterator_template(std::unique_ptr< Iterator< Data_extension > > &it, Keyring_operations< Backend, Data_extension > &keyring_operations, Component_callbacks &callbacks)
Iterator deinitialization.
Definition: keyring_keys_metadata_iterator_service_impl_template.h:100
bool keys_metadata_get_template(std::unique_ptr< Iterator< Data_extension > > &it, char *data_id, size_t data_id_length, char *auth_id, size_t auth_id_length, Keyring_operations< Backend, Data_extension > &keyring_operations, Component_callbacks &callbacks)
Fetch metadata for current key pointed by iterator.
Definition: keyring_keys_metadata_iterator_service_impl_template.h:246
bool keys_metadata_iterator_is_valid(std::unique_ptr< Iterator< Data_extension > > &it, Keyring_operations< Backend, Data_extension > &keyring_operations, Component_callbacks &callbacks)
Check validity of the iterator.
Definition: keyring_keys_metadata_iterator_service_impl_template.h:132
bool init_keys_metadata_iterator_template(std::unique_ptr< Iterator< Data_extension > > &it, Keyring_operations< Backend, Data_extension > &keyring_operations, Component_callbacks &callbacks)
Forward iterator initialization.
Definition: keyring_keys_metadata_iterator_service_impl_template.h:61
bool keys_metadata_iterator_next(std::unique_ptr< Iterator< Data_extension > > &it, Keyring_operations< Backend, Data_extension > &keyring_operations, Component_callbacks &callbacks)
Move iterator forward.
Definition: keyring_keys_metadata_iterator_service_impl_template.h:163
bool keys_metadata_get_length_template(std::unique_ptr< Iterator< Data_extension > > &it, size_t *data_id_length, size_t *auth_id_length, Keyring_operations< Backend, Data_extension > &keyring_operations, Component_callbacks &callbacks)
Fetch length of metadata for current key pointed by iterator.
Definition: keyring_keys_metadata_iterator_service_impl_template.h:199
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