This repository has been archived by the owner on Jan 26, 2022. It is now read-only.
/
services_controller.rb
263 lines (214 loc) · 8.58 KB
/
services_controller.rb
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
require 'json_message'
require 'services/api'
# TODO(mjp): Split these into separate controllers (user facing vs gateway facing, along with tests)
class ServicesController < ApplicationController
include ServicesHelper
before_filter :validate_content_type
before_filter :require_service_auth_token, :only => [:create, :delete, :update_handle, :list_handles, :list_brokered_services]
before_filter :require_user_or_staging_task_id, :only => [:provision, :bind, :bind_external, :unbind, :unprovision]
rescue_from(JsonMessage::Error) {|e| render :status => 400, :json => {:errors => e.to_s}}
rescue_from(ActiveRecord::RecordInvalid) {|e| render :status => 400, :json => {:errors => e.to_s}}
# Registers a new service offering with the CC
#
def create
req = VCAP::Services::Api::ServiceOfferingRequest.decode(request_body)
CloudController.logger.debug("Create service request: #{req.extract.inspect}")
# Should we worry about a race here?
success = nil
svc = Service.find_by_label(req.label)
if svc
raise CloudError.new(CloudError::FORBIDDEN) unless svc.verify_auth_token(@service_auth_token)
attrs = req.extract.dup
attrs.delete(:label)
# Keep DB in sync with configs if the token changes in the config
attrs[:token] = @service_auth_token if svc.is_builtin?
# Special support for changing a service offering's ACLs from
# private to public. The call to ServiceOfferingRequest.decode
# (actually, JsonMessage.from_decoded_json) discards keys with
# nil values, which is the case for key :acls when switching
# from private to public. This issue is more general than just
# :acls, but to avoid breaking anything as a side efffect, we do
# this only for :acls.
attrs[:acls] = nil unless attrs.has_key?(:acls)
# Similar to acls, do the same for timeout
attrs[:timeout] = nil unless attrs.has_key?(:timeout)
svc.update_attributes!(attrs)
else
# Service doesn't exist yet. This can only happen for builtin services since service providers must
# register with us to get a token.
# or, it's a brokered service
svc = Service.new(req.extract)
if AppConfig[:service_broker] and @service_auth_token == AppConfig[:service_broker][:token] and !svc.is_builtin?
attrs = req.extract.dup
attrs[:token] = @service_auth_token
svc.update_attributes!(attrs)
else
raise CloudError.new(CloudError::FORBIDDEN) unless svc.is_builtin? && svc.verify_auth_token(@service_auth_token)
svc.token = @service_auth_token
svc.save!
end
end
render :json => {}
end
# Updates given handle with the new config.
# XXX: This is REALLY inefficient...
#
def update_handle
handle = VCAP::Services::Api::HandleUpdateRequest.decode(request_body)
# We have to check two places here: configs and bindings :/
if cfg = ServiceConfig.find_by_name(handle.service_id)
raise CloudError.new(CloudError::FORBIDDEN) unless cfg.service.verify_auth_token(@service_auth_token)
cfg.data = handle.configuration
cfg.credentials = handle.credentials
cfg.save!
elsif bdg = ServiceBinding.find_by_name(handle.service_id)
svc = bdg.service_config.service
raise CloudError.new(CloudError::FORBIDDEN) unless (svc && (svc.verify_auth_token(@service_auth_token)))
bdg.configuration = handle.configuration
bdg.credentials = handle.credentials
bdg.save!
end
raise CloudError.new(CloudError::BINDING_NOT_FOUND) unless (cfg || bdg)
render :json => {}
end
# Returns the provisioned and bound handles for a service provider
def list_handles
svc = Service.find_by_label(params[:label])
raise CloudError.new(CloudError::SERVICE_NOT_FOUND) unless svc
raise CloudError.new(CloudError::FORBIDDEN) unless svc.verify_auth_token(@service_auth_token)
handles = []
cfgs = svc.service_configs
if cfgs
cfgs.each do |cfg|
handles << {
:service_id => cfg.name,
:configuration => cfg.data,
:credentials => cfg.credentials
}
end
end
bdgs = svc.service_bindings
if bdgs
bdgs.each do |bdg|
handles << {
:service_id => bdg.name,
:configuration => bdg.configuration,
:credentials => bdg.credentials,
}
end
end
render :json => {:handles => handles}
end
# List brokered services
def list_brokered_services
if AppConfig[:service_broker].nil? or @service_auth_token != AppConfig[:service_broker][:token]
raise CloudError.new(CloudError::FORBIDDEN)
end
svcs = Service.all
brokered_svcs = svcs.select {|svc| ! svc.is_builtin? }
result = []
brokered_svcs.each do |svc|
result << {
:label => svc.label,
:description => svc.description,
:acls => svc.acls,
}
end
render :json => {:brokered_services => result}
end
# Unregister a service offering with the CC
#
def delete
svc = Service.find_by_label(params[:label])
raise CloudError.new(CloudError::SERVICE_NOT_FOUND) unless svc
raise CloudError.new(CloudError::FORBIDDEN) unless svc.verify_auth_token(@service_auth_token)
svc.destroy
render :json => {}
end
# Asks the gateway to provision an instance of the requested service
#
def provision
req = VCAP::Services::Api::CloudControllerProvisionRequest.decode(request_body)
svc = Service.find_by_label(req.label)
raise CloudError.new(CloudError::SERVICE_NOT_FOUND) unless svc && svc.visible_to_user?(user)
cfg = ServiceConfig.provision(svc, user, req.name, req.plan, req.plan_option)
handle = {
:service_id => cfg.name,
:data => cfg.data,
:credentials => cfg.credentials,
}
render :json => handle
end
# Deletes a previously provisioned instance of a service
#
def unprovision
cfg = ServiceConfig.find_by_user_id_and_name(user.id, params['id'])
raise CloudError.new(CloudError::SERVICE_NOT_FOUND) unless cfg
raise CloudError.new(CloudError::FORBIDDEN) unless cfg.provisioned_by?(user)
cfg.unprovision
render :json => {}
end
# Binds a provisioned instance to an app
#
def bind
req = VCAP::Services::Api::CloudControllerBindRequest.decode(request_body)
app = ::App.find_by_collaborator_and_id(user, req.app_id)
raise CloudError.new(CloudError::APP_NOT_FOUND) unless app
cfg = ServiceConfig.find_by_name(req.service_id)
raise CloudError.new(CloudError::SERVICE_NOT_FOUND) unless cfg
raise CloudError.new(CloudError::FORBIDDEN) unless cfg.provisioned_by?(user)
binding = app.bind_to_config(cfg)
resp = {
:binding_token => binding.binding_token.uuid,
:label => cfg.service.label
}
render :json => resp
end
# Binds an app to a service using an existing binding token
#
def bind_external
cli_req = VCAP::Services::Api::BindExternalRequest.decode(request_body)
app = ::App.find_by_collaborator_and_id(user, cli_req.app_id)
raise CloudError.new(CloudError::APP_NOT_FOUND) unless app
tok = ::BindingToken.find_by_uuid(cli_req.binding_token)
raise CloudError.new(CloudError::TOKEN_NOT_FOUND) unless tok
cfg = tok.service_config
raise CloudError.new(CloudError::SERVICE_NOT_FOUND) unless cfg
app.bind_to_config(cfg, tok.binding_options)
render :json => {}
end
# Unbinds a previously bound instance from an app
#
def unbind
tok = ::BindingToken.find_by_uuid(params['binding_token'])
raise CloudError.new(CloudError::BINDING_NOT_FOUND) unless tok
# It's possible that a previous attempt at binding failed, leaving a dangling token.
# In this case just log the issue and clean up.
binding = ServiceBinding.find_by_binding_token_id(tok.id)
unless binding
CloudController.logger.info("Removing dangling token #{tok.uuid}")
CloudController.logger.info(tok.inspect)
tok.destroy
render :json => {}
return
end
app = binding.app
svc_config = binding.service_config
app.unbind_from_config(svc_config)
render :json => {}
end
protected
def require_user_or_staging_task_id
unless user
staging_task_id = request.headers['X_VCAP_STAGING_TASK_ID']
staging_task = StagingTask.find_task(staging_task_id)
@current_user = staging_task.user if staging_task
end
raise CloudError.new(CloudError::FORBIDDEN) unless user
end
def require_service_auth_token
hdr = VCAP::Services::Api::GATEWAY_TOKEN_HEADER.upcase.gsub(/-/, '_')
@service_auth_token = request.headers[hdr]
raise CloudError.new(CloudError::FORBIDDEN) unless @service_auth_token
end
end