Add mod_aggregate to ssh_auth to manage authorized_keys#49776
Add mod_aggregate to ssh_auth to manage authorized_keys#49776mchugh19 wants to merge 3 commits intosaltstack:developfrom
Conversation
rallytime
left a comment
There was a problem hiding this comment.
I have one small comment here.
| aggregate | ||
| Removes any existing ssh keys except for those managed by subsequent ``ssh_auth.present`` | ||
|
|
||
| .. versionadded:: Fluorine |
There was a problem hiding this comment.
Can you change this to Neon?
There was a problem hiding this comment.
Whoops! Yep, will do.
| if len(comps) == 3: | ||
| comment = comps[2] | ||
|
|
||
| if 'ssh_keys' in kwargs: |
There was a problem hiding this comment.
Can we expose this so that it can be used directly as well, instead of only through mod_aggregate?
There was a problem hiding this comment.
Probably just needs to be added to the docs for this function.
There was a problem hiding this comment.
Hmmm. Yes, but that's harder.
At the moment, since ssh_keys is only ever set by the mod_aggregate function, the singular ssh_auth.present is able to get away with having ssh_keys only perform the delete on keys that aren't managed by salt. Actually adding the keys is performed by the current code in present. If we expose ssh_keys, then you would have to modify ssh_auth.present to be capable of adding all those ssh_keys as well.
So this PR currently handles "managing" ssh_authorized with:
key1:
ssh_auth.present:
- user: testuser
- enc: ssh-rsa
- comment: testuser 1
- aggregate: True
- names:
- keyNumber1
key2:
ssh_auth.present:
- user: testuser
- enc: ssh-rsa
- comment: testuser 1
- aggregate: True
- names:
- keyNumber2
Each of those ssh_auth.presents performs the key add. Then one of them gets extended by mod_aggregate which adds ssh_keys=['keyNumber1', 'keyNumber2']. Thus this PR only uses ssh_keys to detect what is being added, in order to determine which existing keys should be removed.
If ssh_keys were exposed, I'd expect it to be a synonym for names and thus actually perform the add and removal of everything else. I think names is supported by the state loader, and thus to get this function to support ssh_keys means that we have to add logic to loop.
If that's all correct it's doable, but a bit more invasive. Is that worth it? Or am I missing something?
There was a problem hiding this comment.
Ok, removing the ssh keys is not the purpose of mod_aggregate.
The purpose of mod_aggregate is to combine all of the calls into just one call.
See how it is used in pkg.installed where it makes only one call to the system package manager, instead of once per package state.
I am not sure that we should make this change if it is going to start removing keys that are not managed by the aggregate. @saltstack/team-core opinions?
There was a problem hiding this comment.
Agreed. Hence the initial "I don't know if this is kosher" comment.
In order to perform authorized_keys management where keys not managed by salt are removed, we would need to know about all the keys that salt is managing. Today that means a series of ssh_auth.present stanzas. Because they are indepent, it forces the use of mod_aggregate. This overloads the use of mod_aggregate. In the pkg state it aggregates calls while in this use it aggregates and allows the multiple calls to more completely manage the authorized_keys file. Possibly a hack, or possibly awesome?!
If there are cleaner ways of accomplishing this, I'm for that as well. But a definitely a review is needed before any potential merge.
There was a problem hiding this comment.
yeah, i just want to have more input from the rest of the core team to see if we are ok with this.
terminalmage
left a comment
There was a problem hiding this comment.
This is definitely not how we want to use aggregate.
|
@gtmanfred @terminalmage @cachedout is there a recommendation for how to support #13340 functionality?
|
|
Closing this out in favor of a different approach |
Not sure if this is an appropriate way to handle this scenario. Comments requested!
What does this PR do?
Adds a mod_aggregate function to the ssh_auth state. This then builds up a list of the ssh keys from this and subsequent ssh_auth.present declarations, for each user. The present function then uses this list to determine what ssh_keys are being managed by salt, and removes the rest.
What issues does this PR fix or reference?
#13340
Previous Behavior
ssh_auth.present was only able to add keys. Any old keys not managed by salt need to be removed by specifying them in a ssh_auth.absent state.
New Behavior
By specifying
aggregate: Truefor the ssh_auth.present state, ssh keys not added by salt will be removed.Example
When adding the Asomekeyh ssh key, another Aoldkey= was found for this user. Since it was not provisioned by salt, it was removed.
Here is a state to ensure that Asomekeyh is present. In the past Aoldkey= had been added to the host, but has been forgotten and is not managed by salt.
By running this state, ssh_auth.present ensured the Asomekeyh was present, but also removed any keys found for the testuser user which were not managed by salt.
Tests written?
No
Commits signed with GPG?
No