-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.py
269 lines (224 loc) · 8.6 KB
/
start.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import asyncio
import json
import os
import re
import subprocess
from datetime import datetime, timedelta
from pathlib import Path
import humanize
import ipywidgets as ipw
import requests
__version__ = "v2023.1003"
class UnableToFetchKeyError(Exception):
"""Exception raised when unable to fetch the key."""
def __init__(self, key_type):
super().__init__(f"Unable to fetch {key_type} key.")
class InputNotProvidedError(Exception):
"""Exception raised when a certain input is not provided."""
def __init__(self, input_type):
super().__init__(f"{input_type} is not provided.")
class HeaderWarning(ipw.HTML):
"""Class to display a warning in the header."""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.layout = ipw.Layout(
display="none",
width="600px",
height="auto",
margin="0px 0px 0px 0px",
padding="0px 0px 0px 0px",
)
def show(self, message, danger_level="alert-danger"):
"""Show the warning."""
self.value = (
f"""<div class="alert {danger_level}" role="alert">{message}</div>"""
)
self.layout.display = "block"
def hide(self):
"""Hide the warning."""
self.layout.display = "none"
class MfaAuthenicathionWidget(ipw.VBox):
"""MFA Authentication Widget."""
api_get_keys = "https://sshservice.cscs.ch/api/v1/auth/ssh-keys/signed-key"
key_warning_threshold = 8 # hours
def __init__(self):
self.username = ipw.Text(description="Username:", disabled=False)
self.password = ipw.Password(description="Password:", disabled=False)
self.otp = ipw.Text(description="OTP:", disabled=False)
self.private_key_file = Path.home() / ".ssh" / "cscs-key"
self.public_key_file = Path.home() / ".ssh" / "cscs-key-cert.pub"
setup_button = ipw.Button(description="Update the key")
setup_button.on_click(self.setup)
self.key_validity_info = HeaderWarning()
self.output = ipw.HTML()
super().__init__(
children=[
self.key_validity_info,
ipw.HBox(
[
self.username,
self.password,
self.otp,
]
),
setup_button,
self.output,
]
)
asyncio.ensure_future(self._start_periodic_refresh(5))
self.refresh_info()
def setup(self, _=None):
self.output.value = "Trying to get the keys..."
try:
keys = self.get_keys()
except InputNotProvidedError as exc:
self.output.value = (
f"""<div class="alert alert-danger" role="alert">{str(exc)}</div>"""
)
return
self.store_the_keys(*keys)
# Add the key to the ssh-agent.
self.add_key_to_ssh_agent()
# ssh-keyscan to add proxy server to known hosts.
self.add_proxy_server_to_known_hosts()
self.output.value = "The keys were updated 👍"
def add_key_to_ssh_agent(self):
"""Add the key to the ssh-agent."""
subprocess.run(
["ssh-add", "-t", "1d", str(self.private_key_file)],
encoding="utf-8",
check=True,
)
@staticmethod
def add_proxy_server_to_known_hosts():
"""Run ssh-keyscan to the ela proxy server to `~/.ssh/known_hosts`"""
output = subprocess.run(
["ssh-keyscan", "ela.cscs.ch"],
encoding="utf-8",
check=True,
capture_output=True,
text=True,
)
# create the known_hosts file if it does not exist
known_hosts_file = Path.home() / ".ssh" / "known_hosts"
if not known_hosts_file.exists():
known_hosts_file.touch()
with open(Path.home() / ".ssh" / "known_hosts", "r+") as fh:
# Check if the key is already in the file, if not, add it.
if output.stdout not in fh.read():
fh.write(output.stdout)
def get_keys(self):
headers = {"Content-Type": "application/json", "Accept": "application/json"}
if not self.username.value:
raise InputNotProvidedError("Username")
if not self.password.value:
raise InputNotProvidedError("Password")
if not self.otp.value:
raise InputNotProvidedError("OTP")
auth_data = {
"username": self.username.value,
"password": self.password.value,
"otp": self.otp.value,
}
try:
resp = requests.post(
self.api_get_keys,
data=json.dumps(auth_data),
headers=headers,
verify=True,
)
resp.raise_for_status()
except requests.exceptions.RequestException as e:
try:
d_payload = e.response.json()
except Exception as exc:
raise SystemExit(e) from exc
if "payload" in d_payload and "message" in d_payload["payload"]:
print("Error: " + d_payload["payload"]["message"])
raise SystemExit(e) from e
else:
public_key = resp.json()["public"]
if not public_key:
raise UnableToFetchKeyError(key_type="public")
private_key = resp.json()["private"]
if not private_key:
raise UnableToFetchKeyError(key_type="private")
return public_key, private_key
def store_the_keys(self, public_key, private_key):
"""Store the keys in the correct location."""
ssh_dir = Path.home() / ".ssh"
if not ssh_dir.exists():
ssh_dir.mkdir()
# Write private key to file.
with open(self.private_key_file, "w") as f:
f.write(private_key)
os.chmod(self.private_key_file, 0o600)
# Write public key to file and apply correct permissions.
with open(self.public_key_file, "w") as f:
f.write(public_key)
os.chmod(self.public_key_file, 0o644)
def _parse_validity_time(self):
"""Parse the validity time from the output."""
output = subprocess.run(
["ssh-keygen", "-L", "-f", self.public_key_file],
encoding="utf-8",
capture_output=True,
).stdout
matched_line = (
re.search(r"^.*{}.*$".format("Valid:"), output, flags=re.MULTILINE)
.group(0)
.split()
)
start = datetime.fromisoformat(matched_line[2])
end = datetime.fromisoformat(matched_line[4])
return start, end
def key_is_expiring_soon(self):
"""Check if the key is expiring soon."""
if self.time_left() < timedelta(hours=self.key_warning_threshold):
return True
else:
return False
def key_is_valid(self):
"""Check if the key is valid."""
start, end = self._parse_validity_time()
if start < datetime.now() < end:
return True
else:
return False
def key_exists(self):
"""Check if the key exists."""
return self.public_key_file.exists() and self.private_key_file.exists()
def time_left(self):
"""Check if the key exists."""
_, end = self._parse_validity_time()
return end - datetime.now()
async def _start_periodic_refresh(self, period=2):
"""Start the periodic refresh."""
while True:
self.refresh_info()
await asyncio.sleep(period)
def refresh_info(self):
"""Refresh the info about the key."""
if not self.key_exists():
self.key_validity_info.show(
"🚫 The SSH key was not found.", danger_level="alert-danger"
)
return
if self.key_is_valid():
time_left = humanize.naturaldelta(self.time_left())
if self.key_is_expiring_soon():
self.key_validity_info.show(
f"⚠️ The SSH key is about to expire! Only {time_left} are remaining until it expires.",
danger_level="alert-warning",
)
else:
self.key_validity_info.show(
f"👍 The SSH key is currently valid and will expire in {time_left}.",
danger_level="alert-success",
)
else:
self.key_validity_info.show(
"🛑 The SSH key is expired.", danger_level="alert-danger"
)
def get_start_widget(appbase, jupbase, notebase):
return MfaAuthenicathionWidget()