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

Add usergroup support to user.present #48704

Merged
merged 6 commits into from
Dec 14, 2018

Conversation

boltronics
Copy link
Contributor

What does this PR do?

Replaces the seemingly redundant gid_from_name argument from user.present and replaces it with a usergroup argument that has the effect of adding the --user-group argument to the useradd command on GNU/Linux hosts.

I whitelisted it to GNU/Linux for now since I don't know which other operating systems support the -U/--user-group argument.

I tested it on an OpenSUSE 42.3 and Debian Stretch by creating a basic state sls:

test:
  user.present:
    - usergroup: True

test2:
  user.present:
    - usergroup: False

On both hosts the intended behaviour was observed.

What issues does this PR fix or reference?

#48640

Previous Behavior

gid_from_name has been removed. AFAICT this command no longer has any practical benefit - please correct me if I'm overlooking something. The behaviour it intended to provide can be simulated just as easily by:

test:
  user.present:
    - gid: test

(where we want a test group to match a test user). This is much easier to read and understand. It's also clearer that the group will not be automatically created in this case.

Regardless of the gid_from_name option, the group will not be created since 2017.7.5 due to the regression referenced. This PR does not revert that regression (which is actually a pain to deal with on a stable release and IMO really should be fixed), but I believe this is a better approach for the long-term.

New Behavior

We now mimic the existing --user-group option behaviour of the useradd command which I believe is closer to user expectations - even for users of distributions such as SuSE that don't typically use this.

If usergroup: True is set in the user state sls file, the -U argument will be passed to the useradd command causing the group with the same name as the user name to be created. If usergroup: False is set, the -N/--no-user-group argument is added instead. If left undefined, the defaults that the distribution or user typically defines in /etc/default/useradd and /etc/login.defs are used as usual.

Tests written?

No. There are no existing tests for gid_from_name either. I'm not sure what the best way to do this would be, but I suspect it would involve having the test inspect the constructed cmd list in the useradd.add function to check for the presence of -U instead, as opposed to just inspecting a return value of a mocked command.

Commits signed with GPG?

Yes

@boltronics boltronics changed the title Add usergroup support for user.present Add usergroup support to user.present Jul 23, 2018
@@ -187,6 +192,8 @@ def add(name,
except OSError:
# /etc/usermgmt.conf not present: defaults will be used
pass
if usergroup == False:
Copy link
Contributor

Choose a reason for hiding this comment

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

the default for 'usergroup' is None. Should you compare this to None too? I'm thinking to change this to: if not usergroup:

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There are three possibilities - we explicitly set usergroup to False to trigger -N to require no user group be created, we set it to True to trigger -U to require a user group to be created, or don't touch it at all which will go with the OS defaults - which differs depending on the distribution or user preferences.

If you instead go with if not usergroup, you'll be force overriding the OS defaults (or overriding whatever the user configured the OS to do), so I imagine most people won't be too happy about that.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, okay. So it's intended to use the OS defaults unless specified.

Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't we then document this so that a user knows when to set this to False and what behavior that will produce?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@rares-pop Correct. I don't see any reason to change things the user didn't request. @cachedout Yep, fair call. Will do.

@boltronics boltronics force-pushed the develop-user_present-usergroup branch 3 times, most recently from f986ed7 to e8f83ab Compare July 24, 2018 00:00
@boltronics
Copy link
Contributor Author

That has now been updated. I also rebased onto the latest develop branch.

@rallytime
Copy link
Contributor

@boltronics boltronics force-pushed the develop-user_present-usergroup branch from e8f83ab to 9d013f4 Compare July 25, 2018 04:29
@boltronics
Copy link
Contributor Author

@rallytime Hopefully the lint error is sorted now. Thanks for pointing that out.

@rallytime
Copy link
Contributor

@boltronics Sure thing. The lint check is happy now, but now there are some related test failures with the user states and the git pillar tests: https://jenkinsci.saltstack.com/job/pr-kitchen-ubuntu1604-py3/job/PR-48704/5/#showFailuresLink

Can you take a look?

@boltronics
Copy link
Contributor Author

No worries, can do.

@boltronics
Copy link
Contributor Author

Sorry for the hold-up. I've been busy, but will try to get to this this week.

@cachedout
Copy link
Contributor

Hi @boltronics. Just checking in with you. :)

@boltronics
Copy link
Contributor Author

Sorry for the delay. I haven't forgotten, just taking care of higher priorities first. I hope to get to this soon.

@cachedout
Copy link
Contributor

@boltronics Many thanks!

@cachedout
Copy link
Contributor

Hi @boltronics. Just checking back in with you. :) Any updates here?

@boltronics boltronics force-pushed the develop-user_present-usergroup branch from 4fdf0f4 to 35260bd Compare October 22, 2018 00:47
@boltronics
Copy link
Contributor Author

Thanks @cachedout. I'm actually trying to look into this now (sorry again for the wait), but can't even push to my repo due to some GitHub issues it seems. My commits just keep disappearing...

1 similar comment
@boltronics
Copy link
Contributor Author

Thanks @cachedout. I'm actually trying to look into this now (sorry again for the wait), but can't even push to my repo due to some GitHub issues it seems. My commits just keep disappearing...

@boltronics
Copy link
Contributor Author

Hi @cachedout. Hopefully GitHub is working this time... I think I'm all good for fixing the tests (or I will be when GH is fixed so my pushed code doesn't go to /dev/null). The only one I'm concerned about is https://github.com/saltstack/salt/blob/develop/tests/integration/states/test_user.py#L131 test_user_present_gid_from_name_default as that doesn't make sense to me. Say I rename it to test_user_present_usergroup_default... it would only exist to test undefined behaviour. ie.

        if grains['os_family'] in ('Suse',):                                                                                                                                                       
            self.assertEqual(group_name, 'users')                                                                                                                                                    
        elif grains['os_family'] == 'MacOS':                                                                                                                                                         
            self.assertEqual(group_name, 'staff') 

Of course, Apple, SuSE, etc. could change their mind about their default behaviour at any point which would break the test.

If we set usergroup to True or False we can immediately see what we should expect, but if we set it to None (or just leave it unset) it'll do whatever the OS will do by default. We can try to guess what might happen, or we can try to get a better idea by parsing /etc/login.defs, etc. eg.

login_defs_path = '/etc/login.defs'                                   
# It looks like FreeBSD creates a usergroup by default. I'm           
# working on the assumption that this is usually true outside         
# of MacOS and Linux.                                                 
usergroup = True                                                      
# MacOS users' primary group defaults to staff (20), not the          
# name of user                                                        
if grains['os_family'] == 'MacOS':                                    
    usergroup = False                                                 
elif grains['kernel'] == 'Linux':                                     
    # It looks like login usually defaults to "no".                   
    try:                                                              
        with salt.utils.files.fopen(login_defs_path, 'r') as lfh:     
            for line in lfh:                                          
                if line.startswith('USERGROUPS_ENAB'):                
                    usergroup = line.strip().endswith('yes')          
                break                                                 
    except IOError:                                                   
        usergroup = False                                             

but I'm not sure I see a useful test here. We're only validating that we guessed correctly what the OS might do when the user didn't request anything specific, as opposed to testing the usergroup functionality worked as intended. We already have what will be a test_user_present_usergroup function, so I propose replacing test_user_present_gid_from_name_default with test_user_present_usergroup_disabled instead.

Please let me know your thoughts. In the meantime, I'll proceed with this proposal for this PR.

@boltronics
Copy link
Contributor Author

boltronics commented Oct 22, 2018 via email

@boltronics boltronics force-pushed the develop-user_present-usergroup branch from 85db08c to b7cc810 Compare October 22, 2018 22:24
@boltronics
Copy link
Contributor Author

I have just rebased against the current develop HEAD, and looks like Jenkins triggered successfully now.

...and it also looks like all the comments came through from yesterday that reportedly failed to submit. 😄

@rallytime rallytime requested a review from cachedout October 23, 2018 13:43
@boltronics boltronics force-pushed the develop-user_present-usergroup branch from b7cc810 to 52483af Compare November 22, 2018 20:59
@cachedout
Copy link
Contributor

@boltronics Sorry for the delay here. I think we're about ready to go with this. If you can resolve the merge conflict that snuck in then we'll get this merged.

boltronics and others added 4 commits December 11, 2018 11:06
Fix saltstack#48640: user.present group regression if gid_from_name
The usergroup argument isn't available on all operating systems and is
for the most part a convenience argument.
Here we replace test_user_present_gid_from_name_default (which tried
to guess what different operating systems might do) with tests
specifically for when usergroup is set to either True or False.
Undefined behaviour (usergroup=None) is deliberately left untested
since the results are not always predictable and aren't terribly
useful.
@boltronics boltronics force-pushed the develop-user_present-usergroup branch from 52483af to b1fa539 Compare December 11, 2018 00:25
@boltronics
Copy link
Contributor Author

@cachedout Done.

@boltronics
Copy link
Contributor Author

I also just added the docstring commit since that's dealing with the same function.

@thatch45 thatch45 merged commit 27862fe into saltstack:develop Dec 14, 2018
@thatch45
Copy link
Contributor

Thanks @boltronics ! This looks great to me. And thanks for following the PR for so long!

@boltronics boltronics deleted the develop-user_present-usergroup branch December 16, 2018 11:52
@dwoz dwoz mentioned this pull request Apr 21, 2020
1 task
@dwoz dwoz added the has master-port port to master has been created label Apr 21, 2020
dwoz added a commit to dwoz/salt that referenced this pull request Apr 22, 2020
dwoz added a commit that referenced this pull request Apr 23, 2020
herbetom added a commit to Freifunk-Rhein-Neckar/salt that referenced this pull request Jun 22, 2020
since Salt version 3001 gid_from_name is no longer an valid argument

see: saltstack/salt#48704
and: https://git.darmstadt.ccc.de/ffda/infra/salt/-/merge_requests/83
herbetom added a commit to Freifunk-Rhein-Neckar/salt that referenced this pull request Jun 22, 2020
since Salt version 3001 gid_from_name is no longer an valid argument

see: saltstack/salt#48704
and: https://git.darmstadt.ccc.de/ffda/infra/salt/-/merge_requests/83
jdelic added a commit to jdelic/saltshaker that referenced this pull request Jul 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
code-jam has master-port port to master has been created
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants