Skip to content

Commit

Permalink
Merge branch 'v3.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
rahim-kanji committed Nov 11, 2024
2 parents b63f510 + 85d8445 commit 6cc0358
Show file tree
Hide file tree
Showing 34 changed files with 2,152 additions and 697 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/CI-3p-postgresql.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: CI-3p-postgresql
run-name: '${{ github.event.workflow_run && github.event.workflow_run.head_branch || github.ref_name }} ${{ github.workflow }} ${{ github.event.workflow_run && github.event.workflow_run.head_sha || github.sha }}'

on:
workflow_dispatch:
workflow_run:
workflows: [ CI-trigger ]
types: [ completed ]

concurrency:
group: ${{ github.workflow }}-${{ github.event.workflow_run && github.event.workflow_run.head_branch || github.ref_name }}
cancel-in-progress: true

jobs:
run:
if: ${{ github.event.workflow_run && github.event.workflow_run.conclusion == 'success' || ! github.event.workflow_run }}
uses: sysown/proxysql/.github/workflows/ci-3p-postgresql.yml@GH-Actions
secrets: inherit
with:
trigger: ${{ toJson(github) }}
infradb: ${{ vars.MATRIX_3P_POSTGRESQL_infradb_pgsql }}
connector: ${{ vars.MATRIX_3P_POSTGRESQL_connector_pgsql }}
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ O3 := -O3 -mtune=native
ALL_DEBUG := $(O0) -ggdb -DDEBUG
NO_DEBUG := $(O2) -ggdb
DEBUG := $(ALL_DEBUG)
CURVER ?= 3.0.0
CURVER ?= 3.0.1
#export DEBUG
#export EXTRALINK
export MAKE
Expand Down Expand Up @@ -314,7 +314,7 @@ amd64-fedora: fedora38 fedora38-clang fedora38-dbg fedora39 fedora39-clang fedor
amd64-opensuse: opensuse15 opensuse15-clang opensuse15-dbg
amd64-ubuntu: ubuntu18 ubuntu18-dbg ubuntu20 ubuntu20-clang ubuntu20-dbg ubuntu22 ubuntu22-clang ubuntu22-dbg ubuntu24 ubuntu24-clang ubuntu24-dbg
amd64-pkglist:
@make -nk amd64-packages 2>/dev/null | grep -Po '(?<=binaries/)proxysql\S+'
@make -nk amd64-packages 2>/dev/null | grep -Po '(?<=binaries/)proxysql\S+$$'

arm64-%: SYS_ARCH := aarch64
arm64-packages: arm64-centos arm64-debian arm64-ubuntu arm64-fedora arm64-opensuse arm64-almalinux
Expand All @@ -325,7 +325,7 @@ arm64-fedora: fedora38 fedora39 fedora40 fedora41
arm64-opensuse: opensuse15
arm64-ubuntu: ubuntu18 ubuntu20 ubuntu22 ubuntu24
arm64-pkglist:
@make -nk arm64-packages 2>/dev/null | grep -Po '(?<=binaries/)proxysql\S+'
@make -nk arm64-packages 2>/dev/null | grep -Po '(?<=binaries/)proxysql\S+$$'

almalinux%: build-almalinux% ;
centos%: build-centos% ;
Expand Down
1 change: 1 addition & 0 deletions deps/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ endif
# patches for replication testing
cd mariadb-client-library/mariadb_client && patch -p0 < ../mariadb_rpl.patch
cd mariadb-client-library/mariadb_client && patch -p0 < ../cmakelists.txt.patch
cd mariadb-client-library/mariadb_client && patch -p0 < ../mariadb_lib.c.metadata_column_check.patch
cd mariadb-client-library/mariadb_client && CC=${CC} CXX=${CXX} ${MAKE} mariadbclient
# cd mariadb-client-library/mariadb_client/include && make my_config.h

Expand Down
21 changes: 11 additions & 10 deletions deps/libscram/src/scram.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void scram_reset_error() {
errorBuffer[0] = '\0';
}

static size_t strlcat(char* dst, const char* src, size_t siz)
static size_t my_strlcat(char* dst, const char* src, size_t siz)
{
char* d = dst;
const char* s = src;
Expand Down Expand Up @@ -434,7 +434,7 @@ char *build_client_final_message(ScramState *scram_state,
client_proof))
goto failed;

len = strlcat(buf, ",p=", sizeof(buf));
len = my_strlcat(buf, ",p=", sizeof(buf));
enclen = pg_b64_enc_len(sizeof(client_proof));
enclen = pg_b64_encode((char *) client_proof,
SCRAM_KEY_LEN,
Expand Down Expand Up @@ -597,15 +597,15 @@ static bool calculate_client_proof(ScramState *scram_state,
&errstr) < 0 ||
scram_ClientKey(scram_state->SaltedPassword, PG_SHA256, SCRAM_KEY_LEN, ClientKey,
&errstr) < 0) {
snprintf(errorBuffer, MAX_ERROR_LENGTH, "could not compute client key: %s", *errstr);
snprintf(errorBuffer, MAX_ERROR_LENGTH, "could not compute client key: %s", (const char*) errstr);
goto failed;
}
}

/* Hash it one more time, and compare with StoredKey */
if (scram_H(ClientKey, PG_SHA256, SCRAM_KEY_LEN,
StoredKey, &errstr) < 0) {
snprintf(errorBuffer, MAX_ERROR_LENGTH, "could not hash stored key: %s", *errstr);
snprintf(errorBuffer, MAX_ERROR_LENGTH, "could not hash stored key: %s", (const char*) errstr);
goto failed;
}
/*
Expand Down Expand Up @@ -656,7 +656,7 @@ bool verify_server_signature(ScramState *scram_state, const PgCredentials *crede
memcpy(ServerKey, credentials->scram_ServerKey, SCRAM_KEY_LEN);
else {
if (scram_ServerKey(scram_state->SaltedPassword, PG_SHA256, SCRAM_KEY_LEN, ServerKey, &errstr) < 0) {
snprintf(errorBuffer, MAX_ERROR_LENGTH, "could not compute server key: %s", *errstr);
snprintf(errorBuffer, MAX_ERROR_LENGTH, "could not compute server key: %s", (const char*) errstr);
goto failed;
}
}
Expand Down Expand Up @@ -907,17 +907,17 @@ static bool build_adhoc_scram_secret(const char *plain_password, ScramState *scr
scram_state->iterations,
salted_password, &errstr) < 0 ||
scram_ClientKey(salted_password, PG_SHA256, SCRAM_KEY_LEN, scram_state->StoredKey, &errstr) < 0) {
snprintf(errorBuffer, MAX_ERROR_LENGTH, "could not compute server key: %s", *errstr);
snprintf(errorBuffer, MAX_ERROR_LENGTH, "could not compute server key: %s", (const char*) errstr);
goto failed;
}

if (scram_H(scram_state->StoredKey, PG_SHA256, SCRAM_KEY_LEN, scram_state->StoredKey, &errstr) < 0) {
snprintf(errorBuffer, MAX_ERROR_LENGTH, "could not hash stored key: %s", *errstr);
snprintf(errorBuffer, MAX_ERROR_LENGTH, "could not hash stored key: %s", (const char*) errstr);
goto failed;
}

if (scram_ServerKey(salted_password, PG_SHA256, SCRAM_KEY_LEN, scram_state->ServerKey, &errstr) < 0) {
snprintf(errorBuffer, MAX_ERROR_LENGTH, "could not calculate server key: %s", *errstr);
snprintf(errorBuffer, MAX_ERROR_LENGTH, "could not calculate server key: %s", (const char*) errstr);
goto failed;
}

Expand Down Expand Up @@ -1148,6 +1148,7 @@ char *build_server_final_message(ScramState *scram_state)
if (!result)
goto failed;
snprintf(result, len, "v=%s", server_signature);
free(server_signature);
return result;

failed:
Expand Down Expand Up @@ -1208,7 +1209,7 @@ bool verify_client_proof(ScramState *state, const char *ClientProof)

/* Hash it one more time, and compare with StoredKey */
if (scram_H(state->ClientKey, PG_SHA256, SCRAM_KEY_LEN, client_StoredKey, &errstr) < 0) {
snprintf(errorBuffer, MAX_ERROR_LENGTH, "could not hash stored key: %s", *errstr);
snprintf(errorBuffer, MAX_ERROR_LENGTH, "could not hash stored key: %s", (const char*) errstr);
goto failed;
}

Expand Down Expand Up @@ -1270,7 +1271,7 @@ bool scram_verify_plain_password(const char *username, const char *password,
/* Compute Server Key based on the user-supplied plaintext password */
if (scram_SaltedPassword(password, PG_SHA256, SCRAM_KEY_LEN, salt, saltlen, iterations, salted_password, &errstr) < 0 ||
scram_ServerKey(salted_password, PG_SHA256, SCRAM_KEY_LEN, computed_key, &errstr) < 0) {
snprintf(errorBuffer, MAX_ERROR_LENGTH, "could not compute server key: %s", *errstr);
snprintf(errorBuffer, MAX_ERROR_LENGTH, "could not compute server key: %s", (const char*) errstr);
goto failed;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
diff --git libmariadb/mariadb_lib.c libmariadb/mariadb_lib.c
index 027167f1..58b8283a 100644
--- libmariadb/mariadb_lib.c
+++ libmariadb/mariadb_lib.c
@@ -3021,6 +3021,12 @@ MYSQL_FIELD *ma_duplicate_resultset_metadata(MYSQL_FIELD *fields, size_t count,
return result;
}

+static uint8_t mysql_encode_length(uint64_t len) {
+ if (len < 251) { return 1; }
+ if (len < 65536) { return 3; }
+ if (len < 16777216) { return 4; }
+ return 9;
+}

int mthd_my_read_query_result(MYSQL *mysql)
{
@@ -3070,6 +3076,13 @@ get_info:

if (has_metadata)
{
+ // integrity-check: the length encoding of the field count from 'column-count' packet
+ // must match the packet length from header, otherwise packet is malformed.
+ ulong enc_len = mysql_encode_length(field_count);
+ if (enc_len != length) {
+ my_set_error(mysql, CR_MALFORMED_PACKET, SQLSTATE_UNKNOWN, 0);
+ return -1;
+ }
// read packet metadata
mysql->fields =
mthd_my_read_metadata(mysql, field_count, 7 + ma_extended_type_info_rows(mysql));
3 changes: 2 additions & 1 deletion include/MySQL_Monitor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,8 @@ struct DNS_Resolve_Data {
std::shared_ptr<DNS_Cache> dns_cache;
std::string hostname;
std::set<std::string> cached_ips;
unsigned int ttl;
unsigned int ttl = 0;
unsigned int refresh_intv = 0;
};


Expand Down
21 changes: 17 additions & 4 deletions include/PgSQL_Connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ enum PgSQL_Param_Name {
PG_SERVICE, // Service name to use for additional parameters
PG_TARGET_SESSION_ATTRS, // Determines whether the session must have certain properties to be acceptable
PG_LOAD_BALANCE_HOSTS, // Controls the order in which the client tries to connect to the available hosts and addresses
// Environment Options
PG_DATESTYLE, // Sets the value of the DateStyle parameter
PG_TIMEZONE, // Sets the value of the TimeZone parameter
PG_GEQO, // Enables or disables the use of the GEQO query optimizer

PG_PARAM_SIZE
};
Expand Down Expand Up @@ -114,7 +118,11 @@ static const char* PgSQL_Param_Name_Str[] = {
"gssdelegation",
"service",
"target_session_attrs",
"load_balance_hosts"
"load_balance_hosts",
// Environment Options
"datestyle",
"timezone",
"geqo"
};

struct Param_Name_Validation {
Expand Down Expand Up @@ -172,7 +180,10 @@ static const Param_Name_Validation* PgSQL_Param_Name_Accepted_Values[PG_PARAM_SI
nullptr,
nullptr,
&target_session_attrs,
&load_balance_hosts
&load_balance_hosts,
nullptr,
nullptr,
nullptr
};

#define PG_EVENT_NONE 0x00
Expand Down Expand Up @@ -211,6 +222,7 @@ class PgSQL_Conn_Param {
}

bool set_value(PgSQL_Param_Name key, const char* val) {
if (key == -1) return false;
if (validate(key, val)) {
if (param_value[key]) {
free(param_value[key]);
Expand Down Expand Up @@ -249,8 +261,9 @@ class PgSQL_Conn_Param {
break;
}
}

assert(key != -1);
if (key == -1) {
proxy_warning("Unrecognized connection option. Please report this as a bug for future enhancements:%s\n", name);
}
return key;
}

Expand Down
2 changes: 1 addition & 1 deletion include/PgSQL_HostGroups_Manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,7 @@ class PgSQL_HostGroups_Manager : public Base_HostGroups_Manager<PgSQL_HGC> {
void replication_lag_action_inner(PgSQL_HGC *, const char*, unsigned int, int);
void replication_lag_action(const std::list<replication_lag_server_t>& pgsql_servers);
void read_only_action(char *hostname, int port, int read_only);
void read_only_action_v2(const std::list<read_only_server_t>& pgsql_servers);
void read_only_action_v2(const std::list<read_only_server_t>& pgsql_servers, bool writer_is_also_reader);
unsigned int get_servers_table_version();
void wait_servers_table_version(unsigned, unsigned);
bool shun_and_killall(char *hostname, int port);
Expand Down
10 changes: 9 additions & 1 deletion include/PgSQL_Monitor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

#define MONITOR_SQLITE_TABLE_PGSQL_SERVER_PING_LOG "CREATE TABLE pgsql_server_ping_log ( hostname VARCHAR NOT NULL , port INT NOT NULL DEFAULT 3306 , time_start_us INT NOT NULL DEFAULT 0 , ping_success_time_us INT DEFAULT 0 , ping_error VARCHAR , PRIMARY KEY (hostname, port, time_start_us))"

#define MONITOR_SQLITE_TABLE_PGSQL_SERVER_READ_ONLY_LOG "CREATE TABLE pgsql_server_read_only_log ( hostname VARCHAR NOT NULL , port INT NOT NULL DEFAULT 3306 , time_start_us INT NOT NULL DEFAULT 0 , success_time_us INT DEFAULT 0 , read_only INT DEFAULT 1 , error VARCHAR , PRIMARY KEY (hostname, port, time_start_us))"

#define MONITOR_SQLITE_TABLE_PGSQL_SERVERS "CREATE TABLE pgsql_servers (hostname VARCHAR NOT NULL , port INT NOT NULL DEFAULT 3306 , status INT CHECK (status IN (0, 1, 2, 3, 4)) NOT NULL DEFAULT 0 , use_ssl INT CHECK (use_ssl IN(0,1)) NOT NULL DEFAULT 0 , PRIMARY KEY (hostname, port) )"

#define MONITOR_SQLITE_TABLE_PROXYSQL_SERVERS "CREATE TABLE proxysql_servers (hostname VARCHAR NOT NULL , port INT NOT NULL DEFAULT 6032 , weight INT CHECK (weight >= 0) NOT NULL DEFAULT 0 , comment VARCHAR NOT NULL DEFAULT '' , PRIMARY KEY (hostname, port) )"
Expand All @@ -35,6 +37,8 @@ struct PgSQL_Monitor {
uint64_t connect_check_OK { 0 };
uint64_t ping_check_ERR { 0 };
uint64_t ping_check_OK { 0 };
uint64_t readonly_check_ERR { 0 };
uint64_t readonly_check_OK { 0 };
///////////////////////////////////////////////////////////////////////////

std::vector<table_def_t> tables_defs_monitor {
Expand All @@ -45,7 +49,11 @@ struct PgSQL_Monitor {
{
const_cast<char*>("pgsql_server_ping_log"),
const_cast<char*>(MONITOR_SQLITE_TABLE_PGSQL_SERVER_PING_LOG)
}
},
{
const_cast<char*>("pgsql_server_read_only_log"),
const_cast<char*>(MONITOR_SQLITE_TABLE_PGSQL_SERVER_READ_ONLY_LOG)
},
};

std::vector<table_def_t> tables_defs_monitor_internal {
Expand Down
4 changes: 4 additions & 0 deletions include/PgSQL_Thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -801,16 +801,19 @@ class PgSQL_Threads_Handler

int monitor_history;
int monitor_connect_interval;
int monitor_connect_interval_window;
int monitor_connect_timeout;
//! Monitor ping interval. Unit: 'ms'.
int monitor_ping_interval;
int monitor_ping_interval_window;
int monitor_ping_max_failures;
//! Monitor ping timeout. Unit: 'ms'.
int monitor_ping_timeout;
//! Monitor aws rds topology discovery interval. Unit: 'one discovery check per X monitor_read_only checks'.
int monitor_aws_rds_topology_discovery_interval;
//! Monitor read only timeout. Unit: 'ms'.
int monitor_read_only_interval;
int monitor_read_only_interval_window;
//! Monitor read only timeout. Unit: 'ms'.
int monitor_read_only_timeout;
int monitor_read_only_max_timeout_count;
Expand Down Expand Up @@ -848,6 +851,7 @@ class PgSQL_Threads_Handler
int monitor_local_dns_resolver_queue_maxsize;
char* monitor_username;
char* monitor_password;
char* monitor_dbname;
char* monitor_replication_lag_use_percona_heartbeat;
int ping_interval_server_msec;
int ping_timeout_server;
Expand Down
12 changes: 12 additions & 0 deletions include/gen_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,18 @@ inline unsigned long long realtime_time() {
return (((unsigned long long) ts.tv_sec) * 1000000) + (ts.tv_nsec / 1000);
}

template<int FACTOR, typename T>
inline T overflow_safe_multiply(T val) {
static_assert(std::is_integral<T>::value, "T must be an integer type.");
static_assert(std::is_unsigned_v<T>, "T must be an unsigned integer type.");
static_assert(FACTOR > 0, "Negative factors are not supported.");

if constexpr (FACTOR == 0) return 0;
if (val == 0) return 0;
if (val > std::numeric_limits<T>::max() / FACTOR) return std::numeric_limits<T>::max();
return (val * FACTOR);
}

#endif /* __GEN_FUNCTIONS */

bool Proxy_file_exists(const char *);
Expand Down
3 changes: 2 additions & 1 deletion include/proxysql_glovars.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ class ProxySQL_GlobalVariables {
unsigned long long start_time;
bool gdbg;
bool nostart;
bool monitor;
bool my_monitor;
bool pg_monitor;
bool version_check;
#ifdef SO_REUSEPORT
bool reuseport;
Expand Down
11 changes: 11 additions & 0 deletions include/proxysql_structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,7 @@ enum PROXYSQL_MYSQL_ERR {
ER_PROXYSQL_AWS_HEALTH_CHECK_TIMEOUT = 9018,
ER_PROXYSQL_SRV_NULL_REPLICATION_LAG = 9019,
ER_PROXYSQL_CONNECT_TIMEOUT = 9020,
ER_PROXYSQL_READONLY_TIMEOUT = 9021,
};

enum proxysql_session_type {
Expand Down Expand Up @@ -1085,16 +1086,21 @@ __thread int pgsql_thread___query_processor_regex;
__thread bool pgsql_thread___monitor_enabled;
__thread int pgsql_thread___monitor_history;
__thread int pgsql_thread___monitor_connect_interval;
__thread int pgsql_thread___monitor_connect_interval_window;
__thread int pgsql_thread___monitor_connect_timeout;
__thread int pgsql_thread___monitor_ping_interval;
__thread int pgsql_thread___monitor_ping_interval_window;
__thread int pgsql_thread___monitor_ping_max_failures;
__thread int pgsql_thread___monitor_ping_timeout;
__thread int pgsql_thread___monitor_read_only_interval;
__thread int pgsql_thread___monitor_read_only_interval_window;
__thread int pgsql_thread___monitor_read_only_timeout;
__thread int pgsql_thread___monitor_read_only_max_timeout_count;
__thread bool pgsql_thread___monitor_writer_is_also_reader;
__thread int pgsql_thread___monitor_threads;
__thread char* pgsql_thread___monitor_username;
__thread char* pgsql_thread___monitor_password;
__thread char* pgsql_thread___monitor_dbname;

// PgSQL Query Cache
__thread int pgsql_thread___query_cache_size_MB;
Expand Down Expand Up @@ -1376,16 +1382,21 @@ extern __thread int pgsql_thread___query_processor_regex;
extern __thread bool pgsql_thread___monitor_enabled;
extern __thread int pgsql_thread___monitor_history;
extern __thread int pgsql_thread___monitor_connect_interval;
extern __thread int pgsql_thread___monitor_connect_interval_window;
extern __thread int pgsql_thread___monitor_connect_timeout;
extern __thread int pgsql_thread___monitor_ping_interval;
extern __thread int pgsql_thread___monitor_ping_interval_window;
extern __thread int pgsql_thread___monitor_ping_max_failures;
extern __thread int pgsql_thread___monitor_ping_timeout;
extern __thread int pgsql_thread___monitor_read_only_interval;
extern __thread int pgsql_thread___monitor_read_only_interval_window;
extern __thread int pgsql_thread___monitor_read_only_timeout;
extern __thread int pgsql_thread___monitor_read_only_max_timeout_count;
extern __thread bool pgsql_thread___monitor_writer_is_also_reader;
extern __thread int pgsql_thread___monitor_threads;
extern __thread char* pgsql_thread___monitor_username;
extern __thread char* pgsql_thread___monitor_password;
extern __thread char* pgsql_thread___monitor_dbname;

// PgSQL Query Cache
extern __thread int pgsql_thread___query_cache_size_MB;
Expand Down
Loading

0 comments on commit 6cc0358

Please sign in to comment.