Skip to content

Commit d227ca5

Browse files
committed
Throw appropriate error instead of UnknownError for GET /v3/service_credential_bindings/:binding_guid/details when encryption-key-label is invalid
1 parent f984d22 commit d227ca5

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

app/controllers/v3/service_credential_bindings_controller.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,15 @@ 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-
service_credential_binding.credentials
136+
begin
137+
service_credential_binding.credentials
138+
rescue StandardError => e
139+
logger.error("Failed to decrypt credentials: #{e.message}")
140+
raise CloudController::Errors::ApiError.new_from_details('UnprocessableEntity',
141+
"Cannot read credentials for \
142+
service_credential_binding \
143+
with guid: #{service_credential_binding.guid}")
144+
end
137145
end
138146

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

lib/cloud_controller/encryptor.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,11 @@ def decrypt(encrypted_input, salt, iterations:, label: nil)
4747
return unless encrypted_input
4848

4949
key = key_to_use(label)
50-
51-
decrypt_raw(encrypted_input, key, salt, iterations:)
50+
begin
51+
decrypt_raw(encrypted_input, key, salt, iterations:)
52+
rescue OpenSSL::Cipher::CipherError, StandardError => e
53+
raise StandardError.new("Decryption failed: #{e.message}")
54+
end
5255
end
5356

5457
def decrypt_raw(encrypted_input, key, salt, iterations:)

spec/request/service_credential_bindings_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,27 @@ def check_filtered_bindings(*bindings)
624624
}
625625
end
626626

627+
context 'when the encryption_key_label is invalid' do
628+
before do
629+
VCAP::CloudController::Encryptor.database_encryption_keys = {
630+
encryption_key_0: 'somevalidkeyvalue',
631+
foo: 'fooencryptionkey',
632+
death: 'headbangingdeathmetalkey', 'invalid-key-label': 'fakekey'
633+
}
634+
end
635+
636+
it 'fails to decrypt the credentials and returns a 500 error' do
637+
app_binding.class.db[:service_bindings].where(id: app_binding.id).update(encryption_key_label: 'invalid-key-label')
638+
639+
allow(VCAP::CloudController::Encryptor).to receive(:decrypt_raw).and_raise(StandardError.new('Decryption failed'))
640+
641+
api_call.call(admin_headers)
642+
643+
expect(last_response).to have_status_code(422)
644+
expect(parsed_response['errors'].first['detail']).to match(/Cannot read credentials/i)
645+
end
646+
end
647+
627648
context "last binding operation is in 'create succeeded' state" do
628649
before do
629650
app_binding.save_with_attributes_and_new_operation({}, { type: 'create', state: 'succeeded' })

0 commit comments

Comments
 (0)