Skip to content

Commit

Permalink
Preserve master credentials on spawning platforms
Browse files Browse the repository at this point in the history
Prevent spawning platform minions from having to re-authenticate on
every job when using multiprocessing=True
  • Loading branch information
dwoz committed Aug 9, 2023
1 parent a46d846 commit 5d3896b
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 3 deletions.
2 changes: 2 additions & 0 deletions changelog/64914.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Preserve credentials on spawning platforms, minions no longer re-authenticate
with every job when using `multiprocessing=True`.
10 changes: 7 additions & 3 deletions salt/minion.py
Original file line number Diff line number Diff line change
Expand Up @@ -1763,24 +1763,26 @@ def _handle_decoded_payload(self, data):
# python needs to be able to reconstruct the reference on the other
# side.
instance = self
creds_map = None
multiprocessing_enabled = self.opts.get("multiprocessing", True)
name = "ProcessPayload(jid={})".format(data["jid"])
if multiprocessing_enabled:
if salt.utils.platform.spawning_platform():
# let python reconstruct the minion on the other side if we're
# running on windows
instance = None
creds_map = salt.crypt.AsyncAuth.creds_map
with default_signals(signal.SIGINT, signal.SIGTERM):
process = SignalHandlingProcess(
target=self._target,
name=name,
args=(instance, self.opts, data, self.connected),
args=(instance, self.opts, data, self.connected, creds_map),
)
process.register_after_fork_method(salt.utils.crypt.reinit_crypto)
else:
process = threading.Thread(
target=self._target,
args=(instance, self.opts, data, self.connected),
args=(instance, self.opts, data, self.connected, creds_map),
name=name,
)

Expand All @@ -1804,7 +1806,9 @@ def ctx(self):
return exitstack

@classmethod
def _target(cls, minion_instance, opts, data, connected):
def _target(cls, minion_instance, opts, data, connected, creds_map):
if creds_map:
salt.crypt.AsyncAuth.creds_map = creds_map
if not minion_instance:
minion_instance = cls(opts, load_grains=False)
minion_instance.connected = connected
Expand Down
51 changes: 51 additions & 0 deletions tests/pytests/integration/minion/test_reauth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import time
import logging

import pytest

def test_reauth(salt_master_factory, event_listener):
"""
Validate non of our platform need to re-authenticate when runing a job with
multiprocessing=True.
"""
sls_name = "issue-50221"
sls_contents = """
custom_test_state:
test.configurable_test_state:
- name: example
- changes: True
- result: True
- comment: "Nothing has acutally been changed"
"""
events = []
def handler(data):
events.append(data)
event_listener.register_auth_event_handler("test_reauth-master", handler)
master = salt_master_factory.salt_master_daemon(
"test_reauth-master",
overrides={"log_level": "info"},
)
sls_tempfile = master.state_tree.base.temp_file(
"{}.sls".format(sls_name), sls_contents
)
minion = master.salt_minion_daemon(
"test_reauth-minion",
overrides={"log_level": "info"},
)
cli = master.salt_cli()
start_time = time.time()
with master.started(), minion.started():
events = event_listener.get_events(
[(master.id, "salt/auth")],
after_time=start_time,
)
assert len(events) == 2
proc = cli.run("state.sls", sls_name, minion_tgt="*")
assert proc.returncode == 1
events = event_listener.get_events(
[(master.id, "salt/auth")],
after_time=start_time,
)
assert len(events) == 2


0 comments on commit 5d3896b

Please sign in to comment.