-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinit.py
More file actions
458 lines (339 loc) · 14.1 KB
/
init.py
File metadata and controls
458 lines (339 loc) · 14.1 KB
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
from InquirerPy import prompt
from prompt_toolkit.validation import Validator, ValidationError
import re
import sys
import subprocess
from subprocess import PIPE
import json
import os
import signal
from os.path import exists
print("################")
print("Ready to login to EKS?")
print("Insert all the required information and wait for the connection...")
print("################")
class AccessKeyIDValidator(Validator):
def contains_symbols(self, value):
regex = re.compile('((?:ASIA|AKIA|AROA|AIDA)([A-Z0-7]{16}))')
is_match = regex.match(value) is not None
return is_match
def validate(self, document):
try:
check = document.text
if not self.contains_symbols(check):
raise ValueError
except ValueError:
raise ValidationError(
message="Please enter a valid Access Key ID. Only upper case alphanumeric characters.",
cursor_position=len(document.text))
class SecretAccessKeyValidator(Validator):
def contains_symbols(self, value):
regex = re.compile('([a-zA-Z0-9+/]{40})')
is_match = regex.match(value) is not None
return is_match
def validate(self, document):
try:
check = document.text.lower()
if not self.contains_symbols(check):
raise ValueError
except ValueError:
raise ValidationError(
message="Please enter a valid Secret Access Key. A string of 40 digits is expected; only upper case alphanumeric characters, and '+' or '/' symbols are admitted.",
cursor_position=len(document.text))
class StringValidator(Validator):
def contains_symbols(self, value):
regex = re.compile('^[A-Za-z0-9-_]+$')
is_match = regex.match(value) is not None
return is_match
def validate(self, document):
try:
check = document.text.lower()
if not self.contains_symbols(check):
raise ValueError
except ValueError:
raise ValidationError(
message="Please enter a valid string, with no trailing spaces.",
cursor_position=len(document.text))
class AccountIDValidator(Validator):
def contains_symbols(self, value):
regex = re.compile('^\d{12}$')
is_match = regex.match(value) is not None
return is_match
def validate(self, document):
try:
check = document.text.lower()
if not self.contains_symbols(check):
raise ValueError
except ValueError:
raise ValidationError(
message="Please enter a valid Account ID. Only 12 digits are admitted.",
cursor_position=len(document.text))
def runcmd_call(cmd):
try:
p = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
# p = subprocess.call(cmd)
except subprocess.CalledProcessError as suberr:
p = suberr.returncode
except Exception as e:
print(e)
return p
questions = [
{
"type": "input",
"message": "Access Key ID:",
"name": "access_key_id",
"validate": AccessKeyIDValidator()
},
{
"type": "input",
"message": "Secret Access Key:",
"name": "secret_access_key",
"validate": SecretAccessKeyValidator()
},
{
"type": "list",
"message": "Region:",
"choices": ["eu-central-1", "eu-west-1", "eu-west-2", "eu-south-1", "eu-west-3", "eu-south-2", "eu-north-1",
"eu-central-2"],
"name": "region"
},
{
"type": "input",
"message": "Cluster name:",
"name": "cluster_name",
"validate": StringValidator()
},
{
"type": "input",
"message": "Account ID:",
"name": "account_id",
"validate": AccountIDValidator()
},
{
"type": "input",
"message": "Role name:",
"name": "role_name",
"validate": StringValidator()
},
{
"type": "input",
"message": "Username:",
"name": "username"
},
{
"type": "input",
"message": "Service account to connect to the dashboard:",
"name": "service_account"
},
{
"type": "confirm", "message": "Confirm?",
"name": "confirmation"
},
]
use_config = [
{
"type": "confirm", "message": "File configold.json with some configuration found. Use this file to proceed?",
"name": "confirmation"
},
]
def aws_configure(result):
cmd = ['aws', 'configure', "set", "region", result["region"], "--profile", "eks-automation-py"]
print('\n::: Set Region: ' + ' '.join(cmd) + ' ::: \n')
res = runcmd_call(cmd)
if type(res) == int:
print("Return code:", res)
print(
"Error during the execution. Run the command manually or check the documentation to get more details: http://bit.ly/3YdDA0f")
sys.exit(-1)
print("OK")
cmd = ['aws', 'configure', "set", "aws_access_key_id", result["access_key_id"], "--profile", "eks-automation-py"]
print('\n::: Set Access Key ID: ' + ' '.join(cmd) + ' ::: \n')
res = runcmd_call(cmd)
if type(res) == int:
print("Return code:", res)
print(
"Error during the execution. Run the command manually or check the documentation to get more details: http://bit.ly/3YdDA0f")
sys.exit(-1)
print("OK")
cmd = ['aws', 'configure', "set", "aws_secret_access_key", result["secret_access_key"], "--profile",
"eks-automation-py"]
print('\n::: Set Secret Access Key: ' + ' '.join(cmd) + ' ::: \n')
res = runcmd_call(cmd)
if type(res) == int:
print("Return code:", res)
print(
"Error during the execution. Run the command manually or check the documentation to get more details: http://bit.ly/3YdDA0f")
sys.exit(-1)
print("OK")
cmd = ['aws', 'configure', "set", "output", "json", "--profile", "eks-automation-py"]
print('\n::: Set output to JSON by default: ' + ' '.join(cmd) + ' ::: \n')
res = runcmd_call(cmd)
if type(res) == int:
print("Return code:", res)
print(
"Error during the execution. Run the command manually or check the documentation to get more details: http://bit.ly/3YdDA0f")
sys.exit(-1)
print("Completed.")
def assume_role(result):
cmd = ['aws', 'sts', "assume-role", "--role-arn",
"arn:aws:iam::" + result["account_id"] + ":role/" + result["role_name"],
"--role-session-name", result["username"], "--profile", "eks-automation-py",
"--duration-seconds", "43200"]
print('\n::: Assume role: ' + ' '.join(cmd) + ' ::: \n')
res = runcmd_call(cmd)
if type(res) == int:
print("Return code:", res)
print(
"Error during the execution. Run the command manually or check the documentation to get more details: http://bit.ly/3YdDA0f")
sys.exit(-1)
return res
def aws_set_assumed_profile(access_key_id, secret_access_key, session_token):
cmd = ['aws', 'configure', "set", "aws_access_key_id", access_key_id, "--profile",
"eks-assumed-profile"]
print('\n::: Set Access Key ID: ' + ' '.join(cmd) + ' ::: \n')
res = runcmd_call(cmd)
if type(res) == int:
print("Return code:", res)
print(
"Error during the execution. Run the command manually or check the documentation to get more details: http://bit.ly/3YdDA0f")
sys.exit(-1)
print("OK")
cmd = ['aws', 'configure', "set", "aws_secret_access_key", secret_access_key, "--profile",
"eks-assumed-profile"]
print('\n::: Set Secret Access Key: ' + ' '.join(cmd) + ' ::: \n')
res = runcmd_call(cmd)
if type(res) == int:
print("Return code:", res)
print(
"Error during the execution. Run the command manually or check the documentation to get more details: http://bit.ly/3YdDA0f")
sys.exit(-1)
print("OK")
cmd = ['aws', 'configure', "set", "aws_session_token", session_token, "--profile",
"eks-assumed-profile"]
print('\n::: Set Secret Access Key: ' + ' '.join(cmd) + ' ::: \n')
res = runcmd_call(cmd)
if type(res) == int:
print("Return code:", res)
print(
"Error during the execution. Run the command manually or check the documentation to get more details: http://bit.ly/3YdDA0f")
sys.exit(-1)
print("OK")
def remove_profile(profile):
home = os.path.expanduser('~')
if os.name == "nt":
file = os.path.join(home, ".aws\\credentials")
else:
file = os.path.join(home, ".aws/credentials")
try:
with open(file, "r") as f:
lines = f.readlines()
for number, line in enumerate(lines):
if line.strip("\n") == profile:
start_index = number
except FileNotFoundError as e:
print(e)
print("Configuration not found.")
def connect_to_dashboard(result):
cmd = ['kubectl', '-n', 'kubernetes-dashboard', 'create', 'token', result["service_account"], "--duration=12h"]
print('\n::: Getting token to connect to the Kubernetes Dashboard: ' + ' '.join(cmd) + ' ::: \n')
res = runcmd_call(cmd)
return res.decode("utf-8")
def proxy_to_dashboard():
cmd = ['kubectl', 'proxy']
print('\n::: Connecting to Kubernetes dashboard... ' + ' '.join(cmd) + ' ::: \n')
try:
p = subprocess.Popen(cmd, stdin=PIPE, stdout=PIPE)
print("You should now be authenticated.")
print("Try with 'kubectl get pods' now to use the CLI, or connect to the following URL to connect")
print("")
print(
"http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/#!/login")
print("")
print("To stop the 'kubectl proxy', use Ctrl+C")
print("-------")
for line in p.stdout:
print(line.decode("UTF-8"))
except subprocess.CalledProcessError:
print("Error during the connection to the dashboard. Check the previous logs and retry.")
sys.exit(0)
except Exception as e:
print("Error during the connection to the dashboard. Check the previous logs and retry.")
sys.exit(0)
def prompt_questions(questions):
result = prompt(questions)
if result["confirmation"]:
print("Proceeding with inserted data.")
return result
else:
print("To enter again the data, re-run the script. Bye!")
sys.exit()
def main():
# Step 0: preparing the environment
result = None
try:
with open('config.json') as file:
file_exists = exists("config.json")
file_empty = False if os.stat("config.json").st_size == 0 else True
json_object = json.load(file)
if file_exists and file_empty != False and json_object and len(json_object) > 0:
result = prompt(use_config)
if result["confirmation"]:
print("Proceeding with existing data in config.json.")
result = json_object
else:
result = prompt_questions(questions)
else:
result = prompt_questions(questions)
except KeyboardInterrupt:
print("User exited. Bye!")
except FileNotFoundError as e:
result = prompt_questions(questions)
except Exception as e:
print(e)
print("Syntax error in configold.json. Lint that file or REMOVE it!")
sys.exit()
# Store file for future use
json_object = json.dumps(result, indent=4)
with open("config.json", "w") as outfile:
outfile.write(json_object)
# Remove old profiles and keys
remove_profile("[eks-automation-py]")
# Step 1: AWS configure
aws_configure(result)
# Step 2: assume role through STS
res = assume_role(result)
# Step 3: replace/create profile
remove_profile("[eks-assumed-profile]")
credentials = json.loads(res)
access_key_id = credentials["Credentials"]["AccessKeyId"]
secret_access_key = credentials["Credentials"]["SecretAccessKey"]
session_token = credentials["Credentials"]["SessionToken"]
aws_set_assumed_profile(access_key_id, secret_access_key, session_token)
print("Completed.")
# Step 4: login to EKS
cmd = ["aws", "eks", "update-kubeconfig", "--name", result["cluster_name"], "--profile", "eks-assumed-profile",
"--region", result["region"]]
print('\n::: Assume role: ' + ' '.join(cmd) + ' ::: \n')
res = runcmd_call(cmd)
if type(res) == int:
print("Return code:", res)
print(
"Error during the execution. Run the command manually or check the documentation to get more details: http://bit.ly/3YdDA0f")
sys.exit(-1)
# Step 5 (optional): if a dashboard is available, get the token to access it.
try:
if "service_account" in result and None != result["service_account"]:
token = connect_to_dashboard(result)
print("::: Generated token (valid for 12 hours) :::")
print("::: COPY AND PASTE THIS :::")
print("============================================")
print(token)
print("============================================")
proxy_to_dashboard()
else:
print("::: ERROR: Missing 'service_account' in config.json file. Add it manually with the actual value of the configured service account for EKS, or move the current config.json file and generate it again. :::")
except KeyboardInterrupt:
print("")
print("Closing connection to Kubernetes cluster...")
print("Bye!")
main()