From f1d4494082bdea72af596c0b06bb92d6dfa7a688 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Canna=C3=B2?= Date: Thu, 2 Mar 2023 04:18:35 +0000 Subject: [PATCH] Adding TAP test to trigger X509 cache --- test/tap/tests/mysql-test_ssl_CA-t.cpp | 142 +++++++++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 test/tap/tests/mysql-test_ssl_CA-t.cpp diff --git a/test/tap/tests/mysql-test_ssl_CA-t.cpp b/test/tap/tests/mysql-test_ssl_CA-t.cpp new file mode 100644 index 000000000..075b27864 --- /dev/null +++ b/test/tap/tests/mysql-test_ssl_CA-t.cpp @@ -0,0 +1,142 @@ +#include +#include +#include +#include + +#include +#include +#include +#include + +#include "tap.h" +#include "command_line.h" +#include "utils.h" + +/* +This TAP test: +- configures SSL on various hostgroups +- it happends AWS Aurora bundle PEM certificates to ProxySQL's mysql-ssl_p2s_ca +- creates new connections +*/ + +inline unsigned long long monotonic_time() { + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + return (((unsigned long long) ts.tv_sec) * 1000000) + (ts.tv_nsec / 1000); +} + +int main(int argc, char** argv) { + CommandLine cl; + + if(cl.getEnv()) + return exit_status(); + + + char * p_infra_datadir = std::getenv("REGULAR_INFRA_DATADIR"); + if (p_infra_datadir == NULL) { + // quick exit + plan(1); + ok(0, "REGULAR_INFRA_DATADIR not defined"); + return exit_status(); + } + + std::vector hgs = {}; + std::vector pemfiles = {}; + { + DIR *d; + struct dirent *dir; + std::string path = std::string(cl.workdir) + "/aws_ssl_certs/"; + d = opendir(path.c_str()); + if (d) { + while ((dir = readdir(d)) != NULL) { + std::string n = std::string(dir->d_name); + if (n.size() > 4) { + std::string ext = n.substr(n.size() - 4); + if (ext == ".pem") { + } + diag("Retrieved PEM: %s", dir->d_name); + pemfiles.push_back(dir->d_name); + } + } + closedir(d); + } + } + + if (pemfiles.size() == 0) { + // quick exit + plan(1); + ok(0, "No PEM files found"); + return exit_status(); + } + + MYSQL* mysqladmin = mysql_init(NULL); + if (!mysqladmin) + return exit_status(); + + if (!mysql_real_connect(mysqladmin, cl.host, cl.admin_username, cl.admin_password, NULL, cl.admin_port, NULL, 0)) { + fprintf(stderr, "File %s, line %d, Error: %s\n", + __FILE__, __LINE__, mysql_error(mysqladmin)); + return exit_status(); + } + + MYSQL_RES *res; + + + { + const char *q = "SELECT DISTINCT hostgroup_id FROM runtime_mysql_servers WHERE status='ONLINE' AND hostgroup_id IN (0,1,10,11,20,30,31,50,60,1710,1711)"; + diag("Running query: %s", q); + MYSQL_QUERY(mysqladmin, q); + res = mysql_store_result(mysqladmin); + MYSQL_ROW row; + unsigned long long num_rows = mysql_num_rows(res); + while ((row = mysql_fetch_row(res))) { + int hg = atoi(row[0]); + diag("Retrieve HG id: %d", hg); + hgs.push_back(hg); + } + mysql_free_result(res); + } + + if (hgs.size() > 0 ) { + plan(hgs.size()*pemfiles.size()); + } else { + // quick exit + plan(1); + ok(0, "No hostgroups found"); + return exit_status(); + } + + diag("Setting use_ssl=1 on mysql_servers"); + MYSQL_QUERY(mysqladmin, "UPDATE mysql_servers SET use_ssl=1 WHERE hostgroup_id IN (0,1,10,11,20,30,31,50,60,1710,1711)"); + MYSQL_QUERY(mysqladmin, "LOAD MYSQL SERVERS TO RUNTIME"); + + diag("Setting mysql-ssl_p2s_ca"); + MYSQL_QUERY(mysqladmin, "SET mysql-ssl_p2s_ca='cert-bundle-rnd.pem'"); + MYSQL_QUERY(mysqladmin, "LOAD MYSQL VARIABLES TO RUNTIME"); + + for (std::vector::iterator it = pemfiles.begin(); it != pemfiles.end(); it++ ) { + std::string cmd = "cat " + std::string(cl.workdir) + "/aws_ssl_certs/" + *it + " >> " + p_infra_datadir + "/cert-bundle-rnd.pem"; + diag("Running shell command: %s", cmd.c_str()); + system(cmd.c_str()); + for (int i=0; i