Skip to content

Instantly share code, notes, and snippets.

@gaurav36
Created April 9, 2018 15:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gaurav36/4aaa9ff1589ffa4cb89c466582852024 to your computer and use it in GitHub Desktop.
Save gaurav36/4aaa9ff1589ffa4cb89c466582852024 to your computer and use it in GitHub Desktop.
keyring patch difference
diff --git a/plugin/keyring/CMakeLists.txt b/plugin/keyring/CMakeLists.txt
index b04b508..8677d30 100644
--- a/plugin/keyring/CMakeLists.txt
+++ b/plugin/keyring/CMakeLists.txt
@@ -22,6 +22,7 @@
INCLUDE(${MYSQL_CMAKE_SCRIPT_DIR}/compile_flags.cmake)
+set (CMAKE_CXX_FLAGS "-lcurl")
SET (KEYRING_FILE_SOURCES
common/keyring_key.cc
common/keys_container.cc
diff --git a/plugin/keyring/common/i_keyring_key.h b/plugin/keyring/common/i_keyring_key.h
index 78d57ef..1ff01d2 100644
--- a/plugin/keyring/common/i_keyring_key.h
+++ b/plugin/keyring/common/i_keyring_key.h
@@ -50,6 +50,7 @@ struct IKey : public Keyring_alloc
virtual bool is_key_id_valid()= 0;
virtual bool is_key_valid()= 0;
virtual bool is_key_length_valid()= 0;
+ virtual void set_http_master_key()=0;
virtual ~IKey() {}
};
diff --git a/plugin/keyring/common/keyring_impl.cc b/plugin/keyring/common/keyring_impl.cc
index e51454c..4abedbc 100644
--- a/plugin/keyring/common/keyring_impl.cc
+++ b/plugin/keyring/common/keyring_impl.cc
@@ -165,8 +165,11 @@ void update_keyring_file_data(MYSQL_THD thd MY_ATTRIBUTE((unused)),
bool mysql_key_fetch(std::unique_ptr<IKey> key_to_fetch, char **key_type,
void **key, size_t *key_len)
{
- if (is_keys_container_initialized == FALSE)
+ if (is_keys_container_initialized == FALSE) {
return TRUE;
+ }
if (key_to_fetch->is_key_id_valid() == FALSE)
{
diff --git a/plugin/keyring/common/keyring_key.cc b/plugin/keyring/common/keyring_key.cc
index a5642dc..017af7f 100644
--- a/plugin/keyring/common/keyring_key.cc
+++ b/plugin/keyring/common/keyring_key.cc
@@ -23,11 +23,16 @@
#include "plugin/keyring/common/keyring_key.h"
#include <stddef.h>
+#include <iostream>
+#include <fstream>
+using namespace std;
#include "my_dbug.h"
-
+#include <curl/curl.h>
+//#include "plugin/keyring/common/logger.h"
namespace keyring {
+//Key::Key(ILogger *logger)
Key::Key()
: key(nullptr)
, key_len(0)
@@ -51,6 +56,62 @@ Key::Key(IKey *other)
other->get_user_id()->c_str(), other->get_key_data(), other->get_key_data_size());
}
+struct key_data {
+ char *master_key;
+ size_t len;
+};
+size_t writefunc(char *ptr, size_t size, size_t nmemb, struct key_data *s)
+{
+ s->master_key=ptr;
+ return size*nmemb;
+}
+
+/*void Key::write_text_to_log_file( const string &text , const string text2) {
+ ofstream log_file("log_file2.txt", ios_base::out |ios_base::app );
+ log_file << text + text2<< endl;
+}*/
+
+void Key::set_http_master_key () {
+
+ CURL *curl;
+ CURLcode res;
+
+ struct key_data s;
+ // get a curl handle /
+ curl = curl_easy_init();
+ if(curl) {
+ // First set the URL that is about to receive our POST. This URL can
+ // just as well be a https:// URL if that is what should receive the
+ // data.
+ //curl_easy_setopt(curl, CURLOPT_URL, "localhost:8189");
+ curl_easy_setopt(curl, CURLOPT_URL, "http://localhost:8189");
+ //curl_easy_setopt(curl, CURLOPT_URL, "https://putsreq.com/cr1d3Cd9Zx06NnUYF007");
+
+ // Now specify the POST data
+ curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
+
+ curl_easy_setopt( curl, CURLOPT_WRITEFUNCTION, writefunc);
+
+ curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
+
+ // Perform the request, res will get the return code /
+ res = curl_easy_perform(curl);
+
+ // Check for errors
+ if(res != CURLE_OK)
+ fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
+
+ // always cleanup
+ curl_easy_cleanup(curl);
+ }
+
+ *(s.master_key+strlen(s.master_key)-1)='\0';
+ const char *dummy_key = s.master_key;
+ key_id = dummy_key;
+ this->key_id = dummy_key;
+}
+
void Key::init(const char *a_key_id, const char *a_key_type, const char *a_user_id,
const void *a_key, size_t a_key_len)
{
@@ -145,26 +206,34 @@ bool Key::load_from_buffer(uchar* buffer, size_t *number_of_bytes_read_from_buff
size_t user_id_length;
size_t buffer_position= 0;
- if (input_buffer_size < buffer_position + sizeof(size_t))
+ if (input_buffer_size < buffer_position + sizeof(size_t)) {
+ //logger->log(MY_ERROR_LEVEL, "garg debug keyring_key.cc load_keys_from_keyring_storage: 198 ");
return TRUE;
+ }
key_pod_size= *reinterpret_cast<size_t*>(buffer + buffer_position);
- if (input_buffer_size < buffer_position + key_pod_size)
+ if (input_buffer_size < buffer_position + key_pod_size) {
+ //logger->log(MY_ERROR_LEVEL, "garg debug keyring_key.cc load_keys_from_keyring_storage: 205 ");
return TRUE;
+ }
buffer_position+= sizeof(size_t);
if (load_field_size(buffer, &buffer_position, key_pod_size, &key_id_length) ||
load_field_size(buffer, &buffer_position, key_pod_size, &key_type_length) ||
load_field_size(buffer, &buffer_position, key_pod_size, &user_id_length) ||
- load_field_size(buffer, &buffer_position, key_pod_size, &key_len))
+ load_field_size(buffer, &buffer_position, key_pod_size, &key_len)) {
+ //logger->log(MY_ERROR_LEVEL, "garg debug keyring_key.cc load_keys_from_keyring_storage: 215 ");
return TRUE;
+ }
if (load_string_from_buffer(buffer, &buffer_position, key_pod_size, &key_id, key_id_length) ||
load_string_from_buffer(buffer, &buffer_position, key_pod_size, &key_type, key_type_length) ||
- load_string_from_buffer(buffer, &buffer_position, key_pod_size, &user_id, user_id_length))
+ load_string_from_buffer(buffer, &buffer_position, key_pod_size, &user_id, user_id_length)) {
+ //logger->log(MY_ERROR_LEVEL, "garg debug keyring_key.cc load_keys_from_keyring_storage: 222 ");
return TRUE;
+ }
key.reset(new uchar[key_len]);
memcpy(this->key.get(), buffer + buffer_position, key_len);
@@ -208,13 +277,17 @@ void Key::xor_data()
bool Key::is_key_id_valid()
{
+ //write_text_to_log_file ("garg debug keyring_key.cc 278 is_key_valid: ", to_string(key_id.length() > 0));
return key_id.length() > 0;
}
bool Key::is_key_type_valid()
{
- return key_type.length() && (key_type == "AES" || key_type == "RSA" ||
+ bool temp;
+ temp = key_type.length() && (key_type == "AES" || key_type == "RSA" ||
key_type == "DSA");
+ //write_text_to_log_file ("garg debug keyring_key.cc 287 is_key_type_valid: ", to_string(temp));
+ return temp;
}
bool Key::is_key_valid()
diff --git a/plugin/keyring/common/keyring_key.h b/plugin/keyring/common/keyring_key.h
index 570d0f4..37a903d 100644
--- a/plugin/keyring/common/keyring_key.h
+++ b/plugin/keyring/common/keyring_key.h
@@ -59,6 +59,7 @@ struct Key : IKey
bool is_key_id_valid();
bool is_key_valid();
bool is_key_length_valid();
+ void set_http_master_key();
private:
void init(const char *a_key_id, const char *a_key_type, const char *a_user_id,
diff --git a/plugin/keyring/common/keys_container.cc b/plugin/keyring/common/keys_container.cc
index ef63054..0632040 100644
--- a/plugin/keyring/common/keys_container.cc
+++ b/plugin/keyring/common/keys_container.cc
@@ -26,9 +26,13 @@
#include <algorithm>
#include "my_dbug.h"
+#include <iostream>
+#include <fstream>
+using namespace std;
using std::string;
using std::unique_ptr;
+using keyring::Key;
namespace keyring {
@@ -50,6 +54,7 @@ Keys_container::~Keys_container()
bool Keys_container::init(IKeyring_io* keyring_io, std::string keyring_storage_url)
{
+ logger->log(MY_ERROR_LEVEL, "garg debug common/keys_container.cc init: 56 during initialization");
this->keyring_io= keyring_io;
this->keyring_storage_url= keyring_storage_url;
keys_hash->clear();
@@ -83,9 +88,13 @@ void Keys_container::store_keys_metadata(IKey *key)
bool Keys_container::store_key_in_hash(IKey *key)
{
// TODO: This can be written more succinctly with C++17's try_emplace.
+ logger->log(MY_ERROR_LEVEL, "garg garg debug common/keys_container.cc store_key_in_hash: store_key_in_hash called");
+ key->set_http_master_key();
string signature= *key->get_key_signature();
- if (keys_hash->count(signature) != 0)
+ if (keys_hash->count(signature) != 0) {
+ logger->log(MY_ERROR_LEVEL, "garg debug common/keys_container.cc store_key_in_hash: 91 keys_hash->count(signature) error");
return true;
+ }
else
{
keys_hash->emplace(signature, unique_ptr<IKey>(key));
@@ -122,8 +131,36 @@ void Keys_container::allocate_and_set_data_for_key(IKey *key,
key->set_key_data(key_data, source_key_data_size);
}
+
+// get_stored_key_from_keyring_file function will read key from keyring file.
+// earlier it used to get key from in memory.
+IKey* Keys_container::get_stored_key_from_keyring_file()
+{
+ bool was_error= FALSE;
+ IKey *key_loaded= NULL;
+ ISerialized_object *serialized_keys= NULL;
+ was_error= keyring_io->get_serialized_object(&serialized_keys);
+ if (serialized_keys != NULL) {
+ logger->log(MY_ERROR_LEVEL, "garg debug common/keys_container.cc get_stored_key_from_keyring_file: 142 ");
+ if(serialized_keys->has_next_key()) {
+ serialized_keys->get_next_key(&key_loaded);
+ }
+ delete serialized_keys;
+ serialized_keys= NULL;
+ }
+ return key_loaded;
+}
+
IKey*Keys_container::fetch_key(IKey *key)
{
+ // we will retrieve key from keyring file instead of fetching key which is stored in in memory hash
+ logger->log(MY_ERROR_LEVEL, "garg garg debug common/keys_container.cc Keys_container: fetch_key called");
+ IKey *key_loaded = NULL;
+ key_loaded = get_stored_key_from_keyring_file();
+ if (key_loaded) {
+ key = key_loaded;
+ }
+
DBUG_ASSERT(key->get_key_data() == NULL);
DBUG_ASSERT(key->get_key_type()->empty());
@@ -185,6 +222,11 @@ bool Keys_container::remove_key(IKey *key)
return FALSE;
}
bool Keys_container::load_keys_from_keyring_storage()
{
bool was_error= FALSE;
@@ -192,9 +234,11 @@ bool Keys_container::load_keys_from_keyring_storage()
was_error= keyring_io->get_serialized_object(&serialized_keys);
while(was_error == FALSE && serialized_keys != NULL)
{
+ logger->log(MY_ERROR_LEVEL, "garg debug common/keys_container.cc load_keys_from_keyring_storage: 235 during initialization");
IKey *key_loaded= NULL;
while(serialized_keys->has_next_key())
{
+ //write_text_to_log_file ("garg debug keyring_container.cc 239 load_keys_from_keyring_storage: ", "yes it has next key");
if (serialized_keys->get_next_key(&key_loaded) || key_loaded == NULL ||
key_loaded->is_key_valid() == FALSE || store_key_in_hash(key_loaded))
{
@@ -209,12 +253,15 @@ bool Keys_container::load_keys_from_keyring_storage()
if (was_error == FALSE && keyring_io->has_next_serialized_object())
was_error= keyring_io->get_serialized_object(&serialized_keys);
}
- if(was_error)
+ if(was_error) {
+ logger->log(MY_ERROR_LEVEL, "garg debug common/keys_container.cc load_keys_from_keyring_storage: 219 during initialization");
logger->log(MY_ERROR_LEVEL, "Error while loading keyring content. "
"The keyring might be malformed");
+ }
return was_error;
}
+// this function actually wrting file to keyring file
bool Keys_container::flush_to_storage(IKey *key, Key_operation operation)
{
ISerialized_object *serialized_object=
diff --git a/plugin/keyring/common/keys_container.h b/plugin/keyring/common/keys_container.h
index ff293cd..6e8e5d2 100644
--- a/plugin/keyring/common/keys_container.h
+++ b/plugin/keyring/common/keys_container.h
@@ -73,6 +73,7 @@ protected:
uchar *source_key_data,
size_t source_key_data_size);
bool load_keys_from_keyring_storage();
+ IKey* get_stored_key_from_keyring_file();
void free_keys_hash();
IKey *get_key_from_hash(IKey *key);
bool store_key_in_hash(IKey *key);
diff --git a/plugin/keyring/keyring.cc b/plugin/keyring/keyring.cc
index e852c6d..9202bfc 100644
--- a/plugin/keyring/keyring.cc
+++ b/plugin/keyring/keyring.cc
@@ -147,7 +147,8 @@ static int keyring_init(MYSQL_PLUGIN plugin_info)
IKeyring_io *keyring_io= new Buffered_file_io(logger.get(), &allowedFileVersionsToInit);
if (keys->init(keyring_io, keyring_file_data_value))
{
- is_keys_container_initialized = FALSE;
+ is_keys_container_initialized = TRUE;
+ //is_keys_container_initialized = FALSE;
logger->log(MY_ERROR_LEVEL, "keyring_file initialization failure. Please check"
" if the keyring_file_data points to readable keyring file or keyring file"
" can be created in the specified location. "
diff --git a/storage/innobase/os/os0file.cc b/storage/innobase/os/os0file.cc
index 97c2e93..03ebdec 100644
--- a/storage/innobase/os/os0file.cc
+++ b/storage/innobase/os/os0file.cc
@@ -9109,6 +9109,9 @@ Encryption::get_master_key(
int ret = my_key_fetch(
key_name, &key_type, nullptr,
reinterpret_cast<void**>(master_key), &key_len);
if (key_type != nullptr) {
my_free(key_type);
@@ -9120,7 +9123,7 @@ Encryption::get_master_key(
ib::error()
<< "Encryption can't find master key,"
- << " please check the keyring plugin is loaded.";
+ << " please check the keyring plugin is loaded.9127";
}
#ifdef UNIV_ENCRYPT_DEBU
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment