-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathheadscale.py
437 lines (400 loc) · 16.2 KB
/
headscale.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
# pylint: disable=wrong-import-order
import requests, json, os, logging, yaml
from cryptography.fernet import Fernet
from datetime import timedelta, date
from dateutil import parser
from flask import Flask
from dotenv import load_dotenv
load_dotenv()
LOG_LEVEL = os.environ["LOG_LEVEL"].replace('"', '').upper()
DATA_DIRECTORY = os.environ["DATA_DIRECTORY"].replace('"', '') if os.environ["DATA_DIRECTORY"] else "/data"
# Initiate the Flask application and logging:
app = Flask(__name__, static_url_path="/static")
match LOG_LEVEL:
case "DEBUG" : app.logger.setLevel(logging.DEBUG)
case "INFO" : app.logger.setLevel(logging.INFO)
case "WARNING" : app.logger.setLevel(logging.WARNING)
case "ERROR" : app.logger.setLevel(logging.ERROR)
case "CRITICAL": app.logger.setLevel(logging.CRITICAL)
##################################################################
# Functions related to HEADSCALE and API KEYS
##################################################################
def get_url(inpage=False):
if not inpage:
return os.environ['HS_SERVER']
config_file = ""
try:
config_file = open("/etc/headscale/config.yml", "r")
app.logger.info("Opening /etc/headscale/config.yml")
except:
config_file = open("/etc/headscale/config.yaml", "r")
app.logger.info("Opening /etc/headscale/config.yaml")
config_yaml = yaml.safe_load(config_file)
if "server_url" in config_yaml:
return str(config_yaml["server_url"])
app.logger.warning("Failed to find server_url in the config. Falling back to ENV variable")
return os.environ['HS_SERVER']
def set_api_key(api_key):
# User-set encryption key
encryption_key = os.environ['KEY']
# Key file on the filesystem for persistent storage
key_file = open(os.path.join(DATA_DIRECTORY, "key.txt"), "wb+")
# Preparing the Fernet class with the key
fernet = Fernet(encryption_key)
# Encrypting the key
encrypted_key = fernet.encrypt(api_key.encode())
# Return true if the file wrote correctly
return True if key_file.write(encrypted_key) else False
def get_api_key():
if not os.path.exists(os.path.join(DATA_DIRECTORY, "key.txt")): return False
# User-set encryption key
encryption_key = os.environ['KEY']
# Key file on the filesystem for persistent storage
key_file = open(os.path.join(DATA_DIRECTORY, "key.txt"), "rb+")
# The encrypted key read from the file
enc_api_key = key_file.read()
if enc_api_key == b'': return "NULL"
# Preparing the Fernet class with the key
fernet = Fernet(encryption_key)
# Decrypting the key
decrypted_key = fernet.decrypt(enc_api_key).decode()
return decrypted_key
def test_api_key(url, api_key):
response = requests.get(
str(url)+"/api/v1/apikey",
headers={
'Accept': 'application/json',
'Authorization': 'Bearer '+str(api_key)
}
)
return response.status_code
# Expires an API key
def expire_key(url, api_key):
payload = {'prefix':str(api_key[0:10])}
json_payload=json.dumps(payload)
app.logger.debug("Sending the payload '"+str(json_payload)+"' to the headscale server")
response = requests.post(
str(url)+"/api/v1/apikey/expire",
data=json_payload,
headers={
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': 'Bearer '+str(api_key)
}
)
return response.status_code
# Checks if the key needs to be renewed
# If it does, renews the key, then expires the old key
def renew_api_key(url, api_key):
# 0 = Key has been updated or key is not in need of an update
# 1 = Key has failed validity check or has failed to write the API key
# Check when the key expires and compare it to todays date:
key_info = get_api_key_info(url, api_key)
expiration_time = key_info["expiration"]
today_date = date.today()
expire = parser.parse(expiration_time)
expire_fmt = str(expire.year) + "-" + str(expire.month).zfill(2) + "-" + str(expire.day).zfill(2)
expire_date = date.fromisoformat(expire_fmt)
delta = expire_date - today_date
tmp = today_date + timedelta(days=90)
new_expiration_date = str(tmp)+"T00:00:00.000000Z"
# If the delta is less than 5 days, renew the key:
if delta < timedelta(days=5):
app.logger.warning("Key is about to expire. Delta is "+str(delta))
payload = {'expiration':str(new_expiration_date)}
json_payload=json.dumps(payload)
app.logger.debug("Sending the payload '"+str(json_payload)+"' to the headscale server")
response = requests.post(
str(url)+"/api/v1/apikey",
data=json_payload,
headers={
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': 'Bearer '+str(api_key)
}
)
new_key = response.json()
app.logger.debug("JSON: "+json.dumps(new_key))
app.logger.debug("New Key is: "+new_key["apiKey"])
api_key_test = test_api_key(url, new_key["apiKey"])
app.logger.debug("Testing the key: "+str(api_key_test))
# Test if the new key works:
if api_key_test == 200:
app.logger.info("The new key is valid and we are writing it to the file")
if not set_api_key(new_key["apiKey"]):
app.logger.error("We failed writing the new key!")
return False # Key write failed
app.logger.info("Key validated and written. Moving to expire the key.")
expire_key(url, api_key)
return True # Key updated and validated
else:
app.logger.error("Testing the API key failed.")
return False # The API Key test failed
else: return True # No work is required
# Gets information about the current API key
def get_api_key_info(url, api_key):
app.logger.info("Getting API key information")
response = requests.get(
str(url)+"/api/v1/apikey",
headers={
'Accept': 'application/json',
'Authorization': 'Bearer '+str(api_key)
}
)
json_response = response.json()
# Find the current key in the array:
key_prefix = str(api_key[0:10])
app.logger.info("Looking for valid API Key...")
for key in json_response["apiKeys"]:
if key_prefix == key["prefix"]:
app.logger.info("Key found.")
return key
app.logger.error("Could not find a valid key in Headscale. Need a new API key.")
return "Key not found"
##################################################################
# Functions related to MACHINES
##################################################################
# register a new machine
def register_machine(url, api_key, machine_key, user):
app.logger.info("Registering machine %s to user %s", str(machine_key), str(user))
response = requests.post(
str(url)+"/api/v1/machine/register?user="+str(user)+"&key="+str(machine_key),
headers={
'Accept': 'application/json',
'Authorization': 'Bearer '+str(api_key)
}
)
return response.json()
# Sets the machines tags
def set_machine_tags(url, api_key, machine_id, tags_list):
app.logger.info("Setting machine_id %s tag %s", str(machine_id), str(tags_list))
response = requests.post(
str(url)+"/api/v1/machine/"+str(machine_id)+"/tags",
data=tags_list,
headers={
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': 'Bearer '+str(api_key)
}
)
return response.json()
# Moves machine_id to user "new_user"
def move_user(url, api_key, machine_id, new_user):
app.logger.info("Moving machine_id %s to user %s", str(machine_id), str(new_user))
response = requests.post(
str(url)+"/api/v1/machine/"+str(machine_id)+"/user?user="+str(new_user),
headers={
'Accept': 'application/json',
'Authorization': 'Bearer '+str(api_key)
}
)
return response.json()
def update_route(url, api_key, route_id, current_state):
action = "disable" if current_state == "True" else "enable"
app.logger.info("Updating Route %s: Action: %s", str(route_id), str(action))
# Debug
app.logger.debug("URL: "+str(url))
app.logger.debug("Route ID: "+str(route_id))
app.logger.debug("Current State: "+str(current_state))
app.logger.debug("Action to take: "+str(action))
response = requests.post(
str(url)+"/api/v1/routes/"+str(route_id)+"/"+str(action),
headers={
'Accept': 'application/json',
'Authorization': 'Bearer '+str(api_key)
}
)
return response.json()
# Get all machines on the Headscale network
def get_machines(url, api_key):
app.logger.info("Getting machine information")
response = requests.get(
str(url)+"/api/v1/machine",
headers={
'Accept': 'application/json',
'Authorization': 'Bearer '+str(api_key)
}
)
return response.json()
# Get machine with "machine_id" on the Headscale network
def get_machine_info(url, api_key, machine_id):
app.logger.info("Getting information for machine ID %s", str(machine_id))
response = requests.get(
str(url)+"/api/v1/machine/"+str(machine_id),
headers={
'Accept': 'application/json',
'Authorization': 'Bearer '+str(api_key)
}
)
return response.json()
# Delete a machine from Headscale
def delete_machine(url, api_key, machine_id):
app.logger.info("Deleting machine %s", str(machine_id))
response = requests.delete(
str(url)+"/api/v1/machine/"+str(machine_id),
headers={
'Accept': 'application/json',
'Authorization': 'Bearer '+str(api_key)
}
)
status = "True" if response.status_code == 200 else "False"
if response.status_code == 200:
app.logger.info("Machine deleted.")
else:
app.logger.error("Deleting machine failed! %s", str(response.json()))
return {"status": status, "body": response.json()}
# Rename "machine_id" with name "new_name"
def rename_machine(url, api_key, machine_id, new_name):
app.logger.info("Renaming machine %s", str(machine_id))
response = requests.post(
str(url)+"/api/v1/machine/"+str(machine_id)+"/rename/"+str(new_name),
headers={
'Accept': 'application/json',
'Authorization': 'Bearer '+str(api_key)
}
)
status = "True" if response.status_code == 200 else "False"
if response.status_code == 200:
app.logger.info("Machine renamed")
else:
app.logger.error("Machine rename failed! %s", str(response.json()))
return {"status": status, "body": response.json()}
# Gets routes for the passed machine_id
def get_machine_routes(url, api_key, machine_id):
app.logger.info("Getting routes for machine %s", str(machine_id))
response = requests.get(
str(url)+"/api/v1/machine/"+str(machine_id)+"/routes",
headers={
'Accept': 'application/json',
'Authorization': 'Bearer '+str(api_key)
}
)
if response.status_code == 200:
app.logger.info("Routes obtained")
else:
app.logger.error("Failed to get routes: %s", str(response.json()))
return response.json()
# Gets routes for the entire tailnet
def get_routes(url, api_key):
app.logger.info("Getting routes")
response = requests.get(
str(url)+"/api/v1/routes",
headers={
'Accept': 'application/json',
'Authorization': 'Bearer '+str(api_key)
}
)
return response.json()
##################################################################
# Functions related to USERS
##################################################################
# Get all users in use
def get_users(url, api_key):
app.logger.info("Getting Users")
response = requests.get(
str(url)+"/api/v1/user",
headers={
'Accept': 'application/json',
'Authorization': 'Bearer '+str(api_key)
}
)
return response.json()
# Rename "old_name" with name "new_name"
def rename_user(url, api_key, old_name, new_name):
app.logger.info("Renaming user %s to %s.", str(old_name), str(new_name))
response = requests.post(
str(url)+"/api/v1/user/"+str(old_name)+"/rename/"+str(new_name),
headers={
'Accept': 'application/json',
'Authorization': 'Bearer '+str(api_key)
}
)
status = "True" if response.status_code == 200 else "False"
if response.status_code == 200:
app.logger.info("User renamed.")
else:
app.logger.error("Renaming User failed!")
return {"status": status, "body": response.json()}
# Delete a user from Headscale
def delete_user(url, api_key, user_name):
app.logger.info("Deleting a User: %s", str(user_name))
response = requests.delete(
str(url)+"/api/v1/user/"+str(user_name),
headers={
'Accept': 'application/json',
'Authorization': 'Bearer '+str(api_key)
}
)
status = "True" if response.status_code == 200 else "False"
if response.status_code == 200:
app.logger.info("User deleted.")
else:
app.logger.error("Deleting User failed!")
return {"status": status, "body": response.json()}
# Add a user from Headscale
def add_user(url, api_key, data):
app.logger.info("Adding user: %s", str(data))
response = requests.post(
str(url)+"/api/v1/user",
data=data,
headers={
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': 'Bearer '+str(api_key)
}
)
status = "True" if response.status_code == 200 else "False"
if response.status_code == 200:
app.logger.info("User added.")
else:
app.logger.error("Adding User failed!")
return {"status": status, "body": response.json()}
##################################################################
# Functions related to PREAUTH KEYS in USERS
##################################################################
# Get all PreAuth keys associated with a user "user_name"
def get_preauth_keys(url, api_key, user_name):
app.logger.info("Getting PreAuth Keys in User %s", str(user_name))
response = requests.get(
str(url)+"/api/v1/preauthkey?user="+str(user_name),
headers={
'Accept': 'application/json',
'Authorization': 'Bearer '+str(api_key)
}
)
return response.json()
# Add a preauth key to the user "user_name" given the booleans "ephemeral"
# and "reusable" with the expiration date "date" contained in the JSON payload "data"
def add_preauth_key(url, api_key, data):
app.logger.info("Adding PreAuth Key: %s", str(data))
response = requests.post(
str(url)+"/api/v1/preauthkey",
data=data,
headers={
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': 'Bearer '+str(api_key)
}
)
status = "True" if response.status_code == 200 else "False"
if response.status_code == 200:
app.logger.info("PreAuth Key added.")
else:
app.logger.error("Adding PreAuth Key failed!")
return {"status": status, "body": response.json()}
# Expire a pre-auth key. data is {"user": "string", "key": "string"}
def expire_preauth_key(url, api_key, data):
app.logger.info("Expiring PreAuth Key...")
response = requests.post(
str(url)+"/api/v1/preauthkey/expire",
data=data,
headers={
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': 'Bearer '+str(api_key)
}
)
status = "True" if response.status_code == 200 else "False"
app.logger.debug("expire_preauth_key - Return: "+str(response.json()))
app.logger.debug("expire_preauth_key - Status: "+str(status))
return {"status": status, "body": response.json()}