-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
228 lines (167 loc) · 5.77 KB
/
main.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
# from oath_toolkit import HOTP
import inspect
from fastapi import FastAPI
from pprint import pprint, pformat
import subprocess
import json
import base64
from subprocess import Popen, PIPE
app = FastAPI()
cache = {}
debug_output = True
def debug(out):
if debug_output:
print(out)
def pexec(cmd):
debug(inspect.stack()[0][3])
debug(cmd)
proc = Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True)
out, err = proc.communicate()
exitcode = proc.returncode
debug("Out = " + out.decode("UTF-8").strip())
debug("Err = " + err.decode("UTF-8").strip())
if exitcode != 0:
debug(pformat(err.decode("UTF-8").strip()))
return [True if exitcode == 0 else False, out.decode("UTF-8").strip()]
def get_from_store(the_item):
debug(inspect.stack()[0][3])
pw = pexec("pass show redhat.com/" + the_item)
if pw[0]:
return pw[1]
else:
return False
def get_from_file(the_file):
debug(inspect.stack()[0][3])
try:
kfile = open(the_file, "r")
return kfile.read().strip()
except:
return False
def set_namespace(namespace):
debug(inspect.stack()[0][3])
pexec("oc project " + namespace)
def ephemeral_login(username, headless):
debug(inspect.stack()[0][3])
namespace = get_namespace_name(username, headless)
password = get_namespace_password(namespace)
return password
def get_namespace_password(namespace):
debug(inspect.stack()[0][3])
global cache
if "eph-password" in cache:
return cache["eph-password"]
set_namespace(namespace)
sc = pexec('kubectl get secret "env-' + namespace + '-keycloak" -o json')
secret = sc[1] if sc[0] else "{}"
password = json.loads(secret)
password = password["data"]["defaultPassword"]
password = base64.b64decode(password).decode("utf-8")
cache["eph-password"] = password
return cache["eph-password"]
def get_namespace(username, headless=True):
debug(inspect.stack()[0][3])
global cache
if "namespace" in cache:
debug(cache["namespace"])
return cache["namespace"]
server = pexec("oc project | awk -F'\"' '{print $4}'")
server = server[1] if server[0] else ""
headlessstr = "--headless" if headless else ""
if server != "https://api.c-rh-c-eph.8p0c.p1.openshiftapps.com:6443":
subprocess.call("/usr/bin/rhtoken e " + headlessstr, shell=True)
namespace = pexec("bonfire namespace list | grep " + username)
debug("namespace[0] = " + str(namespace[0]))
debug("namespace[1] = " + namespace[1])
debug("namespace[1].split = " + pformat(namespace[1].split()))
namespace = namespace[1] if namespace[0] else ""
debug("namespace = " + namespace)
cache["namespace"] = namespace.split()
debug("cache[\"namespace\"] = " + pformat(cache["namespace"]))
pprint(cache["namespace"])
return cache["namespace"]
def extend_namespace(namespace, headless):
debug(inspect.stack()[0][3])
global cache
pexec("bonfire namespace extend " + namespace + " -d 72h")
cache = {}
return get_namespace(namespace, headless)
def get_namespace_name(username, headless):
debug(inspect.stack()[0][3])
res = get_namespace(username, headless)
if type(res) == list and len(res) > 0:
return res[0]
else:
return "No visible reservation"
def get_namespace_expires(username, headless):
debug(inspect.stack()[0][3])
res = get_namespace(username, headless)
if type(res) == list and len(res) > 5:
return res[6]
else:
return "No expiration"
def get_namespace_route(namespace):
debug(inspect.stack()[0][3])
global cache
if "eph-route" in cache:
return cache["eph-route"]
set_namespace(namespace)
server = pexec(
"kubectl get route 2>/dev/null | tail -n 1 | awk '{print $2}'")
server = server[1] if server[0] else ""
cache["eph-route"] = server
return cache["eph-route"]
@app.get("/")
def top_level():
debug(inspect.stack()[0][3])
return "Nothing to see here"
@app.get("/get_creds")
def get_creds(context: str = "associate", headless: bool = False):
debug(inspect.stack()[0][3])
username = get_from_file("username")
if context == "associate":
key = get_from_store("associate-password")
key = key.strip() if key is not False else get_from_file("associate-password")
if key is False:
return "Failed"
token = pexec("./getpw")
debug("token = " + pformat(token))
token = token[1] if token[0] else ""
debug("token = " + token)
return username + "," + key + token
elif context == "jdoeEphemeral":
return "jdoe," + ephemeral_login(username, headless)
else:
debug("Context not defined:")
@app.get("/get_namespace_details")
def get_namespace_details(headless: bool = False):
debug(inspect.stack()[0][3])
username = get_from_file("username")
if username is False:
return "Failed"
namespace = get_namespace_name(username, headless)
result = (
namespace
+ ","
+ get_namespace_route(namespace)
+ ","
+ get_namespace_expires(username, headless)
)
return result
@app.get("/clear_cache")
def get_clear_cache(headless: bool = False):
debug(inspect.stack()[0][3])
global cache
cache = {}
username = get_from_file("username")
if username is False:
return "Failed"
namespace = get_namespace_name(username, headless)
return get_namespace(namespace, headless)
@app.get("/extend_namespace")
def get_extend_namespace(headless: bool = False):
debug(inspect.stack()[0][3])
username = get_from_file("username")
if username is False:
return "Failed"
namespace = get_namespace_name(username, headless)
return extend_namespace(namespace, headless)