forked from cloudfoundry/cloud_controller_ng
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbindings_create.rb
More file actions
388 lines (326 loc) · 15.6 KB
/
bindings_create.rb
File metadata and controls
388 lines (326 loc) · 15.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
RSpec.shared_examples 'service credential binding create endpoint' do |klass, check_app, audit_event_type, display_name|
describe 'managed instance' do
let(:api_call) { ->(user_headers) { post '/v3/service_credential_bindings', create_body.to_json, user_headers } }
let(:job) { VCAP::CloudController::PollableJobModel.last }
let(:binding) { klass.last }
describe 'a successful creation' do
before do
api_call.call(admin_headers)
end
it 'creates a credential binding in the database' do
expect(binding.app).to eq(app_to_bind_to) if check_app
expect(binding.service_instance).to eq(service_instance)
expect(binding.last_operation.state).to eq('initial')
expect(binding.last_operation.type).to eq('create')
expect(binding).to have_labels({ prefix: nil, key_name: 'foo', value: 'bar' })
expect(binding).to have_annotations({ prefix: nil, key_name: 'foz', value: 'baz' })
end
it 'responds with a job resource' do
expect(last_response).to have_status_code(202)
expect(last_response.headers['Location']).to end_with("/v3/jobs/#{job.guid}")
expect(job.state).to eq(VCAP::CloudController::PollableJobModel::PROCESSING_STATE)
expect(job.operation).to eq("#{display_name}.create")
expect(job.resource_guid).to eq(binding.guid)
expect(job.resource_type).to eq('service_credential_binding')
get "/v3/jobs/#{job.guid}", nil, admin_headers
expect(last_response).to have_status_code(200)
expect(parsed_response['guid']).to eq(job.guid)
binding_link = parsed_response.dig('links', 'service_credential_binding', 'href')
expect(binding_link).to end_with("/v3/service_credential_bindings/#{binding.guid}")
end
end
describe 'the pollable job' do
let(:credentials) { { 'password' => 'special sauce' } }
let(:broker_base_url) { service_instance.service_broker.broker_url }
let(:broker_bind_url) { "#{broker_base_url}/v2/service_instances/#{service_instance.guid}/service_bindings/#{binding.guid}" }
let(:broker_status_code) { 201 }
let(:broker_response) { { credentials: } }
let(:app_binding_attributes) do
if check_app
{
app_guid: app_to_bind_to.guid,
bind_resource: {
app_guid: app_to_bind_to.guid,
space_guid: service_instance.space.guid,
app_annotations: { 'pre.fix/foo': 'bar' }
}
}
else
{}
end
end
let(:client_body) do
{
context: {
platform: 'cloudfoundry',
organization_guid: org.guid,
organization_name: org.name,
organization_annotations: { 'pre.fix/foo': 'bar' },
space_guid: space.guid,
space_name: space.name,
space_annotations: { 'pre.fix/baz': 'wow' }
},
service_id: service_instance.service_plan.service.unique_id,
plan_id: service_instance.service_plan.unique_id,
bind_resource: {
credential_client_id: 'cc_service_key_client'
}
}.merge(app_binding_attributes)
end
before do
api_call.call(headers_for(user, scopes: %w[cloud_controller.admin]))
expect(last_response).to have_status_code(202)
stub_request(:put, broker_bind_url).
with(query: { accepts_incomplete: true }).
to_return(status: broker_status_code, body: broker_response.to_json, headers: {})
end
it 'sends a bind request with the right arguments to the service broker' do
execute_all_jobs(expected_successes: 1, expected_failures: 0)
encoded_user_guid = Base64.strict_encode64("{\"user_id\":\"#{user.guid}\"}")
expect(
a_request(:put, broker_bind_url).
with(
body: client_body,
query: { accepts_incomplete: true },
headers: { 'X-Broker-Api-Originating-Identity' => "cloudfoundry #{encoded_user_guid}" }
)
).to have_been_made.once
end
context 'parameters are specified' do
let(:request_extra) { { parameters: { foo: 'bar' } } }
it 'sends the parameters to the broker' do
execute_all_jobs(expected_successes: 1, expected_failures: 0)
expect(
a_request(:put, broker_bind_url).
with(
body: client_body.deep_merge(request_extra),
query: { accepts_incomplete: true }
)
).to have_been_made.once
end
end
context 'when the bind completes synchronously' do
it 'updates the the binding' do
execute_all_jobs(expected_successes: 1, expected_failures: 0)
binding.reload
expect(binding.credentials).to eq(credentials)
expect(binding.last_operation.type).to eq('create')
expect(binding.last_operation.state).to eq('succeeded')
end
it 'completes the job' do
execute_all_jobs(expected_successes: 1, expected_failures: 0)
expect(job.state).to eq(VCAP::CloudController::PollableJobModel::COMPLETE_STATE)
end
it 'logs an audit event' do
execute_all_jobs(expected_successes: 1, expected_failures: 0)
event = VCAP::CloudController::Event.find(type: "audit.#{audit_event_type}.create")
expect(event).to be
expect(event.actee).to eq(binding.guid)
expect(event.actee_name).to eq(binding.name)
expect(event.data).to include({
'request' => create_body.with_indifferent_access
})
end
end
context 'when the broker fails to bind' do
let(:broker_status_code) { 422 }
let(:broker_response) { { error: 'RequiresApp' } }
it 'updates the the binding with a failure' do
execute_all_jobs(expected_successes: 0, expected_failures: 1)
binding.reload
expect(binding.last_operation.type).to eq('create')
expect(binding.last_operation.state).to eq('failed')
end
it 'fails the job' do
execute_all_jobs(expected_successes: 0, expected_failures: 1)
expect(job.state).to eq(VCAP::CloudController::PollableJobModel::FAILED_STATE)
end
end
context 'when the binding completes asynchronously' do
let(:broker_status_code) { 202 }
let(:operation) { Sham.guid }
let(:broker_response) { { operation: } }
let(:broker_binding_last_operation_url) { "#{broker_base_url}/v2/service_instances/#{service_instance.guid}/service_bindings/#{binding.guid}/last_operation" }
let(:last_operation_status_code) { 200 }
let(:description) { Sham.description }
let(:state) { 'in progress' }
let(:last_operation_body) do
{
description:,
state:
}
end
before do
stub_request(:get, broker_binding_last_operation_url).
with(query: hash_including({
operation:
})).
to_return(status: last_operation_status_code, body: last_operation_body.to_json, headers: {})
end
it 'polls the last operation endpoint' do
execute_all_jobs(expected_successes: 1, expected_failures: 0)
encoded_user_guid = Base64.strict_encode64("{\"user_id\":\"#{user.guid}\"}")
expect(
a_request(:get, broker_binding_last_operation_url).
with(
query: {
operation: operation,
service_id: service_instance.service_plan.service.unique_id,
plan_id: service_instance.service_plan.unique_id
},
headers: { 'X-Broker-Api-Originating-Identity' => "cloudfoundry #{encoded_user_guid}" }
)
).to have_been_made.once
end
it 'updates the binding and job' do
execute_all_jobs(expected_successes: 1, expected_failures: 0)
expect(binding.last_operation.type).to eq('create')
expect(binding.last_operation.state).to eq(state)
expect(binding.last_operation.description).to eq(description)
expect(job.state).to eq(VCAP::CloudController::PollableJobModel::POLLING_STATE)
end
it 'logs an audit event' do
execute_all_jobs(expected_successes: 1, expected_failures: 0)
event = VCAP::CloudController::Event.find(type: "audit.#{audit_event_type}.start_create")
expect(event).to be
expect(event.actee).to eq(binding.guid)
expect(event.data).to include({
'request' => create_body.with_indifferent_access
})
end
it 'enqueues the next fetch last operation job' do
execute_all_jobs(expected_successes: 1, expected_failures: 0)
expect(Delayed::Job.count).to eq(1)
end
it 'keeps track of the broker operation' do
execute_all_jobs(expected_successes: 1, expected_failures: 0)
expect(Delayed::Job.count).to eq(1)
Timecop.travel(Time.now + 1.minute)
execute_all_jobs(expected_successes: 1, expected_failures: 0)
encoded_user_guid = Base64.strict_encode64("{\"user_id\":\"#{user.guid}\"}")
expect(
a_request(:get, broker_binding_last_operation_url).
with(
query: {
operation: operation,
service_id: service_instance.service_plan.service.unique_id,
plan_id: service_instance.service_plan.unique_id
},
headers: { 'X-Broker-Api-Originating-Identity' => "cloudfoundry #{encoded_user_guid}" }
)
).to have_been_made.twice
end
context 'last operation response is 200 OK and indicates success' do
let(:state) { 'succeeded' }
let(:fetch_binding_status_code) { 200 }
let(:credentials) { { password: 'foo' } }
let(:parameters) { { foo: 'bar', another_foo: 'another_bar' } }
let(:broker_fetch_binding_query) do
{
service_id: service_instance.service_plan.service.unique_id,
plan_id: service_instance.service_plan.unique_id
}
end
let(:app_binding_attributes) do
if check_app
{
syslog_drain_url:
}
else
{}
end
end
let(:fetch_binding_body) do
{
credentials: credentials,
parameters: parameters,
service_id: 'extra-field-service_id-should-ignore',
name: 'extra-field-name-should-not-update'
}.merge(app_binding_attributes)
end
before do
stub_request(:get, broker_bind_url).
with(query: broker_fetch_binding_query).
to_return(status: fetch_binding_status_code, body: fetch_binding_body.to_json, headers: {})
end
it 'fetches the binding' do
execute_all_jobs(expected_successes: 1, expected_failures: 0)
encoded_user_guid = Base64.strict_encode64("{\"user_id\":\"#{user.guid}\"}")
expect(
a_request(:get, broker_bind_url).with(
query: broker_fetch_binding_query,
headers: { 'X-Broker-Api-Originating-Identity' => "cloudfoundry #{encoded_user_guid}" }
)
).to have_been_made.once
end
it 'updates the binding and job' do
execute_all_jobs(expected_successes: 1, expected_failures: 0)
expect(binding.last_operation.type).to eq('create')
expect(binding.last_operation.state).to eq(state)
expect(binding.last_operation.description).to eq(description)
expect(job.state).to eq(VCAP::CloudController::PollableJobModel::COMPLETE_STATE)
end
it 'updates the binding details with the fetch binding response ignoring extra fields' do
execute_all_jobs(expected_successes: 1, expected_failures: 0)
expect(binding.reload.credentials).to eq(credentials.with_indifferent_access)
expect(binding.syslog_drain_url).to eq(syslog_drain_url) if check_app
expect(binding.name).to eq(binding_name)
end
it 'logs an audit event' do
execute_all_jobs(expected_successes: 1, expected_failures: 0)
event = VCAP::CloudController::Event.find(type: "audit.#{audit_event_type}.create")
expect(event).to be
expect(event.actee).to eq(binding.guid)
expect(event.data).to include({
'request' => create_body.with_indifferent_access
})
end
context 'fetching binding fails' do
let(:fetch_binding_status_code) { 404 }
let(:fetch_binding_body) {}
it 'fails the job' do
execute_all_jobs(expected_successes: 0, expected_failures: 1)
expect(binding.last_operation.type).to eq('create')
expect(binding.last_operation.state).to eq('failed')
expect(binding.last_operation.description).to include('The service broker rejected the request. Status Code: 404 Not Found')
expect(job.state).to eq(VCAP::CloudController::PollableJobModel::FAILED_STATE)
expect(job.cf_api_error).not_to be_nil
error = YAML.safe_load(job.cf_api_error)
expect(error['errors'].first).to include({
'code' => 10_009,
'title' => 'CF-UnableToPerform',
'detail' => 'bind could not be completed: The service broker rejected the request. Status Code: 404 Not Found, Body: null'
})
end
end
end
it_behaves_like 'binding last operation response handling', 'create'
context 'binding not retrievable' do
let(:offering) { VCAP::CloudController::Service.make(bindings_retrievable: false) }
it 'fails the job with an appropriate error' do
execute_all_jobs(expected_successes: 0, expected_failures: 1)
expect(binding.last_operation.type).to eq('create')
expect(binding.last_operation.state).to eq('failed')
expect(binding.last_operation.description).to eq('The broker responded asynchronously but does not support fetching binding data')
expect(job.state).to eq(VCAP::CloudController::PollableJobModel::FAILED_STATE)
expect(job.cf_api_error).not_to be_nil
error = YAML.safe_load(job.cf_api_error)
# TODO: check error message
expect(error['errors'].first).to include({
'code' => 90_001,
'title' => 'CF-ServiceBindingInvalid',
'detail' => 'The service binding is invalid: The broker responded asynchronously but does not support fetching binding data'
})
end
end
end
context 'orphan mitigation' do
it_behaves_like 'create binding orphan mitigation' do
let(:bind_url) { broker_bind_url }
let(:plan_id) { plan.unique_id }
let(:offering_id) { offering.unique_id }
end
end
end
end
end