From 656d97f121ad930551b479184d21ddd297a1c7cb Mon Sep 17 00:00:00 2001 From: aaron-congo Date: Tue, 20 Oct 2026 12:56:27 -0700 Subject: [PATCH] chore: ship the KMS trigger SQL with the gem and stamp its version Move the KMS Encryption Plugin's PostgreSQL and MySQL trigger SQL from the integration test resources to lib/aws_advanced_ruby_driver_wrapper/plugins/ kms_encryption/sql/, so the published SQL has one source the docs can link to and users get it with the gem. The integration triggers spec now reads the files from there. Each file now records its version in a database comment (PostgreSQL v2 on enforce_encrypted_column, MySQL v1 on validate_encrypted_data_hmac_before_insert), so users can check which version is installed and whether to re-run it. --- .../plugins/kms_encryption/sql/trigger_mysql.sql | 4 ++++ .../plugins/kms_encryption/sql/trigger_pg.sql | 4 ++++ .../integration/container/kms_encryption_triggers_spec.rb | 8 ++++---- 3 files changed, 12 insertions(+), 4 deletions(-) rename spec/integration/host/src/test/resources/sql/kms_encryption_trigger_mysql.sql => lib/aws_advanced_ruby_driver_wrapper/plugins/kms_encryption/sql/trigger_mysql.sql (92%) rename spec/integration/host/src/test/resources/sql/kms_encryption_trigger_pg.sql => lib/aws_advanced_ruby_driver_wrapper/plugins/kms_encryption/sql/trigger_pg.sql (88%) diff --git a/spec/integration/host/src/test/resources/sql/kms_encryption_trigger_mysql.sql b/lib/aws_advanced_ruby_driver_wrapper/plugins/kms_encryption/sql/trigger_mysql.sql similarity index 92% rename from spec/integration/host/src/test/resources/sql/kms_encryption_trigger_mysql.sql rename to lib/aws_advanced_ruby_driver_wrapper/plugins/kms_encryption/sql/trigger_mysql.sql index b9122239..9939f2c1 100644 --- a/spec/integration/host/src/test/resources/sql/kms_encryption_trigger_mysql.sql +++ b/lib/aws_advanced_ruby_driver_wrapper/plugins/kms_encryption/sql/trigger_mysql.sql @@ -57,12 +57,16 @@ END$$ -- Called from a BEFORE INSERT trigger (and by the BEFORE UPDATE procedure) to reject a value that is -- not a valid kms_encryption payload. Replace SCHEMA_NAME with encryption_metadata_schema. +-- The COMMENT records which version of this SQL is installed. Check it with: +-- SELECT ROUTINE_COMMENT FROM information_schema.ROUTINES +-- WHERE ROUTINE_SCHEMA = DATABASE() AND ROUTINE_NAME = 'validate_encrypted_data_hmac_before_insert'; DROP PROCEDURE IF EXISTS validate_encrypted_data_hmac_before_insert$$ CREATE PROCEDURE validate_encrypted_data_hmac_before_insert( IN p_table_name VARCHAR(64), IN p_column_name VARCHAR(64), IN column_value VARBINARY(65535) ) +COMMENT 'aws-advanced-ruby-driver-wrapper kms_encryption trigger v1' BEGIN DECLARE v_hmac_key VARBINARY(32); diff --git a/spec/integration/host/src/test/resources/sql/kms_encryption_trigger_pg.sql b/lib/aws_advanced_ruby_driver_wrapper/plugins/kms_encryption/sql/trigger_pg.sql similarity index 88% rename from spec/integration/host/src/test/resources/sql/kms_encryption_trigger_pg.sql rename to lib/aws_advanced_ruby_driver_wrapper/plugins/kms_encryption/sql/trigger_pg.sql index bb7376b2..11998b33 100644 --- a/spec/integration/host/src/test/resources/sql/kms_encryption_trigger_pg.sql +++ b/lib/aws_advanced_ruby_driver_wrapper/plugins/kms_encryption/sql/trigger_pg.sql @@ -48,3 +48,7 @@ BEGIN RETURN NEW; END; $$ LANGUAGE plpgsql; + +-- Records which version of this SQL is installed. Check it with: +-- SELECT obj_description('enforce_encrypted_column'::regproc, 'pg_proc'); +COMMENT ON FUNCTION enforce_encrypted_column() IS 'aws-advanced-ruby-driver-wrapper kms_encryption trigger v2'; diff --git a/spec/integration/container/kms_encryption_triggers_spec.rb b/spec/integration/container/kms_encryption_triggers_spec.rb index e0c0b288..6dc6bc4a 100644 --- a/spec/integration/container/kms_encryption_triggers_spec.rb +++ b/spec/integration/container/kms_encryption_triggers_spec.rb @@ -29,7 +29,7 @@ # Server-side enforcement. The plugin cannot guarantee an encrypted column never holds a plaintext - # only a database trigger can, by verifying each value's HMAC tag as it goes in (the HMAC key is # stored in key_storage, so the server can check integrity without any data key). This installs the -# trigger SQL from host/src/test/resources/sql on both engines: a value in the plugin's real +# trigger SQL the gem ships in lib/.../plugins/kms_encryption/sql on both engines: a value in the plugin's real # encrypted format is accepted, a plaintext is rejected, and a random binary value that is not a # valid payload is rejected. After a data key rotation, rows written with the previous key can still # be updated, while a changed value must carry the current key's HMAC tag. @@ -162,13 +162,13 @@ # -- trigger installation -- def sql_resource(name) - File.read(File.expand_path("host/src/test/resources/sql/#{name}", "#{__dir__}/..")) + File.read(File.expand_path("../../../lib/aws_advanced_ruby_driver_wrapper/plugins/kms_encryption/sql/#{name}", __dir__)) end # PostgreSQL understands the file's dollar-quoted bodies and multiple statements in one call. The # file names the default metadata schema, encrypt, which the specs replace with their own. def install_pg_trigger_functions(conn) - conn.exec(sql_resource('kms_encryption_trigger_pg.sql') + conn.exec(sql_resource('trigger_pg.sql') .gsub(/\bencrypt\.(encryption_metadata|key_storage)\b/, "#{metadata_schema}.\\1")) end @@ -183,7 +183,7 @@ def create_pg_trigger(conn) # The MySQL file is written for the mysql CLI (DELIMITER $$), which no driver understands, so the # DELIMITER directives are stripped and each $$-separated statement is sent on its own. def install_mysql_trigger_functions(conn) - sql = sql_resource('kms_encryption_trigger_mysql.sql').gsub('SCHEMA_NAME', metadata_schema) + sql = sql_resource('trigger_mysql.sql').gsub('SCHEMA_NAME', metadata_schema) body = sql.lines.reject { |line| line.strip.start_with?('DELIMITER') }.join body.split('$$').each do |chunk|