File tree Expand file tree Collapse file tree 6 files changed +92
-2
lines changed Expand file tree Collapse file tree 6 files changed +92
-2
lines changed Original file line number Diff line number Diff line change @@ -93,6 +93,7 @@ class ApplicationController < ActionController::Base
93
93
rescue_from CloudController ::Errors ::CompoundError , with : :handle_compound_error
94
94
rescue_from ActionDispatch ::Http ::Parameters ::ParseError , with : :handle_invalid_request_body
95
95
rescue_from Sequel ::DatabaseConnectionError , Sequel ::DatabaseDisconnectError , with : :handle_db_connection_error
96
+ rescue_from OpenSSL ::Cipher ::CipherError , with : :handle_key_derivation_error
96
97
97
98
def configuration
98
99
Config . config
@@ -219,6 +220,11 @@ def handle_db_connection_error(_)
219
220
handle_api_error ( error )
220
221
end
221
222
223
+ def handle_key_derivation_error ( _ )
224
+ error = CloudController ::Errors ::V3 ::ApiError . new_from_details ( 'InternalServerError' , 'Error while processing encrypted data' )
225
+ handle_api_error ( error )
226
+ end
227
+
222
228
def handle_exception ( error )
223
229
presenter = ErrorPresenter . new ( error , Rails . env . test? , V3ErrorHasher . new ( error ) )
224
230
logger . info ( presenter . log_message )
Original file line number Diff line number Diff line change 17
17
name : UaaRateLimited
18
18
http_code : 429
19
19
message : " The UAA is currently rate limited. Please try again later"
20
+
21
+ 10001 :
22
+ name : InternalServerError
23
+ http_code : 500
24
+ message : " %s"
Original file line number Diff line number Diff line change 3398
3398
end
3399
3399
end
3400
3400
end
3401
+
3402
+ context 'when the encryption_key_label is invalid' do
3403
+ before do
3404
+ allow_any_instance_of ( ErrorPresenter ) . to receive ( :raise_500? ) . and_return ( false )
3405
+ end
3406
+
3407
+ it 'fails to decrypt the environment variables and returns a 500 error' do
3408
+ app_model # ensure that app model is created before run_cipher is mocked to throw an error
3409
+ allow ( VCAP ::CloudController ::Encryptor ) . to receive ( :run_cipher ) . and_raise ( OpenSSL ::Cipher ::CipherError )
3410
+ api_call . call ( admin_headers )
3411
+
3412
+ expect ( last_response ) . to have_status_code ( 500 )
3413
+ expect ( parsed_response [ 'errors' ] . first [ 'detail' ] ) . to match ( /Error while processing encrypted data/i )
3414
+ end
3415
+ end
3401
3416
end
3402
3417
3403
3418
describe 'GET /v3/apps/:guid/permissions' do
Original file line number Diff line number Diff line change @@ -899,6 +899,34 @@ def expect_empty_list(user_headers)
899
899
expect ( response ) . to include ( 'detail' => 'Service broker not found' )
900
900
end
901
901
end
902
+
903
+ context 'when updating credentials and the encryption_key_label is invalid' do
904
+ let ( :broker ) { VCAP ::CloudController ::ServiceBroker . make }
905
+ let ( :api_call ) do
906
+ lambda { |headers |
907
+ patch "/v3/service_brokers/#{ broker . guid } " , { authentication : {
908
+ type : 'basic' ,
909
+ credentials : {
910
+ username : 'your-username' ,
911
+ password : 'your-password'
912
+ }
913
+ } } . to_json , headers
914
+ }
915
+ end
916
+
917
+ before do
918
+ allow_any_instance_of ( ErrorPresenter ) . to receive ( :raise_500? ) . and_return ( false )
919
+ end
920
+
921
+ it 'fails to decrypt the broker data and returns a 500 error' do
922
+ broker # ensure the broker is created before run_cipher is mocked to throw an error
923
+ allow ( VCAP ::CloudController ::Encryptor ) . to receive ( :run_cipher ) . and_raise ( OpenSSL ::Cipher ::CipherError )
924
+ api_call . call ( admin_headers )
925
+
926
+ expect ( last_response ) . to have_status_code ( 500 )
927
+ expect ( parsed_response [ 'errors' ] . first [ 'detail' ] ) . to match ( /Error while processing encrypted data/i )
928
+ end
929
+ end
902
930
end
903
931
904
932
describe 'POST /v3/service_brokers' do
Original file line number Diff line number Diff line change @@ -624,6 +624,21 @@ def check_filtered_bindings(*bindings)
624
624
}
625
625
end
626
626
627
+ context 'when the encryption_key_label is invalid' do
628
+ before do
629
+ allow_any_instance_of ( ErrorPresenter ) . to receive ( :raise_500? ) . and_return ( false )
630
+ end
631
+
632
+ it 'fails to decrypt the credentials and returns a 500 error' do
633
+ app_binding # ensure that binding is created before run_cipher is mocked to throw an error
634
+ allow ( VCAP ::CloudController ::Encryptor ) . to receive ( :run_cipher ) . and_raise ( OpenSSL ::Cipher ::CipherError )
635
+ api_call . call ( admin_headers )
636
+
637
+ expect ( last_response ) . to have_status_code ( 500 )
638
+ expect ( parsed_response [ 'errors' ] . first [ 'detail' ] ) . to match ( /Error while processing encrypted data/i )
639
+ end
640
+ end
641
+
627
642
context "last binding operation is in 'create succeeded' state" do
628
643
before do
629
644
app_binding . save_with_attributes_and_new_operation ( { } , { type : 'create' , state : 'succeeded' } )
Original file line number Diff line number Diff line change @@ -38,14 +38,18 @@ def not_found
38
38
raise CloudController ::Errors ::NotFound . new_from_details ( 'NotFound' )
39
39
end
40
40
41
- def db_connection_error
42
- raise Sequel :: DatabaseConnectionError . new
41
+ def key_derivation_error
42
+ raise OpenSSL :: Cipher :: CipherError
43
43
end
44
44
45
45
def db_disconnect_error
46
46
raise Sequel ::DatabaseDisconnectError . new
47
47
end
48
48
49
+ def db_connection_error
50
+ raise Sequel ::DatabaseConnectionError . new
51
+ end
52
+
49
53
def warnings_is_nil
50
54
add_warning_headers ( nil )
51
55
render status : :ok , json : { }
@@ -320,6 +324,23 @@ def warnings_incorrect_type
320
324
end
321
325
end
322
326
327
+ describe '#handle_key_derivation_error' do
328
+ let! ( :user ) { set_current_user ( VCAP ::CloudController ::User . make ) }
329
+
330
+ before do
331
+ allow_any_instance_of ( ErrorPresenter ) . to receive ( :raise_500? ) . and_return ( false )
332
+ routes . draw do
333
+ get 'key_derivation_error' => 'anonymous#key_derivation_error'
334
+ end
335
+ end
336
+
337
+ it 'rescues from OpenSSL::Cipher::CipherError and renders an error presenter' do
338
+ get :key_derivation_error
339
+ expect ( response ) . to have_http_status ( :internal_server_error )
340
+ expect ( response ) . to have_error_message ( /Error while processing encrypted data/ )
341
+ end
342
+ end
343
+
323
344
describe '#add_warning_headers' do
324
345
let! ( :user ) { set_current_user ( VCAP ::CloudController ::User . make ) }
325
346
You can’t perform that action at this time.
0 commit comments