MySQL 9.0.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
40
44
45/**
46 Store data in keyring
47
48 @param [in] data_id Data Identifier
49 @param [in] auth_id Authorization ID
50 @param [in] data Data to be stored
51 @param [in] data_size Size of data to be stored
52 @param [in] data_type Type of data
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 false Success
59 @retval true Failure
60*/
61
62template <typename Backend, typename Data_extension = data::Data>
64 const char *data_id, const char *auth_id, const unsigned char *data,
65 size_t data_size, const char *data_type,
67 Component_callbacks &callbacks) {
68 try {
69 if (!callbacks.keyring_initialized()) {
70 LogComponentErr(INFORMATION_LEVEL,
71 ER_NOTE_KEYRING_COMPONENT_NOT_INITIALIZED);
72 return true;
73 }
74
75 if (data_id == nullptr || !*data_id) {
76 LogComponentErr(INFORMATION_LEVEL,
77 ER_NOTE_KEYRING_COMPONENT_EMPTY_DATA_ID);
78 assert(false);
79 return true;
80 }
81
82 if (data_size > keyring_operations.maximum_data_length()) {
83 LogComponentErr(INFORMATION_LEVEL,
84 ER_NOTE_KEYRING_COMPONENT_WRITE_MAXIMUM_DATA_LENGTH,
85 keyring_operations.maximum_data_length());
86 return true;
87 }
88
89 Metadata metadata(data_id, auth_id);
90 Data data_to_be_stored({reinterpret_cast<const char *>(data), data_size},
91 {data_type, data_type ? strlen(data_type) : 0});
92 if (keyring_operations.store(metadata, data_to_be_stored)) {
93 LogComponentErr(INFORMATION_LEVEL, ER_NOTE_KEYRING_COMPONENT_STORE_FAILED,
94 data_id,
95 (auth_id == nullptr || !*auth_id) ? "NULL" : auth_id);
96 return true;
97 }
98 return false;
99 } catch (...) {
100 LogComponentErr(ERROR_LEVEL, ER_KEYRING_COMPONENT_EXCEPTION, "store",
101 "keyring_writer");
102 return true;
103 }
104}
105
106/**
107 Remove data from keyring
108
109 @param [in] data_id Data Identifier
110 @param [in] auth_id Authorization ID
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 - Key removed successfully or key not present.
117 @retval true Failure
118*/
119template <typename Backend, typename Data_extension = data::Data>
121 const char *data_id, const char *auth_id,
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
131 if (data_id == nullptr || !*data_id) {
132 LogComponentErr(INFORMATION_LEVEL,
133 ER_NOTE_KEYRING_COMPONENT_EMPTY_DATA_ID);
134 assert(false);
135 return true;
136 }
137
138 Metadata metadata(data_id, auth_id);
139 if (keyring_operations.erase(metadata)) {
140 LogComponentErr(INFORMATION_LEVEL,
141 ER_NOTE_KEYRING_COMPONENT_REMOVE_FAILED, data_id,
142 (auth_id == nullptr || !*auth_id) ? "NULL" : auth_id);
143 return true;
144 }
145 return false;
146 } catch (...) {
147 LogComponentErr(ERROR_LEVEL, ER_KEYRING_COMPONENT_EXCEPTION, "remove",
148 "keyring_writer");
149 return true;
150 }
151}
152
153} // namespace keyring_common::service_implementation
154
155#endif // !KEYRING_WRITER_SERVICE_IMPL_TEMPLATE_INCLUDED
Sensitive data storage.
Definition: data.h:39
Common metadata.
Definition: meta.h:38
Keyring operations A class to perform operations on keyring.
Definition: operations.h:481
bool erase(const meta::Metadata &metadata)
Remove API.
Definition: operations.h:608
bool store(const meta::Metadata &metadata, const data::Data &data)
Store API.
Definition: operations.h:569
size_t maximum_data_length() const
Maximum data length supported.
Definition: operations.h:777
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 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:63
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:120