Debian Codegraph @

components/keyrings/common/component_helpers/include/keyring_writer_service_impl_template.h

raw
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_WRITER_SERVICE_IMPL_TEMPLATE_INCLUDED
25 #define KEYRING_WRITER_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
34 #include <components/keyrings/common/component_helpers/include/service_requirements.h>
35 #include <components/keyrings/common/data/data.h>
36 #include <components/keyrings/common/data/meta.h>
37 #include <components/keyrings/common/operations/operations.h>
38
39 namespace keyring_common {
40 namespace service_implementation {
41
42 using keyring_common::data::Data;
43 using keyring_common::meta::Metadata;
44 using keyring_common::operations::Keyring_operations;
45
46 /**
47 Store data in keyring
48
49 @param [in] data_id Data Identifier
50 @param [in] auth_id Authorization ID
51 @param [in] data Data to be stored
52 @param [in] data_size Size of data to be stored
53 @param [in] data_type Type of data
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 false Success
60 @retval true Failure
61 */
62
63 template <typename Backend, typename Data_extension = data::Data>
64 bool store_template(
65 const char *data_id, const char *auth_id, const unsigned char *data,
66 size_t data_size, const char *data_type,
67 Keyring_operations<Backend, Data_extension> &keyring_operations,
68 Component_callbacks &callbacks) {
69 try {
70 if (callbacks.keyring_initialized() == false) {
71 return true;
72 }
73
74 if (data_id == nullptr || !*data_id) {
75 assert(false);
76 return true;
77 }
78
79 if (data_size > keyring_operations.maximum_data_length()) {
80 LogComponentErr(INFORMATION_LEVEL,
81 ER_NOTE_KEYRING_COMPONENT_WRITE_MAXIMUM_DATA_LENGTH,
82 keyring_operations.maximum_data_length());
83 return true;
84 }
85
86 Metadata metadata(data_id, auth_id);
87 Data data_to_be_stored({reinterpret_cast<const char *>(data), data_size},
88 {data_type, data_type ? strlen(data_type) : 0});
89 if (keyring_operations.store(metadata, data_to_be_stored) == true) {
90 LogComponentErr(INFORMATION_LEVEL, ER_NOTE_KEYRING_COMPONENT_STORE_FAILED,
91 data_id,
92 (auth_id == nullptr || !*auth_id) ? "NULL" : auth_id);
93 return true;
94 }
95 return false;
96 } catch (...) {
97 LogComponentErr(ERROR_LEVEL, ER_KEYRING_COMPONENT_EXCEPTION, "store",
98 "keyring_writer");
99 return true;
100 }
101 }
102
103 /**
104 Remove data from keyring
105
106 @param [in] data_id Data Identifier
107 @param [in] auth_id Authorization ID
108 @param [in] keyring_operations Reference to the object
109 that handles cache and backend
110 @param [in] callbacks Handle to component specific callbacks
111
112 @returns status of the operation
113 @retval false Success - Key removed successfully or key not present.
114 @retval true Failure
115 */
116 template <typename Backend, typename Data_extension = data::Data>
117 bool remove_template(
118 const char *data_id, const char *auth_id,
119 Keyring_operations<Backend, Data_extension> &keyring_operations,
120 Component_callbacks &callbacks) {
121 try {
122 if (callbacks.keyring_initialized() == false) {
123 return true;
124 }
125
126 if (data_id == nullptr || !*data_id) {
127 assert(false);
128 return true;
129 }
130
131 Metadata metadata(data_id, auth_id);
132 if (keyring_operations.erase(metadata) == true) {
133 LogComponentErr(INFORMATION_LEVEL,
134 ER_NOTE_KEYRING_COMPONENT_REMOVE_FAILED, data_id,
135 (auth_id == nullptr || !*auth_id) ? "NULL" : auth_id);
136 return true;
137 }
138 return false;
139 } catch (...) {
140 LogComponentErr(ERROR_LEVEL, ER_KEYRING_COMPONENT_EXCEPTION, "remove",
141 "keyring_writer");
142 return true;
143 }
144 }
145
146 } // namespace service_implementation
147 } // namespace keyring_common
148
149 #endif // !KEYRING_WRITER_SERVICE_IMPL_TEMPLATE_INCLUDED