2020-02-14 09:11:38 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2020-03-10 07:55:55 +01:00
|
|
|
import time
|
2020-02-14 09:11:38 +01:00
|
|
|
import base
|
|
|
|
import v2_swagger_client
|
|
|
|
from v2_swagger_client.rest import ApiException
|
|
|
|
|
2021-10-22 09:39:19 +02:00
|
|
|
|
|
|
|
report_mime_types = [
|
|
|
|
'application/vnd.security.vulnerability.report; version=1.1',
|
|
|
|
'application/vnd.scanner.adapter.vuln.report.harbor+json; version=1.0',
|
|
|
|
]
|
|
|
|
|
2020-03-16 03:13:28 +01:00
|
|
|
class Artifact(base.Base, object):
|
|
|
|
def __init__(self):
|
|
|
|
super(Artifact,self).__init__(api_type = "artifact")
|
|
|
|
|
|
|
|
def list_artifacts(self, project_name, repo_name, **kwargs):
|
2022-01-19 11:26:09 +01:00
|
|
|
params = {}
|
|
|
|
if "with_accessory" in kwargs:
|
|
|
|
params["with_accessory"] = kwargs["with_accessory"]
|
|
|
|
return self._get_client(**kwargs).list_artifacts(project_name, repo_name, **params)
|
2020-03-16 03:13:28 +01:00
|
|
|
|
2020-11-03 08:32:13 +01:00
|
|
|
def get_reference_info(self, project_name, repo_name, reference, expect_status_code = 200, ignore_not_found = False,**kwargs):
|
2020-02-25 03:40:29 +01:00
|
|
|
params = {}
|
|
|
|
if "with_signature" in kwargs:
|
|
|
|
params["with_signature"] = kwargs["with_signature"]
|
2020-03-16 08:46:43 +01:00
|
|
|
if "with_tag" in kwargs:
|
|
|
|
params["with_tag"] = kwargs["with_tag"]
|
2020-03-10 07:55:55 +01:00
|
|
|
if "with_scan_overview" in kwargs:
|
|
|
|
params["with_scan_overview"] = kwargs["with_scan_overview"]
|
2021-10-22 09:39:19 +02:00
|
|
|
params["x_accept_vulnerabilities"] = ",".join(report_mime_types)
|
2020-11-03 08:32:13 +01:00
|
|
|
if "with_immutable_status" in kwargs:
|
|
|
|
params["with_immutable_status"] = kwargs["with_immutable_status"]
|
2022-01-19 11:26:09 +01:00
|
|
|
if "with_accessory" in kwargs:
|
|
|
|
params["with_accessory"] = kwargs["with_accessory"]
|
2020-07-21 03:45:13 +02:00
|
|
|
|
|
|
|
try:
|
2021-03-15 09:08:48 +01:00
|
|
|
data, status_code, _ = self._get_client(**kwargs).get_artifact_with_http_info(project_name, repo_name, reference, **params)
|
2020-11-03 08:32:13 +01:00
|
|
|
return data
|
2020-07-21 03:45:13 +02:00
|
|
|
except ApiException as e:
|
|
|
|
if e.status == 404 and ignore_not_found == True:
|
2020-11-03 08:32:13 +01:00
|
|
|
return None
|
|
|
|
else:
|
|
|
|
raise Exception("Failed to get reference, {} {}".format(e.status, e.body))
|
|
|
|
else:
|
|
|
|
base._assert_status_code(expect_status_code, status_code)
|
|
|
|
base._assert_status_code(200, status_code)
|
|
|
|
return None
|
2020-03-16 03:13:28 +01:00
|
|
|
|
|
|
|
def delete_artifact(self, project_name, repo_name, reference, expect_status_code = 200, expect_response_body = None, **kwargs):
|
|
|
|
try:
|
2021-03-15 09:08:48 +01:00
|
|
|
_, status_code, _ = self._get_client(**kwargs).delete_artifact_with_http_info(project_name, repo_name, reference)
|
2020-03-16 03:13:28 +01:00
|
|
|
except ApiException as e:
|
|
|
|
base._assert_status_code(expect_status_code, e.status)
|
|
|
|
if expect_response_body is not None:
|
|
|
|
base._assert_status_body(expect_response_body, e.body)
|
|
|
|
return
|
2020-11-03 08:32:13 +01:00
|
|
|
else:
|
|
|
|
base._assert_status_code(expect_status_code, status_code)
|
|
|
|
base._assert_status_code(200, status_code)
|
2020-03-16 03:13:28 +01:00
|
|
|
|
|
|
|
def get_addition(self, project_name, repo_name, reference, addition, **kwargs):
|
2021-03-15 09:08:48 +01:00
|
|
|
return self._get_client(**kwargs).get_addition_with_http_info(project_name, repo_name, reference, addition)
|
2020-02-25 03:40:29 +01:00
|
|
|
|
2020-12-16 11:06:41 +01:00
|
|
|
def add_label_to_reference(self, project_name, repo_name, reference, label_id, expect_status_code = 200, **kwargs):
|
2020-02-25 03:40:29 +01:00
|
|
|
label = v2_swagger_client.Label(id = label_id)
|
2020-12-16 11:06:41 +01:00
|
|
|
try:
|
2021-03-15 09:08:48 +01:00
|
|
|
body, status_code, _ = self._get_client(**kwargs).add_label_with_http_info(project_name, repo_name, reference, label)
|
2020-12-16 11:06:41 +01:00
|
|
|
except ApiException as e:
|
|
|
|
base._assert_status_code(expect_status_code, e.status)
|
|
|
|
else:
|
|
|
|
base._assert_status_code(expect_status_code, status_code)
|
|
|
|
base._assert_status_code(200, status_code)
|
|
|
|
return body
|
2020-02-21 10:08:27 +01:00
|
|
|
|
|
|
|
def copy_artifact(self, project_name, repo_name, _from, expect_status_code = 201, expect_response_body = None, **kwargs):
|
|
|
|
try:
|
2021-03-15 09:08:48 +01:00
|
|
|
data, status_code, _ = self._get_client(**kwargs).copy_artifact_with_http_info(project_name, repo_name, _from)
|
2020-02-21 10:08:27 +01:00
|
|
|
except ApiException as e:
|
|
|
|
base._assert_status_code(expect_status_code, e.status)
|
|
|
|
if expect_response_body is not None:
|
|
|
|
base._assert_status_body(expect_response_body, e.body)
|
|
|
|
return
|
2020-11-03 08:32:13 +01:00
|
|
|
else:
|
|
|
|
base._assert_status_code(expect_status_code, status_code)
|
|
|
|
base._assert_status_code(201, status_code)
|
|
|
|
return data
|
2020-03-16 08:46:43 +01:00
|
|
|
|
2020-07-21 03:45:13 +02:00
|
|
|
def create_tag(self, project_name, repo_name, reference, tag_name, expect_status_code = 201, ignore_conflict = False, **kwargs):
|
2020-03-16 08:46:43 +01:00
|
|
|
tag = v2_swagger_client.Tag(name = tag_name)
|
2020-07-21 03:45:13 +02:00
|
|
|
try:
|
2021-03-15 09:08:48 +01:00
|
|
|
_, status_code, _ = self._get_client(**kwargs).create_tag_with_http_info(project_name, repo_name, reference, tag)
|
2020-07-21 03:45:13 +02:00
|
|
|
except ApiException as e:
|
|
|
|
if e.status == 409 and ignore_conflict == True:
|
|
|
|
return
|
2020-12-16 11:06:41 +01:00
|
|
|
base._assert_status_code(expect_status_code, e.status)
|
|
|
|
|
2020-11-03 08:32:13 +01:00
|
|
|
else:
|
|
|
|
base._assert_status_code(expect_status_code, status_code)
|
2020-12-16 11:06:41 +01:00
|
|
|
base._assert_status_code(201, status_code)
|
2020-03-16 08:46:43 +01:00
|
|
|
|
|
|
|
def delete_tag(self, project_name, repo_name, reference, tag_name, expect_status_code = 200, **kwargs):
|
2020-11-03 08:32:13 +01:00
|
|
|
try:
|
2021-03-15 09:08:48 +01:00
|
|
|
_, status_code, _ = self._get_client(**kwargs).delete_tag_with_http_info(project_name, repo_name, reference, tag_name)
|
2020-11-03 08:32:13 +01:00
|
|
|
except ApiException as e:
|
|
|
|
base._assert_status_code(expect_status_code, e.status)
|
|
|
|
else:
|
|
|
|
base._assert_status_code(expect_status_code, status_code)
|
2020-12-16 11:06:41 +01:00
|
|
|
base._assert_status_code(200, status_code)
|
2020-03-10 07:55:55 +01:00
|
|
|
|
2022-01-19 11:26:09 +01:00
|
|
|
def list_accessories(self, project_name, repo_name, reference, **kwargs):
|
|
|
|
return self._get_client(**kwargs).list_accessories(project_name, repo_name, reference)
|
|
|
|
|
2020-03-10 07:55:55 +01:00
|
|
|
def check_image_scan_result(self, project_name, repo_name, reference, expected_scan_status = "Success", **kwargs):
|
|
|
|
timeout_count = 30
|
|
|
|
scan_status=""
|
|
|
|
while True:
|
|
|
|
time.sleep(5)
|
|
|
|
timeout_count = timeout_count - 1
|
|
|
|
if (timeout_count == 0):
|
|
|
|
break
|
|
|
|
artifact = self.get_reference_info(project_name, repo_name, reference, **kwargs)
|
2020-12-13 11:00:16 +01:00
|
|
|
if expected_scan_status in ["Not Scanned", "No Scan Overview"]:
|
|
|
|
if artifact.scan_overview is None:
|
|
|
|
if (timeout_count > 24):
|
|
|
|
continue
|
|
|
|
print("artifact is not scanned.")
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
raise Exception("Artifact should not be scanned {}.".format(artifact.scan_overview))
|
|
|
|
|
2021-10-22 09:39:19 +02:00
|
|
|
scan_status = ''
|
|
|
|
for mime_type in report_mime_types:
|
|
|
|
overview = artifact.scan_overview.get(mime_type)
|
|
|
|
if overview:
|
|
|
|
scan_status = overview.scan_status
|
|
|
|
|
2020-03-10 07:55:55 +01:00
|
|
|
if scan_status == expected_scan_status:
|
|
|
|
return
|
|
|
|
raise Exception("Scan image result is {}, not as expected {}.".format(scan_status, expected_scan_status))
|
2020-07-21 03:45:13 +02:00
|
|
|
|
|
|
|
def check_reference_exist(self, project_name, repo_name, reference, ignore_not_found = False, **kwargs):
|
|
|
|
artifact = self.get_reference_info( project_name, repo_name, reference, ignore_not_found=ignore_not_found, **kwargs)
|
|
|
|
return {
|
2020-11-03 08:32:13 +01:00
|
|
|
None: False,
|
|
|
|
}.get(artifact, True)
|
2020-10-22 03:55:38 +02:00
|
|
|
|
2020-11-10 10:33:09 +01:00
|
|
|
def waiting_for_reference_exist(self, project_name, repo_name, reference, ignore_not_found = True, period = 60, loop_count = 20, **kwargs):
|
2020-10-22 03:55:38 +02:00
|
|
|
_loop_count = loop_count
|
|
|
|
while True:
|
|
|
|
print("Waiting for reference {} round...".format(_loop_count))
|
|
|
|
_loop_count = _loop_count - 1
|
|
|
|
if (_loop_count == 0):
|
|
|
|
break
|
|
|
|
artifact = self.get_reference_info(project_name, repo_name, reference, ignore_not_found=ignore_not_found, **kwargs)
|
|
|
|
print("Returned artifact by get reference info:", artifact)
|
|
|
|
if artifact and artifact !=[]:
|
|
|
|
return artifact
|
|
|
|
time.sleep(period)
|
2020-11-03 08:32:13 +01:00
|
|
|
raise Exception("Reference is not exist {} {} {}.".format(project_name, repo_name, reference))
|