Skip to content

Commit

Permalink
Allow set key_filename/key_policy in computer configure
Browse files Browse the repository at this point in the history
[pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci
  • Loading branch information
unkcpz committed Nov 15, 2023
1 parent 363845c commit 7bb157c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
31 changes: 26 additions & 5 deletions aiidalab_widgets_base/computational_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -839,7 +839,19 @@ def _configure_computer(self, computer: orm.Computer, transport: str):
raise common.ValidationError(msg)

def _configure_computer_ssh(self, computer: orm.Computer, user: orm.User):
"""Configure the computer with SSH transport"""
"""Configure the computer with SSH transport
There are three sources of authparams information:
- the SSH config file
- the computer_configure dictionary
- the default values
Priority is given to the computer_configure dictionary, then to the SSH config file, then to the default values.
At the moment, there is no overlap between the SSH config file and the computer_configure dictionary.
The proxyjump and proxycommend can be read from both the SSH config file and the computer_configure dictionary,
since the SSH config file is generated from the computer_configure dictionary in `SshComputerSetup._write_ssh_config`.
"""
sshcfg = aiida_ssh_plugin.parse_sshconfig(self.hostname.value)
authparams = {
"port": int(sshcfg.get("port", 22)),
Expand Down Expand Up @@ -871,10 +883,19 @@ def _configure_computer_ssh(self, computer: orm.Computer, user: orm.User):
message = "SSH username is not provided"
raise RuntimeError(message) from exc

if "proxycommand" in sshcfg:
authparams["proxy_command"] = sshcfg["proxycommand"]
elif "proxyjump" in sshcfg:
authparams["proxy_jump"] = sshcfg["proxyjump"]
if "proxy_jump" in self.computer_configure:
authparams["proxy_jump"] = self.computer_configure["proxy_jump"]

if "proxy_command" in self.computer_configure:
authparams["proxy_command"] = self.computer_configure["proxy_command"]

if "key_filename" in self.computer_configure:
authparams["key_filename"] = os.path.expanduser(
self.computer_configure["key_filename"]
)

if "key_policy" in self.computer_configure:
authparams["key_policy"] = self.computer_configure["key_policy"]

computer.configure(user=user, **authparams)
return True
Expand Down
7 changes: 7 additions & 0 deletions tests/test_computational_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ def test_aiida_computer_setup_widget_default():
"proxy_jump": "ela.cscs.ch",
"safe_interval": 10,
"use_login_shell": True,
"key_filename": "~/.ssh/cscs-key",
"key_policy": "AutoAddPolicy",
"username": "aiida",
}

Expand All @@ -124,6 +126,11 @@ def test_aiida_computer_setup_widget_default():
computer = orm.load_computer("daint")
assert computer.label == "daint"
assert computer.hostname == "daint.cscs.ch"
assert computer.configure().get_auth_params()["proxy_jump"] == "ela.cscs.ch"
assert computer.configure().get_auth_params()["safe_interval"] == 10
assert computer.configure().get_auth_params()["use_login_shell"] is True
assert computer.configure().get_auth_params()["key_filename"] == "~/.ssh/cscs-key"
assert computer.configure().get_auth_params()["key_policy"] == "AutoAddPolicy"

# Reset the widget and check that a few attributes are reset.
widget.computer_setup = {}
Expand Down

0 comments on commit 7bb157c

Please sign in to comment.