Skip to content

Commit e90425e

Browse files
committed
handle cipher error in central place in applications controller
1 parent 3e15dbf commit e90425e

File tree

3 files changed

+4
-14
lines changed

3 files changed

+4
-14
lines changed

app/controllers/v3/service_credential_bindings_controller.rb

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,7 @@ def details
133133
credentials = if service_credential_binding.is_a?(ServiceKey) && service_credential_binding.credhub_reference?
134134
fetch_credentials_value(service_credential_binding.credhub_reference)
135135
else
136-
begin
137-
service_credential_binding.credentials
138-
rescue VCAP::CloudController::Encryptor::KeyDerivationError => e
139-
logger.error("Failed to decrypt credentials: #{e.message}")
140-
raise CloudController::Errors::V3::ApiError.new_from_details('InternalServerError', 'Failed to decrypt credentials')
141-
end
136+
service_credential_binding.credentials
142137
end
143138

144139
details = Presenters::V3::ServiceCredentialBindingDetailsPresenter.new(

lib/cloud_controller/encryptor.rb

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
module VCAP::CloudController
99
module Encryptor
1010
ENCRYPTION_ITERATIONS = 2048
11-
class KeyDerivationError < StandardError; end
11+
1212
class << self
1313
ALGORITHM = 'AES-128-CBC'.freeze
1414

@@ -83,12 +83,7 @@ def run_cipher(cipher, input, salt, key, iterations:)
8383
if deprecated_short_salt?(salt)
8484
cipher.pkcs5_keyivgen(key, salt)
8585
else
86-
begin
87-
cipher.key = OpenSSL::PKCS5.pbkdf2_hmac(key, salt, iterations, 16, OpenSSL::Digest.new('SHA256'))
88-
rescue OpenSSL::Cipher::CipherError => e
89-
logger.error("Failed to derive cipher key due to missing key for encryption_key_label=#{current_encryption_key_label}: #{e.class}: #{e.message}") if key.nil?
90-
raise KeyDerivationError
91-
end
86+
cipher.key = OpenSSL::PKCS5.pbkdf2_hmac(key, salt, iterations, 16, OpenSSL::Digest.new('SHA256'))
9287
cipher.iv = salt
9388
end
9489
cipher.update(input) << cipher.final

spec/request/service_credential_bindings_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ def check_filtered_bindings(*bindings)
637637
it 'fails to decrypt the credentials and returns a 500 error' do
638638
app_binding.class.db[:service_bindings].where(id: app_binding.id).update(encryption_key_label: 'invalid-key-label')
639639

640-
allow(VCAP::CloudController::Encryptor).to receive(:run_cipher).and_raise(VCAP::CloudController::Encryptor::KeyDerivationError)
640+
allow(VCAP::CloudController::Encryptor).to receive(:run_cipher).and_raise(OpenSSL::Cipher::CipherError)
641641
api_call.call(admin_headers)
642642

643643
expect(last_response).to have_status_code(500)

0 commit comments

Comments
 (0)