MySQL 8.4.0
Source Code Documentation
keyring_writer_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_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
38
39namespace keyring_common {
40namespace service_implementation {
41
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
63template <typename Backend, typename Data_extension = data::Data>
65 const char *data_id, const char *auth_id, const unsigned char *data,
66 size_t data_size, const char *data_type,
68 Component_callbacks &callbacks) {
69 try {
70 if (callbacks.keyring_initialized() == false) {
71 LogComponentErr(INFORMATION_LEVEL,
72 ER_NOTE_KEYRING_COMPONENT_NOT_INITIALIZED);
73 return true;
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 true;
81 }
82
83 if (data_size > keyring_operations.maximum_data_length()) {
84 LogComponentErr(INFORMATION_LEVEL,
85 ER_NOTE_KEYRING_COMPONENT_WRITE_MAXIMUM_DATA_LENGTH,
86 keyring_operations.maximum_data_length());
87 return true;
88 }
89
90 Metadata metadata(data_id, auth_id);
91 Data data_to_be_stored({reinterpret_cast<const char *>(data), data_size},
92 {data_type, data_type ? strlen(data_type) : 0});
93 if (keyring_operations.store(metadata, data_to_be_stored) == true) {
94 LogComponentErr(INFORMATION_LEVEL, ER_NOTE_KEYRING_COMPONENT_STORE_FAILED,
95 data_id,
96 (auth_id == nullptr || !*auth_id) ? "NULL" : auth_id);
97 return true;
98 }
99 return false;
100 } catch (...) {
101 LogComponentErr(ERROR_LEVEL, ER_KEYRING_COMPONENT_EXCEPTION, "store",
102 "keyring_writer");
103 return true;
104 }
105}
106
107/**
108 Remove data from keyring
109
110 @param [in] data_id Data Identifier
111 @param [in] auth_id Authorization ID
112 @param [in] keyring_operations Reference to the object
113 that handles cache and backend
114 @param [in] callbacks Handle to component specific callbacks
115
116 @returns status of the operation
117 @retval false Success - Key removed successfully or key not present.
118 @retval true Failure
119*/
120template <typename Backend, typename Data_extension = data::Data>
122 const char *data_id, const char *auth_id,
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
132 if (data_id == nullptr || !*data_id) {
133 LogComponentErr(INFORMATION_LEVEL,
134 ER_NOTE_KEYRING_COMPONENT_EMPTY_DATA_ID);
135 assert(false);
136 return true;
137 }
138
139 Metadata metadata(data_id, auth_id);
140 if (keyring_operations.erase(metadata) == true) {
141 LogComponentErr(INFORMATION_LEVEL,
142 ER_NOTE_KEYRING_COMPONENT_REMOVE_FAILED, data_id,
143 (auth_id == nullptr || !*auth_id) ? "NULL" : auth_id);
144 return true;
145 }
146 return false;
147 } catch (...) {
148 LogComponentErr(ERROR_LEVEL, ER_KEYRING_COMPONENT_EXCEPTION, "remove",
149 "keyring_writer");
150 return true;
151 }
152}
153
154} // namespace service_implementation
155} // namespace keyring_common
156
157#endif // !KEYRING_WRITER_SERVICE_IMPL_TEMPLATE_INCLUDED
Sensitive data storage.
Definition: data.h:40
Common metadata.
Definition: meta.h:39
Keyring operations A class to perform operations on keyring.
Definition: operations.h:483
bool erase(const meta::Metadata &metadata)
Remove API.
Definition: operations.h:610
bool store(const meta::Metadata &metadata, const data::Data &data)
Store API.
Definition: operations.h:571
size_t maximum_data_length() const
Maximum data length supported.
Definition: operations.h:779
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
bool store_template(const char *data_id, const char *auth_id, const unsigned char *data, size_t data_size, const char *data_type, Keyring_operations< Backend, Data_extension > &keyring_operations, Component_callbacks &callbacks)
Store data in keyring.
Definition: keyring_writer_service_impl_template.h:64
bool remove_template(const char *data_id, const char *auth_id, Keyring_operations< Backend, Data_extension > &keyring_operations, Component_callbacks &callbacks)
Remove data from keyring.
Definition: keyring_writer_service_impl_template.h:121
Definition: keyring_encryption_service_definition.h:32