Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Container* belongsto filter in Rbac::Filterer #18654

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 44 additions & 2 deletions lib/rbac/filterer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,18 @@ class Filterer
).freeze

BELONGSTO_FILTER_CLASSES = %w(
Container
ContainerBuild
ContainerGroup
ContainerImage
ContainerImageRegistry
ContainerNode
ContainerProject
ContainerReplicator
ContainerRoute
ContainerService
ContainerTemplate
ContainerVolume
EmsCluster
EmsFolder
ExtManagementSystem
Expand Down Expand Up @@ -714,15 +726,45 @@ def get_belongsto_matches(blist, klass)
# typically, this is the only one we want:
vcmeta = vcmeta_list.last

if ([ExtManagementSystem, Host].any? { |x| vcmeta.kind_of?(x) } && klass <= VmOrTemplate) ||
(vcmeta.kind_of?(ManageIQ::Providers::NetworkManager) && NETWORK_MODELS_FOR_BELONGSTO_FILTER.any? { |association_class| klass <= association_class.safe_constantize })
if belongsto_association_filtered?(vcmeta, klass)
vcmeta.send(association_name).to_a
else
vcmeta_list.grep(klass) + vcmeta.descendants.grep(klass)
end
end.uniq
end

def belongsto_association_filtered?(vcmeta, klass)
if [ExtManagementSystem, Host].any? { |x| vcmeta.kind_of?(x) }
# Eject early if true
return true if associated_belongsto_models.any? { |associated| klass <= associated }
end

if vcmeta.kind_of?(ManageIQ::Providers::NetworkManager)
NETWORK_MODELS_FOR_BELONGSTO_FILTER.any? do |association_class|
klass <= association_class.safe_constantize
end
end
end

def associated_belongsto_models
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

future reference: putting into a constant preloads all classes. not good for load time and development re-loading.

he was not a fan of safe_constantize - so we agreed that this was the best solution.

[
VmOrTemplate,
Container,
ContainerBuild,
ContainerGroup,
ContainerImage,
ContainerImageRegistry,
ContainerNode,
ContainerProject,
ContainerReplicator,
ContainerRoute,
ContainerService,
ContainerTemplate,
ContainerVolume
]
end

def get_belongsto_matches_for_host(blist)
clusters = []
hosts = []
Expand Down
73 changes: 73 additions & 0 deletions spec/lib/rbac/filterer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,79 @@ def combine_filtered_ids(user_filtered_ids, belongsto_filtered_ids, managed_filt
end
end

context "with ContainerManagers with user roles" do
let(:owned_ems) { FactoryBot.create(:ems_openshift) }
let(:other_ems) { FactoryBot.create(:ems_openshift) }

before do
filters = ["/belongsto/ExtManagementSystem|#{owned_ems.name}"]

owner_group.entitlement = Entitlement.new
owner_group.entitlement.set_managed_filters([])
owner_group.entitlement.set_belongsto_filters(filters)
owner_group.save!
end

%w[
Container
ContainerBuild
ContainerGroup
ContainerImage
ContainerImageRegistry
ContainerNode
ContainerProject
ContainerReplicator
ContainerRoute
ContainerService
ContainerTemplate
].each do |object_klass|
context "with #{object_klass}s" do
let(:subklass) { owned_ems.class.const_get(object_klass) }
let!(:object1) { subklass.create(:ems_id => owned_ems.id) }
let!(:object2) { subklass.create(:ems_id => owned_ems.id) }
let!(:object3) { subklass.create(:ems_id => other_ems.id) }
let!(:object4) { subklass.create(:ems_id => other_ems.id) }

it "properly filters" do
search_opts = {
:targets => subklass,
:userid => owner_user.userid
}
results = described_class.search(search_opts)
objects = results.first

expect(objects.length).to eq(2)
expect(objects.to_a).to match_array([object1, object2])
end
end
end

# ContainerVolumes are the only class that has a `has_many :through`
# relationship with EMS.
context "with ContainerVolumes" do
let(:subklass) { owned_ems.class.const_get(:ContainerGroup) }
let(:volume_klass) { owned_ems.class.const_get(:ContainerVolume) }
let!(:group1) { subklass.create(:ems_id => owned_ems.id) }
let!(:group2) { subklass.create(:ems_id => other_ems.id) }
let!(:volume1) { volume_klass.create(:parent => group1) }
let!(:volume2) { volume_klass.create(:parent => group1) }
let!(:volume3) { volume_klass.create(:parent => group2) }
let!(:volume4) { volume_klass.create(:parent => group2) }

it "properly filters" do
search_opts = {
:targets => volume_klass,
:userid => owner_user.userid
}
results = described_class.search(search_opts)
objects = results.first

expect(objects.length).to eq(2)
expect(objects.to_a).to match_array([volume1, volume2])
end
end
end

context 'when class does not participate in RBAC' do
before do
@vm = FactoryBot.create(:vm_vmware, :name => "VM1", :host => @host1, :ext_management_system => @ems)
Expand Down