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

[master] Fix inconsistent use of args in ssh_auth.managed #64443

Closed

Conversation

rittycat
Copy link
Contributor

@rittycat rittycat commented Jun 9, 2023

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 config

What 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 specified

New 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.

@rittycat rittycat requested a review from a team as a code owner June 9, 2023 11:03
@rittycat rittycat requested review from MKLeb and removed request for a team June 9, 2023 11:03
@salt-project-bot-prod-environment salt-project-bot-prod-environment bot changed the title Fix inconsistent use of args in ssh_auth.managed [master] Fix inconsistent use of args in ssh_auth.managed Jun 9, 2023
@rittycat rittycat temporarily deployed to ci June 9, 2023 11:30 — with GitHub Actions Inactive
@rittycat rittycat temporarily deployed to ci June 9, 2023 11:30 — with GitHub Actions Inactive
@rittycat rittycat temporarily deployed to ci June 9, 2023 11:30 — with GitHub Actions Inactive
@rittycat rittycat temporarily deployed to ci June 9, 2023 11:30 — with GitHub Actions Inactive
@rittycat rittycat temporarily deployed to ci June 9, 2023 11:48 — with GitHub Actions Inactive
@rittycat rittycat temporarily deployed to ci June 9, 2023 11:50 — with GitHub Actions Inactive
@rittycat rittycat temporarily deployed to ci June 9, 2023 12:34 — with GitHub Actions Inactive
@rittycat rittycat temporarily deployed to ci June 9, 2023 12:34 — with GitHub Actions Inactive
@rittycat rittycat temporarily deployed to ci June 9, 2023 12:34 — with GitHub Actions Inactive
@rittycat rittycat temporarily deployed to ci June 9, 2023 12:34 — with GitHub Actions Inactive
@rittycat rittycat temporarily deployed to ci June 9, 2023 12:34 — with GitHub Actions Inactive
@rittycat rittycat temporarily deployed to ci June 9, 2023 12:34 — with GitHub Actions Inactive
@rittycat rittycat temporarily deployed to ci June 9, 2023 12:41 — with GitHub Actions Inactive
@rittycat rittycat temporarily deployed to ci June 9, 2023 12:41 — with GitHub Actions Inactive
@rittycat rittycat temporarily deployed to ci June 9, 2023 12:41 — with GitHub Actions Inactive
@rittycat rittycat temporarily deployed to ci June 9, 2023 12:41 — with GitHub Actions Inactive
@rittycat rittycat temporarily deployed to ci June 9, 2023 12:41 — with GitHub Actions Inactive
@rittycat rittycat temporarily deployed to ci June 9, 2023 12:41 — with GitHub Actions Inactive
@Ch3LL Ch3LL added backport:3006.x Backport PR to 3006.x branch Sulfur v3006.2 labels Jun 9, 2023
Copy link
Contributor

@Ch3LL Ch3LL left a 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.

remove_comment = absent(
remove_key,
user,
enc,
Copy link
Contributor

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.

Copy link
Contributor Author

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!

@Ch3LL
Copy link
Contributor

Ch3LL commented Aug 7, 2023

bump @rittycat did you see my review comments?

@Ch3LL Ch3LL temporarily deployed to ci August 15, 2023 19:09 — with GitHub Actions Inactive
@Ch3LL Ch3LL temporarily deployed to ci August 15, 2023 19:10 — with GitHub Actions Inactive
@Ch3LL Ch3LL temporarily deployed to ci August 15, 2023 19:10 — with GitHub Actions Inactive
@Ch3LL Ch3LL temporarily deployed to ci August 15, 2023 19:27 — with GitHub Actions Inactive
@Ch3LL Ch3LL temporarily deployed to ci August 15, 2023 19:29 — with GitHub Actions Inactive
@Ch3LL Ch3LL temporarily deployed to ci August 15, 2023 20:44 — with GitHub Actions Inactive
@Ch3LL Ch3LL temporarily deployed to ci August 15, 2023 20:44 — with GitHub Actions Inactive
@Ch3LL Ch3LL temporarily deployed to ci August 15, 2023 20:44 — with GitHub Actions Inactive
@Ch3LL Ch3LL temporarily deployed to ci August 15, 2023 20:44 — with GitHub Actions Inactive
@Ch3LL Ch3LL temporarily deployed to ci August 15, 2023 20:44 — with GitHub Actions Inactive
@Ch3LL Ch3LL temporarily deployed to ci August 15, 2023 20:44 — with GitHub Actions Inactive
@Ch3LL Ch3LL temporarily deployed to ci August 15, 2023 21:11 — with GitHub Actions Inactive
@Ch3LL Ch3LL temporarily deployed to ci August 15, 2023 21:11 — with GitHub Actions Inactive
@Ch3LL Ch3LL temporarily deployed to ci August 15, 2023 21:11 — with GitHub Actions Inactive
@Ch3LL Ch3LL temporarily deployed to ci August 15, 2023 21:11 — with GitHub Actions Inactive
@Ch3LL Ch3LL temporarily deployed to ci August 15, 2023 21:11 — with GitHub Actions Inactive
@Ch3LL Ch3LL temporarily deployed to ci August 15, 2023 21:11 — with GitHub Actions Inactive
@davama
Copy link

davama commented Aug 16, 2023

thank you for the PR @rittycat

@Ch3LL Ch3LL requested a review from s0undt3ch as a code owner August 23, 2023 15:50
@Ch3LL Ch3LL temporarily deployed to ci August 23, 2023 22:28 — with GitHub Actions Inactive
@Ch3LL Ch3LL temporarily deployed to ci August 23, 2023 22:28 — with GitHub Actions Inactive
@Ch3LL Ch3LL temporarily deployed to ci August 23, 2023 22:28 — with GitHub Actions Inactive
@Ch3LL Ch3LL temporarily deployed to ci August 23, 2023 22:28 — with GitHub Actions Inactive
@Ch3LL Ch3LL temporarily deployed to ci August 23, 2023 22:48 — with GitHub Actions Inactive
@Ch3LL Ch3LL temporarily deployed to ci August 23, 2023 22:51 — with GitHub Actions Inactive
Copy link
Collaborator

@s0undt3ch s0undt3ch left a 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.

Comment on lines +647 to +657
@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)


Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
@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)

Comment on lines +24 to +60
# 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
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
# 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

@Ch3LL
Copy link
Contributor

Ch3LL commented Aug 24, 2023

I cherry-picked this fix into 3006.x: #65046

I'm going to close here as this change will be merged forward to master.

@Ch3LL Ch3LL closed this Aug 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport:3006.x Backport PR to 3006.x branch
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] Inconsistent use of args in ssh_auth.managed
4 participants