|
| 1 | +#ifndef PROXYSQL_MYSQL_PASSTHROUGH_AUTH_CACHE_H |
| 2 | +#define PROXYSQL_MYSQL_PASSTHROUGH_AUTH_CACHE_H |
| 3 | + |
| 4 | +#include <pthread.h> |
| 5 | +#include <atomic> |
| 6 | +#include <cstddef> |
| 7 | +#include <cstdint> |
| 8 | +#include <deque> |
| 9 | +#include <string> |
| 10 | +#include <unordered_map> |
| 11 | +#include <vector> |
| 12 | + |
| 13 | +/** |
| 14 | + * @brief Forward declaration of re2::RE2 to keep this header light. |
| 15 | + * |
| 16 | + * The full re2/re2.h pulls in a large set of headers and dependencies; we |
| 17 | + * only need a pointer to RE2 in the class state, so a forward declaration |
| 18 | + * suffices. The translation unit that owns the compiled regex |
| 19 | + * (lib/MySQL_Passthrough_Auth_Cache.cpp) includes the full header. |
| 20 | + */ |
| 21 | +namespace re2 { class RE2; } |
| 22 | + |
| 23 | +#ifdef DEBUG |
| 24 | +#define MYSQL_PASSTHROUGH_AUTH_CACHE_DEB "_DEBUG" |
| 25 | +#else |
| 26 | +#define MYSQL_PASSTHROUGH_AUTH_CACHE_DEB "" |
| 27 | +#endif |
| 28 | +#define MYSQL_PASSTHROUGH_AUTH_CACHE_VERSION "0.1.0000" MYSQL_PASSTHROUGH_AUTH_CACHE_DEB |
| 29 | + |
| 30 | +struct passthrough_entry_view { |
| 31 | + std::string username; |
| 32 | + uint64_t learned_at_us; |
| 33 | + int hostgroup_probed; |
| 34 | +}; |
| 35 | + |
| 36 | +class MySQL_Passthrough_Auth_Cache { |
| 37 | + private: |
| 38 | + struct entry_t { |
| 39 | + std::string cleartext_password; |
| 40 | + uint64_t learned_at_us; |
| 41 | + int hostgroup_probed; |
| 42 | + }; |
| 43 | + mutable pthread_rwlock_t lock; |
| 44 | + std::unordered_map<std::string, entry_t> entries; |
| 45 | + std::atomic<int> inflight_probes; |
| 46 | + |
| 47 | + /** |
| 48 | + * @brief Atomic counters for operational observability (spec §7.4 follow-up). |
| 49 | + * |
| 50 | + * Exposed via @c stats_mysql_passthrough_auth_metrics. Each |
| 51 | + * counter increments at exactly one well-defined point in |
| 52 | + * @c handler_again___status_AUTHENTICATING_BACKEND_FOR_CLIENT |
| 53 | + * (see the corresponding @c bump_* methods below). Monotonic |
| 54 | + * since process start; reset only by process restart. |
| 55 | + * |
| 56 | + * Naming mirrors the existing @c stats_mysql_global pattern of |
| 57 | + * "what happened" snake-case-counters; no special suffixes. |
| 58 | + */ |
| 59 | + std::atomic<uint64_t> stat_probes_attempted; |
| 60 | + std::atomic<uint64_t> stat_probes_ok; |
| 61 | + std::atomic<uint64_t> stat_probes_failed_credentials; |
| 62 | + std::atomic<uint64_t> stat_probes_failed_transport; |
| 63 | + std::atomic<uint64_t> stat_lockouts_user; |
| 64 | + std::atomic<uint64_t> stat_lockouts_ip; |
| 65 | + std::atomic<uint64_t> stat_inflight_cap_rejects; |
| 66 | + std::atomic<uint64_t> stat_cache_hits; |
| 67 | + std::atomic<uint64_t> stat_cache_invalidations; |
| 68 | + // Sliding-window failure counters (spec §7.2). Per-username and |
| 69 | + // per-source-IP. Mutated only behind failure_lock — a separate |
| 70 | + // mutex from `lock` since these are write-mostly and accessed on |
| 71 | + // every probe. |
| 72 | + mutable pthread_mutex_t failure_lock; |
| 73 | + mutable std::unordered_map<std::string, std::deque<uint64_t>> failures_by_user; |
| 74 | + mutable std::unordered_map<std::string, std::deque<uint64_t>> failures_by_ip; |
| 75 | + |
| 76 | + /** |
| 77 | + * @brief Compiled-regex cache for the username allowlist (spec §7.1). |
| 78 | + * |
| 79 | + * Compiling an re2::RE2 is cheap (single-digit microseconds) but |
| 80 | + * is paid every time a candidate connect is checked. Cache the |
| 81 | + * last-seen pattern string alongside its compiled form so that as |
| 82 | + * long as @c mysql-passthrough_auth_username_pattern is unchanged |
| 83 | + * we hit the compiled form. A pattern change (admin SET, reload) |
| 84 | + * triggers a re-compile under the write lock. |
| 85 | + * |
| 86 | + * @c pattern_lock is a pthread_rwlock so the COMMON case |
| 87 | + * (steady-state pattern, every probe takes the read lock for |
| 88 | + * FullMatch) doesn't serialize through a single mutex. The write |
| 89 | + * lock is taken only when the pattern STRING changes, which |
| 90 | + * happens on admin SET / LOAD MYSQL VARIABLES TO RUNTIME and is |
| 91 | + * effectively rare. re2::RE2::FullMatch is documented as |
| 92 | + * thread-safe on a const RE2 instance, so concurrent readers |
| 93 | + * are fine. |
| 94 | + * |
| 95 | + * Holds a raw pointer (forward-declared above) rather than |
| 96 | + * unique_ptr so we don't need to drag re2/re2.h into this header. |
| 97 | + */ |
| 98 | + mutable pthread_rwlock_t pattern_lock; |
| 99 | + mutable std::string compiled_pattern_str; |
| 100 | + mutable re2::RE2 *compiled_pattern; |
| 101 | + |
| 102 | + public: |
| 103 | + MySQL_Passthrough_Auth_Cache(); |
| 104 | + ~MySQL_Passthrough_Auth_Cache(); |
| 105 | + |
| 106 | + // Look up a cached credential. Returns true on hit (and populates |
| 107 | + // out_cleartext); false on miss. If ttl_s > 0 and the entry is older |
| 108 | + // than ttl_s, the entry is evicted and a miss is returned. |
| 109 | + bool lookup(const std::string& username, std::string& out_cleartext, uint32_t ttl_s); |
| 110 | + |
| 111 | + // Insert or replace a cached credential. |
| 112 | + void insert(const std::string& username, const std::string& cleartext, int hostgroup_probed); |
| 113 | + |
| 114 | + // Evict a single entry. Returns true if the entry was present. |
| 115 | + bool evict(const std::string& username); |
| 116 | + |
| 117 | + // Remove every entry. |
| 118 | + void clear(); |
| 119 | + |
| 120 | + // Number of entries currently held. |
| 121 | + size_t size() const; |
| 122 | + |
| 123 | + // Snapshot of entries (without password) for stats / observability. |
| 124 | + std::vector<passthrough_entry_view> snapshot() const; |
| 125 | + |
| 126 | + // Global in-flight probe counter (spec §7.3). Sessions wishing to |
| 127 | + // start a backend probe call try_acquire_inflight with the current |
| 128 | + // configured cap; on true they MUST pair with release_inflight when |
| 129 | + // the probe completes (success or failure). On false the session |
| 130 | + // must reject the auth with a generic ERR. |
| 131 | + bool try_acquire_inflight(int max_inflight); |
| 132 | + void release_inflight(); |
| 133 | + int inflight() const; |
| 134 | + |
| 135 | + // Sliding-window failure counters (spec §7.2). Sessions check |
| 136 | + // would_lockout before probing; on probe failure, record a |
| 137 | + // failure. window_s defines the sliding window in seconds; older |
| 138 | + // timestamps are dropped lazily on check. |
| 139 | + bool would_lockout_user(const std::string& username, int max_failures, uint32_t window_s) const; |
| 140 | + bool would_lockout_ip(const std::string& ip, int max_failures, uint32_t window_s) const; |
| 141 | + /** |
| 142 | + * @brief Record a probe failure against (username, ip) deques. |
| 143 | + * |
| 144 | + * @param max_keys Operator-tunable cap on the size of each |
| 145 | + * failure map (failures_by_user, failures_by_ip). |
| 146 | + * Driven by @c mysql-passthrough_auth_failure_map_cap. |
| 147 | + * When the map exceeds the cap, evict_oldest |
| 148 | + * reclaims an entry (defense-in-depth against |
| 149 | + * username/IP churn that would otherwise grow |
| 150 | + * the maps unbounded). |
| 151 | + */ |
| 152 | + void record_failure(const std::string& username, const std::string& ip, int max_keys); |
| 153 | + |
| 154 | + /** |
| 155 | + * @brief Observability counters (B7 follow-up). |
| 156 | + * |
| 157 | + * Each @c bump_* method increments the corresponding atomic at |
| 158 | + * the single call site documented in @c MySQL_Session.cpp. The |
| 159 | + * @c metrics_snapshot helper returns the current values for the |
| 160 | + * @c stats_mysql_passthrough_auth_metrics virtual table. |
| 161 | + */ |
| 162 | + void bump_probes_attempted(); |
| 163 | + void bump_probes_ok(); |
| 164 | + void bump_probes_failed_credentials(); |
| 165 | + void bump_probes_failed_transport(); |
| 166 | + void bump_lockouts_user(); |
| 167 | + void bump_lockouts_ip(); |
| 168 | + void bump_inflight_cap_rejects(); |
| 169 | + void bump_cache_hits(); |
| 170 | + void bump_cache_invalidations(); |
| 171 | + |
| 172 | + /** |
| 173 | + * @brief Snapshot of metric counters + current-state gauges. |
| 174 | + * |
| 175 | + * Returns a vector of (name, value) pairs ordered for stable JSON / |
| 176 | + * stats-table output. Values are read with relaxed memory ordering |
| 177 | + * since stats are advisory, not synchronizing. |
| 178 | + */ |
| 179 | + struct metric_kv { |
| 180 | + std::string name; |
| 181 | + uint64_t value; |
| 182 | + }; |
| 183 | + std::vector<metric_kv> metrics_snapshot() const; |
| 184 | + |
| 185 | + /** |
| 186 | + * @brief Check whether @p username matches the configured allowlist |
| 187 | + * regex (spec §7.1, mysql-passthrough_auth_username_pattern). |
| 188 | + * |
| 189 | + * @param username Frontend user attempting pass-through. |
| 190 | + * @param pattern Regex string from the global variable. Empty means |
| 191 | + * "allow every username" (back-compat default). |
| 192 | + * @return @c true when the pattern is empty or @p username FullMatches |
| 193 | + * the compiled regex; @c false when the regex is set and |
| 194 | + * either fails to compile or the username doesn't match. |
| 195 | + * |
| 196 | + * The compiled regex is cached on the class behind @c pattern_lock; |
| 197 | + * a pattern-string change triggers a re-compile on the next call. |
| 198 | + * Match semantics are RE2 FullMatch (the entire username must |
| 199 | + * match), matching how query rules use re2 elsewhere in ProxySQL. |
| 200 | + * A regex that fails to compile is treated as a deny-all -- the |
| 201 | + * fail-safe direction for a security gate. |
| 202 | + */ |
| 203 | + bool username_allowed(const std::string& username, const std::string& pattern); |
| 204 | + |
| 205 | + void print_version(); |
| 206 | +}; |
| 207 | + |
| 208 | +#endif // PROXYSQL_MYSQL_PASSTHROUGH_AUTH_CACHE_H |
0 commit comments