Skip to content

Commit

Permalink
Merge pull request #5709 from ScottWales/ssh-forward-vars
Browse files Browse the repository at this point in the history
Forward arbitrary environment variables over SSH
  • Loading branch information
hjoliver authored Oct 29, 2023
2 parents f768aad + ac071c6 commit abfe30d
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 3 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ requests_).
- Prasanna Challuri
- David Matthews
- Tim Whitcomb
- (Scott Wales)
- Scott Wales
- Tomek Trzeciak
- Thomas Coleman
- Bruno Kinoshita
Expand Down
1 change: 1 addition & 0 deletions changes.d/5709.feat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Forward arbitrary environment variables over SSH connections
8 changes: 8 additions & 0 deletions cylc/flow/cfgspec/globalcfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -1652,6 +1652,14 @@ def default_for(
.. versionadded:: 8.0.0
''')
Conf('ssh forward environment variables', VDR.V_STRING_LIST, '',
desc='''
A list containing the names of the environment variables to
forward with SSH connections to the workflow host from
the host running 'cylc play'
.. versionadded:: 8.3.0
''')
with Conf('selection', desc='''
How to select a host from the list of platform hosts.
Expand Down
3 changes: 2 additions & 1 deletion cylc/flow/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,8 @@ def construct_ssh_cmd(
'CYLC_CONF_PATH',
'CYLC_COVERAGE',
'CLIENT_COMMS_METH',
'CYLC_ENV_NAME'
'CYLC_ENV_NAME',
*platform['ssh forward environment variables'],
]:
if envvar in os.environ:
command.append(
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/cfgspec/test_globalcfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,13 @@ def test_source_dir_validation(
assert "must be an absolute path" in str(excinfo.value)
else:
glblcfg.load()

def test_platform_ssh_forward_variables(mock_global_config):

glblcfg: GlobalConfig = mock_global_config('''
[platforms]
[[foo]]
ssh forward environment variables = "FOO", "BAR"
''')

assert glblcfg.get(['platforms','foo','ssh forward environment variables']) == ["FOO", "BAR"]
29 changes: 28 additions & 1 deletion tests/unit/test_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""Test the cylc.flow.remote module."""

from cylc.flow.remote import run_cmd, construct_rsync_over_ssh_cmd
from cylc.flow.remote import run_cmd, construct_rsync_over_ssh_cmd, construct_ssh_cmd
from unittest import mock
import cylc.flow


def test_run_cmd_stdin_str():
Expand Down Expand Up @@ -86,3 +88,28 @@ def test_construct_rsync_over_ssh_cmd():
'/foo/',
'miklegard:/bar/',
]


def test_construct_ssh_cmd_forward_env():
""" Test for 'ssh forward environment variables'
"""
import os

host = 'example.com'
config = {
'ssh command': 'ssh',
'use login shell': None,
'cylc path': None,
'ssh forward environment variables': ['FOO', 'BAZ'],
}

# Variable isn't set, no change to command
expect = ['ssh', host, 'env', f'CYLC_VERSION={cylc.flow.__version__}', 'cylc', 'play']
cmd = construct_ssh_cmd(['play'], config, host)
assert cmd == expect

# Variable is set, appears in `env` list
with mock.patch.dict(os.environ, {'FOO': 'BAR'}):
expect = ['ssh', host, 'env', f'CYLC_VERSION={cylc.flow.__version__}', 'FOO=BAR', 'cylc', 'play']
cmd = construct_ssh_cmd(['play'], config, host)
assert cmd == expect

0 comments on commit abfe30d

Please sign in to comment.