-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathharbor-image-replication-manager.py
629 lines (501 loc) · 20.5 KB
/
harbor-image-replication-manager.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
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
#!python3
# Copyright 2021 SVA System Vertrieb Alexander GmbH
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Author: Niko Wenselowski <[email protected]>
"""
Harbor Image Replication Manager
"""
import argparse
import json
import logging
import time
from collections import namedtuple
from functools import partial
from urllib.parse import quote
import requests
import urllib3
from requests.auth import HTTPBasicAuth
__version__ = "1.0.0"
DELETION_SLEEP_TIME = 10
Image = namedtuple("Image", "namespace name tag")
logger = logging.getLogger("replication-manager")
def main():
options = parse_cli()
if options.verbosity:
logging.basicConfig(level=logging.DEBUG)
else:
logging.basicConfig(level=logging.INFO)
if not options.verify_cert:
urllib3.disable_warnings()
h = HarborClient(
options.base_url, options.username, options.password, options.verify_cert
)
manager = HarborManager(
h,
replication_rule_prefix=options.rule_prefix,
trigger_execution=options.trigger,
)
if options.mode == "create":
if not options.registry:
raise RuntimeError("No registry given for rule!")
registry = h.get_registry(options.registry)
process_func = partial(manager.replicate_image, registry=registry)
elif options.mode == "delete":
process_func = partial(
manager.remove_image_replication, delete_projects=options.delete_projects
)
else:
raise RuntimeError(f"Invalid mode: {options.mode!r}")
images = get_images(options.imagefile)
for image in images:
process_func(image)
def parse_cli():
parser = argparse.ArgumentParser(
description="Manage your image replication to harbor with ease!",
epilog="Made with <3 by SVA",
)
parser.add_argument("--version", action="version", version=__version__)
parser.add_argument(
"-v", "--verbose", dest="verbosity", action="count", help="Verbose output"
)
parser.add_argument("--image-file", "-i", dest="imagefile", required=True)
parser.add_argument(
"--rule-prefix",
dest="rule_prefix",
default="hir-",
help="The prefix for managed policies",
)
parser.add_argument(
"--no-rule-trigger",
dest="trigger",
action="store_false",
help="Trigger replication for policies",
)
parser.add_argument("mode", choices=["create", "delete"])
harbor = parser.add_argument_group(
"Harbor", description="Configuration for the connection to Harbor"
)
harbor.add_argument(
"--harbor", dest="base_url", required=True, help="Harbor address"
)
harbor.add_argument(
"--insecure",
action="store_false",
dest="verify_cert",
help="Do not verify server certificates",
)
harbor.add_argument(
"--user", dest="username", required=True, help="Username to use"
)
harbor.add_argument(
"--password", dest="password", required=True, help="Password to use"
)
behaviour = parser.add_argument_group("Behavioural options")
behaviour.add_argument(
"--registry", help="Name of the registry to use when creating new policies"
)
behaviour.add_argument(
"--delete-projects",
dest="delete_projects",
action="store_true",
help="Delete projects (and not just rules) if deleting",
)
return parser.parse_args()
def get_images(image_file):
with open(image_file) as imagefile:
images = imagefile.readlines()
return [split_image_line(image) for image in images]
def split_image_line(line):
line = line.strip()
try:
namespace, image_and_tag = line.split("/", 1)
except ValueError:
namespace = "library"
image_and_tag = line
try:
image, tag = image_and_tag.split(":", 1)
except ValueError:
image = image_and_tag
tag = "latest"
return Image(namespace, image, tag)
class HarborProject:
def __init__(self, name, *, raw=None):
self.name = name
self._raw = raw or None
def create(self, harbor_client):
data = {
"project_name": self.name,
"cve_allowlist": {
"update_time": "0001-01-01T00:00:00.000Z",
"items": [],
"project_id": 0,
"creation_time": "0001-01-01T00:00:00.000Z",
"id": 0,
"expires_at": 0,
},
"public": True,
"storage_limit": 0,
}
harbor_client.create_project(data)
self._raw = harbor_client.get_project_json(self.name)
def delete(self, harbor_client):
if not self._raw:
return
if not harbor_client.is_project_deletable(self.name):
harbor_client.remove_project_content(self.name)
logger.info("Triggered emptying of %s", self.name)
logger.info("Waiting for project %s to be empty", self.name)
while not harbor_client.is_project_deletable(self.name):
time.sleep(DELETION_SLEEP_TIME)
logger.debug("Project %s still exists...", self.name)
harbor_client.delete_project(self.name)
def exists(self):
return bool(self._raw)
@classmethod
def from_json(cls, project_json):
return HarborProject(project_json["name"], raw=project_json)
class ReplicationPolicy:
def __init__(self, name, *, raw=None):
self.name = name
self._raw = raw or None
try:
self.tags = self._get_raw_tags()
except TypeError:
self.tags = set()
def _get_raw_tags(self):
tags = [
tagfilter["value"]
for tagfilter in self._raw["filters"]
if tagfilter["type"] == "tag"
]
if len(tags) > 1:
raise RuntimeError("Invalid tag configuration at {self.name} detected!")
logger.debug("Tags before transformation: %r", tags)
try:
tags = tags[0]
if tags.startswith("{"):
tags = tags[1:]
if tags.endswith("}"):
tags = tags[:-1]
tags = set(tags.split(","))
logger.debug("Tags after transformation: %r", tags)
return tags
except IndexError:
logger.debug("Tag transformation failed")
return set()
@property
def id(self):
try:
return self._raw["id"]
except TypeError:
raise RuntimeError("Policy not created on harbor.")
def create(self, harbor_client, project_name, image, registry):
tag = image.tag or "**"
name = f"{image.namespace}/{image.name}"
data = {
"description": f"Managed by {__file__}",
# Flattening: Since we have the same namespace as a project we
# set this to 1 in order to have the same image path as before.
"dest_namespace_replace_count": 1,
"filters": [{"type": "name", "value": name}, {"type": "tag", "value": tag}],
"dest_registry": {
"creation_time": "0001-01-01T00:00:00.000Z",
"credential": {"access_secret": "*****", "type": "secret"},
"id": 0,
"insecure": True,
"name": "Local",
"type": "harbor",
"update_time": "0001-01-01T00:00:00.000Z",
"url": "http://core:8080",
},
"src_registry": registry,
"dest_namespace": project_name,
"trigger": {"trigger_settings": {}, "type": "manual"},
"replicate_deletion": True,
"deletion": True,
"override": True,
"enabled": True,
"name": self.name,
}
harbor_client.create_replication_policy(data)
self._raw = harbor_client.get_replication_policy_json(self.name)
self.tags = self._get_raw_tags()
def update_filters(self, harbor_client):
raw_tags = self._get_raw_tags()
logger.debug("tags: %r (local) vs %r (API)", self.tags, raw_tags)
if self.tags == raw_tags:
# Filters match, no need for an update
logger.debug("Tag filters of %s already set", self.name)
return
new_filters = [
oldfilter
for oldfilter in self._raw["filters"]
if oldfilter["type"] != "tag"
]
new_tag_filter = {"type": "tag", "value": self._create_filter_expression()}
new_filters.append(new_tag_filter)
new_config = self._raw.copy()
new_config["filters"] = new_filters
policy_id = self._raw["id"]
harbor_client.update_replication_policy(policy_id, new_config)
self._raw = harbor_client.get_replication_policy_json(self.name)
def _create_filter_expression(self):
if not self.tags:
return "**" # Match everything
if "**" in self.tags:
return "**"
return "{%s}" % ",".join(self.tags)
def trigger_execution(self, harbor_client):
harbor_client.trigger_replication_policy_execution(self.id)
def delete(self, harbor_client):
if not self._raw:
return
harbor_client.delete_replication_policy(self.id)
def exists(self):
return bool(self._raw)
def __repr__(self):
return "<ReplicationPolicy(name=%s)>" % self.name
@classmethod
def from_json(cls, policy_json):
return ReplicationPolicy(policy_json["name"], raw=policy_json)
class HarborManager:
def __init__(
self, harbor_client, *, replication_rule_prefix, trigger_execution=True
):
self._harbor_client = harbor_client
self._replication_rule_prefix = replication_rule_prefix
self._trigger_replication_rule = trigger_execution
self._replication_policies = {
policy.name: policy
for policy in self._harbor_client.get_replication_policies()
}
self._projects = {
project.name: project for project in self._harbor_client.get_projects()
}
def replicate_image(self, image, registry):
logger.debug("Attempting to replicate %s from %s", image, registry['name'])
project_name = image.namespace
try:
project = self._projects[project_name]
except KeyError:
logger.debug("Creating project %s", project_name)
project = HarborProject(project_name)
project.create(self._harbor_client)
self._projects[project_name] = project
logger.info("Created project %s", project.name)
replication_rule_name = self.create_replication_rule_name(image)
try:
policy = self._replication_policies[replication_rule_name]
logger.debug("Tags for %s: %s", policy.name, policy.tags)
policy.tags.add(image.tag)
policy.update_filters(self._harbor_client)
except KeyError:
logger.debug("policy %s does not exist", replication_rule_name)
policy = ReplicationPolicy(replication_rule_name)
policy.create(self._harbor_client, project_name, image, registry)
self._replication_policies[replication_rule_name] = policy
logger.info("Created policy %s", policy.name)
if self._trigger_replication_rule:
policy.trigger_execution(self._harbor_client)
logger.info("Triggered execution of policy %s", policy.name)
logger.info("Replication of %s has been created", image)
def remove_image_replication(self, image, delete_projects):
logger.debug("Attempting to remove replication for %s", image)
replication_rule_name = self.create_replication_rule_name(image)
try:
policy = self._replication_policies[replication_rule_name]
try:
policy.tags.remove(image.tag)
except KeyError: # tag not in rule
pass
if policy.tags:
policy.update_filters(self._harbor_client)
else: # no tags - delete rule
policy.delete(self._harbor_client)
logger.info("Removed policy %s", policy.name)
del self._replication_policies[replication_rule_name]
except KeyError:
logger.debug("policy %s does not exist", replication_rule_name)
if delete_projects:
project_name = image.namespace
try:
project = self._projects[project_name]
project.delete(self._harbor_client)
logger.info("Removed project %s", project_name)
del self._projects[project_name]
except KeyError as kerr:
logger.debug("Received key error: %s", kerr)
logger.debug("Project %r does not exist", project_name)
logger.info("Removed replication of %s", image)
def create_replication_rule_name(self, image):
rule_name = f"{image.namespace}-{image.name}"
return self._replication_rule_prefix + rule_name
class HarborClient:
def __init__(self, base_url, username, password, verify_certificate=True):
if not base_url.startswith("http"):
base_url = "https://" + base_url
if not base_url.endswith("/"):
base_url = base_url + "/"
self.base_url = base_url
self._credentials = HTTPBasicAuth(username, password)
self._session = requests.Session()
self._session.verify = verify_certificate
def get(self, path):
address = self.base_url + path
response = self._session.get(address, auth=self._credentials)
try:
response.raise_for_status()
except Exception as err:
logger.debug("Request failed: %s", err)
logger.debug("Response headers: %s", response.headers)
logger.debug("Response body: %s", response.json())
raise
csrf_token = response.headers.get("X-Harbor-Csrf-Token")
if csrf_token:
self._session.headers.update({"X-Harbor-Csrf-Token": csrf_token})
returned_elements = response.json()
total_count = int(response.headers.get("X-Total-Count", 0))
if not total_count or total_count == len(returned_elements):
return returned_elements
# Get all elements...
elements = returned_elements
def get_next_page():
# 'Link': '</api/v2.0/projects?page=2&page_size=10>; rel="next"'
try:
next_page = response.headers["Link"]
except KeyError:
return None
if "next" not in next_page:
return None
if next_page.count(";") >= 2:
# Multiple references - usually pre and next
for page in next_page.split(", "):
if "next" in page:
next_page = page
break
next_page, _ = next_page.split(";")
return next_page[1:-1]
next_page = get_next_page()
while next_page:
response = self._session.get(
f"{self.base_url}{next_page}", auth=self._credentials
)
elements.extend(response.json())
next_page = get_next_page()
logger.debug("Received elements %d: %r", len(elements), elements)
return elements
def get_replication_policies(self):
for policy in self.get("api/v2.0/replication/policies"):
yield ReplicationPolicy.from_json(policy)
def get_replication_policy_json(self, name):
for policy in self.get_replication_policies():
if policy.name == name:
return policy._raw
else:
raise RuntimeError(f"No replication policy {name}")
def create_replication_policy(self, config):
address = self.base_url + "api/v2.0/replication/policies/"
response = self._session.post(address, auth=self._credentials, json=config)
try:
response.raise_for_status()
except Exception:
logger.debug("Creating a replication policy failed.")
logger.debug("Response headers: %s", response.headers)
raise
def update_replication_policy(self, policy_id, config):
address = self.base_url + f"api/v2.0/replication/policies/{policy_id}"
response = self._session.put(address, auth=self._credentials, json=config)
try:
response.raise_for_status()
except Exception:
logger.debug("Updating a replication policy failed.")
logger.debug("Response headers: %s", response.headers)
raise
def delete_replication_policy(self, id):
address = self.base_url + f"api/v2.0/replication/policies/{id}"
response = self._session.delete(address, auth=self._credentials)
response.raise_for_status()
def trigger_replication_policy_execution(self, id):
address = self.base_url + "api/v2.0/replication/executions/"
response = self._session.post(
address, auth=self._credentials, json={"policy_id": id}
)
try:
response.raise_for_status()
except Exception:
logger.debug("Triggering replication for rule #%s failed.", id)
logger.debug("Headers: %s", response.headers)
raise
def get_projects(self):
for project in self.get("api/v2.0/projects"):
yield HarborProject.from_json(project)
def get_project_json(self, name_or_id):
project = self.get(f"api/v2.0/projects/{name_or_id}")
if not project:
raise RuntimeError(f"No project {name_or_id}")
return project
def create_project(self, config):
address = self.base_url + "api/v2.0/projects"
response = self._session.post(address, auth=self._credentials, json=config)
logger.debug("Response headers: %s", response.headers)
logger.debug("Response body: %s", response.content)
response.raise_for_status()
try:
return response.json()
except json.decoder.JSONDecodeError:
return None
def is_project_deletable(self, name_or_id):
response = self.get(f"api/v2.0/projects/{name_or_id}/_deletable")
logger.debug("Deletable for %s: %s", name_or_id, response)
try:
return response["deletable"]
except KeyError:
try:
logger.debug(response["message"])
except KeyError: # no message
pass
return False
def remove_project_content(self, name):
repos = self.get(f"api/v2.0/projects/{name}/repositories")
for repo in repos:
logger.debug("Repo: %r", repo)
repo_name = repo["name"]
name_prefix = name + "/"
if repo_name.startswith(name_prefix):
repo_name = repo_name[len(name_prefix) :]
if "/" in repo_name:
# Double URL encoded
repo_name = quote(quote(repo_name, safe=""))
logger.debug("Deleting repository %s in %s", repo_name, name)
address = (
self.base_url + f"api/v2.0/projects/{name}/repositories/{repo_name}"
)
response = self._session.delete(address, auth=self._credentials)
logger.debug("Response headers: %s", response.headers)
response.raise_for_status()
def delete_project(self, name_or_id):
address = self.base_url + f"api/v2.0/projects/{name_or_id}"
response = self._session.delete(
address, auth=self._credentials, headers={"X-Is-Resource-Name": "true"}
)
response.raise_for_status()
def get_registry(self, name):
registries = self.get("api/v2.0/registries")
registry_names = set()
for registry in registries:
registry_name = registry["name"]
registry_names.add(registry_name)
if registry_name == name:
return registry
logger.info("Available registries: %s", ", ".join(registry_names))
raise ValueError(f"No registry {name!r} found")
if __name__ == "__main__":
main()