Skip to content
This repository was archived by the owner on Jun 2, 2021. It is now read-only.

Commit b3c1265

Browse files
FelisiaMBrian Butz
authored andcommitted
v3(services): Get a single credential binding for key type - implement permissions
[#173243882](https://www.pivotaltracker.com/story/show/173243882) Signed-off-by: Brian Butz <bbutz@pivotal.io>
1 parent 756d78a commit b3c1265

5 files changed

Lines changed: 112 additions & 8 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class ServiceCredentialBindingsController < ApplicationController
2+
before_action :ensure_service_key_exists!
3+
before_action :ensure_user_has_access!
4+
5+
def show
6+
render status: :ok, json: hashed_params.slice(:guid)
7+
end
8+
9+
private
10+
11+
def ensure_service_key_exists!
12+
service_key_not_found! unless service_key_exists?
13+
end
14+
15+
def ensure_user_has_access!
16+
service_key_not_found! unless allowed_to_access_space?
17+
end
18+
19+
def service_key_not_found!
20+
resource_not_found!(:service_credential_binding)
21+
end
22+
23+
def service_key
24+
@service_key ||= ServiceKey.first(guid: hashed_params[:guid])
25+
end
26+
27+
def service_key_exists?
28+
!!service_key
29+
end
30+
31+
def allowed_to_access_space?
32+
space = service_key.service_instance.space
33+
34+
permission_queryer.can_read_from_space?(space.guid, space.organization_guid)
35+
end
36+
end

app/controllers/v3/service_instances_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ def relationships_shared_spaces
168168
def credentials
169169
service_instance = UserProvidedServiceInstance.first(guid: hashed_params[:guid])
170170
service_instance_not_found! unless service_instance && can_read_service_instance?(service_instance)
171-
unauthorized! unless permission_queryer.can_read_secrets_in_space?(service_instance.space.guid, service_instance.space.organization.guid)
171+
unauthorized! unless permission_queryer.can_read_secrets_in_space?(service_instance.space.guid, service_instance.space.organization_guid)
172172

173173
render status: :ok, json: (service_instance.credentials || {})
174174
end
@@ -308,7 +308,7 @@ def can_read_service_instance?(service_instance)
308308
end
309309

310310
def can_read_space?(space)
311-
permission_queryer.can_read_from_space?(space.guid, space.organization.guid)
311+
permission_queryer.can_read_from_space?(space.guid, space.organization_guid)
312312
end
313313

314314
def can_write_space?(space)

config/routes.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,9 @@
184184
get '/service_bindings', to: 'service_bindings#index'
185185
delete '/service_bindings/:guid', to: 'service_bindings#destroy'
186186

187+
# service_credential_bindings
188+
get '/service_credential_bindings/:guid', to: 'service_credential_bindings#show'
189+
187190
# service_brokers
188191
get '/service_brokers', to: 'service_brokers#index'
189192
get '/service_brokers/:guid', to: 'service_brokers#show'
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
require 'spec_helper'
2+
require 'request_spec_shared_examples'
3+
4+
RSpec.describe 'v3 service credential bindings' do
5+
let(:user) { VCAP::CloudController::User.make }
6+
let(:org) { VCAP::CloudController::Organization.make }
7+
let(:space) { VCAP::CloudController::Space.make(organization: org) }
8+
let(:other_space) { VCAP::CloudController::Space.make }
9+
10+
describe 'GET /v3/service_credential_bindings/:key_guid' do
11+
context 'key exists' do
12+
let(:key) { VCAP::CloudController::ServiceKey.make(service_instance: instance) }
13+
let(:instance) { VCAP::CloudController::ManagedServiceInstance.make(space: space) }
14+
let(:api_call) { ->(user_headers) { get "/v3/service_credential_bindings/#{key.guid}", nil, user_headers } }
15+
16+
context 'global roles' do
17+
let(:expected_codes_and_responses) do
18+
Hash.new({ code: 200, response_object: { guid: key.guid } })
19+
end
20+
21+
it_behaves_like 'permissions for single object endpoint', GLOBAL_SCOPES
22+
end
23+
24+
context 'local roles' do
25+
context 'user is in the original space of the service instance' do
26+
let(:expected_codes_and_responses) do
27+
Hash.new({ code: 200, response_object: { guid: key.guid } }).tap do |h|
28+
h['org_auditor'] = { code: 404 }
29+
h['org_billing_manager'] = { code: 404 }
30+
h['no_role'] = { code: 404 }
31+
end
32+
end
33+
34+
it_behaves_like 'permissions for single object endpoint', LOCAL_ROLES
35+
end
36+
37+
context 'user is in a space that the service instance is shared to' do
38+
let(:instance) { VCAP::CloudController::ManagedServiceInstance.make(space: other_space) }
39+
40+
before do
41+
instance.add_shared_space(space)
42+
end
43+
44+
let(:api_call) { ->(user_headers) { get "/v3/service_credential_bindings/#{key.guid}", nil, user_headers } }
45+
46+
let(:expected_codes_and_responses) do
47+
Hash.new(code: 404)
48+
end
49+
50+
it_behaves_like 'permissions for single object endpoint', LOCAL_ROLES
51+
end
52+
end
53+
end
54+
55+
context 'no such binding exists' do
56+
let(:api_call) { ->(user_headers) { get '/v3/service_credential_bindings/no-binding', nil, user_headers } }
57+
58+
let(:expected_codes_and_responses) do
59+
Hash.new(code: 404)
60+
end
61+
62+
it_behaves_like 'permissions for single object endpoint', ALL_PERMISSIONS
63+
end
64+
end
65+
end

spec/request/service_instances_spec.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@
2525
let(:guid) { instance.guid }
2626

2727
let(:expected_codes_and_responses) do
28-
h = Hash.new(
28+
Hash.new(
2929
code: 200,
3030
response_object: create_managed_json(instance),
31-
)
32-
h['org_auditor'] = { code: 404 }
33-
h['org_billing_manager'] = { code: 404 }
34-
h['no_role'] = { code: 404 }
35-
h
31+
).tap do |h|
32+
h['org_auditor'] = { code: 404 }
33+
h['org_billing_manager'] = { code: 404 }
34+
h['no_role'] = { code: 404 }
35+
end
3636
end
3737

3838
it_behaves_like 'permissions for single object endpoint', ALL_PERMISSIONS

0 commit comments

Comments
 (0)