3333#include < components/keyrings/common/memstore/cache.h>
3434#include < components/keyrings/common/memstore/iterator.h>
3535#include < components/keyrings/common/utils/utils.h>
36+ #include < mysql/components/services/log_builtins.h>
37+ #include < mysqld_error.h>
3638
3739namespace keyring_kmip {
3840
@@ -54,18 +56,30 @@ bool Keyring_kmip_backend::load_cache(
5456 Keyring_kmip_backend, keyring_common::data::Data_extension<IdExt>>
5557 &operations) {
5658 DBUG_TRACE;
59+ // We have to load keys and secrets with state==ACTIVE only
60+ // TODO: implement better logic with the new KMIP library
5761 try {
5862 auto ctx = kmip_ctx ();
59-
63+ // get all keys in the group
6064 auto keys = (config_.object_group .empty ()
6165 ? ctx.op_all ()
6266 : ctx.op_locate_by_group (config_.object_group ));
6367
6468 for (auto const &id : keys) {
6569 auto key = ctx.op_get (id);
70+ if (key.empty ()) {
71+ std::string err_msg =
72+ " Cannot get key with ID: " + id + " Cause: " + ctx.get_last_result ();
73+ LogComponentErr (ERROR_LEVEL, ER_LOG_PRINTF_MSG, err_msg.c_str ());
74+ continue ;
75+ }
6676 auto key_name = ctx.op_get_name_attr (id);
67-
68- if (key_name.empty ()) continue ;
77+ if (key_name.empty ()) {
78+ std::string err_msg = " Cannot get key name for ID: " + id +
79+ " Cause: " + ctx.get_last_result ();
80+ LogComponentErr (ERROR_LEVEL, ER_LOG_PRINTF_MSG, err_msg.c_str ());
81+ continue ;
82+ }
6983
7084 Metadata metadata (key_name, " " );
7185
@@ -79,6 +93,39 @@ bool Keyring_kmip_backend::load_cache(
7993 return true ;
8094 }
8195 }
96+ // get all secrets in the group
97+ auto secrets = (config_.object_group .empty ()
98+ ? ctx.op_all_secrets ()
99+ : ctx.op_locate_secrets_by_group (config_.object_group ));
100+
101+ for (auto const &id : secrets) {
102+ auto secret = ctx.op_get_secret (id);
103+ if (secret.empty ()) {
104+ std::string err_msg = " Cannot get secret with ID: " + id +
105+ " Cause: " + ctx.get_last_result ();
106+ LogComponentErr (ERROR_LEVEL, ER_LOG_PRINTF_MSG, err_msg.c_str ());
107+ continue ;
108+ }
109+ auto secret_name = ctx.op_get_name_attr (id);
110+
111+ if (secret_name.empty ()) {
112+ std::string err_msg = " Cannot get secret name for ID: " + id +
113+ " Cause: " + ctx.get_last_result ();
114+ LogComponentErr (ERROR_LEVEL, ER_LOG_PRINTF_MSG, err_msg.c_str ());
115+ continue ;
116+ }
117+
118+ Metadata metadata (secret_name, " " );
119+
120+ Data_extension<IdExt> data (Data{keyring_common::data::Sensitive_data (
121+ secret.c_str (), secret.size ()),
122+ " SECRET" },
123+ IdExt{id});
124+
125+ if (operations.insert (metadata, data) == true ) {
126+ return true ;
127+ }
128+ }
82129
83130 } catch (...) {
84131 mysql_components_handle_std_exception (__func__);
@@ -98,19 +145,40 @@ bool Keyring_kmip_backend::store(const Metadata &metadata,
98145 Data_extension<IdExt> &data) {
99146 DBUG_TRACE;
100147 if (!metadata.valid () || !data.valid ()) return true ;
101- if (data.type () != " AES" ) {
102- // we only support AES keys
103- return true ;
104- }
148+ kmippp::context::id_t id;
105149 try {
106150 auto ctx = kmip_ctx ();
107151 auto key = data.data ().decode ();
108- kmippp::context::key_t keyv (key.begin (), key.end ());
109- auto id = ctx.op_register (metadata.key_id (), config_.object_group , keyv);
110- if (id.empty ()) {
152+ if (data.type () == " AES" ) {
153+ kmippp::context::key_t keyv (key.begin (), key.end ());
154+ id = ctx.op_register (metadata.key_id (), config_.object_group , keyv);
155+ if (id.empty ()) {
156+ std::string err_msg = " Cannot register key with name: " + metadata.key_id ()
157+ + " and group: " + config_.object_group
158+ + ctx.get_last_result ();
159+ LogComponentErr (ERROR_LEVEL, ER_LOG_PRINTF_MSG, err_msg.c_str ());
160+ return true ;
161+ }
162+ } else if (data.type () == " SECRET" ) {
163+ kmippp::context::name_t secret (key);
164+ id = ctx.op_register_secret (metadata.key_id (), config_.object_group ,
165+ secret, 1 );
166+ if (id.empty ()) {
167+ std::string err_msg = " Cannot register secret with name: " + metadata.key_id ()
168+ + " and group: " + config_.object_group
169+ + ctx.get_last_result ();
170+ LogComponentErr (ERROR_LEVEL, ER_LOG_PRINTF_MSG, err_msg.c_str ());
171+ return true ;
172+ }
173+ } else { // we only support AES keys and SECRET type (passwords)
174+ LogComponentErr (ERROR_LEVEL, ER_LOG_PRINTF_MSG,
175+ " Unsupported KMIP entity" + data.type () + " , can not store" );
111176 return true ;
112177 }
113178 if (!ctx.op_activate (id)) {
179+ std::string err_msg =
180+ " Cannot activate key/secret. " + ctx.get_last_result ();
181+ LogComponentErr (ERROR_LEVEL, ER_LOG_PRINTF_MSG, err_msg.c_str ());
114182 return true ;
115183 }
116184 data.set_extension ({id});
@@ -128,8 +196,12 @@ size_t Keyring_kmip_backend::size() const {
128196 auto keys = (config_.object_group .empty ()
129197 ? ctx.op_all ()
130198 : ctx.op_locate_by_group (config_.object_group ));
131-
132- return keys.size ();
199+ auto secrets = (config_.object_group .empty ()
200+ ? ctx.op_all_secrets ()
201+ : ctx.op_locate_secrets_by_group (config_.object_group ));
202+ return keys.size () + secrets.size ();
203+ // we may have deactivated keys counted, so we need to count active keys only
204+ // TODO: implement better logic with the new KMIP library
133205 } catch (...) {
134206 mysql_components_handle_std_exception (__func__);
135207 return 0 ;
@@ -142,7 +214,23 @@ bool Keyring_kmip_backend::erase(const Metadata &metadata,
142214 if (!metadata.valid ()) return true ;
143215
144216 auto ctx = kmip_ctx ();
145- return !ctx.op_destroy (data.get_extension ().uuid );
217+ // reason 1 means deactivate, and then incident occurrence time should be 0.
218+ if (!ctx.op_revoke (data.get_extension ().uuid , 1 , " Deleting the key" , 0 )) {
219+ std::string err_msg =
220+ " Cannot deactivate key/secret with ID: " + data.get_extension ().uuid
221+ + " Cause: " + ctx.get_last_result ();
222+ LogComponentErr (WARNING_LEVEL, ER_LOG_PRINTF_MSG, err_msg.c_str ());
223+ // no reason to fail here, if we're deactivating non-exiting key
224+ // TODO: implement better logic with the new KMIP library
225+ }
226+
227+ if (!ctx.op_destroy (data.get_extension ().uuid )) {
228+ std::string err_msg = " Cannot delete key/secret. " + ctx.get_last_result ();
229+ LogComponentErr (WARNING_LEVEL, ER_LOG_PRINTF_MSG, err_msg.c_str ());
230+ // no reason to fail here, if we're deleting non-exiting key
231+ // TODO: implement better logic with the new KMIP library
232+ }
233+ return false ;
146234}
147235
148236bool Keyring_kmip_backend::generate (const Metadata &metadata,
0 commit comments