Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
8 changes: 4 additions & 4 deletions spec/integration/container/kms_encryption_triggers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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

Expand All @@ -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|
Expand Down
Loading