Skip to content

Commit ca34311

Browse files
committed
'~' actually works in IdentityFile on Windows!
Signed-off-by: Fabrice Normandin <[email protected]>
1 parent f1343cd commit ca34311

File tree

4 files changed

+59
-59
lines changed

4 files changed

+59
-59
lines changed

milatools/cli/init_command.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,6 +1130,7 @@ def _copy_if_needed(linux_key_file: Path, windows_key_file: Path):
11301130

11311131
@functools.lru_cache
11321132
def get_windows_home_path_in_wsl() -> Path:
1133+
"""Returns the path to the Windows home directory from within WSL."""
11331134
assert running_inside_WSL()
11341135
windows_username = subprocess.getoutput("powershell.exe '$env:UserName'").strip()
11351136
return Path(f"/mnt/c/Users/{windows_username}")
@@ -1438,8 +1439,8 @@ def _copy_valid_ssh_entries_to_windows_ssh_config_file(
14381439
# Tricky: need to remap the path to the Windows path.
14391440
identityfile_path = Path(identityfile).expanduser().resolve()
14401441
windows_identityfile = (
1441-
get_windows_home_path_in_wsl()
1442-
/ identityfile_path.relative_to(Path.home())
1442+
# NOTE: This actually works, even on Windows!
1443+
"~" / identityfile_path.relative_to(Path.home())
14431444
)
14441445
windows_ssh_entry["identityfile"] = str(windows_identityfile)
14451446

tests/cli/test_init_command.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ def test_setup_windows_ssh_config_from_wsl(
672672
windows_home: Path,
673673
linux_ssh_config: SSHConfig,
674674
file_regression: FileRegressionFixture,
675-
fake_linux_ssh_keypair: tuple[Path, Path], # add this fixture so the keys exist.
675+
# fake_linux_ssh_keypair: tuple[Path, Path], # add this fixture so the keys exist.
676676
accept_changes: bool,
677677
input_stream: io.StringIO,
678678
linux_home: Path,
@@ -681,6 +681,8 @@ def test_setup_windows_ssh_config_from_wsl(
681681
# home during testing.
682682
# This way, we can check that running the function will correctly change the
683683
# identityfile from linux to windows home.
684+
initial_contents = linux_ssh_config.cfg.config()
685+
684686
linux_ssh_config.path.write_text(
685687
linux_ssh_config.path.read_text()
686688
.replace("~", str(linux_home))
@@ -689,7 +691,6 @@ def test_setup_windows_ssh_config_from_wsl(
689691
)
690692
linux_ssh_config = SSHConfig(linux_ssh_config.path)
691693

692-
initial_contents = linux_ssh_config.cfg.config()
693694
linux_ssh_dir = linux_ssh_config.path.parent
694695
assert isinstance(linux_ssh_dir, PosixPath)
695696
windows_ssh_config_path = windows_home / ".ssh" / "config"
@@ -714,22 +715,21 @@ def test_setup_windows_ssh_config_from_wsl(
714715
# Unclear why paramiko returns a list here.
715716
assert isinstance(identity_file_entries, list)
716717
assert len(identity_file_entries) == 1
717-
assert Path(identity_file_entries[0]) == (windows_home / ".ssh" / "id_rsa_mila")
718+
# .lookup("mila") does the expansion of '~', so we can't compare to Path.home()
719+
# (which we mocked with the `linux_home` fixture above).
720+
# We mocked Path.home() so a `identity_file_path.relative_to(Path.home())` would
721+
# not crash during testing.
722+
assert (
723+
Path(identity_file_entries[0])
724+
== Path(os.environ["HOME"]) / ".ssh/id_rsa_mila"
725+
)
718726

719727
assert windows_ssh_config_path.exists()
720728
assert windows_ssh_config_path.stat().st_mode & 0o777 == 0o600
721729
assert windows_ssh_config_path.parent.stat().st_mode & 0o777 == 0o700
722730
if not accept_changes:
723731
assert windows_ssh_config_path.read_text() == ""
724732

725-
initial_contents = initial_contents.replace(
726-
str(windows_home), "<WINDOWS_HOME>"
727-
).replace(str(linux_home), "<WSL_HOME>")
728-
actual_contents = (
729-
windows_ssh_config_path.read_text()
730-
.replace(str(windows_home), "<WINDOWS_HOME>")
731-
.replace(str(linux_home), "<WSL_HOME>")
732-
)
733733
expected_text = "\n".join(
734734
[
735735
"When this SSH config is already present in the WSL environment with "
@@ -751,7 +751,7 @@ def test_setup_windows_ssh_config_from_wsl(
751751
"leads the following ssh config file on the Windows side:",
752752
"",
753753
"```",
754-
actual_contents,
754+
windows_ssh_config_path.read_text(),
755755
"```",
756756
]
757757
)
@@ -889,7 +889,6 @@ def test_setup_windows_ssh_config_from_wsl_copies_keys(
889889
windows_home: Path,
890890
linux_home: PosixPath,
891891
fake_linux_ssh_keypair: tuple[Path, Path],
892-
monkeypatch: pytest.MonkeyPatch,
893892
input_stream: io.StringIO,
894893
):
895894
# The references to the linux SSH keys in "home" need to be adjusted to point to the

tests/cli/test_init_command/test_setup_windows_ssh_config_from_wsl_accept_.md

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Host mila
88
ServerAliveInterval 120
99
ServerAliveCountMax 5
1010
User bob
11-
IdentityFile <WSL_HOME>/.ssh/id_rsa_mila
11+
IdentityFile ~/.ssh/id_rsa_mila
1212
1313
Host mila-cpu
1414
Port 2222
@@ -22,70 +22,70 @@ Host mila-cpu
2222
ProxyCommand ssh mila "/cvmfs/config.mila.quebec/scripts/milatools/slurm-proxy.sh mila-cpu --mem=8G"
2323
RemoteCommand /cvmfs/config.mila.quebec/scripts/milatools/entrypoint.sh mila-cpu
2424
User bob
25-
IdentityFile <WSL_HOME>/.ssh/id_rsa_mila
25+
IdentityFile ~/.ssh/id_rsa_mila
2626
2727
Host *.server.mila.quebec !*login.server.mila.quebec
2828
ProxyJump mila
2929
User bob
30-
IdentityFile <WSL_HOME>/.ssh/id_rsa_mila
30+
IdentityFile ~/.ssh/id_rsa_mila
3131
3232
Host cn-????
3333
ProxyJump mila
3434
User bob
35-
IdentityFile <WSL_HOME>/.ssh/id_rsa_mila
35+
IdentityFile ~/.ssh/id_rsa_mila
3636
3737
Host narval rorqual fir nibi trillium trillium-gpu tamia killarney vulcan
3838
HostName %h.alliancecan.ca
3939
ControlMaster auto
40-
ControlPath <WSL_HOME>/.cache/ssh/%r@%h:%p
40+
ControlPath ~/.cache/ssh/%r@%h:%p
4141
ControlPersist yes
4242
User bob
43-
IdentityFile <WSL_HOME>/.ssh/id_rsa_drac
43+
IdentityFile ~/.ssh/id_rsa_drac
4444
4545
Host nc????? ng?????
4646
ProxyJump narval
4747
User bob
48-
IdentityFile <WSL_HOME>/.ssh/id_rsa_drac
48+
IdentityFile ~/.ssh/id_rsa_drac
4949
5050
Host rc????? rg????? rl?????
5151
ProxyJump rorqual
5252
User bob
53-
IdentityFile <WSL_HOME>/.ssh/id_rsa_drac
53+
IdentityFile ~/.ssh/id_rsa_drac
5454
5555
Host fc????? fb?????
5656
ProxyJump fir
5757
User bob
58-
IdentityFile <WSL_HOME>/.ssh/id_rsa_drac
58+
IdentityFile ~/.ssh/id_rsa_drac
5959
6060
Host c? c?? c??? g? g?? l? l?? m? m?? u?
6161
ProxyJump nibi
6262
User bob
63-
IdentityFile <WSL_HOME>/.ssh/id_rsa_drac
63+
IdentityFile ~/.ssh/id_rsa_drac
6464
6565
Host tg????? tc?????
6666
ProxyJump tamia
6767
User bob
68-
IdentityFile <WSL_HOME>/.ssh/id_rsa_drac
68+
IdentityFile ~/.ssh/id_rsa_drac
6969
7070
Host kn???
7171
ProxyJump killarney
7272
User bob
73-
IdentityFile <WSL_HOME>/.ssh/id_rsa_drac
73+
IdentityFile ~/.ssh/id_rsa_drac
7474
7575
Host rack??-??
7676
ProxyJump vulcan
7777
User bob
78-
IdentityFile <WSL_HOME>/.ssh/id_rsa_drac
78+
IdentityFile ~/.ssh/id_rsa_drac
7979
8080
Host !trillium tri????
8181
ProxyJump trillium
8282
User bob
83-
IdentityFile <WSL_HOME>/.ssh/id_rsa_drac
83+
IdentityFile ~/.ssh/id_rsa_drac
8484
8585
Host !trillium trig????
8686
ProxyJump trillium-gpu
8787
User bob
88-
IdentityFile <WSL_HOME>/.ssh/id_rsa_drac
88+
IdentityFile ~/.ssh/id_rsa_drac
8989
```
9090

9191

@@ -101,7 +101,7 @@ Host mila
101101
ServerAliveInterval 120
102102
ServerAliveCountMax 5
103103
User bob
104-
IdentityFile <WINDOWS_HOME>/.ssh/id_rsa_mila
104+
IdentityFile ~/.ssh/id_rsa_mila
105105
106106
Host mila-cpu
107107
Port 2222
@@ -115,65 +115,65 @@ Host mila-cpu
115115
ProxyCommand ssh mila "/cvmfs/config.mila.quebec/scripts/milatools/slurm-proxy.sh mila-cpu --mem=8G"
116116
remotecommand /cvmfs/config.mila.quebec/scripts/milatools/entrypoint.sh mila-cpu
117117
User bob
118-
IdentityFile <WINDOWS_HOME>/.ssh/id_rsa_mila
118+
IdentityFile ~/.ssh/id_rsa_mila
119119
120120
Host *.server.mila.quebec !*login.server.mila.quebec
121121
ProxyJump mila
122122
User bob
123-
IdentityFile <WINDOWS_HOME>/.ssh/id_rsa_mila
123+
IdentityFile ~/.ssh/id_rsa_mila
124124
125125
Host cn-????
126126
ProxyJump mila
127127
User bob
128-
IdentityFile <WINDOWS_HOME>/.ssh/id_rsa_mila
128+
IdentityFile ~/.ssh/id_rsa_mila
129129
130130
Host narval rorqual fir nibi trillium trillium-gpu tamia killarney vulcan
131131
HostName %h.alliancecan.ca
132132
User bob
133-
IdentityFile <WINDOWS_HOME>/.ssh/id_rsa_drac
133+
IdentityFile ~/.ssh/id_rsa_drac
134134
135135
Host nc????? ng?????
136136
ProxyJump narval
137137
User bob
138-
IdentityFile <WINDOWS_HOME>/.ssh/id_rsa_drac
138+
IdentityFile ~/.ssh/id_rsa_drac
139139
140140
Host rc????? rg????? rl?????
141141
ProxyJump rorqual
142142
User bob
143-
IdentityFile <WINDOWS_HOME>/.ssh/id_rsa_drac
143+
IdentityFile ~/.ssh/id_rsa_drac
144144
145145
Host fc????? fb?????
146146
ProxyJump fir
147147
User bob
148-
IdentityFile <WINDOWS_HOME>/.ssh/id_rsa_drac
148+
IdentityFile ~/.ssh/id_rsa_drac
149149
150150
Host c? c?? c??? g? g?? l? l?? m? m?? u?
151151
ProxyJump nibi
152152
User bob
153-
IdentityFile <WINDOWS_HOME>/.ssh/id_rsa_drac
153+
IdentityFile ~/.ssh/id_rsa_drac
154154
155155
Host tg????? tc?????
156156
ProxyJump tamia
157157
User bob
158-
IdentityFile <WINDOWS_HOME>/.ssh/id_rsa_drac
158+
IdentityFile ~/.ssh/id_rsa_drac
159159
160160
Host kn???
161161
ProxyJump killarney
162162
User bob
163-
IdentityFile <WINDOWS_HOME>/.ssh/id_rsa_drac
163+
IdentityFile ~/.ssh/id_rsa_drac
164164
165165
Host rack??-??
166166
ProxyJump vulcan
167167
User bob
168-
IdentityFile <WINDOWS_HOME>/.ssh/id_rsa_drac
168+
IdentityFile ~/.ssh/id_rsa_drac
169169
170170
Host !trillium tri????
171171
ProxyJump trillium
172172
User bob
173-
IdentityFile <WINDOWS_HOME>/.ssh/id_rsa_drac
173+
IdentityFile ~/.ssh/id_rsa_drac
174174
175175
Host !trillium trig????
176176
ProxyJump trillium-gpu
177177
User bob
178-
IdentityFile <WINDOWS_HOME>/.ssh/id_rsa_drac
178+
IdentityFile ~/.ssh/id_rsa_drac
179179
```

tests/cli/test_init_command/test_setup_windows_ssh_config_from_wsl_reject_.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Host mila
88
ServerAliveInterval 120
99
ServerAliveCountMax 5
1010
User bob
11-
IdentityFile <WSL_HOME>/.ssh/id_rsa_mila
11+
IdentityFile ~/.ssh/id_rsa_mila
1212
1313
Host mila-cpu
1414
Port 2222
@@ -22,70 +22,70 @@ Host mila-cpu
2222
ProxyCommand ssh mila "/cvmfs/config.mila.quebec/scripts/milatools/slurm-proxy.sh mila-cpu --mem=8G"
2323
RemoteCommand /cvmfs/config.mila.quebec/scripts/milatools/entrypoint.sh mila-cpu
2424
User bob
25-
IdentityFile <WSL_HOME>/.ssh/id_rsa_mila
25+
IdentityFile ~/.ssh/id_rsa_mila
2626
2727
Host *.server.mila.quebec !*login.server.mila.quebec
2828
ProxyJump mila
2929
User bob
30-
IdentityFile <WSL_HOME>/.ssh/id_rsa_mila
30+
IdentityFile ~/.ssh/id_rsa_mila
3131
3232
Host cn-????
3333
ProxyJump mila
3434
User bob
35-
IdentityFile <WSL_HOME>/.ssh/id_rsa_mila
35+
IdentityFile ~/.ssh/id_rsa_mila
3636
3737
Host narval rorqual fir nibi trillium trillium-gpu tamia killarney vulcan
3838
HostName %h.alliancecan.ca
3939
ControlMaster auto
40-
ControlPath <WSL_HOME>/.cache/ssh/%r@%h:%p
40+
ControlPath ~/.cache/ssh/%r@%h:%p
4141
ControlPersist yes
4242
User bob
43-
IdentityFile <WSL_HOME>/.ssh/id_rsa_drac
43+
IdentityFile ~/.ssh/id_rsa_drac
4444
4545
Host nc????? ng?????
4646
ProxyJump narval
4747
User bob
48-
IdentityFile <WSL_HOME>/.ssh/id_rsa_drac
48+
IdentityFile ~/.ssh/id_rsa_drac
4949
5050
Host rc????? rg????? rl?????
5151
ProxyJump rorqual
5252
User bob
53-
IdentityFile <WSL_HOME>/.ssh/id_rsa_drac
53+
IdentityFile ~/.ssh/id_rsa_drac
5454
5555
Host fc????? fb?????
5656
ProxyJump fir
5757
User bob
58-
IdentityFile <WSL_HOME>/.ssh/id_rsa_drac
58+
IdentityFile ~/.ssh/id_rsa_drac
5959
6060
Host c? c?? c??? g? g?? l? l?? m? m?? u?
6161
ProxyJump nibi
6262
User bob
63-
IdentityFile <WSL_HOME>/.ssh/id_rsa_drac
63+
IdentityFile ~/.ssh/id_rsa_drac
6464
6565
Host tg????? tc?????
6666
ProxyJump tamia
6767
User bob
68-
IdentityFile <WSL_HOME>/.ssh/id_rsa_drac
68+
IdentityFile ~/.ssh/id_rsa_drac
6969
7070
Host kn???
7171
ProxyJump killarney
7272
User bob
73-
IdentityFile <WSL_HOME>/.ssh/id_rsa_drac
73+
IdentityFile ~/.ssh/id_rsa_drac
7474
7575
Host rack??-??
7676
ProxyJump vulcan
7777
User bob
78-
IdentityFile <WSL_HOME>/.ssh/id_rsa_drac
78+
IdentityFile ~/.ssh/id_rsa_drac
7979
8080
Host !trillium tri????
8181
ProxyJump trillium
8282
User bob
83-
IdentityFile <WSL_HOME>/.ssh/id_rsa_drac
83+
IdentityFile ~/.ssh/id_rsa_drac
8484
8585
Host !trillium trig????
8686
ProxyJump trillium-gpu
8787
User bob
88-
IdentityFile <WSL_HOME>/.ssh/id_rsa_drac
88+
IdentityFile ~/.ssh/id_rsa_drac
8989
```
9090

9191

0 commit comments

Comments
 (0)