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

Correct user <-> group relationship on removal #9114

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

ekohl
Copy link
Contributor

@ekohl ekohl commented Sep 29, 2023

When a group needs to be removed, it can't be the primary group of a user. So if a user is ensured absent, it needs to happen before the group is removed.

This PR first adds tests to the existing code, before refactoring it. Then in the last commit it adds autobefore and makes sure autorequires is empty when ensure is absent.

This refactors the code using safe navigators and modern Ruby functions.
When a group needs to be removed, it can't be the primary group of a
user. So if a user is ensured absent, it needs to happen before the
group is removed.
@ekohl ekohl requested a review from a team as a code owner September 29, 2023 14:19
@puppetlabs-jenkins
Copy link
Collaborator

Can one of the admins verify this patch?

}
end

if @parameters[:groups]&.should
Copy link
Contributor

Choose a reason for hiding this comment

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

what is .should doing? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's mostly based on what was already there. I'm just refactoring.

Copy link
Contributor

Choose a reason for hiding this comment

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

The .should is the desired value specified in the catalog, which has been canonicalized by the property's munge method. Whereas .shouldorig is the desired value before munging:

@shouldorig = values

end

if @parameters[:groups]&.should
autos += @parameters[:groups].should.split(",")
Copy link
Contributor

Choose a reason for hiding this comment

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

can multiple groups be passed as comma separated string?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good question, was just refactoring. Now I question why it was there. I did notice in the other example that shouldorig was an array while should wasn't. So perhaps that's the problem?

Copy link
Contributor

Choose a reason for hiding this comment

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

Internally puppet stores the desired values as an array (both should and shouldorig) for example https://github.com/puppetlabs/puppet/blob/7ca202bd2faf876e1904a8a2e891c10c2cee6ffc/lib/puppet/property.rb#L552C1-L552C1

However, the should reader will either return a single value (default case) or an array depending on whether the property is single or multi-valued:

if match_all?
return @should.collect { |val| self.unmunge(val) }
else
return self.unmunge(@should[0])
end

The behavior is determined based on the array_matching value specified when the property is defined using newproperty. For example, members of the group is multi-valued:

newproperty(:members, :array_matching => :all, :required_features => :manages_members) do


# Autobefore the primary group, if it's around. Otherwise group removal
# fails when that group is also ensured absent.
autobefore(:group) do
Copy link
Contributor

Choose a reason for hiding this comment

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

There's a long standing bug in puppet that auto* relationships and ensure => absent don't work well together. I filed a redmine ticket a long time ago, which was exported to https://puppet.atlassian.net/browse/PUP-2451. Ideally I think puppet would know to reverse the direction for any auto* relationship when ensuring absent. Worse, if you try to set an explicit relationship, it will create a cycle due to the auto relationship.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have written Puppet code where I had a lot of conditionals based on absent/present. I'd love for Puppet to be smarter here. But reading https://github.com/voxpupuli/puppet-systemd/blob/dbaa56f690e36f9f164fee464bf7b0e8a9a6d37c/manifests/unit_file.pp#L131-L142 I really don't know how it could be designed in a way that's not more confusing than the status quo.

Overall few people write modules with (full) support to ensure things are absent.

@joshcooper joshcooper added the bug Something isn't working label Nov 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants