|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "semian/adapter" |
| 4 | +require "active_record" |
| 5 | + |
| 6 | +module Semian |
| 7 | + module ActiveRecordAdapter |
| 8 | + QUERY_ALLOWLIST = %r{\A(?:/\*.*?\*/)?\s*(ROLLBACK|COMMIT|RELEASE\s+SAVEPOINT)}i |
| 9 | + |
| 10 | + module ClassMethods |
| 11 | + def query_allowlisted?(sql, *) |
| 12 | + # COMMIT, ROLLBACK |
| 13 | + tx_command_statement = sql.end_with?("T", "K") |
| 14 | + |
| 15 | + # RELEASE SAVEPOINT. Nesting past _3 levels won't get bypassed. |
| 16 | + # Active Record does not send trailing spaces or `;`, so we are in the realm of hand crafted queries here. |
| 17 | + savepoint_statement = sql.end_with?("_1", "_2") |
| 18 | + unclear = sql.end_with?(" ", ";") |
| 19 | + |
| 20 | + if !tx_command_statement && !savepoint_statement && !unclear |
| 21 | + false |
| 22 | + else |
| 23 | + QUERY_ALLOWLIST.match?(sql) |
| 24 | + end |
| 25 | + rescue ArgumentError |
| 26 | + return false unless sql.valid_encoding? |
| 27 | + |
| 28 | + raise |
| 29 | + end |
| 30 | + end |
| 31 | + |
| 32 | + class << self |
| 33 | + def included(base) |
| 34 | + base.extend(ClassMethods) |
| 35 | + base.class_eval do |
| 36 | + attr_reader(:raw_semian_options, :semian_identifier) |
| 37 | + end |
| 38 | + end |
| 39 | + end |
| 40 | + |
| 41 | + def initialize(*options) |
| 42 | + *, config = options |
| 43 | + config = config.dup |
| 44 | + @raw_semian_options = config.delete(:semian) |
| 45 | + @semian_identifier = begin |
| 46 | + name = semian_options && semian_options[:name] |
| 47 | + unless name |
| 48 | + host = config[:host] || "localhost" |
| 49 | + port = config[:port] || semian_adapter_default_port |
| 50 | + name = "#{host}:#{port}" |
| 51 | + end |
| 52 | + :"#{semian_adapter_identifier_prefix}_#{name}" |
| 53 | + end |
| 54 | + super |
| 55 | + end |
| 56 | + |
| 57 | + if ActiveRecord.version >= Gem::Version.new("8.2.a") |
| 58 | + def execute_intent(intent) |
| 59 | + return super if self.class.query_allowlisted?(intent.processed_sql) |
| 60 | + |
| 61 | + acquire_semian_resource(adapter: semian_adapter_name, scope: :query) do |
| 62 | + super |
| 63 | + end |
| 64 | + end |
| 65 | + else |
| 66 | + def raw_execute(sql, *args, **kwargs, &block) |
| 67 | + if self.class.query_allowlisted?(sql) |
| 68 | + super |
| 69 | + else |
| 70 | + acquire_semian_resource(adapter: semian_adapter_name, scope: :query) do |
| 71 | + super(sql, *args, **kwargs, &block) |
| 72 | + end |
| 73 | + end |
| 74 | + end |
| 75 | + end |
| 76 | + |
| 77 | + def active? |
| 78 | + acquire_semian_resource(adapter: semian_adapter_name, scope: :ping) do |
| 79 | + super |
| 80 | + end |
| 81 | + rescue resource_busy_error_class, circuit_open_error_class |
| 82 | + false |
| 83 | + end |
| 84 | + |
| 85 | + def with_resource_timeout |
| 86 | + raise NotImplementedError, "#{self.class} must implement a `with_resource_timeout` method" |
| 87 | + end |
| 88 | + |
| 89 | + private |
| 90 | + |
| 91 | + def resource_exceptions |
| 92 | + [ |
| 93 | + ActiveRecord::AdapterTimeout, |
| 94 | + ActiveRecord::ConnectionFailed, |
| 95 | + ActiveRecord::ConnectionNotEstablished, |
| 96 | + ] |
| 97 | + end |
| 98 | + |
| 99 | + def resource_busy_error_class |
| 100 | + self.class::ResourceBusyError |
| 101 | + end |
| 102 | + |
| 103 | + def circuit_open_error_class |
| 104 | + self.class::CircuitOpenError |
| 105 | + end |
| 106 | + |
| 107 | + def connect(*args) |
| 108 | + acquire_semian_resource(adapter: semian_adapter_name, scope: :connection) do |
| 109 | + super |
| 110 | + end |
| 111 | + end |
| 112 | + |
| 113 | + def semian_adapter_name |
| 114 | + raise NotImplementedError, "#{self.class} must implement an `semian_adapter_name` method" |
| 115 | + end |
| 116 | + |
| 117 | + def semian_adapter_default_port |
| 118 | + raise NotImplementedError, "#{self.class} must implement an `semian_adapter_default_port` method" |
| 119 | + end |
| 120 | + |
| 121 | + def semian_adapter_identifier_prefix |
| 122 | + raise NotImplementedError, "#{self.class} must implement an `semian_adapter_identifier_prefix` method" |
| 123 | + end |
| 124 | + end |
| 125 | +end |
0 commit comments