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

Fixes #36273 - Use proper permission for editing Ansible variable #633

Conversation

pmoravec
Copy link

@pmoravec pmoravec commented May 29, 2023

@nofaralfasi
Copy link
Contributor

Hey @pmoravec, I agree with the change you made in this PR. However, the fix only applies to the API. We also need to address the problem in the web UI.
Furthermore, I noticed that there are no tests for this functionality. Could you please add them?

@adamruzicka
Copy link
Contributor

We also need to address the problem in the web UI.

That's true, there are two other places where arguably wrong permission (edit_external_parameters) is used.

Could you please add them?

I'd be willing to let this slide. We can reason whether the permission used is the right one or not, but the whole authorization framework is thoroughly tested in Foreman itself.

@pmoravec
Copy link
Author

pmoravec commented Jun 2, 2023

I see. Sadly I dont have bandwidth to complete the fix as requested - please let somebody else adds a fix for WebUI "variant" and add some tests.

@nofaralfasi nofaralfasi force-pushed the foreman-ansible-pmoravec-edit-external-variables-auth branch from 1fa573b to c6850cd Compare June 25, 2023 12:02
@stejskalleos stejskalleos self-assigned this Jul 13, 2023
lib/foreman_ansible/register.rb Outdated Show resolved Hide resolved
lib/foreman_ansible/register.rb Outdated Show resolved Hide resolved
@ofedoren
Copy link
Member

Agree on that it should be using :*_ansible_variables here, since as noted @stejskalleos it somehow defined to use lookup_values permissions as well:

permission :edit_ansible_variables,
{ :lookup_values => [:update],
:ansible_variables => [:edit, :update],
:'api/v2/ansible_variables' => [:update],
:'api/v2/ansible_override_values' => [:create, :destroy] },
:resource_type => 'AnsibleVariable'
.

The only thing that bothers me, I didn't find any defined permissions on LookupValue model in Foreman itself :/

@nofaralfasi
Copy link
Contributor

The only thing that bothers me, I didn't find any defined permissions on LookupValue model in Foreman itself :/

I see the LookupValue is defined in foreman core. Would it solve the issue if I add the permissions for the LookupValue resource there?

@ofedoren
Copy link
Member

Would it solve the issue if I add the permissions for the LookupValue resource there?

It should 🤷

@nofaralfasi nofaralfasi changed the title Fixes #36273 - Use proper permission for editing a variable [WIP] Fixes #36273 - Use proper permission for editing a variable Jul 25, 2023
@nofaralfasi nofaralfasi marked this pull request as draft July 25, 2023 12:03
@nofaralfasi nofaralfasi force-pushed the foreman-ansible-pmoravec-edit-external-variables-auth branch from c6850cd to 9ddbe72 Compare August 9, 2023 13:44
@nofaralfasi nofaralfasi changed the title [WIP] Fixes #36273 - Use proper permission for editing a variable Fixes #36273 - Use proper permission for editing Ansible variable Aug 9, 2023
@nofaralfasi nofaralfasi marked this pull request as ready for review August 9, 2023 14:10
@nofaralfasi
Copy link
Contributor

How to test:

  1. Create a non-admin user, with 'Ansible Roles Manager' and View hosts permissions.
  2. UI - Login as the new user, and try to create, edit and delete Ansible variables from the host details page.
  3. API - Test the following requests using the new user credentials: POST /ansible/api/ansible_override_values, DELETE /ansible/api/ansible_override_values/:id.

Note:

  • Even if the user doesn't have the Edit Host permission, he will still be able to modify Ansible variables from the host details page. This functionality existed prior to this PR's changes, and if we want to change that behaviour, it would require disabling the corresponding option within the Specify Matchers section on the ansible_variable edit page.

@nofaralfasi
Copy link
Contributor

The tests are not passing due to the dependency on the merge of theforeman/foreman#9803.

Copy link
Member

@ofedoren ofedoren left a comment

Choose a reason for hiding this comment

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

Thanks, @nofaralfasi and @pmoravec, can be merged once theforeman/foreman#9803 is in.

@nofaralfasi nofaralfasi force-pushed the foreman-ansible-pmoravec-edit-external-variables-auth branch from 9ddbe72 to 8dfedc2 Compare August 27, 2023 14:27
@nofaralfasi
Copy link
Contributor

In my opinion, it seems necessary to include the lookup_values permissions (view, create, edit and destroy) in the register.rb file.
Without it, we are not able to use them for the Ansible Roles Manager role.

@nofaralfasi nofaralfasi force-pushed the foreman-ansible-pmoravec-edit-external-variables-auth branch from 8dfedc2 to 99e68de Compare August 29, 2023 13:04
Comment on lines 163 to 166
permission :view_lookup_values, {}, :resource_type => 'LookupValue'
permission :create_lookup_values, {}, :resource_type => 'LookupValue'
permission :edit_lookup_values, {}, :resource_type => 'LookupValue'
permission :destroy_lookup_values, {}, :resource_type => 'LookupValue'
Copy link
Member

Choose a reason for hiding this comment

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

Why is this needed? I mean, I've removed those lines and it still works...

Copy link
Contributor

Choose a reason for hiding this comment

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

Did you first remove these permissions from the DB?
From the rails console: Permission.where("name LIKE '%_lookup_values%'").destroy_all
And then re-run the rails server to test it.

Copy link
Member

Choose a reason for hiding this comment

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

I've done this and it looks... not quite good.

I've destroyed permissions, but we still need to create them, to use here. Thus, I needed to run db:seed to re-create them (we have defined them in core), but due to Ansible plugin needing these perms to create ARM role, it failed. Thus, after I removed Ansible plugin from loading, I've re-created the permissions, then enabled the plugin and run the server. I was able to change variable values even without this block.

I mean, we already create these perms in the core, why would we try to re-create them here? Seems weird. I've checked what we do with e.g. create_job_invocations, since this permission is used for ARM role as well: it seems REX plugin creates it and Ansible plugin then simply uses it to define a role. As you can see, Ansible plugin doesn't call permission :create_job_invocations, which is right.

Since I'm not so well familiar with the internals of permission logic, @adamruzicka, maybe you can help us out?

Copy link
Contributor

Choose a reason for hiding this comment

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

I forgot I need to make a change in the db/seeds.d/020-permissions_list.rb file for the db:seed command to include that file. That's why it didn't work as expected for me.
Therefore, as long as we run the db:seed command, there's no requirement to additionally include these permissions in the register.rb file, right?

Copy link
Member

Choose a reason for hiding this comment

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

🤷 that's what I hoped for...

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, we shouldn't redefine permissions. Let's get rid of it, it seemed to work for me when I did it locally

Copy link
Member

Choose a reason for hiding this comment

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

But the test failures now show what I was facing as well :/ Not sure why it complains about perms that are being created in DB by Foreman itself, isn't here some issues with load ordering?

@nofaralfasi nofaralfasi force-pushed the foreman-ansible-pmoravec-edit-external-variables-auth branch from 99e68de to 54b8e3f Compare September 26, 2023 09:48
@nofaralfasi nofaralfasi force-pushed the foreman-ansible-pmoravec-edit-external-variables-auth branch from 54b8e3f to ee61573 Compare November 2, 2023 17:02
Add LookupValue permissions to allow editing of Ansible variables,
for non-admin users.

Signed-off-by: Pavel Moravec <pmoravec@redhat.com>
@nofaralfasi nofaralfasi force-pushed the foreman-ansible-pmoravec-edit-external-variables-auth branch from ee61573 to 6b73038 Compare November 6, 2023 18:13
@nofaralfasi nofaralfasi merged commit ff31fe2 into theforeman:master Nov 6, 2023
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
6 participants