-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigmap.yaml
775 lines (612 loc) · 23.8 KB
/
configmap.yaml
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
apiVersion: v1
data:
controller.py: |
import base64
import copy
import http
import http.server as http_server
import http.client as http_client
import json
import logging
import os
import pprint
import re
import string
import secrets
from urllib import parse
ADMIN_USERNAME = os.environ["GITEA_ADMIN_USERNAME"]
ADMIN_PASSWORD = os.environ["GITEA_ADMIN_PASSWORD"]
BASE_URL = os.environ["GITEA_BASE_URL"]
PARSED_BASE_URL = None
try:
PARSED_BASE_URL = parse.urlparse(BASE_URL)
except Exception as e:
raise RuntimeError(f"bad GITEA_BASE_URL '{BASE_URL}': {e}")
tmp = PARSED_BASE_URL.path
if len(tmp) > 1 and tmp[-1] == "/":
tmp = tmp[:-1]
API_BASE_URL = f"{tmp}/api/v1"
def b64string(s):
return base64.b64encode(s.encode("utf-8")).decode("utf-8")
def from_b64string(s):
return base64.b64decode(s.encode("utf-8")).decode("utf-8")
credentials = b64string(f"{ADMIN_USERNAME}:{ADMIN_PASSWORD}")
HEADERS = {
"accept": "application/json",
"content-type": "application/json",
"authorization": f"Basic {credentials}",
}
PASSWORD_APLHABET = (
string.ascii_letters + string.digits + string.punctuation + string.ascii_letters
)
PASSWORD_LENGTH = 12
CASE_RX = re.compile(r"(?<!^)(?=[A-Z])")
def create_result(state, message="awesome", children=[]):
return {
"status": {
"state": state,
"message": message,
},
"children": children,
}
def recase(x):
if isinstance(x, list):
return [recase(tmp) for tmp in x]
if isinstance(x, tuple):
return tuple([recase(tmp) for tmp in x])
if not isinstance(x, dict):
return x
result = {}
for k, v in x.items():
k = CASE_RX.sub("_", k).lower()
result[k] = recase(v)
return result
def do_request(method, endpoint, body=None):
logging.info(f"connecting to: {PARSED_BASE_URL.scheme}://{PARSED_BASE_URL.netloc}")
if PARSED_BASE_URL.scheme == "https":
conn = http_client.HTTPSConnection(PARSED_BASE_URL.netloc)
else:
conn = http_client.HTTPConnection(PARSED_BASE_URL.netloc)
url = f"{API_BASE_URL}{endpoint}"
logging.info(
f"querying: {method} {PARSED_BASE_URL.scheme}://{PARSED_BASE_URL.netloc}{url}"
)
logging.debug(f"query body: {body}")
try:
if body is not None:
body = json.dumps(body)
conn.request(method, url, body=body, headers=HEADERS)
except Exception as e:
raise RuntimeError(f"failed to query server: {e}")
return conn.getresponse()
def parse_response(response):
try:
tmp = response.read().decode("utf-8")
logging.debug("gitea response: %s", tmp)
return json.loads(tmp)
except Exception as e:
raise RuntimeError(f"failed to read server response: {e}")
def do_get(endpoint):
try:
response = do_request("GET", endpoint)
except Exception as e:
raise RuntimeError(f"request failed: {e}")
logging.info(f"query response status: {response.status}")
if response.status == http.HTTPStatus.NOT_FOUND:
return None
if response.status != http.HTTPStatus.OK:
raise RuntimeError(f"failed to GET: {response.reason}")
return parse_response(response)
def do_post(endpoint, data):
try:
response = do_request("POST", endpoint, data)
except Exception as e:
raise RuntimeError(f"request failed: {e}")
logging.info(f"query response status: {response.status}")
if response.status != http.HTTPStatus.CREATED:
raise RuntimeError(f"failed to POST: {response.reason}")
return parse_response(response)
def do_put(endpoint, data):
try:
response = do_request("PUT", endpoint, data)
except Exception as e:
raise RuntimeError(f"request failed: {e}")
logging.info(f"query response status: {response.status}")
if response.status != http.HTTPStatus.NO_CONTENT:
raise RuntimeError(f"failed to PUT: {response.reason}")
return None
def do_patch(endpoint, data):
try:
response = do_request("PATCH", endpoint, data)
except Exception as e:
raise RuntimeError(f"request failed: {e}")
logging.info(f"query response status: {response.status}")
if response.status != http.HTTPStatus.OK:
raise RuntimeError(f"failed to PATCH: {response.reason}")
return parse_response(response)
def do_delete(endpoint):
try:
response = do_request("DELETE", endpoint)
except Exception as e:
raise RuntimeError(f"request failed: {e}")
logging.info(f"query response status: {response.status}")
if response.status == http.HTTPStatus.NOT_FOUND:
return None
if response.status != http.HTTPStatus.NO_CONTENT:
raise RuntimeError(f"failed to DELETE: {response.reason}")
return None
#
# repository stuff
#
def get_organization_repositories(organization):
endpoint = f"/orgs/{organization}/repos"
return do_get(endpoint)
def create_organization_repository(organization, data):
endpoint = f"/orgs/{organization}/repos"
return do_post(endpoint, data)
def delete_repository(owner, name):
# raise NotImplementedError("not implemented: gitea api is strange")
endpoint = f"/users/{owner}/repos/{name}"
return do_delete(endpoint)
def sync_repository(parent, _, delete=False):
spec = parent["spec"]
name = spec["name"]
organization = spec["organization"]
all_repositories = get_organization_repositories(organization)
if all_repositories is None:
return create_result(
"error", message=f"organization '{organization}' does not exist"
)
existing = list(filter(lambda r: r["name"] == name, all_repositories))
if len(existing) < 1:
if delete:
return create_result("deleted")
try:
existing = create_organization_repository(organization, spec)
except Exception as e:
raise RuntimeError(f"failed to create repository: {e}")
else:
existing = existing[0]
if delete:
owner = existing["owner"]
# doesn't work if owner is org
owner_name = owner["username"]
# doesn't work either then
# owner_name = ADMIN_USERNAME
# just pretend ...
try:
delete_repository(owner_name, name)
return create_result("deleted")
except Exception as e:
raise RuntimeError(f"failed to delete repository: {e}")
return create_result("ready")
#
# organization stuff
#
def get_organization(name):
endpoint = f"/orgs/{name}"
return do_get(endpoint)
def get_organization_teams(name):
endpoint = f"/orgs/{name}/teams"
return do_get(endpoint)
def create_organization(data):
endpoint = "/orgs"
return do_post(endpoint, data)
def delete_organization(name):
endpoint = f"/orgs/{name}"
return do_delete(endpoint)
# TODO: don't hardcode ;)
TEAM_UNITS = [
"repo.pulls",
"repo.projects",
"repo.wiki",
"repo.ext_wiki",
"repo.packages",
"repo.actions",
"repo.code",
"repo.issues",
"repo.ext_issues",
"repo.releases",
]
TEAM_UNITS_MAP = {
"repo.actions": "none",
"repo.code": "none",
"repo.ext_issues": "none",
"repo.ext_wiki": "none",
"repo.issues": "none",
"repo.packages": "none",
"repo.projects": "none",
"repo.pulls": "none",
"repo.releases": "none",
"repo.wiki": "none",
}
team_preset = {
"can_create_org_repo": False,
"includes_all_repositories": False,
"permission": "read",
"units": TEAM_UNITS,
"units_map": TEAM_UNITS_MAP,
}
def create_organization_team(name, data):
endpoint = f"/orgs/{name}/teams"
team = copy.deepcopy(team_preset)
team.update(data)
return do_post(endpoint, team)
def patch_organization_team(team_id, data):
endpoint = f"/teams/{team_id}"
team = copy.deepcopy(team_preset)
team.update(data)
return do_patch(endpoint, team)
def delete_organization_team(team_id):
endpoint = f"/teams/{team_id}"
return do_delete(endpoint)
def sync_organization(parent, children, delete=False):
spec = parent["spec"]
username = spec["username"]
if delete:
try:
delete_organization(username)
return create_result("deleted")
except Exception as e:
raise RuntimeError(f"failed to delete organization: {e}")
existing = get_organization(username)
teams = []
if existing is None:
try:
existing = create_organization(spec)
except Exception as e:
raise RuntimeError(f"failed to create org: {e}")
else:
try:
teams = get_organization_teams(username)
except Exception as e:
raise RuntimeError(f"failed to get org teams: {e}")
existing_teams_by_name = dict([(t["name"], t) for t in teams])
to_delete = list(existing_teams_by_name.keys())
for team in spec.get("teams", []):
team_name = team["name"]
if team_name not in existing_teams_by_name:
try:
create_organization_team(username, team)
except Exception as e:
raise RuntimeError(f"failed to create org team: {e}")
else:
to_delete.remove(team_name)
existing_team = existing_teams_by_name[team_name]
patched = copy.deepcopy(existing_team)
patched.update(team)
if patched != existing_team:
try:
patch_organization_team(existing_team["id"], patched)
except Exception as e:
raise RuntimeError(f"failed to patch org team: {e}")
for team_name in to_delete:
existing_team = existing_teams_by_name[team_name]
try:
delete_organization_team(existing_team["id"])
except Exception as e:
raise RuntimeError(f"failed to delete org team: {e}")
return create_result("ready")
#
# user stuff
#
def get_user(name):
endpoint = f"/users/{name}"
return do_get(endpoint)
def create_user(data):
endpoint = "/admin/users"
return do_post(endpoint, data)
def patch_user(name, data):
endpoint = f"/admin/users/{name}"
return do_patch(endpoint, data)
def delete_user(name):
endpoint = f"/admin/users/{name}"
return do_delete(endpoint)
def create_pw():
return "".join(secrets.choice(PASSWORD_APLHABET) for i in range(PASSWORD_LENGTH))
def get_users_teams(username):
endpoint = f"/user/teams?sudo={username}"
return do_get(endpoint)
def remove_users_team(team_id, username):
endpoint = f"/teams/{team_id}/members/{username}"
return do_delete(endpoint)
def add_users_team(team_id, username):
endpoint = f"/teams/{team_id}/members/{username}"
return do_put(endpoint, None)
def sync_user(parent, children, delete=False):
spec = parent["spec"]
username = spec["username"]
if delete:
try:
delete_user(username)
return create_result("deleted")
except Exception as e:
raise RuntimeError(f"failed to delete user: {e}")
secret_name = parent["metadata"]["name"] + "-password"
secrets = children.get("Secret.v1", {})
secret = secrets.get(secret_name)
password = None
if secret is not None:
password_b64 = secret["data"]["password"]
password = from_b64string(password_b64)
else:
password = create_pw()
password_b64 = b64string(password)
existing = get_user(username)
if existing is None:
spec["password"] = password
spec["login_name"] = spec.get("login_name", username)
try:
create_user(spec)
except Exception as e:
raise RuntimeError(f"failed to create user: {e}")
else:
try:
patch_user(
username,
{
"username": username,
"password": password,
"login_name": existing.get("login_name", username),
"source_id": existing.get("source_id", 0),
},
)
except Exception as e:
raise RuntimeError(f"failed to patch user: {e}")
children = [
{
"apiVersion": "v1",
"kind": "Secret",
"type": "Opaque",
"metadata": {"name": secret_name},
"data": {
"password": password_b64,
},
}
]
try:
users_teams = get_users_teams(username)
except Exception as e:
raise RuntimeError(f"failed to user's teams: {e}")
assigned_teams_lookup = dict(
[((t["name"], t["organization"]["name"]), t) for t in users_teams]
)
to_remove = list(assigned_teams_lookup.keys())
teams = spec.get("teams", [])
for team in teams:
team_name = team["name"]
organization_name = team["organization"]
key = (team_name, organization_name)
if key in assigned_teams_lookup:
to_remove.remove(key)
continue
try:
organization_teams = get_organization_teams(organization_name)
except Exception as e:
raise RuntimeError(f"failed to get teams for organization: {e}")
if not organization_teams:
raise ValueError("organization has no teams")
organization_team = list(
filter(lambda t: t["name"] == team_name, organization_teams)
)
if len(organization_team) < 1:
raise ValueError("team not found in organization")
organization_team = organization_team[0]
try:
add_users_team(organization_team["id"], username)
except Exception as e:
raise RuntimeError(f"failed to add user to team: {e}")
for key in to_remove:
existing_team = assigned_teams_lookup[key]
try:
remove_users_team(existing_team["id"], username)
except Exception as e:
raise RuntimeError(f"failed to remove user from team: {e}")
return create_result("ready", children=children)
#
# application stuff
#
def get_applications():
endpoint = "/user/applications/oauth2"
return do_get(endpoint)
def get_application(application_id):
endpoint = f"/user/applications/oauth2/{application_id}"
return do_get(endpoint)
def create_application(data):
endpoint = "/user/applications/oauth2"
return do_post(endpoint, data)
def patch_application(application_id, data):
endpoint = f"/user/applications/oauth2/{application_id}"
return do_patch(endpoint, data)
def delete_application(application_id):
endpoint = f"/user/applications/oauth2/{application_id}"
return do_delete(endpoint)
def sync_application(parent, children, delete=False):
spec = parent["spec"]
name = spec["name"]
if delete:
try:
delete_application(name)
return create_result("deleted")
except Exception as e:
raise RuntimeError(f"failed to delete application: {e}")
secret_name = parent["metadata"]["name"] + "-secret"
secrets = children.get("Secret.v1", {})
secret = secrets.get(secret_name)
client_secret_b64 = client_id_b64 = ""
if secret is not None:
client_secret_b64 = secret["data"]["clientSecret"]
client_secret = from_b64string(client_secret_b64)
client_id_b64 = secret["data"]["clientId"]
client_id = from_b64string(client_id_b64)
all_applications = get_applications()
existing = list(filter(lambda a: a["name"] == name, all_applications))
if len(existing) < 1:
if delete:
return create_result("deleted")
try:
app = create_application(spec)
except Exception as e:
raise RuntimeError(f"failed to create application: {e}")
client_id = app["client_id"]
client_id_b64 = b64string(client_id)
client_secret = app["client_secret"]
client_secret_b64 = b64string(client_secret)
else:
existing = existing[0]
existing_id = existing["id"]
if delete:
try:
delete_application(existing_id)
return create_result("deleted")
except Exception as e:
raise RuntimeError(f"failed to delete application: {e}")
if client_secret_b64 == "" or client_id_b64 == "":
try:
app = patch_application(existing_id, spec)
except Exception as e:
raise RuntimeError(f"failed to create application: {e}")
client_id = app["client_id"]
client_id_b64 = b64string(client_id)
client_secret = app["client_secret"]
client_secret_b64 = b64string(client_secret)
children = [
{
"apiVersion": "v1",
"kind": "Secret",
"type": "Opaque",
"metadata": {"name": parent["metadata"]["name"] + "-secret"},
"data": {
"clientId": client_id_b64,
"clientSecret": client_secret_b64,
},
}
]
return create_result("ready", children=children)
#
# token stuff
#
def get_user_tokens(username):
endpoint = f"/users/{username}/tokens"
return do_get(endpoint)
def create_user_token(username, data):
endpoint = f"/users/{username}/tokens"
return do_post(endpoint, data)
def delete_user_token(username, name):
endpoint = f"/users/{username}/tokens/{name}"
return do_delete(endpoint)
def sync_token(parent, children, delete=False):
spec = parent["spec"]
username = spec["username"]
all_tokens = get_user_tokens(username)
if all_tokens is None:
raise RuntimeError("user does not exist")
name = spec["name"]
if delete:
try:
delete_user_token(username, name)
return create_result("deleted")
except Exception as e:
raise RuntimeError(f"failed to delete user token: {e}")
token = token_b64 = ""
secret_name = parent["metadata"]["name"] + "-secret"
secrets = children.get("Secret.v1", {})
secret = secrets.get(secret_name)
if secret is not None:
token_b64 = secret["data"]["token"]
token = from_b64string(token_b64)
existing = list(filter(lambda t: t["name"] == name, all_tokens))
if len(existing) == 0:
try:
existing = create_user_token(username, spec)
except Exception as e:
raise RuntimeError(f"failed to create user token: {e}")
else:
existing = existing[0]
if token_b64 == "" or set(existing["scopes"]) != set(spec["scopes"]):
try:
delete_user_token(username, name)
except Exception as e:
raise RuntimeError(f"failed to delete user token for update: {e}")
try:
existing = create_user_token(username, spec)
except Exception as e:
raise RuntimeError(f"failed to re-create user token: {e}")
token = existing["sha1"]
token_b64 = b64string(token)
assert token_b64 != ""
children = [
{
"apiVersion": "v1",
"kind": "Secret",
"type": "Opaque",
"metadata": {"name": parent["metadata"]["name"] + "-secret"},
"data": {
"token": token_b64,
},
}
]
return create_result("ready", children=children)
class Dispatcher(http_server.BaseHTTPRequestHandler):
def do_POST(self):
handler = globals().get(f"sync_{self.path[1:]}")
if handler is None:
raise RuntimeError(f"no handler found for {self.path[1:]}")
content_length = int(self.headers["content-length"])
observed = json.loads(self.rfile.read(content_length))
logging.debug(f"received: {observed}")
result = create_result("error", message="something didn't work")
delete = observed.get("finalizing", False)
parent = observed["parent"]
parent["spec"] = recase(parent["spec"])
try:
result = handler(
parent,
observed.get("children", {}),
delete=delete,
)
except Exception as e:
logging.exception("sync exception")
result = create_result("error", message=f"sync failed: {type(e)}: {e}")
else:
if delete:
result["finalized"] = True
logging.debug(f"result:\n{pprint.pformat(result)}")
self.send_response(http.HTTPStatus.OK)
self.send_header("Content-type", "application/json")
self.end_headers()
self.wfile.write(json.dumps(result).encode())
def log_error(self, fmt, *args):
logging.error(fmt, *args)
def log_message(self, fmt, *args):
logging.info(fmt, *args)
if __name__ == "__main__":
log_format = "%(asctime)s %(levelname)s %(name)s: %(message)s"
log_level = os.getenv("LOG_LEVEL", "INFO")
log_cfg = {
"version": 1,
"formatters": {"standard": {"format": log_format}},
"disable_existing_loggers": True,
"handlers": {
"default": {
"class": "logging.StreamHandler",
"formatter": "standard",
"stream": "ext://sys.stdout",
"level": log_level,
}
},
"loggers": {
"": {"handlers": ["default"], "level": log_level},
},
}
import logging.config
logging.config.dictConfig(log_cfg)
bind_host, bind_port = "", 8080
logging.info(f"starting http server on {bind_host}:{bind_port}")
server = http_server.HTTPServer((bind_host, bind_port), Dispatcher)
server.serve_forever()
kind: ConfigMap
metadata:
creationTimestamp: null
name: gitea-resource-controller
namespace: gitea