-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
[master] Fix inconsistent use of args in ssh_auth.managed #64443
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will also require test coverage to ensure this doesn't regress in the future.
salt/states/ssh_auth.py
Outdated
remove_comment = absent( | ||
remove_key, | ||
user, | ||
enc, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you pass these in as kwargs, for example enc=enc
. Just future proofing in case any kwargs's position moves.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oop. Sorry for never getting back to this, notifications get spammed a lot by private repos. Glad to see this got picked and merged though!
bump @rittycat did you see my review comments? |
thank you for the PR @rittycat |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's helper code to create/delete temporary accounts.
Also, if the fixture is not needed anywhere else, define the fixture on the module using it.
@pytest.fixture | ||
def system_user(salt_call_cli): | ||
try: | ||
rand_str = "".join(random.choices(string.ascii_lowercase, k=3)) | ||
username = f"salt-{rand_str}" | ||
salt_call_cli.run("--local", "user.add", username) | ||
yield username | ||
finally: | ||
salt_call_cli.run("--local", "user.delete", username, purge=True, force=True) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pytest.fixture | |
def system_user(salt_call_cli): | |
try: | |
rand_str = "".join(random.choices(string.ascii_lowercase, k=3)) | |
username = f"salt-{rand_str}" | |
salt_call_cli.run("--local", "user.add", username) | |
yield username | |
finally: | |
salt_call_cli.run("--local", "user.delete", username, purge=True, force=True) |
# ADD DESTRUCTIVE DE | ||
@pytest.mark.slow_test | ||
def test_ssh_auth_config(tmp_path, system_user, state_tree): | ||
""" | ||
test running ssh_auth state when | ||
different config is set. Ensure | ||
it does not edit the default config. | ||
""" | ||
userdetails = user_module.info(system_user) | ||
user_ssh_dir = pathlib.Path(userdetails["home"], ".ssh") | ||
ret = ssh_auth_state.manage( | ||
name="test", | ||
user=system_user, | ||
ssh_keys=["ssh-dss AAAAB3NzaCL0sQ9fJ5bYTEyY== root@domain"], | ||
) | ||
|
||
with salt.utils.files.fopen(user_ssh_dir / "authorized_keys") as fp: | ||
pre_data = fp.read() | ||
|
||
file_contents = "ssh-dss AAAAB3NzaCL0sQ9fJ5bYTEyY== root@domain" | ||
new_auth_file = tmp_path / "authorized_keys3" | ||
|
||
with pytest.helpers.temp_file("authorized", file_contents, state_tree): | ||
ssh_auth_state.manage( | ||
name="test", | ||
user=system_user, | ||
source=f"salt://authorized", | ||
config=str(new_auth_file), | ||
ssh_keys=[""], | ||
) | ||
with salt.utils.files.fopen(user_ssh_dir / "authorized_keys") as fp: | ||
post_data = fp.read() | ||
|
||
assert pre_data == post_data | ||
with salt.utils.files.fopen(new_auth_file) as fp: | ||
data = fp.read().strip() | ||
assert data == file_contents |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# ADD DESTRUCTIVE DE | |
@pytest.mark.slow_test | |
def test_ssh_auth_config(tmp_path, system_user, state_tree): | |
""" | |
test running ssh_auth state when | |
different config is set. Ensure | |
it does not edit the default config. | |
""" | |
userdetails = user_module.info(system_user) | |
user_ssh_dir = pathlib.Path(userdetails["home"], ".ssh") | |
ret = ssh_auth_state.manage( | |
name="test", | |
user=system_user, | |
ssh_keys=["ssh-dss AAAAB3NzaCL0sQ9fJ5bYTEyY== root@domain"], | |
) | |
with salt.utils.files.fopen(user_ssh_dir / "authorized_keys") as fp: | |
pre_data = fp.read() | |
file_contents = "ssh-dss AAAAB3NzaCL0sQ9fJ5bYTEyY== root@domain" | |
new_auth_file = tmp_path / "authorized_keys3" | |
with pytest.helpers.temp_file("authorized", file_contents, state_tree): | |
ssh_auth_state.manage( | |
name="test", | |
user=system_user, | |
source=f"salt://authorized", | |
config=str(new_auth_file), | |
ssh_keys=[""], | |
) | |
with salt.utils.files.fopen(user_ssh_dir / "authorized_keys") as fp: | |
post_data = fp.read() | |
assert pre_data == post_data | |
with salt.utils.files.fopen(new_auth_file) as fp: | |
data = fp.read().strip() | |
assert data == file_contents | |
@pytest.fixture(scope="module") | |
def system_user(): | |
with pytest.helpers.create_account() as system_account: | |
yield system_account | |
@pytest.mark.skip_if_not_root | |
@pytest.mark.destructive_test | |
@pytest.mark.slow_test | |
def test_ssh_auth_config(tmp_path, system_user, state_tree): | |
""" | |
test running ssh_auth state when | |
different config is set. Ensure | |
it does not edit the default config. | |
""" | |
userdetails = system_user.info | |
user_ssh_dir = pathlib.Path(userdetails["home"], ".ssh") | |
ret = ssh_auth_state.manage( | |
name="test", | |
user=system_user.username, | |
ssh_keys=["ssh-dss AAAAB3NzaCL0sQ9fJ5bYTEyY== root@domain"], | |
) | |
with salt.utils.files.fopen(user_ssh_dir / "authorized_keys") as fp: | |
pre_data = fp.read() | |
file_contents = "ssh-dss AAAAB3NzaCL0sQ9fJ5bYTEyY== root@domain" | |
new_auth_file = tmp_path / "authorized_keys3" | |
with pytest.helpers.temp_file("authorized", file_contents, state_tree): | |
ssh_auth_state.manage( | |
name="test", | |
user=system_user.username, | |
source=f"salt://authorized", | |
config=str(new_auth_file), | |
ssh_keys=[""], | |
) | |
with salt.utils.files.fopen(user_ssh_dir / "authorized_keys") as fp: | |
post_data = fp.read() | |
assert pre_data == post_data | |
with salt.utils.files.fopen(new_auth_file) as fp: | |
data = fp.read().strip() | |
assert data == file_contents |
I cherry-picked this fix into 3006.x: #65046 I'm going to close here as this change will be merged forward to master. |
What does this PR do?
Ensures that arguments are properly used in all functions called by
ssh_auth.managed
. Particularly when it checks for what to remove from an existing configWhat issues does this PR fix or reference?
Fixes: #64442
Previous Behavior
When calling
ssh_auth.absent
, it would not use all arguments given to the state, thus, if you for example specified a custom config file, the state would delete keys from the default config file instead, while only adding new keys to the custom path specifiedNew Behavior
Uses all arguments as you would expect in any functions called by the managed state
Merge requirements satisfied?
[NOTICE] Bug fixes or features added to Salt require tests.
Commits signed with GPG?
Yes
Please review Salt's Contributing Guide for best practices.
See GitHub's page on GPG signing for more information about signing commits with GPG.