Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
philippthun committed Jul 7, 2023
1 parent 1750fec commit c559b57
Show file tree
Hide file tree
Showing 20 changed files with 62 additions and 73 deletions.
10 changes: 6 additions & 4 deletions app/actions/role_create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ def initialize(message, user_audit_info)
def create_space_role(type:, user:, space:)
error!("Users cannot be assigned roles in a space if they do not have a role in that space's organization.") unless space.in_organization?(user)

uaa_client = CloudController::DependencyLocator.instance.uaa_client
UsernamePopulator.new(uaa_client).transform(user)
UsernamePopulator.new(uaa_username_lookup_client).transform(user)

case type
when RoleTypes::SPACE_AUDITOR
Expand All @@ -34,8 +33,7 @@ def create_space_role(type:, user:, space:)
end

def create_organization_role(type:, user:, organization:)
uaa_client = CloudController::DependencyLocator.instance.uaa_client
UsernamePopulator.new(uaa_client).transform(user)
UsernamePopulator.new(uaa_username_lookup_client).transform(user)

case type
when RoleTypes::ORGANIZATION_USER
Expand Down Expand Up @@ -126,5 +124,9 @@ def organization_validation_error!(type, error, user, organization)
def error!(message)
raise Error.new(message)
end

def uaa_username_lookup_client
CloudController::DependencyLocator.instance.uaa_username_lookup_client
end
end
end
12 changes: 7 additions & 5 deletions app/controllers/v3/roles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,7 @@ def fetch_readable_user(user_guid)

def fetch_role_owner_with_name(role)
user = User.first(id: role.user_id)
uaa_client = CloudController::DependencyLocator.instance.uaa_client
UsernamePopulator.new(uaa_client).transform(user)
UsernamePopulator.new(uaa_username_lookup_client).transform(user)
user
end

Expand Down Expand Up @@ -175,11 +174,10 @@ def unprocessable_space_user!

def lookup_user_guid_in_uaa(username, given_origin, creating_space_role: false)
FeatureFlag.raise_unless_enabled!(:set_roles_by_username)
uaa_client = CloudController::DependencyLocator.instance.uaa_client

origin = given_origin
if given_origin.nil?
origins = uaa_client.origins_for_username(username).sort
origins = uaa_username_lookup_client.origins_for_username(username).sort

if origins.length > 1
unprocessable!(
Expand All @@ -190,11 +188,15 @@ def lookup_user_guid_in_uaa(username, given_origin, creating_space_role: false)
origin = origins[0]
end

guid = uaa_client.id_for_username(username, origin: origin)
guid = uaa_username_lookup_client.id_for_username(username, origin: origin)
return guid if guid

unprocessable_space_user! if creating_space_role
unprocessable!("No user exists with the username '#{username}' and origin '#{origin}'.") if given_origin
unprocessable!("No user exists with the username '#{username}'.")
end

def uaa_username_lookup_client
CloudController::DependencyLocator.instance.uaa_username_lookup_client
end
end
12 changes: 1 addition & 11 deletions app/controllers/v3/service_credential_bindings_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -307,18 +307,8 @@ def volume_services_enabled?
config.get(:volume_services_enabled)
end

def uaa_client
@uaa_client ||= UaaClient.new(
uaa_target: config.get(:uaa, :internal_url),
client_id: config.get(:cc_service_key_client_name),
secret: config.get(:cc_service_key_client_secret),
ca_file: config.get(:uaa, :ca_file),
)
end

def credhub_client
# TODO: use the credhub client provided by `DependencyLocator`
@credhub_client ||= Credhub::Client.new(config.get(:credhub_api, :internal_url), uaa_client)
@credhub_client ||= CloudController::DependencyLocator.instance.credhub_client
end

def fetch_credentials_value(name)
Expand Down
8 changes: 4 additions & 4 deletions app/fetchers/user_list_fetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ def fetch_all(message, readable_users_dataset)

def filter(message, dataset)
if message.requested?(:usernames)
guids = uaa_client.ids_for_usernames_and_origins(message.usernames, message.origins)
guids = uaa_username_lookup_client.ids_for_usernames_and_origins(message.usernames, message.origins)
dataset = dataset.where(guid: guids)
end

if message.requested?(:partial_usernames)
guids = uaa_client.ids_for_usernames_and_origins(message.partial_usernames, message.origins, false)
guids = uaa_username_lookup_client.ids_for_usernames_and_origins(message.partial_usernames, message.origins, false)
dataset = dataset.where(guid: guids)
end

Expand All @@ -34,8 +34,8 @@ def filter(message, dataset)
super(message, dataset, User)
end

def uaa_client
CloudController::DependencyLocator.instance.uaa_client
def uaa_username_lookup_client
CloudController::DependencyLocator.instance.uaa_username_lookup_client
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions app/models/runtime/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,8 @@ def readable_users(can_read_globally)
end

def self.uaa_users_info(user_guids)
uaa_client = CloudController::DependencyLocator.instance.uaa_client
uaa_client.users_for_ids(user_guids)
uaa_username_lookup_client = CloudController::DependencyLocator.instance.uaa_username_lookup_client
uaa_username_lookup_client.users_for_ids(user_guids)
end

def self.user_visibility_filter(_)
Expand Down
8 changes: 4 additions & 4 deletions lib/cloud_controller/dependency_locator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ def object_renderer
end

def username_populating_object_renderer
create_object_renderer(object_transformer: UsernamePopulator.new(uaa_client))
create_object_renderer(object_transformer: UsernamePopulator.new(uaa_username_lookup_client))
end

def service_key_credential_object_renderer
Expand All @@ -259,22 +259,22 @@ def large_paginated_collection_renderer
end

def username_populating_collection_renderer
create_paginated_collection_renderer(collection_transformer: UsernamePopulator.new(uaa_client))
create_paginated_collection_renderer(collection_transformer: UsernamePopulator.new(uaa_username_lookup_client))
end

def service_key_credential_collection_renderer
create_paginated_collection_renderer(collection_transformer: CredhubCredentialPopulator.new(credhub_client))
end

def username_and_roles_populating_collection_renderer
create_paginated_collection_renderer(collection_transformer: UsernamesAndRolesPopulator.new(uaa_client))
create_paginated_collection_renderer(collection_transformer: UsernamesAndRolesPopulator.new(uaa_username_lookup_client))
end

def router_group_type_populating_collection_renderer
create_paginated_collection_renderer(collection_transformer: RouterGroupTypePopulator.new(routing_api_client))
end

def uaa_client
def uaa_username_lookup_client
UaaClient.new(
uaa_target: config.get(:uaa, :internal_url),
client_id: config.get(:cloud_controller_username_lookup_client_name),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def decode_token(header_token)
end

def is_user_in_uaadb?(id)
CloudController::DependencyLocator.instance.uaa_client.usernames_for_ids(Array(id)).present?
CloudController::DependencyLocator.instance.uaa_username_lookup_client.usernames_for_ids(Array(id)).present?
end

def is_uuid_shaped?(id)
Expand Down
16 changes: 8 additions & 8 deletions spec/api/documentation/organizations_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@
put 'v2/organizations/:guid/users' do
example 'Associate User with the Organization by Username' do
uaa_client = double(:uaa_client)
allow(CloudController::DependencyLocator.instance).to receive(:uaa_client).and_return(uaa_client)
allow(CloudController::DependencyLocator.instance).to receive(:uaa_username_lookup_client).and_return(uaa_client)
allow(uaa_client).to receive(:id_for_username).and_return('user-guid')
allow(uaa_client).to receive(:origins_for_username).and_return(['uaa'])

Expand All @@ -178,7 +178,7 @@
delete 'v2/organizations/:guid/users' do
example 'Remove User with the Organization by Username' do
uaa_client = double(:uaa_client)
allow(CloudController::DependencyLocator.instance).to receive(:uaa_client).and_return(uaa_client)
allow(CloudController::DependencyLocator.instance).to receive(:uaa_username_lookup_client).and_return(uaa_client)
allow(uaa_client).to receive(:id_for_username).and_return(associated_user.guid)
allow(uaa_client).to receive(:origins_for_username).and_return(['uaa'])

Expand Down Expand Up @@ -219,7 +219,7 @@
put 'v2/organizations/:guid/managers' do
example 'Associate Manager with the Organization by Username' do
uaa_client = double(:uaa_client)
allow(CloudController::DependencyLocator.instance).to receive(:uaa_client).and_return(uaa_client)
allow(CloudController::DependencyLocator.instance).to receive(:uaa_username_lookup_client).and_return(uaa_client)
allow(uaa_client).to receive(:id_for_username).and_return('user-guid')
allow(uaa_client).to receive(:origins_for_username).and_return(['uaa'])

Expand All @@ -233,7 +233,7 @@
delete 'v2/organizations/:guid/managers' do
example 'Remove Manager with the Organization by Username' do
uaa_client = double(:uaa_client)
allow(CloudController::DependencyLocator.instance).to receive(:uaa_client).and_return(uaa_client)
allow(CloudController::DependencyLocator.instance).to receive(:uaa_username_lookup_client).and_return(uaa_client)
allow(uaa_client).to receive(:id_for_username).and_return(associated_manager_guid)
allow(uaa_client).to receive(:origins_for_username).and_return(['uaa'])

Expand Down Expand Up @@ -273,7 +273,7 @@
put 'v2/organizations/:guid/billing_managers' do
example 'Associate Billing Manager with the Organization by Username' do
uaa_client = double(:uaa_client)
allow(CloudController::DependencyLocator.instance).to receive(:uaa_client).and_return(uaa_client)
allow(CloudController::DependencyLocator.instance).to receive(:uaa_username_lookup_client).and_return(uaa_client)
allow(uaa_client).to receive(:id_for_username).and_return('user-guid')
allow(uaa_client).to receive(:origins_for_username).and_return(['uaa'])

Expand All @@ -287,7 +287,7 @@
delete 'v2/organizations/:guid/billing_managers' do
example 'Remove Billing Manager with the Organization by Username' do
uaa_client = double(:uaa_client)
allow(CloudController::DependencyLocator.instance).to receive(:uaa_client).and_return(uaa_client)
allow(CloudController::DependencyLocator.instance).to receive(:uaa_username_lookup_client).and_return(uaa_client)
allow(uaa_client).to receive(:id_for_username).and_return(associated_billing_manager_guid)
allow(uaa_client).to receive(:origins_for_username).and_return(['uaa'])

Expand Down Expand Up @@ -327,7 +327,7 @@
put 'v2/organizations/:guid/auditors' do
example 'Associate Auditor with the Organization by Username' do
uaa_client = double(:uaa_client)
allow(CloudController::DependencyLocator.instance).to receive(:uaa_client).and_return(uaa_client)
allow(CloudController::DependencyLocator.instance).to receive(:uaa_username_lookup_client).and_return(uaa_client)
allow(uaa_client).to receive(:id_for_username).and_return('user-guid')
allow(uaa_client).to receive(:origins_for_username).and_return(['uaa'])

Expand All @@ -341,7 +341,7 @@
delete 'v2/organizations/:guid/auditors' do
example 'Remove Auditor with the Organization by Username' do
uaa_client = double(:uaa_client)
allow(CloudController::DependencyLocator.instance).to receive(:uaa_client).and_return(uaa_client)
allow(CloudController::DependencyLocator.instance).to receive(:uaa_username_lookup_client).and_return(uaa_client)
allow(uaa_client).to receive(:id_for_username).and_return(associated_auditor_guid)
allow(uaa_client).to receive(:origins_for_username).and_return(['uaa'])

Expand Down
12 changes: 6 additions & 6 deletions spec/api/documentation/spaces_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def after_standard_model_delete(guid)
put 'v2/spaces/:guid/developers' do
example 'Associate Developer with the Space by Username' do
uaa_client = double(:uaa_client)
allow(CloudController::DependencyLocator.instance).to receive(:uaa_client).and_return(uaa_client)
allow(CloudController::DependencyLocator.instance).to receive(:uaa_username_lookup_client).and_return(uaa_client)
allow(uaa_client).to receive(:id_for_username).and_return(developer.guid)
allow(uaa_client).to receive(:origins_for_username).and_return(['uaa'])

Expand All @@ -150,7 +150,7 @@ def after_standard_model_delete(guid)
delete 'v2/spaces/:guid/developers' do
example 'Remove Developer with the Space by Username' do
uaa_client = double(:uaa_client)
allow(CloudController::DependencyLocator.instance).to receive(:uaa_client).and_return(uaa_client)
allow(CloudController::DependencyLocator.instance).to receive(:uaa_username_lookup_client).and_return(uaa_client)
allow(uaa_client).to receive(:id_for_username).and_return(associated_developer.guid)
allow(uaa_client).to receive(:origins_for_username).and_return(['uaa'])

Expand Down Expand Up @@ -194,7 +194,7 @@ def after_standard_model_delete(guid)
put 'v2/spaces/:guid/managers' do
example 'Associate Manager with the Space by Username' do
uaa_client = double(:uaa_client)
allow(CloudController::DependencyLocator.instance).to receive(:uaa_client).and_return(uaa_client)
allow(CloudController::DependencyLocator.instance).to receive(:uaa_username_lookup_client).and_return(uaa_client)
allow(uaa_client).to receive(:id_for_username).and_return(manager.guid)
allow(uaa_client).to receive(:origins_for_username).and_return(['uaa'])

Expand All @@ -208,7 +208,7 @@ def after_standard_model_delete(guid)
delete 'v2/spaces/:guid/managers' do
example 'Remove Manager with the Space by Username' do
uaa_client = double(:uaa_client)
allow(CloudController::DependencyLocator.instance).to receive(:uaa_client).and_return(uaa_client)
allow(CloudController::DependencyLocator.instance).to receive(:uaa_username_lookup_client).and_return(uaa_client)
allow(uaa_client).to receive(:id_for_username).and_return(associated_manager.guid)
allow(uaa_client).to receive(:origins_for_username).and_return(['uaa'])

Expand Down Expand Up @@ -252,7 +252,7 @@ def after_standard_model_delete(guid)
put 'v2/spaces/:guid/auditors' do
example 'Associate Auditor with the Space by Username' do
uaa_client = double(:uaa_client)
allow(CloudController::DependencyLocator.instance).to receive(:uaa_client).and_return(uaa_client)
allow(CloudController::DependencyLocator.instance).to receive(:uaa_username_lookup_client).and_return(uaa_client)
allow(uaa_client).to receive(:id_for_username).and_return(auditor.guid)
allow(uaa_client).to receive(:origins_for_username).and_return(['uaa'])

Expand All @@ -266,7 +266,7 @@ def after_standard_model_delete(guid)
delete 'v2/spaces/:guid/auditors' do
example 'Remove Auditor with the Space by Username' do
uaa_client = double(:uaa_client)
allow(CloudController::DependencyLocator.instance).to receive(:uaa_client).and_return(uaa_client)
allow(CloudController::DependencyLocator.instance).to receive(:uaa_username_lookup_client).and_return(uaa_client)
allow(uaa_client).to receive(:id_for_username).and_return(associated_auditor.guid)
allow(uaa_client).to receive(:origins_for_username).and_return(['uaa'])

Expand Down
2 changes: 1 addition & 1 deletion spec/request/organizations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module VCAP::CloudController
Domain.dataset.destroy # this will clean up the seeded test domains
TestConfig.override(kubernetes: {})

allow(CloudController::DependencyLocator.instance).to receive(:uaa_client).and_return(uaa_client)
allow(CloudController::DependencyLocator.instance).to receive(:uaa_username_lookup_client).and_return(uaa_client)
allow(uaa_client).to receive(:usernames_for_ids).with([user.guid]).and_return(
{ user.guid => 'Ragnaros' }
)
Expand Down
2 changes: 1 addition & 1 deletion spec/request/roles_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
let(:uaa_client) { instance_double(VCAP::CloudController::UaaClient) }

before do
allow(CloudController::DependencyLocator.instance).to receive(:uaa_client).and_return(uaa_client)
allow(CloudController::DependencyLocator.instance).to receive(:uaa_username_lookup_client).and_return(uaa_client)
allow(uaa_client).to receive(:usernames_for_ids).with([user_with_role.guid]).and_return(
{ user_with_role.guid => 'mona' }
)
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/actions/organization_create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module VCAP::CloudController
subject(:org_create) { OrganizationCreate.new(user_audit_info: user_audit_info) }

before do
allow(CloudController::DependencyLocator.instance).to receive(:uaa_client).and_return(uaa_client)
allow(CloudController::DependencyLocator.instance).to receive(:uaa_username_lookup_client).and_return(uaa_client)
allow(uaa_client).to receive(:usernames_for_ids).with([user.guid]).and_return(
{ user.guid => 'Ragnaros' }
)
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/actions/role_create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module VCAP::CloudController
let(:uaa_client) { instance_double(VCAP::CloudController::UaaClient) }

before do
allow(CloudController::DependencyLocator.instance).to receive(:uaa_client).and_return(uaa_client)
allow(CloudController::DependencyLocator.instance).to receive(:uaa_username_lookup_client).and_return(uaa_client)
allow(uaa_client).to receive(:usernames_for_ids).with([user.guid]).and_return(
{ user.guid => 'mona' }
)
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/actions/role_delete_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module VCAP::CloudController
let(:uaa_client) { instance_double(VCAP::CloudController::UaaClient) }

before do
allow(CloudController::DependencyLocator.instance).to receive(:uaa_client).and_return(uaa_client)
allow(CloudController::DependencyLocator.instance).to receive(:uaa_username_lookup_client).and_return(uaa_client)
allow(user_with_role).to receive(:username).and_return('kiwi')
end

Expand Down
2 changes: 1 addition & 1 deletion spec/unit/controllers/v3/organizations_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
let(:uaa_client) { instance_double(VCAP::CloudController::UaaClient) }
before do
set_current_user(user)
allow(CloudController::DependencyLocator.instance).to receive(:uaa_client).and_return(uaa_client)
allow(CloudController::DependencyLocator.instance).to receive(:uaa_username_lookup_client).and_return(uaa_client)
allow(uaa_client).to receive(:usernames_for_ids).with([user.guid]).and_return(
{ user.guid => 'Ragnaros' }
)
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/decorators/include_role_user_decorator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module VCAP::CloudController
end

before do
allow(CloudController::DependencyLocator.instance).to receive(:uaa_client).and_return(uaa_client)
allow(CloudController::DependencyLocator.instance).to receive(:uaa_username_lookup_client).and_return(uaa_client)
allow(uaa_client).to receive(:users_for_ids).with([user1.guid, user2.guid]).and_return(user_uaa_info)
end

Expand Down
Loading

0 comments on commit c559b57

Please sign in to comment.