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

Forward arbitrary environment variables over SSH #5709

Merged
merged 7 commits into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Member

Choose a reason for hiding this comment

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

🎉

- 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 @@ -1632,6 +1632,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
Loading