-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgitauth.py
350 lines (284 loc) · 14.2 KB
/
gitauth.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
import logging
import os
import sys
import base64
import json
import time
import jwt
import boto3
from boto3.dynamodb.conditions import Key
from botocore.exceptions import ClientError
from webexteamssdk import WebexTeamsAPI
import requests
class gitauth:
def __init__(self):
# Initialize logging
logging.basicConfig(level=os.environ.get("LOGLEVEL", "DEBUG"))
self.logging = logging.getLogger()
if "CLIENTID" in os.environ:
self.client_id = os.getenv("CLIENTID")
else:
logging.error("Environment variable CLIENTID must be set")
sys.exit(1)
if "CLIENTSECRET" in os.environ:
self.client_secret = os.getenv("CLIENTSECRET")
else:
logging.error("Environment variable CLIENTSECRET must be set")
sys.exit(1)
if "CALLBACKURL" in os.environ:
self.callback_url = os.getenv("CALLBACKURL")
else:
logging.error("Environment variable CALLBACKURL must be set")
sys.exit(1)
if "WEBEX_BOT_ID" in os.environ:
self.webex_bot_id = os.getenv("WEBEX_BOT_ID")
else:
logging.error("Environment variable WEBEX_BOT_ID must be set")
sys.exit(1)
if 'WEBEX_TEAMS_ACCESS_TOKEN' in os.environ:
self.wxt_access_token = os.getenv("WEBEX_TEAMS_ACCESS_TOKEN")
else:
logging.error("Environment variable WEBEX_TEAMS_ACCESS_TOKEN must be set")
sys.exit(1)
if "DYNAMODB_ROOM_TABLE" in os.environ:
self.db_room_name = os.getenv("DYNAMODB_ROOM_TABLE")
else:
logging.error("Environment variable DYNAMODB_ROOM_TABLE must be set")
sys.exit(1)
if "DYNAMODB_INSTALLATION_TABLE" in os.environ:
self.db_installation_name = os.getenv("DYNAMODB_INSTALLATION_TABLE")
else:
logging.error("Environment variable DYNAMODB_INSTALLATION_TABLE must be set")
sys.exit(1)
if "DYNAMODB_AUTH_TABLE" in os.environ:
self.db_auth_name = os.getenv("DYNAMODB_AUTH_TABLE")
else:
logging.error("Environment variable DYNAMODB_AUTH_TABLE must be set")
sys.exit(1)
if "SECRET_NAME" in os.environ:
self.secret_name = os.getenv("SECRET_NAME")
else:
logging.error("Environment variable SECRET_NAME must be set")
sys.exit(1)
if "REGION_NAME" in os.environ:
self.region_name = os.getenv("REGION_NAME")
else:
logging.error("Environment variable REGION_NAME must be set")
sys.exit(1)
if "APP_ID" in os.environ:
self.app_id = os.getenv("APP_ID")
else:
logging.error("Environment variable APP_ID must be set")
sys.exit(1)
if 'GITHUB_BOT_NAME' in os.environ:
self.git_bot_name = os.getenv("GITHUB_BOT_NAME")
else:
logging.error("Environment variable GITHUB_BOT_NAME must be set")
sys.exit(1)
self.dynamodb = ""
self.table = ''
self.Api = WebexTeamsAPI()
self.private_key = ''
self.timeout_value = 60
def get_git_key(self):
session = boto3.session.Session()
client = session.client(service_name='secretsmanager', region_name=self.region_name)
try:
get_secret_value_response = client.get_secret_value(SecretId=self.secret_name)
except ClientError as e:
raise e
else:
if 'SecretString' in get_secret_value_response:
secret = get_secret_value_response['SecretString']
self.private_key = secret
else:
decoded_binary_secret = base64.b64decode(get_secret_value_response['SecretBinary'])
self.private_key = json.loads(decoded_binary_secret)
def webhook_request(self, event):
json_string = event
if json_string['headers']['referer'] == 'https://github.com/':
#code = json_string['queryStringParameters']['code']
install_id = json_string['queryStringParameters']['installation_id']
if json_string['queryStringParameters']['setup_action'] == 'install':
if 'state' in json_string['queryStringParameters']:
state = json_string['queryStringParameters']['state']
else:
self.logging.debug("This is probably a user using the public auth link, abort")
return {
"statusCode":
302,
"headers": {
"Content-Type": "application/json",
"Refresh": "15; url=https://github.com/apps/" + self.git_bot_name
},
"body":
json.dumps({
"User authenticated app without using webex auth flow":
"Please uninstall app (Uninstall cidrbot) then follow this process: " +
" 1) Invite the bot to a secure webex teams room " + " 2) Type @CIDRbot add repo " +
" 3) Complete the auth process by clicking the link the bot messages you " +
" 4) You will receive a message in both the room and direct messages that the bot authed successfully. REDIRECTING IN 15 SECONDS"
})
}
state_status = self.check_state(state)
if state_status is not False:
time_epoch = int(time.time())
payload = {'iat': time_epoch, 'exp': time_epoch + (10 * 60), 'iss': self.app_id}
self.get_git_key()
encoded_key = jwt.encode(payload, self.private_key, algorithm="RS256")
token_payload = self.create_token(install_id, encoded_key)
if token_payload is not None:
self.logging.debug("Success the state matches and the payload was true")
#self.logging.debug(str(token_payload) + " " + str(state) + " " + str(state_status))
person_id = state_status[0]['personId']
room_id = state_status[0]['roomId']
pt_id = state_status[0]['ptId']
room = self.Api.rooms.get(room_id)
room_name = room.title
payload_dict = json.loads(token_payload)
token = payload_dict['token']
expire_date = int(time.time()) + 3600
repo_info = self.git_repo_info(token, 'https://api.github.com/installation/repositories')
user_info = self.git_user_info(
encoded_key, f'https://api.github.com/app/installations/{install_id}'
)
repo_info_dict = json.loads(repo_info)
user_info_dict = json.loads(user_info)
self.logging.debug(user_info_dict)
self.logging.debug(repo_info_dict)
user_id = user_info_dict['account']['id']
user_name = user_info_dict['account']['login']
count = repo_info_dict['total_count']
repo_path_list = []
repo_list = ''
i = 0
while i < count:
repo_full_name = repo_info_dict['repositories'][i]['full_name']
repo_path_list.append(repo_full_name.lower())
Repo_hypr_lnk = f'<a href="{"https://github.com/" + repo_full_name}">{repo_full_name}</a>'
repo_list += " - " + Repo_hypr_lnk + " \n "
i += 1
text_direct = f"Authentication successful, the following repos are added to room: {room_name} \n" + repo_list
text = (
"Authentication successful, type @CIDRbot help to begin. To add repos, please" +
''' visit <a href="https://github.com/settings/installations/">Github applications</a> and click "configure" for the cidrbot app '''
)
post_direct_message = {'toPersonId': person_id, 'markdown': text_direct}
post_message = {'roomId': room_id, 'parentId': pt_id, 'markdown': text}
#self.add_installation(str(user_id), install_id, person_id, user_name, room_id, token, repo_path_list, expire_date, refresh_token)
self.add_installation(
str(user_id), install_id, person_id, user_name, room_id, token, repo_path_list, expire_date
)
self.send_webex_message(post_direct_message)
self.send_webex_message(post_message)
self.logging.debug("Auth cycle completed, redirecting user")
return None
self.logging.debug("Token payload is none")
return {
"statusCode":
302,
"headers": {
"Content-Type": "application/json",
"Refresh": "15; url=https://github.com/apps/" + self.git_bot_name
},
"body":
json.dumps({
"Expired link was used":
"Please uninstall cidrbot app and follow the process " +
"described by the page you will be redirected to. REDIRECTING IN 15 SECONDS"
})
}
self.logging.debug("State status is false")
return None
self.logging.debug("Bad setup action")
return None
self.logging.debug(json_string['headers']['referer'])
self.logging.debug("Unknown referer")
return None
def git_repo_info(self, token, URL):
headers = {'Authorization': 'token ' + token, 'Accept': 'application/vnd.github.v3+json'}
session = requests.Session()
response = session.get(URL, headers=headers)
if response.status_code == 200:
self.logging.debug("Repo info granted")
resp = response.text
return resp
self.logging.debug(str(response.status_code))
self.logging.debug(str(response.text))
return None
def git_user_info(self, encoded_key, URL):
headers = {"Authorization": f"Bearer {encoded_key}", 'Accept': 'application/vnd.github.v3+json'}
post_data = {}
response = requests.get(URL, json=post_data, headers=headers, timeout=self.timeout_value)
if response.status_code == 200:
self.logging.debug("User info granted")
resp = str(response.text)
return resp
self.logging.debug(str(response.status_code))
self.logging.debug(str(response.text))
return None
def send_webex_message(self, post_data):
URL = 'https://webexapis.com/v1/messages'
headers = {'Authorization': 'Bearer ' + self.wxt_access_token, 'Content-type': 'application/json;charset=utf-8'}
requests.post(URL, json=post_data, headers=headers, timeout=self.timeout_value)
def add_installation(self, user_id, install_id, person_id, user_name, room_id, token, repo_path_list, expire_date):
self.dynamodb = boto3.resource('dynamodb')
self.table = self.dynamodb.Table(self.db_installation_name)
self.table.put_item(
Item={
'installation_id': install_id,
'user_id': user_id,
'user_name': user_name,
'person_id': person_id,
'room_id': room_id,
'access_token': token,
'expire_date': expire_date,
}
)
self.table = self.dynamodb.Table(self.db_room_name)
response = self.table.query(KeyConditionExpression=Key('room_id').eq(room_id))
current_repos = response['Items'][0]['repos']
for repo in repo_path_list:
db_repo_name = None
if repo in current_repos:
db_repo_name = repo
if db_repo_name is None:
self.table.update_item(
Key={'room_id': room_id},
UpdateExpression="set #repo.#reponame= :name",
ExpressionAttributeNames={
'#repo': 'repos',
'#reponame': repo
},
ExpressionAttributeValues={':name': {
'installation_id': str(install_id),
'required_approvals': 1
}}
)
def check_state(self, state):
self.dynamodb = boto3.resource('dynamodb')
self.table = self.dynamodb.Table(self.db_auth_name)
state_condition = False
current_time = int(time.time())
try:
response = self.table.query(KeyConditionExpression=Key('state').eq(state))
self.logging.debug(current_time)
self.logging.debug(response['Items'][0]['ttl'])
if current_time < int(response['Items'][0]['ttl']):
state_condition = response['Items']
except Exception:
pass
self.table.delete_item(Key={'state': state})
return state_condition
def create_token(self, installation_id, encoded_key):
URL = f'https://api.github.com/app/installations/{installation_id}/access_tokens'
headers = {"Authorization": f"Bearer {encoded_key}", 'Accept': 'application/vnd.github.v3+json'}
post_data = {}
response = requests.post(URL, json=post_data, headers=headers, timeout=self.timeout_value)
if response.status_code == 201:
self.logging.debug("Access key granted")
resp = str(response.text)
return resp
self.logging.debug(str(response.status_code))
self.logging.debug(str(response.text))
return None