Skip to content

Commit ca06765

Browse files
committed
Add error handling for invalid encryption keys with logging
- Wrapped pbkdf2_hmac in a begin-rescue block to catch encryption key errors. - Added detailed error logging for failed key derivation.
1 parent d6ff5b2 commit ca06765

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

lib/cloud_controller/encryptor.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,20 @@ def run_cipher(cipher, input, salt, key, iterations:)
8383
if deprecated_short_salt?(salt)
8484
cipher.pkcs5_keyivgen(key, salt)
8585
else
86-
cipher.key = OpenSSL::PKCS5.pbkdf2_hmac(key, salt, iterations, 16, OpenSSL::Digest.new('SHA256'))
86+
begin
87+
cipher.key = OpenSSL::PKCS5.pbkdf2_hmac(key, salt, iterations, 16, OpenSSL::Digest.new('SHA256'))
88+
rescue StandardError => e
89+
if key.nil?
90+
logger.error("Failed to derive cipher key due to missing key for encryption_key_label=#{current_encryption_key_label}: #{e.class}: #{e.message}")
91+
elsif salt.nil?
92+
logger.error("Failed to derive cipher key due to missing salt: #{e.class}: #{e.message}")
93+
elsif input.nil?
94+
logger.error("Failed to derive cipher key due to missing input: #{e.class}: #{e.message}")
95+
elsif !iterations.is_a?(Integer)
96+
logger.error("Failed to derive cipher key due to wrong type of iterations (must be integer): #{e.class}: #{e.message}")
97+
end
98+
raise
99+
end
87100
cipher.iv = salt
88101
end
89102
cipher.update(input) << cipher.final

0 commit comments

Comments
 (0)