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: with_role_excluding has bad subquery #17930

Merged
merged 1 commit into from
Sep 7, 2018

Conversation

kbrock
Copy link
Member

@kbrock kbrock commented Aug 30, 2018

Rbac.filtered and search uses with_role_excluding to filter for non tenant_admin users.

Unfortunately, the subquery was using the select from the main query and
was blowing up.

It is now properly isolating the queries and tests have been added to
protect against regressions.

A similar issue exists for User.with_same_userid. That was corrected here as well.

Thanks @yrudman for the teamwork.
Fixes https://bugzilla.redhat.com/show_bug.cgi?id=1623464

@@ -61,7 +61,7 @@ def settings=(new_settings)
end

def self.with_roles_excluding(identifier)
where.not(:id => MiqGroup.joins(:miq_product_features)
where.not(:id => MiqGroup.unscoped.joins(:miq_product_features)
Copy link
Member

Choose a reason for hiding this comment

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

Should this be scoped to in_my_region?


def self.with_roles_excluding(identifier)
where.not(:id => User.joins(:miq_groups => :miq_product_features)
where.not(:id => User.unscoped.joins(:miq_groups => :miq_product_features)
Copy link
Member

Choose a reason for hiding this comment

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

Same here

b = FactoryGirl.create(:miq_group, :features => %w(good everything), :description => "combo")
c = FactoryGirl.create(:miq_group, :features => "everything", :description => "admins")

expect(MiqGroup.select(:id, :description).with_roles_excluding("everything")).to include(a)
Copy link
Member

Choose a reason for hiding this comment

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

Should this be a single expectation using match_array?

Copy link
Member Author

Choose a reason for hiding this comment

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

some other groups were coming back - I'll double check.

b = FactoryGirl.create(:miq_user_role, :features => %w(good everything), :name => "combo")
c = FactoryGirl.create(:miq_user_role, :features => "everything", :name => "admins")

expect(MiqUserRole.select(:id, :name).with_roles_excluding("everything")).to include(a)
Copy link
Member

Choose a reason for hiding this comment

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

same here

u3 = FactoryGirl.create(:user, :miq_groups => [a1, b]) # nope
u4 = FactoryGirl.create(:user, :miq_groups => [c]) # nope

expect(User.with_roles_excluding("everything").select(:id, :name)).to include(u1, u2)
Copy link
Member

Choose a reason for hiding this comment

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

and here


describe ".with_roles_excluding" do
it "handles multiple columns" do
a1 = FactoryGirl.create(:miq_group, :features => "good", :description => "simple")
Copy link
Member

Choose a reason for hiding this comment

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

description is sequenced in the factory, maybe those would be better as variable names?

Copy link
Member Author

Choose a reason for hiding this comment

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

I can remove. I just had them in for when I was debugging

@@ -61,7 +61,7 @@ def settings=(new_settings)
end

def self.with_roles_excluding(identifier)
where.not(:id => MiqGroup.joins(:miq_product_features)
where.not(:id => MiqGroup.unscoped.joins(:miq_product_features)
Copy link
Member

Choose a reason for hiding this comment

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

How do we know we want to remove the scopes here unconditionally?

Copy link
Member Author

@kbrock kbrock Sep 1, 2018

Choose a reason for hiding this comment

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

So this is saying, "bring back all groups except those associated with the product feature of 'everything'"

@bdunne very good question. That had slipped my mind.

In this case, I think it is best to not have in_my_region.
This filter is removing entries. If a user asked for all users in all regions, we would want to filter out the groups with high privileged features in other regions as well.
If we add that scope we would have ALL_GROUPS_ALL_REGIONS - SUPER_GROUPS_MY_REGION - Mis-displaying SUPER_GROUPS_OTHER_REGIONS

though this is probably a stretch case

Copy link
Member Author

Choose a reason for hiding this comment

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

@jrafanie ok - I changed to only remove the select portion since that is what was blowing up

@jrafanie
Copy link
Member

@kbrock Wow, I'll need to review this more but I think I follow what happens. I don't know how we know we want to unscope in all cases.

@kbrock kbrock force-pushed the bz_1623464 branch 2 times, most recently from b532c0e to 3edea92 Compare September 5, 2018 15:49

describe ".with_same_userid" do
# this is testing the select does not break and in general, the scope works
it "properly unsopes" do
Copy link
Member

Choose a reason for hiding this comment

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

typo: unscopes

Rbac.filtered/search uses this method to filter by tenant admin
Unfortunately, the subquery was using the select from the main query and
producing bad queries

It is now properly isolating the queries and tests have been added to
protect against regressions

https://bugzilla.redhat.com/show_bug.cgi?id=1623464
@miq-bot
Copy link
Member

miq-bot commented Sep 5, 2018

Checked commit kbrock@9a9b5dd with ruby 2.3.3, rubocop 0.52.1, haml-lint 0.20.0, and yamllint 1.10.0
7 files checked, 0 offenses detected
Everything looks fine. 🍪

@kbrock
Copy link
Member Author

kbrock commented Sep 7, 2018

@jrafanie thanks for pointing out typo - this good for you now?

@jrafanie
Copy link
Member

jrafanie commented Sep 7, 2018

LGTM, please add backport labels... @kbrock how confident are you for backporting this?

@jrafanie jrafanie merged commit aff9450 into ManageIQ:master Sep 7, 2018
@jrafanie jrafanie added this to the Sprint 94 Ending Sept 10, 2018 milestone Sep 7, 2018
@kbrock kbrock deleted the bz_1623464 branch September 7, 2018 22:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants