def verify_repository_pushable(self, project_access_list, system_ra_client): for project_access in project_access_list: print(r"project_access:", project_access) if project_access["check_list"][1]: #---repository:push--- repo = push_self_build_image_to_project(project_access["project_name"], harbor_server, system_ra_client["username"], system_ra_client["password"], "test_pushable"+base._random_name("repo"), "v6.8.1"+base._random_name("tag")) else: push_self_build_image_to_project(project_access["project_name"], harbor_server, system_ra_client["username"], system_ra_client["password"], "test_unpushable"+base._random_name("repo"), "v6.8.1"+base._random_name("tag"), expected_error_message = "unauthorized to access repository")
def testRegistryAPI(self): """ Test case: Catalog API to list all repositories by system admin Test step and expected result:G 1. Create user Alice; 2. Create 1 new private projects project_Alice; 3. Push 3 images to project_Alice and Add 3 tags to the 3rd image 4. Call the image_list_tag API 5. Call the catalog API using admin account without pagination, can get all 3 images 5.1 Call the catalog API using admin account with pagination n=1, page=2, twice to get all 3 images. 5.2 Call the catalog API using Alice account, no repos should be found. Tear down: 1. Delete Alice's repository; 2. Delete Alice's project; 3. Delete user Alice. """ url = ADMIN_CLIENT["endpoint"] user_common_password = "******" #1. Create user Alice and Luca; TestProjects.user_Alice_id, user_Alice_name = self.user.create_user(user_password = user_common_password, **ADMIN_CLIENT) USER_ALICE_CLIENT=dict(endpoint = url, username = user_Alice_name, password = user_common_password) #2. Create 2 new private projects project_Alice and project_Luca; TestProjects.project_Alice_id, TestProjects.project_Alice_name = self.project.create_project(metadata = {"public": "false"}, **USER_ALICE_CLIENT) #3. Push 3 images to project_Alice and Add 3 tags to the 3rd image. src_tag = "latest" image_a = "image_a" TestProjects.repo_a, tag_a = push_self_build_image_to_project(TestProjects.project_Alice_name, harbor_server, user_Alice_name, user_common_password, image_a, src_tag) image_b = "image_b" TestProjects.repo_b, tag_b = push_self_build_image_to_project(TestProjects.project_Alice_name, harbor_server, user_Alice_name, user_common_password, image_b, src_tag) image_c = "image_c" TestProjects.repo_c, tag_c = push_self_build_image_to_project(TestProjects.project_Alice_name, harbor_server, user_Alice_name, user_common_password, image_c, src_tag) create_tags = ["1.0","2.0","3.0"] for tag in create_tags: self.artifact.create_tag(TestProjects.project_Alice_name, image_c, tag_c, tag, **USER_ALICE_CLIENT) #4. Call the image_list_tags API tags = list_image_tags(harbor_server,TestProjects.repo_c,user_Alice_name,user_common_password) for tag in create_tags: self.assertTrue(tags.count(tag)>0, "Expect tag: %s is not listed"%tag) page_tags = list_image_tags(harbor_server,TestProjects.repo_c,user_Alice_name,user_common_password,len(tags)/2+1) page_tags += list_image_tags(harbor_server,TestProjects.repo_c,user_Alice_name,user_common_password,len(tags)/2+1,tags[int(len(tags)/2)]) for tag in create_tags: self.assertTrue(page_tags.count(tag)>0, "Expect tag: %s is not listed by the pagination query"%tag) #5. Call the catalog API; repos = list_repositories(harbor_server,admin_user,admin_pwd) self.assertTrue(repos.count(TestProjects.repo_a)>0 and repos.count(TestProjects.repo_b)>0 and repos.count(TestProjects.repo_c)>0, "Expected repo not found") for repo in [TestProjects.repo_a,TestProjects.repo_b,TestProjects.repo_c]: self.assertTrue(repos.count(repo)>0,"Expected repo: %s is not listed"%repo) page_repos = list_repositories(harbor_server,admin_user,admin_pwd,len(repos)/2+1) page_repos += list_repositories(harbor_server,admin_user,admin_pwd,len(repos)/2+1,repos[int(len(repos)/2)]) for repo in [TestProjects.repo_a,TestProjects.repo_b,TestProjects.repo_c]: self.assertTrue(page_repos.count(repo)>0,"Expected repo: %s is not listed by the pagination query"%repo) null_repos = list_repositories(harbor_server,user_Alice_name,user_common_password) self.assertEqual(null_repos, "")
def push_artifact_index(self, project, name, tag, **kwargs): image_a = "alpine" image_b = "busybox" repo_name_a, tag_a = push_self_build_image_to_project(project, args.endpoint, 'admin', 'Harbor12345', image_a, "latest") repo_name_b, tag_b = push_self_build_image_to_project(project, args.endpoint, 'admin', 'Harbor12345', image_b, "latest") manifests = [args.endpoint+"/"+repo_name_a+":"+tag_a, args.endpoint+"/"+repo_name_b+":"+tag_b] index = args.endpoint+"/"+project+"/"+name+":"+tag docker_manifest_push_to_harbor(index, manifests, args.endpoint, 'admin', 'Harbor12345', cfg_file = args.libpath + "/update_docker_cfg.sh")
def do_data_creation(): harborAPI = HarborAPI() set_url(version=args.version) harborAPI.get_ca(version=args.version) for user in data["users"]: harborAPI.create_user(user["name"]) for user in data["admin"]: harborAPI.set_user_admin(user["name"], version=args.version) # Make sure to create endpoint first, it's for proxy cache project creation. for endpoint in data["endpoint"]: print("endpoint:", endpoint) harborAPI.add_endpoint(endpoint["url"], endpoint["name"], endpoint["user"], endpoint["pass"], endpoint["insecure"], endpoint["type"], version=args.version) for distribution in data["distributions"]: harborAPI.add_distribution(distribution, version=args.version) harborAPI.populate_projects(version=args.version) harborAPI.populate_quotas(version=args.version) harborAPI.push_artifact_index( data["projects"][0]["name"], data["projects"][0]["artifact_index"]["name"], data["projects"][0]["artifact_index"]["tag"], version=args.version) #pull_image("busybox", "redis", "haproxy", "alpine", "httpd:2") push_self_build_image_to_project(data["projects"][0]["name"], args.endpoint, 'admin', 'Harbor12345', "busybox", "latest") push_signed_image("alpine", data["projects"][0]["name"], "latest") for replicationrule in data["replicationrule"]: harborAPI.add_replication_rule(replicationrule, version=args.version) harborAPI.update_interrogation_services( data["interrogation_services"]["cron"], version=args.version) harborAPI.update_systemsetting( data["configuration"]["emailsetting"]["emailfrom"], data["configuration"]["emailsetting"]["emailserver"], float(data["configuration"]["emailsetting"]["emailport"]), data["configuration"]["emailsetting"]["emailuser"], data["configuration"]["projectcreation"], data["configuration"]["selfreg"], float(data["configuration"]["token"]), float(data["configuration"]["robot_token"]), version=args.version) harborAPI.add_sys_allowlist(data["configuration"]["deployment_security"], version=args.version)
def verify_repository_unpushable(self, project_access_list, system_ra_client, expected_login_error_message = "unauthorized: authentication required", expected_error_message = ""): for project_access in project_access_list: #---repository:push--- push_self_build_image_to_project( project_access["project_name"], harbor_server, system_ra_client["username"], system_ra_client["password"], "test_unpushable"+base._random_name("repo"), "v6.8.1"+base._random_name("tag"), expected_login_error_message = expected_login_error_message, expected_error_message = expected_error_message )
def populate_quotas(self, **kwargs): for quotas in data["quotas"]: self.create_project(quotas, version=args.version) push_self_build_image_to_project(quotas["name"], args.endpoint, 'admin', 'Harbor12345', quotas["name"], "latest", size=quotas["size"])
def testSystemLevelScanALL(self): """ Test case: System level Scan All Test step and expected result: 1. Create user Alice and Luca; 2. Create 2 new private projects project_Alice and project_Luca; 3. Push a image to project_Alice and push another image to project_Luca; 4. Trigger scan all event; 5. Check if image in project_Alice and another image in project_Luca were both scanned. Tear down: 1. Delete Alice's repository and Luca's repository; 2. Delete Alice's project and Luca's project; 3. Delete user Alice and Luca. """ url = ADMIN_CLIENT["endpoint"] user_common_password = "******" #1. Create user Alice and Luca; TestScanAll.user_Alice_id, user_Alice_name = self.user.create_user(user_password = user_common_password, **ADMIN_CLIENT) TestScanAll.user_Luca_id, user_Luca_name = self.user.create_user(user_password = user_common_password, **ADMIN_CLIENT) USER_ALICE_CLIENT=dict(endpoint = url, username = user_Alice_name, password = user_common_password, with_scan_overview = True) USER_LUCA_CLIENT=dict(endpoint = url, username = user_Luca_name, password = user_common_password, with_scan_overview = True) #2. Create 2 new private projects project_Alice and project_Luca; TestScanAll.project_Alice_id, TestScanAll.project_Alice_name = self.project.create_project(metadata = {"public": "false"}, **USER_ALICE_CLIENT) TestScanAll.project_Luca_id, TestScanAll.project_Luca_name = self.project.create_project(metadata = {"public": "false"}, **USER_LUCA_CLIENT) #3. Push a image to project_Alice and push another image to project_Luca; #Note: Please make sure that this Image has never been pulled before by any other cases, # so it is a not-scanned image rigth after repository creation. #image = "tomcat" image_a = "mariadb" src_tag = "latest" #3.1 Push a image to project_Alice; TestScanAll.repo_Alice_name, tag_Alice = push_self_build_image_to_project(TestScanAll.project_Alice_name, harbor_server, user_Alice_name, user_common_password, image_a, src_tag) #Note: Please make sure that this Image has never been pulled before by any other cases, # so it is a not-scanned image rigth after repository creation. image_b = "httpd" src_tag = "latest" #3.2 push another image to project_Luca; TestScanAll.repo_Luca_name, tag_Luca = push_self_build_image_to_project(TestScanAll.project_Luca_name, harbor_server, user_Luca_name, user_common_password, image_b, src_tag) #4. Trigger scan all event; self.system.scan_now(**ADMIN_CLIENT) self.system.wait_until_scans_all_finish(**ADMIN_CLIENT) #5. Check if image in project_Alice and another image in project_Luca were both scanned. self.artifact.check_image_scan_result(TestScanAll.project_Alice_name, image_a, tag_Alice, **USER_ALICE_CLIENT) self.artifact.check_image_scan_result(TestScanAll.project_Luca_name, image_b, tag_Luca, **USER_LUCA_CLIENT)
def testScanImageArtifact(self): """ Test case: Scan An Image Artifact Test step and expected result: 1. Create a new user(UA); 2. Create a new private project(PA) by user(UA); 3. Add user(UA) as a member of project(PA) with project-admin role; 4. Get private project of user(UA), user(UA) can see only one private project which is project(PA); 5. Create a new repository(RA) and tag(TA) in project(PA) by user(UA); 6. Send scan image command and get tag(TA) information to check scan result, it should be finished; 7. Swith Scanner; 8. Send scan another image command and get tag(TA) information to check scan result, it should be finished. Tear down: 1. Delete repository(RA) by user(UA); 2. Delete project(PA); 3. Delete user(UA); """ #4. Get private project of user(UA), user(UA) can see only one private project which is project(PA); self.project.projects_should_exist(dict(public=False), expected_count = 1, expected_project_id = self.project_id, **self.USER_CLIENT) #Note: Please make sure that this Image has never been pulled before by any other cases, # so it is a not-scanned image right after repository creation. image = "docker" src_tag = "1.13" #5. Create a new repository(RA) and tag(TA) in project(PA) by user(UA); self.repo_name1, tag = push_self_build_image_to_project(self.project_name, harbor_server, self.user_name, self.user_password, image, src_tag) #6. Send scan image command and get tag(TA) information to check scan result, it should be finished; self.scan.scan_artifact(self.project_name, self.repo_name1.split('/')[1], tag, **self.USER_CLIENT) self.artifact.check_image_scan_result(self.project_name, image, tag, **self.USER_CLIENT) self.do_tearDown()
def testAddSysLabelToRepo(self): """ Test case: Add Global Label To Tag Test step and expected result: 1. Create a new user(UA); 2. Create a new private project(PA) by user(UA); 3. Add user(UA) as a member of project(PA) with project-admin role; 4. Get private project of user(UA), user(UA) can see only one private project which is project(PA); 5. Create a new repository(RA) and tag(TA) in project(PA) by user(UA); 6. Create a new label(LA) in project(PA) by admin;; 7. Add this system global label to repository(RA)/tag(TA); Tear down: 1. Delete repository(RA) by user(UA); 2. Delete project(PA); 3. Delete user(UA); 4. Delete label(LA). """ url = ADMIN_CLIENT["endpoint"] user_001_password = "******" #1. Create user-001 TestProjects.user_add_g_lbl_id, user_add_g_lbl_name = self.user.create_user( user_password=user_001_password, **ADMIN_CLIENT) TestProjects.USER_add_g_lbl_CLIENT = dict(endpoint=url, username=user_add_g_lbl_name, password=user_001_password) #2. Create private project-001 TestProjects.project_add_g_lbl_id, TestProjects.project_add_g_lbl_name = self.project.create_project( metadata={"public": "false"}, **ADMIN_CLIENT) #3. Add user-001 as a member of project-001 with project-admin role self.project.add_project_members( TestProjects.project_add_g_lbl_id, user_id=TestProjects.user_add_g_lbl_id, **ADMIN_CLIENT) #4. Get private project of user(UA), user(UA) can see only one private project which is project(PA); self.project.projects_should_exist( dict(public=False), expected_count=1, expected_project_id=TestProjects.project_add_g_lbl_id, **TestProjects.USER_add_g_lbl_CLIENT) #5. Create a new repository(RA) and tag(TA) in project(PA) by user(UA); TestProjects.repo_name, tag = push_self_build_image_to_project( TestProjects.project_add_g_lbl_name, harbor_server, user_add_g_lbl_name, user_001_password, "test_sys_label", "latest") #6. Create a new label(LA) in project(PA) by admin; TestProjects.label_id, _ = self.label.create_label(**ADMIN_CLIENT) #7. Add this system global label to repository(RA)/tag(TA). self.artifact.add_label_to_reference( TestProjects.project_add_g_lbl_name, TestProjects.repo_name.split('/')[1], tag, int(TestProjects.label_id), **TestProjects.USER_add_g_lbl_CLIENT)
def testScanImageInPublicProject(self): """ Test case: Scan An Image Artifact In Public Project Test step and expected result: 1. Create a new user(UA); 2. Create a new public project(PA) by user(UA); 3. Add user(UA) as a member of project(PA) with project-admin role; 4. Create a new repository(RA) and tag(TA) in project(PA) by user(UA); 5. Send scan image command without credential (anonymous), the API response should be 401; 6. Create a new user(UB) which is non member of the project(PA); 7. Send scan image command with credential of the new created user(UB), the API response should be 403; 8. Delete user(UB); 9. Send scan image command with credential of the user(UA) and get tag(TA) information to check scan result, it should be finished; 10. Delete repository(RA) by user(UA); 11. Delete project(PA); 12. Delete user(UA); """ password = '******' # nosec with created_user(password) as (user_id, username): with created_project(metadata={"public": "true"}, user_id=user_id) as (_, project_name): image, src_tag = "docker", "1.13" full_name, tag = push_self_build_image_to_project( project_name, harbor_server, username, password, image, src_tag) repo_name = full_name.split('/')[1] # scan image with anonymous user self.scan.scan_artifact(project_name, repo_name, tag, expect_status_code=401, username=None, password=None) with created_user(password) as (_, username1): # scan image with non project memeber self.scan.scan_artifact(project_name, repo_name, tag, expect_status_code=403, username=username1, password=password) self.scan.scan_artifact(project_name, repo_name, tag, username=username, password=password) self.artifact.check_image_scan_result(project_name, image, tag, username=username, password=password, with_scan_overview=True) self.repo.delete_repository(project_name, repo_name)
def testUserViewLogs(self): """ Test case: User View Logs Test step and expected result: 1. Create a new user(UA); 2. Create a new project(PA) by user(UA), in project(PA), there should be 1 'create' log record;; 3. Push a new image(IA) in project(PA) by admin, in project(PA), there should be 1 'push' log record;; 4. Delete repository(RA) by user(UA), in project(PA), there should be 1 'delete' log record;; Tear down: 1. Delete project(PA); 2. Delete user(UA). """ test_result= TestResult() url = ADMIN_CLIENT["endpoint"] admin_name = ADMIN_CLIENT["username"] admin_password = ADMIN_CLIENT["password"] user_content_trust_password = "******" #1. Create a new user(UA); TestProjects.user_user_view_logs_id, user_user_view_logs_name = self.user.create_user(user_password = user_content_trust_password, **ADMIN_CLIENT) TestProjects.USER_USER_VIEW_LOGS_CLIENT=dict(endpoint = url, username = user_user_view_logs_name, password = user_content_trust_password) #2.1 Create a new project(PA) by user(UA); TestProjects.project_user_view_logs_id, project_user_view_logs_name = self.project.create_project(metadata = {"public": "false"}, **TestProjects.USER_USER_VIEW_LOGS_CLIENT) time.sleep(2) #2.2 In project(PA), there should be 1 'create' log record; operation = "create" log_count = self.projectv2.filter_project_logs(project_user_view_logs_name, user_user_view_logs_name, project_user_view_logs_name, "project", operation, **TestProjects.USER_USER_VIEW_LOGS_CLIENT) if log_count != 1: test_result.add_test_result("1 - Failed to get log with user:{}, resource:{}, resource_type:{} and operation:{}, expect count 1, but actual is {}.". format(user_user_view_logs_name, project_user_view_logs_name, "project", operation, log_count)) #3.1 Push a new image(IA) in project(PA) by admin; repo_name, tag = push_self_build_image_to_project(project_user_view_logs_name, harbor_server, admin_name, admin_password, "tomcat", "latest") time.sleep(2) #3.2 In project(PA), there should be 1 'push' log record; operation = "create" log_count = self.projectv2.filter_project_logs(project_user_view_logs_name, admin_name, r'{}:{}'.format(repo_name, tag), "artifact", operation, **TestProjects.USER_USER_VIEW_LOGS_CLIENT) if log_count != 1: test_result.add_test_result("2 - Failed to get log with user:{}, resource:{}, resource_type:{} and operation:{}, expect count 1, but actual is {}.". format(user_user_view_logs_name, project_user_view_logs_name, "artifact", operation, log_count)) #4.1 Delete repository(RA) by user(UA); self.repo.delete_repoitory(project_user_view_logs_name, repo_name.split('/')[1], **TestProjects.USER_USER_VIEW_LOGS_CLIENT) time.sleep(6) #4.2 In project(PA), there should be 1 'delete' log record; operation = "delete" log_count = self.projectv2.filter_project_logs(project_user_view_logs_name, user_user_view_logs_name, repo_name, "repository", operation, **TestProjects.USER_USER_VIEW_LOGS_CLIENT) if log_count != 1: test_result.add_test_result("5 - Failed to get log with user:{}, resource:{}, resource_type:{} and operation:{}, expect count 1, but actual is {}.". format(user_user_view_logs_name, project_user_view_logs_name, "repository", operation, log_count)) test_result.get_final_result()
def testDelRepo(self): """ Test case: Delete a repository Test step and expected result: 1. Create a new user(UA); 2. Create a new project(PA) by user(UA); 3. Create a new repository(RA) in project(PA) by user(UA); 4. Get repository in project(PA), there should be one repository which was created by user(UA); 5. Delete repository(RA) by user(UA); 6. Get repository by user(UA), it should get nothing; Tear down: 1. Delete project(PA); 2. Delete user(UA). """ url = ADMIN_CLIENT["endpoint"] user_del_repo_password = "******" #1. Create a new user(UA); TestProjects.user_del_repo_id, user_del_repo_name = self.user.create_user( user_password=user_del_repo_password, **ADMIN_CLIENT) TestProjects.USER_del_repo_CLIENT = dict( endpoint=url, username=user_del_repo_name, password=user_del_repo_password) #2. Create a new project(PA) by user(UA); TestProjects.project_del_repo_id, TestProjects.project_del_repo_name = self.project.create_project( metadata={"public": "false"}, **TestProjects.USER_del_repo_CLIENT) #3. Create a new repository(RA) in project(PA) by user(UA); repo_name, _ = push_self_build_image_to_project( TestProjects.project_del_repo_name, harbor_server, 'admin', 'Harbor12345', "test_del_repo", "latest", size=512) #4. Get repository in project(PA), there should be one repository which was created by user(UA); repo_data = self.repo.list_repositories( TestProjects.project_del_repo_name, **TestProjects.USER_del_repo_CLIENT) _assert_status_code(repo_name, repo_data[0].name) #5. Delete repository(RA) by user(UA); self.repo.delete_repository(TestProjects.project_del_repo_name, repo_name.split('/')[1], **TestProjects.USER_del_repo_CLIENT) #6. Get repository by user(UA), it should get nothing; repo_data = self.repo.list_repositories( TestProjects.project_del_repo_name, **TestProjects.USER_del_repo_CLIENT) _assert_status_code(len(repo_data), 0)
def testCreateDeleteTag(self): """ Test case: Create/Delete tag Test step and expected result: 1. Create a new user(UA); 2. Create a new project(PA) by user(UA); 3. Push an image(IA) to Harbor by docker successfully; 4. Create a tag(1.0) for the image(IA); 5. Get the image(latest) from Harbor successfully; 6. Verify the image(IA) contains tag named 1.0; 7. Delete the tag(1.0) from image(IA); 8. Get the image(IA) from Harbor successfully; 9. Verify the image(IA) contains no tag named 1.0; Tear down: 1. Delete repository(RA,IA) by user(UA); 2. Delete project(PA); 3. Delete user(UA). """ #1. Create a new user(UA); TestProjects.user_id, user_name = self.user.create_user(user_password = self.user_password, **ADMIN_CLIENT) TestProjects.USER_CLIENT=dict(with_tag = True, endpoint = self.url, username = user_name, password = self.user_password) #2. Create a new project(PA) by user(UA); TestProjects.project_id, TestProjects.project_name = self.project.create_project(metadata = {"public": "false"}, **TestProjects.USER_CLIENT) #3. Push an image(IA) to Harbor by docker successfully; repo_name, tag = push_self_build_image_to_project(TestProjects.project_name, harbor_server, 'admin', 'Harbor12345', self.repo_name, "latest") #4. Create a tag(1.0) for the image(IA) self.artifact.create_tag(TestProjects.project_name, self.repo_name, tag, "1.0",**TestProjects.USER_CLIENT) #5. Get the image(IA) from Harbor successfully; artifact = self.artifact.get_reference_info(TestProjects.project_name, self.repo_name, tag, **TestProjects.USER_CLIENT) #6. Verify the image(IA) contains tag named 1.0; self.assertEqual(artifact.tags[0].name, "1.0") self.assertEqual(artifact.tags[1].name, tag) #7. Delete the tag(1.0) from image(IA); self.artifact.delete_tag(TestProjects.project_name, self.repo_name, tag, "1.0",**TestProjects.USER_CLIENT) #8. Get the image(latest) from Harbor successfully; artifact = self.artifact.get_reference_info(TestProjects.project_name, self.repo_name, tag, **TestProjects.USER_CLIENT) #9. Verify the image(IA) contains no tag named 1.0; self.assertEqual(artifact.tags[0].name, tag)
def testProjectLevelPolicyContentTrust(self): """ Test case: Project Level Policy Content Trust Test step and expected result: 1. Create a new user(UA); 2. Create a new project(PA) by user(UA); 3. Push a new image(IA) in project(PA) by admin; 4. Image(IA) should exist; 5. Pull image(IA) successfully; 6. Enable content trust in project(PA) configuration; 7. Pull image(IA) failed and the reason is "The image is not signed in Notary". Tear down: 1. Delete repository(RA) by user(UA); 2. Delete project(PA); 3. Delete user(UA); """ url = ADMIN_CLIENT["endpoint"] image = "test_content_trust" user_content_trust_password = "******" #1. Create a new user(UA); TestProjects.user_content_trust_id, user_content_trust_name = self.user.create_user( user_password=user_content_trust_password, **ADMIN_CLIENT) TestProjects.USER_CONTENT_TRUST_CLIENT = dict( endpoint=url, username=user_content_trust_name, password=user_content_trust_password) #2. Create a new project(PA) by user(UA); TestProjects.project_content_trust_id, TestProjects.project_content_trust_name = self.project.create_project( metadata={"public": "false"}, **TestProjects.USER_CONTENT_TRUST_CLIENT) #3. Push a new image(IA) in project(PA) by admin; TestProjects.repo_name, tag = push_self_build_image_to_project( TestProjects.project_content_trust_name, harbor_server, ADMIN_CLIENT["username"], ADMIN_CLIENT["password"], image, "latest") #4. Image(IA) should exist; artifact = self.artifact.get_reference_info( TestProjects.project_content_trust_name, image, tag, **TestProjects.USER_CONTENT_TRUST_CLIENT) self.assertEqual(artifact.tags[0].name, tag) docker_image_clean_all() #5. Pull image(IA) successfully; pull_harbor_image(harbor_server, ADMIN_CLIENT["username"], ADMIN_CLIENT["password"], TestProjects.repo_name, tag) self.project.get_project(TestProjects.project_content_trust_id) #6. Enable content trust in project(PA) configuration; self.project.update_project(TestProjects.project_content_trust_id, metadata={"enable_content_trust": "true"}, **TestProjects.USER_CONTENT_TRUST_CLIENT) self.project.get_project(TestProjects.project_content_trust_id) #7. Pull image(IA) failed and the reason is "The image is not signed in Notary". docker_image_clean_all() pull_harbor_image( harbor_server, ADMIN_CLIENT["username"], ADMIN_CLIENT["password"], TestProjects.repo_name, tag, expected_error_message="The image is not signed in Notary")
def testManageProjectMember(self): """ Test case: Manage Project members Test step and expected result: 1. Create user Alice, Bob, Carol; 2. Create private project(Alice) by Alice, Add a repository to project(Alice) by Alice; 3. Bob is not a member of project(Alice); 4. Alice Add Bob as a guest member of project(Alice), Check Bob is a guest member of project(Alice); 5. Update role of Bob to developer of project(Alice), Check Bob is developer member of project(Alice); 6. Update role of Bob to admin member of project(Alice), Check Bob is admin member of project(Alice); 7. Bob add Carol to project(Alice) as a guest member, Carol is a member of project(Alice) as a guest; 8. Alice delete Bob from project(Alice), Bob is no longer a member of project(Alice) and Bob can see project(Alice), Carol is still a member of project(Alice) as a guest. Tear down: 1. Delete repository(RA) by admin; 2. Delete project(Alice); 3. Delete user Alice, Bob and Carol. """ url = ADMIN_CLIENT["endpoint"] user_alice_password = "******" user_bob_password = "******" user_carol_password = "******" #1.1 Create user Alice TestProjects.user_alice_id, user_alice_name = self.user.create_user(user_password = user_alice_password, **ADMIN_CLIENT) USER_ALICE_CLIENT=dict(endpoint = url, username = user_alice_name, password = user_alice_password) #1.2 Create user Bob TestProjects.user_bob_id, user_bob_name = self.user.create_user(user_password = user_bob_password, **ADMIN_CLIENT) USER_BOB_CLIENT=dict(endpoint = url, username = user_bob_name, password = user_bob_password) #1.3 Create user Carol TestProjects.user_carol_id, user_carol_name = self.user.create_user(user_password = user_carol_password, **ADMIN_CLIENT) #2.1 Create private project(PA) by Alice TestProjects.project_alice_id, TestProjects.project_alice_name = self.project.create_project(metadata = {"public": "false"}, **USER_ALICE_CLIENT) #2.2 Add a repository to project(PA) by Alice TestProjects.repo_name, _ = push_self_build_image_to_project(TestProjects.project_alice_name, harbor_server, user_alice_name, user_alice_password, "hello-world", "latest") #3. Bob is not a member of project(PA); self.project.check_project_member_not_exist(TestProjects.project_alice_id, user_bob_name, **USER_ALICE_CLIENT) #4.1 Alice Add Bob as a guest member of project(PA) member_id_bob = self.project.add_project_members(TestProjects.project_alice_id, user_id=TestProjects.user_bob_id, member_role_id = 3, **USER_ALICE_CLIENT) #4.2 Check Bob is a guest member of project(PA) self.project.check_project_members_exist(TestProjects.project_alice_id, user_bob_name, expected_member_role_id = 3, user_name = user_bob_name, user_password = user_bob_password, **USER_ALICE_CLIENT) #5.1 Update role of Bob to developer of project(PA) self.project.update_project_member_role(TestProjects.project_alice_id, member_id_bob, 2, **USER_ALICE_CLIENT) #5.2 Check Bob is developer member of project(PA) self.project.check_project_members_exist(TestProjects.project_alice_id, user_bob_name, expected_member_role_id = 2, user_name = user_bob_name, user_password = user_bob_password, **USER_ALICE_CLIENT) #6.1 Update role of Bob to admin member of project(PA) self.project.update_project_member_role(TestProjects.project_alice_id, member_id_bob, 1, **USER_ALICE_CLIENT) #6.2 Check Bob is admin member of project(PA) self.project.check_project_members_exist(TestProjects.project_alice_id, user_bob_name, expected_member_role_id = 1, user_name = user_bob_name, user_password = user_bob_password, **USER_ALICE_CLIENT) #7.1 Bob add Carol to project(PA) as a guest member. self.project.add_project_members(TestProjects.project_alice_id, TestProjects.user_carol_id, member_role_id = 3, **USER_BOB_CLIENT) #7.2 Carol is a member of project(PA) as a guest. self.project.check_project_members_exist(TestProjects.project_alice_id, user_carol_name, expected_member_role_id = 3, user_name = user_carol_name, user_password = user_carol_password, **USER_ALICE_CLIENT) #8.1 Alice delete Bob from project(PA). self.project.delete_project_member(TestProjects.project_alice_id, member_id_bob, **USER_ALICE_CLIENT) #8.2 Bob is no longer a member of project(PA) and Bob can see project(PA). self.project.check_project_member_not_exist(TestProjects.project_alice_id, user_bob_name, **USER_ALICE_CLIENT) #8.3 Carol is still a member of project(PA) as a guest. self.project.check_project_members_exist(TestProjects.project_alice_id, user_carol_name, expected_member_role_id = 3, user_name = user_carol_name, user_password = user_carol_password, **USER_ALICE_CLIENT)
def testCosignArtifact(self): """ Test case: Cosign Artifact API Test step and expected result: 1. Create a new user(UA); 2. Create a new project(PA) by user(UA); 3. Push a new image(IA) in project(PA) by user(UA); 4. Verify that the image (IA) is not signed by cosign; 5. Sign image(IA) with cosign; 6. Verify that the image (IA) is signed by cosign; Tear down: 1. Delete project(PA); 2. Delete user(UA). """ url = ADMIN_CLIENT["endpoint"] user_password = "******" # 1. Create user(UA) TestCosign.user_id, user_name = self.user.create_user( user_password=user_password, **ADMIN_CLIENT) TestCosign.user_client = dict(endpoint=url, username=user_name, password=user_password, with_accessory=True) # 2.1. Create private project(PA) by user(UA) TestCosign.project_id, TestCosign.project_name = self.project.create_project( metadata={"public": "false"}, **TestCosign.user_client) # 2.2. Get private project of uesr-001, uesr-001 can see only one private project which is project-001 self.project.projects_should_exist( dict(public=False), expected_count=1, expected_project_id=TestCosign.project_id, **TestCosign.user_client) # 3. Push a new image(IA) in project(PA) by user(UA) TestCosign.repo_name, tag = push_self_build_image_to_project( TestCosign.project_name, harbor_server, user_name, user_password, self.image, self.tag) # 4.1. Verify list_artifacts API; artifact_list = self.artifact.list_artifacts(TestCosign.project_name, self.image, **TestCosign.user_client) first_artifact = artifact_list[0] artifact_reference = first_artifact.digest self.assertTrue(len(artifact_list) == 1) self.assertIsNone(artifact_list[0].accessories) # 4.2. Verify get_reference_info API; artifact_info = self.artifact.get_reference_info( TestCosign.project_name, self.image, artifact_reference, **TestCosign.user_client) self.assertIsNone(artifact_info.accessories) # 4.3. Verify list_accessories API; accessory_list = self.artifact.list_accessories( TestCosign.project_name, self.image, artifact_reference, **TestCosign.user_client) self.assertTrue(len(accessory_list) == 0) # 5.1. Generate cosign key pair; cosign.generate_key_pair() # 5.2. Generate cosign key pair; docker_api.docker_login_cmd(harbor_server, user_name, user_password, enable_manifest=False) cosign.sign_artifact("{}/{}/{}:{}".format(harbor_server, TestCosign.project_name, self.image, self.tag)) # 6.1. Verify list_artifacts API; artifact_list = self.artifact.list_artifacts(TestCosign.project_name, self.image, **TestCosign.user_client) self.assertTrue(len(artifact_list) == 1) first_artifact = artifact_list[0] self.assertTrue(len(first_artifact.accessories) == 1) first_accessory = first_artifact.accessories[0] self.assertEqual(first_accessory.type, self.expect_accessory_type) accessory_reference = first_accessory.digest # 6.2. Verify get_reference_info API; artifact_info = self.artifact.get_reference_info( TestCosign.project_name, self.image, artifact_reference, **TestCosign.user_client) self.assertEqual(artifact_info.accessories[0].type, self.expect_accessory_type) # 6.3. Verify list_accessories API; accessory_list = self.artifact.list_accessories( TestCosign.project_name, self.image, artifact_reference, **TestCosign.user_client) self.assertTrue(len(accessory_list) == 1) self.assertEqual(accessory_list[0].type, self.expect_accessory_type) # 6.4. Verify list_accessories API; accessory_info = self.artifact.get_reference_info( TestCosign.project_name, self.image, accessory_reference, **TestCosign.user_client) self.assertEqual(accessory_info.digest, accessory_reference)
def testRobotAccount(self): """ Test case: Robot Account Test step and expected result: 1. Create user(UA); 2. Create private project(PA), private project(PB) and public project(PC) by user(UA); 3. Push image(ImagePA) to project(PA), image(ImagePB) to project(PB) and image(ImagePC) to project(PC) by user(UA); 4. Create a new robot account(RA) with pull and push privilige in project(PA) by user(UA); 5. Check robot account info, it should has both pull and push priviliges; 6. Pull image(ImagePA) from project(PA) by robot account(RA), it must be successful; 7. Push image(ImageRA) to project(PA) by robot account(RA), it must be successful; 8. Push image(ImageRA) to project(PB) by robot account(RA), it must be not successful; 9. Pull image(ImagePB) from project(PB) by robot account(RA), it must be not successful; 10. Pull image from project(PC), it must be successful; 11. Push image(ImageRA) to project(PC) by robot account(RA), it must be not successful; 12. Update action property of robot account(RA); 13. Pull image(ImagePA) from project(PA) by robot account(RA), it must be not successful; 14. Push image(ImageRA) to project(PA) by robot account(RA), it must be not successful; 15. Delete robot account(RA), it must be not successful. Tear down: 1. Delete repository(RA) by user(UA); 2. Delete project(PA); 3. Delete user(UA). """ url = ADMIN_CLIENT["endpoint"] admin_name = ADMIN_CLIENT["username"] admin_password = ADMIN_CLIENT["password"] user_ra_password = "******" image_project_a = "haproxy" image_project_b = "hello-world" image_project_c = "httpd" image_robot_account = "alpine" tag = "latest" #1. Create user(UA);" TestProjects.user_ra_id, user_ra_name = self.user.create_user(user_password = user_ra_password, **ADMIN_CLIENT) TestProjects.USER_RA_CLIENT=dict(endpoint = url, username = user_ra_name, password = user_ra_password) #2. Create private project(PA), private project(PB) and public project(PC) by user(UA); TestProjects.project_ra_id_a, TestProjects.project_ra_name_a = self.project.create_project(metadata = {"public": "false"}, **TestProjects.USER_RA_CLIENT) TestProjects.project_ra_id_b, TestProjects.project_ra_name_b = self.project.create_project(metadata = {"public": "false"}, **TestProjects.USER_RA_CLIENT) TestProjects.project_ra_id_c, TestProjects.project_ra_name_c = self.project.create_project(metadata = {"public": "true"}, **TestProjects.USER_RA_CLIENT) #3. Push image(ImagePA) to project(PA), image(ImagePB) to project(PB) and image(ImagePC) to project(PC) by user(UA); TestProjects.repo_name_in_project_a, tag_a = push_self_build_image_to_project(TestProjects.project_ra_name_a, harbor_server, user_ra_name, user_ra_password, image_project_a, tag) TestProjects.repo_name_in_project_b, tag_b = push_self_build_image_to_project(TestProjects.project_ra_name_b, harbor_server, user_ra_name, user_ra_password, image_project_b, tag) TestProjects.repo_name_in_project_c, tag_c = push_self_build_image_to_project(TestProjects.project_ra_name_c, harbor_server, user_ra_name, user_ra_password, image_project_c, tag) #4. Create a new robot account(RA) with pull and push privilege in project(PA) by user(UA); robot_id, robot_account = self.robot.create_project_robot(TestProjects.project_ra_name_a, 30 ,**TestProjects.USER_RA_CLIENT) #5. Check robot account info, it should has both pull and push privilege; data = self.robot.get_robot_account_by_id(robot_id, **TestProjects.USER_RA_CLIENT) _assert_status_code(robot_account.name, data.name) #6. Pull image(ImagePA) from project(PA) by robot account(RA), it must be successful; pull_harbor_image(harbor_server, robot_account.name, robot_account.secret, TestProjects.repo_name_in_project_a, tag_a) #7. Push image(ImageRA) to project(PA) by robot account(RA), it must be successful; TestProjects.repo_name_pa, _ = push_self_build_image_to_project(TestProjects.project_ra_name_a, harbor_server, robot_account.name, robot_account.secret, image_robot_account, tag) #8. Push image(ImageRA) to project(PB) by robot account(RA), it must be not successful; push_self_build_image_to_project(TestProjects.project_ra_name_b, harbor_server, robot_account.name, robot_account.secret, image_robot_account, tag, expected_error_message = "unauthorized to access repository") #9. Pull image(ImagePB) from project(PB) by robot account(RA), it must be not successful; pull_harbor_image(harbor_server, robot_account.name, robot_account.secret, TestProjects.repo_name_in_project_b, tag_b, expected_error_message = "unauthorized to access repository") #10. Pull image from project(PC), it must be successful; pull_harbor_image(harbor_server, robot_account.name, robot_account.secret, TestProjects.repo_name_in_project_c, tag_c) #11. Push image(ImageRA) to project(PC) by robot account(RA), it must be not successful; push_self_build_image_to_project(TestProjects.project_ra_name_c, harbor_server, robot_account.name, robot_account.secret, image_robot_account, tag, expected_error_message = "unauthorized to access repository") #12. Update action property of robot account(RA);" self.robot.disable_robot_account(robot_id, True, **TestProjects.USER_RA_CLIENT) #13. Pull image(ImagePA) from project(PA) by robot account(RA), it must be not successful; pull_harbor_image(harbor_server, robot_account.name, robot_account.secret, TestProjects.repo_name_in_project_a, tag_a, expected_login_error_message = "unauthorized: authentication required") #14. Push image(ImageRA) to project(PA) by robot account(RA), it must be not successful; push_self_build_image_to_project(TestProjects.project_ra_name_a, harbor_server, robot_account.name, robot_account.secret, image_robot_account, tag, expected_login_error_message = "unauthorized: authentication required") #15. Delete robot account(RA), it must be not successful. self.robot.delete_robot_account(robot_id, **TestProjects.USER_RA_CLIENT)
def testRetag(self): """ Test case: Retag Image Test step and expected result: 1. Create a new user(UA); 2. Create a new project(PA) by user(UA); 3. Create a new project(PB) by user(UA); 4. Update role of user-retag as guest member of project(PB); 5. Create a new repository(RA) in project(PA) by user(UA); 6. Get repository in project(PA), there should be one repository which was created by user(UA); 7. Get repository(RA)'s image tag detail information; 8. Retag image in project(PA) to project(PB), it should be forbidden; 9. Update role of user-retag as admin member of project(PB); 10. Retag image in project(PA) to project(PB), it should be successful; 11. Get repository(RB)'s image tag detail information; 12. Read digest of retaged image, it must be the same with the image in repository(RA); 13. Pull image from project(PB) by user_retag, it must be successful; Tear down: 1. Delete repository(RA); 2. Delete repository by retag; 3. Delete project(PA); 4. Delete user(UA). """ url = ADMIN_CLIENT["endpoint"] user_retag_password = "******" pull_tag_name = "latest" dst_repo_sub_name = "repo" #1. Create a new user(UA); TestProjects.user_retag_id, user_retag_name = self.user.create_user( user_password=user_retag_password, **ADMIN_CLIENT) TestProjects.USER_RETAG_CLIENT = dict(endpoint=url, username=user_retag_name, password=user_retag_password) #2. Create a new project(PA) by user(UA); TestProjects.project_src_repo_id, TestProjects.project_src_repo_name = self.project.create_project( metadata={"public": "false"}, **TestProjects.USER_RETAG_CLIENT) #3. Create a new project(PB) by user(UA); TestProjects.project_dst_repo_id, TestProjects.project_dst_repo_name = self.project.create_project( metadata={"public": "false"}, **TestProjects.USER_RETAG_CLIENT) retag_member_id = self.project.get_project_member_id( TestProjects.project_dst_repo_id, user_retag_name, **TestProjects.USER_RETAG_CLIENT) #4. Update role of user-retag as guest member of project(PB); self.project.update_project_member_role( TestProjects.project_dst_repo_id, retag_member_id, 3, **ADMIN_CLIENT) #5. Create a new repository(RA) in project(PA) by user(UA); TestProjects.src_repo_name, tag_name = push_self_build_image_to_project( TestProjects.project_src_repo_name, harbor_server, 'admin', 'Harbor12345', "hello-world", pull_tag_name) #6. Get repository in project(PA), there should be one repository which was created by user(UA); src_repo_data = self.repo.list_repositories( TestProjects.project_src_repo_name, **TestProjects.USER_RETAG_CLIENT) _assert_status_code(TestProjects.src_repo_name, src_repo_data[0].name) #7. Get repository(RA)'s image tag detail information; src_tag_data = self.artifact.get_reference_info( TestProjects.project_src_repo_name, TestProjects.src_repo_name.split('/')[1], tag_name, **TestProjects.USER_RETAG_CLIENT) TestProjects.dst_repo_name = TestProjects.project_dst_repo_name + "/" + dst_repo_sub_name #8. Retag image in project(PA) to project(PB), it should be forbidden; self.artifact.copy_artifact(TestProjects.project_dst_repo_name, dst_repo_sub_name, TestProjects.src_repo_name + "@" + src_tag_data.digest, expect_status_code=403, **TestProjects.USER_RETAG_CLIENT) #9. Update role of user-retag as admin member of project(PB); self.project.update_project_member_role( TestProjects.project_dst_repo_id, retag_member_id, 1, **ADMIN_CLIENT) #10. Retag image in project(PA) to project(PB), it should be successful; self.artifact.copy_artifact( TestProjects.project_dst_repo_name, dst_repo_sub_name, TestProjects.src_repo_name + "@" + src_tag_data.digest, **TestProjects.USER_RETAG_CLIENT) #11. Get repository(RB)'s image tag detail information; dst_tag_data = self.artifact.get_reference_info( TestProjects.project_dst_repo_name, dst_repo_sub_name, tag_name, **TestProjects.USER_RETAG_CLIENT) #12. Read digest of retaged image, it must be the same with the image in repository(RA); self.assertEqual(src_tag_data.digest, dst_tag_data.digest) #13. Pull image from project(PB) by user_retag, it must be successful;" pull_harbor_image(harbor_server, user_retag_name, user_retag_password, TestProjects.dst_repo_name, tag_name)
def test_02_SystemlevelRobotAccount(self): """ Test case: Robot Account Test step and expected result: 1. Define a number of access lists; 2. Create the same number of private projects; 3. Create a system robot account has permission for those projects; 4. Verify the system robot account has the corresponding rights; 5. Disable the system robot account; 6. Verify the system robot account has no the corresponding rights; 7. Enable the system robot account; 8. Verify the system robot account has the corresponding rights; 9. Refresh secret for the system robot account; 10. Verify the system robot account has no the corresponding right with the old secret already; 11. Verify the system robot account still has the corresponding right with the new secret; 12. List system robot account, then add a new project to the system robot account project permission list; 13. Delete this project; 14. List system robot account successfully; 15. Delete the system robot account; 16. Verify the system robot account has no the corresponding right; 17. Add a system robot account with all project coverd; 18. Verify the system robot account has no the corresponding right; """ #1. Define a number of access lists; CHART_FILE_LIST = [ dict(name='prometheus', version='7.0.2'), dict(name='harbor', version='0.2.0') ] for i in range(2): base.run_command([ "curl", r"-o", "./tests/apitests/python/{}-{}.tgz".format( CHART_FILE_LIST[i]["name"], CHART_FILE_LIST[i]["version"]), "https://storage.googleapis.com/harbor-builds/helm-chart-test-files/{}-{}.tgz" .format(CHART_FILE_LIST[i]["name"], CHART_FILE_LIST[i]["version"]) ]) #Make sure that whether 'True' or 'False' must be included in each line or row. check_list = [ [True, True, True, True, True, True, False, True, False, True], [False, False, False, False, True, True, False, True, True, False], [True, False, True, False, True, False, True, False, True, True], [False, False, False, True, False, True, False, True, True, False] ] access_list_list = [] for i in range(len(check_list)): access_list_list.append( self.robot.create_access_list(check_list[i])) #2. Create the same number of private projects; robot_account_Permissions_list = [] project_access_list = [] for i in range(len(check_list)): with created_user(TestRobotAccount.user_ra_password, _teardown=False) as (user_id, username): with created_project(metadata={"public": "false"}, user_id=user_id, _teardown=False) as (project_id, project_name): project_access_list.append( dict(project_name=project_name, project_id=project_id, check_list=check_list[i])) robot_account_Permissions = v2_swagger_client.Permission( kind="project", namespace=project_name, access=access_list_list[i]) robot_account_Permissions_list.append( robot_account_Permissions) #3. Create a system robot account has permission for those projects; system_robot_account_id, system_robot_account = self.robot.create_system_robot( robot_account_Permissions_list, 300) print("system_robot_account:", system_robot_account) SYSTEM_RA_CLIENT = dict(endpoint=TestRobotAccount.url, username=system_robot_account.name, password=system_robot_account.secret) SYSTEM_RA_CHART_CLIENT = dict(endpoint=CHART_API_CLIENT["endpoint"], username=SYSTEM_RA_CLIENT["username"], password=SYSTEM_RA_CLIENT["password"]) #4. Verify the system robot account has the corresponding rights; for project_access in project_access_list: print(r"project_access:", project_access) if project_access["check_list"][1]: #---repository:push--- repo = push_self_build_image_to_project( project_access["project_name"], harbor_server, SYSTEM_RA_CLIENT["username"], SYSTEM_RA_CLIENT["password"], "test_pushable", "v6.8.1") else: push_self_build_image_to_project( project_access["project_name"], harbor_server, SYSTEM_RA_CLIENT["username"], SYSTEM_RA_CLIENT["password"], "test_unpushable", "v6.8.1", expected_error_message="unauthorized to access repository") tag_for_del = "v1.0.0" repo_name, tag = push_self_build_image_to_project( project_access["project_name"], harbor_server, ADMIN_CLIENT["username"], ADMIN_CLIENT["password"], "test_del_artifact", tag_for_del) if project_access["check_list"][0]: #---repository:pull--- pull_harbor_image(harbor_server, SYSTEM_RA_CLIENT["username"], SYSTEM_RA_CLIENT["password"], repo_name, tag_for_del) else: pull_harbor_image( harbor_server, SYSTEM_RA_CLIENT["username"], SYSTEM_RA_CLIENT["password"], repo_name, tag_for_del, expected_error_message= "action: pull: unauthorized to access repository") if project_access["check_list"][2]: #---artifact:delete--- self.artifact.delete_artifact(project_access["project_name"], repo_name.split('/')[1], tag_for_del, **SYSTEM_RA_CLIENT) else: self.artifact.delete_artifact(project_access["project_name"], repo_name.split('/')[1], tag_for_del, expect_status_code=403, **SYSTEM_RA_CLIENT) #Prepare for chart read and delete self.chart.upload_chart( project_access["project_name"], r'./tests/apitests/python/{}-{}.tgz'.format( CHART_FILE_LIST[1]["name"], CHART_FILE_LIST[1]["version"]), **CHART_API_CLIENT) if project_access["check_list"][3]: #---helm-chart:read--- library.helm.helm2_fetch_chart_file( "chart_repo_" + base._random_name("repo"), harbor_url, project_access["project_name"], SYSTEM_RA_CLIENT["username"], SYSTEM_RA_CLIENT["password"], CHART_FILE_LIST[1]["name"]) else: library.helm.helm2_fetch_chart_file( "chart_repo_" + base._random_name("repo"), harbor_url, project_access["project_name"], SYSTEM_RA_CLIENT["username"], SYSTEM_RA_CLIENT["password"], CHART_FILE_LIST[1]["name"], expected_add_repo_error_message="403 Forbidden") if project_access["check_list"][ 4]: #---helm-chart-version:create--- self.chart.upload_chart( project_access["project_name"], r'./tests/apitests/python/{}-{}.tgz'.format( CHART_FILE_LIST[0]["name"], CHART_FILE_LIST[0]["version"]), **SYSTEM_RA_CHART_CLIENT) else: self.chart.upload_chart( project_access["project_name"], r'./tests/apitests/python/{}-{}.tgz'.format( CHART_FILE_LIST[0]["name"], CHART_FILE_LIST[0]["version"]), expect_status_code=403, **SYSTEM_RA_CHART_CLIENT) if project_access["check_list"][ 5]: #---helm-chart-version:delete--- self.chart.delete_chart_with_version( project_access["project_name"], CHART_FILE_LIST[1]["name"], CHART_FILE_LIST[1]["version"], **SYSTEM_RA_CHART_CLIENT) else: self.chart.delete_chart_with_version( project_access["project_name"], CHART_FILE_LIST[1]["name"], CHART_FILE_LIST[1]["version"], expect_status_code=403, **SYSTEM_RA_CHART_CLIENT) repo_name, tag = push_self_build_image_to_project( project_access["project_name"], harbor_server, ADMIN_CLIENT["username"], ADMIN_CLIENT["password"], "test_create_tag", "latest_1") self.artifact.create_tag(project_access["project_name"], repo_name.split('/')[1], tag, "for_delete", **ADMIN_CLIENT) if project_access["check_list"][6]: #---tag:create--- self.artifact.create_tag(project_access["project_name"], repo_name.split('/')[1], tag, "1.0", **SYSTEM_RA_CLIENT) else: self.artifact.create_tag(project_access["project_name"], repo_name.split('/')[1], tag, "1.0", expect_status_code=403, **SYSTEM_RA_CLIENT) if project_access["check_list"][7]: #---tag:delete--- self.artifact.delete_tag(project_access["project_name"], repo_name.split('/')[1], tag, "for_delete", **SYSTEM_RA_CLIENT) else: self.artifact.delete_tag(project_access["project_name"], repo_name.split('/')[1], tag, "for_delete", expect_status_code=403, **SYSTEM_RA_CLIENT) repo_name, tag = push_self_build_image_to_project( project_access["project_name"], harbor_server, ADMIN_CLIENT["username"], ADMIN_CLIENT["password"], "test_create_artifact_label", "latest_1") #Add project level label to artifact label_id, _ = self.label.create_label( project_id=project_access["project_id"], scope="p", **ADMIN_CLIENT) if project_access["check_list"][8]: #---artifact-label:create--- self.artifact.add_label_to_reference( project_access["project_name"], repo_name.split('/')[1], tag, int(label_id), **SYSTEM_RA_CLIENT) else: self.artifact.add_label_to_reference( project_access["project_name"], repo_name.split('/')[1], tag, int(label_id), expect_status_code=403, **SYSTEM_RA_CLIENT) if project_access["check_list"][9]: #---scan:create--- self.scan.scan_artifact(project_access["project_name"], repo_name.split('/')[1], tag, **SYSTEM_RA_CLIENT) else: self.scan.scan_artifact(project_access["project_name"], repo_name.split('/')[1], tag, expect_status_code=403, **SYSTEM_RA_CLIENT) #5. Disable the system robot account; self.robot.update_system_robot_account(system_robot_account_id, system_robot_account.name, robot_account_Permissions_list, disable=True, **ADMIN_CLIENT) #6. Verify the system robot account has no the corresponding rights; self.verify_repository_unpushable(project_access_list, SYSTEM_RA_CLIENT) #7. Enable the system robot account; self.robot.update_system_robot_account(system_robot_account_id, system_robot_account.name, robot_account_Permissions_list, disable=False, **ADMIN_CLIENT) #8. Verify the system robot account has the corresponding rights; self.verify_repository_pushable(project_access_list, SYSTEM_RA_CLIENT) #9. Refresh secret for the system robot account; new_secret = "new_secret_At_321" self.robot.refresh_robot_account_secret(system_robot_account_id, new_secret, **ADMIN_CLIENT) #10. Verify the system robot account has no the corresponding right with the old secret already; self.verify_repository_unpushable(project_access_list, SYSTEM_RA_CLIENT) #11. Verify the system robot account still has the corresponding right with the new secret; SYSTEM_RA_CLIENT["password"] = new_secret self.verify_repository_pushable(project_access_list, SYSTEM_RA_CLIENT) #12. List system robot account, then add a new project to the system robot account project permission list; self.robot.list_robot(**ADMIN_CLIENT) project_for_del_id, project_for_del_name = self.project.create_project( metadata={"public": "true"}, **ADMIN_CLIENT) robot_account_Permissions = v2_swagger_client.Permission( kind="project", namespace=project_for_del_name, access=access_list_list[0]) robot_account_Permissions_list.append(robot_account_Permissions) self.robot.update_system_robot_account(system_robot_account_id, system_robot_account.name, robot_account_Permissions_list, **ADMIN_CLIENT) self.robot.list_robot(**ADMIN_CLIENT) #13. Delete this project; self.project.delete_project(project_for_del_id, **ADMIN_CLIENT) #14. List system robot account successfully; self.robot.list_robot(**ADMIN_CLIENT) #15. Delete the system robot account; self.robot.delete_robot_account(system_robot_account_id, **ADMIN_CLIENT) #16. Verify the system robot account has no the corresponding right; self.verify_repository_unpushable(project_access_list, SYSTEM_RA_CLIENT) #17. Add a system robot account with all project coverd; all_true_access_list = self.robot.create_access_list([True] * 10) robot_account_Permissions_list = [] robot_account_Permissions = v2_swagger_client.Permission( kind="project", namespace="*", access=all_true_access_list) robot_account_Permissions_list.append(robot_account_Permissions) _, system_robot_account_cover_all = self.robot.create_system_robot( robot_account_Permissions_list, 300) #18. Verify the system robot account has no the corresponding right; print("system_robot_account_cover_all:", system_robot_account_cover_all) SYSTEM_RA_CLIENT_COVER_ALL = dict( endpoint=TestRobotAccount.url, username=system_robot_account_cover_all.name, password=system_robot_account_cover_all.secret) projects = self.project.get_projects(dict(), **ADMIN_CLIENT) print("All projects:", projects) project_access_list = [] for i in range(len(projects)): project_access_list.append( dict(project_name=projects[i].name, project_id=projects[i].project_id, check_list=all_true_access_list)) self.verify_repository_pushable(project_access_list, SYSTEM_RA_CLIENT_COVER_ALL)
def testAssignRoleToLdapGroup(self): """ Test case: Assign Role To Ldap Group Test step and expected result: 1. Set LDAP Auth configurations; 2. Create a new public project(PA) by Admin; 3. Add 3 member groups to project(PA); 4. Push image by each member role; 5. Verfify that admin_user can add project member, dev_user and guest_user can not add project member; 6. Verfify that admin_user and dev_user can push image, guest_user can not push image; 7. Verfify that admin_user, dev_user and guest_user can view logs, test user can not view logs. 8. Delete repository(RA) by user(UA); 9. Delete project(PA); """ url = ADMIN_CLIENT["endpoint"] USER_ADMIN = dict(endpoint=url, username="******", password="******", repo="haproxy") USER_DEV = dict(endpoint=url, username="******", password="******", repo="alpine") USER_GUEST = dict(endpoint=url, username="******", password="******", repo="busybox") USER_TEST = dict(endpoint=url, username="******", password="******") USER_MIKE = dict(endpoint=url, username="******", password="******") #USER001 is in group harbor_group3 self.conf.set_configurations_of_ldap( ldap_filter="", ldap_group_attribute_name="cn", ldap_group_base_dn="ou=groups,dc=example,dc=com", ldap_group_search_filter="objectclass=groupOfNames", ldap_group_search_scope=2, **ADMIN_CLIENT) with created_project(metadata={"public": "false"}) as (project_id, project_name): self.project.add_project_members( project_id, member_role_id=1, _ldap_group_dn="cn=harbor_admin,ou=groups,dc=example,dc=com", **ADMIN_CLIENT) self.project.add_project_members( project_id, member_role_id=2, _ldap_group_dn="cn=harbor_dev,ou=groups,dc=example,dc=com", **ADMIN_CLIENT) self.project.add_project_members( project_id, member_role_id=3, _ldap_group_dn="cn=harbor_guest,ou=groups,dc=example,dc=com", **ADMIN_CLIENT) projects = self.project.get_projects(dict(name=project_name), **USER_ADMIN) self.assertTrue(len(projects) == 1) self.assertEqual(1, projects[0].current_user_role_id) #Mike has logged in harbor in previous test. mike = self.user.get_user_by_name(USER_MIKE["username"], **ADMIN_CLIENT) #Verify role difference in add project member feature, to distinguish between admin and dev role self.project.add_project_members(project_id, user_id=mike.user_id, member_role_id=3, **USER_ADMIN) self.project.add_project_members(project_id, user_id=mike.user_id, member_role_id=3, expect_status_code=403, **USER_DEV) self.project.add_project_members(project_id, user_id=mike.user_id, member_role_id=3, expect_status_code=403, **USER_GUEST) repo_name_admin, _ = push_self_build_image_to_project( project_name, harbor_server, USER_ADMIN["username"], USER_ADMIN["password"], USER_ADMIN["repo"], "latest") artifacts = self.artifact.list_artifacts(project_name, USER_ADMIN["repo"], **USER_ADMIN) self.assertTrue(len(artifacts) == 1) repo_name_dev, _ = push_self_build_image_to_project( project_name, harbor_server, USER_DEV["username"], USER_DEV["password"], USER_DEV["repo"], "latest") artifacts = self.artifact.list_artifacts(project_name, USER_DEV["repo"], **USER_DEV) self.assertTrue(len(artifacts) == 1) push_self_build_image_to_project( project_name, harbor_server, USER_GUEST["username"], USER_GUEST["password"], USER_GUEST["repo"], "latest", expected_error_message="unauthorized to access repository") artifacts = self.artifact.list_artifacts(project_name, USER_GUEST["repo"], **USER_GUEST) self.assertTrue(len(artifacts) == 0) self.assertTrue( self.project.query_user_logs(project_name, **USER_ADMIN) > 0, "admin user can see logs") self.assertTrue( self.project.query_user_logs(project_name, **USER_DEV) > 0, "dev user can see logs") self.assertTrue( self.project.query_user_logs(project_name, **USER_GUEST) > 0, "guest user can see logs") self.assertTrue( self.project.query_user_logs(project_name, status_code=403, **USER_TEST) == 0, "test user can not see any logs") self.repo.delete_repository(project_name, repo_name_admin.split('/')[1], **USER_ADMIN) self.repo.delete_repository(project_name, repo_name_dev.split('/')[1], **USER_ADMIN)
def test_01_AddIndexByDockerManifest(self): """ Test case: Push Index By Docker Manifest Test step and expected result: 1. Create a new user(UA); 2. Create a new project(PA) by user(UA); 3. Create 2 new repositorys(RA,RB) in project(PA) by user(UA); 4. Push an index(IA) to Harbor by docker manifest CLI successfully; 5. Get Artifacts successfully; 6. Get index(IA) by reference successfully; 7. Verify harbor index is index(IA) pushed by docker manifest CLI; 8.1 Verify harbor index(IA) can be pulled by docker CLI successfully; 8.2 Verify harbor index(IA) can be pulled by docker CLI successfully; 9. Get addition successfully; 10. Unable to Delete artifact in manifest list; 11. Delete index successfully. Tear down: 1. Delete repository(RA,RB,IA) by user(UA); 2. Delete project(PA); 3. Delete user(UA). """ TestManifest.project = Project() TestManifest.user = User() TestManifest.artifact = Artifact() TestManifest.repo = Repository() TestManifest.scan = Scan() TestManifest.url = ADMIN_CLIENT["endpoint"] TestManifest.user_push_index_password = "******" TestManifest.index_name = "ci_test_index" TestManifest.index_tag = "test_tag" TestManifest.image_a = "alpine" TestManifest.image_b = "busybox" #1. Create a new user(UA); TestManifest.user_id, TestManifest.user_name = TestManifest.user.create_user( user_password=TestManifest.user_push_index_password, **ADMIN_CLIENT) TestManifest.USER_CLIENT = dict( endpoint=TestManifest.url, username=TestManifest.user_name, password=TestManifest.user_push_index_password, with_scan_overview=True) #2. Create a new project(PA) by user(UA); TestManifest.project_id, TestManifest.project_name = TestManifest.project.create_project( metadata={"public": "false"}, **TestManifest.USER_CLIENT) #3. Create 2 new repositorys(RA,RB) in project(PA) by user(UA); repo_name_a, tag_a = push_self_build_image_to_project( TestManifest.project_name, harbor_server, 'admin', 'Harbor12345', TestManifest.image_a, "latest") repo_name_b, tag_b = push_self_build_image_to_project( TestManifest.project_name, harbor_server, 'admin', 'Harbor12345', TestManifest.image_b, "latest") #4. Push an index(IA) to Harbor by docker manifest CLI successfully; manifests = [ harbor_server + "/" + repo_name_a + ":" + tag_a, harbor_server + "/" + repo_name_b + ":" + tag_b ] index = harbor_server + "/" + TestManifest.project_name + "/" + TestManifest.index_name + ":" + TestManifest.index_tag TestManifest.index_sha256_cli_ret, TestManifest.manifests_sha256_cli_ret = library.docker_api.docker_manifest_push_to_harbor( index, manifests, harbor_server, TestManifest.user_name, TestManifest.user_push_index_password) #5. Get Artifacts successfully; artifacts = TestManifest.artifact.list_artifacts( TestManifest.project_name, TestManifest.index_name, **TestManifest.USER_CLIENT) TestManifest.artifacts_ref_child_list = [ artifacts[0].references[1].child_digest, artifacts[0].references[0].child_digest ] self.assertEqual( TestManifest.artifacts_ref_child_list.count( TestManifest.manifests_sha256_cli_ret[0]), 1) self.assertEqual( TestManifest.artifacts_ref_child_list.count( TestManifest.manifests_sha256_cli_ret[1]), 1) #6. Get index(IA) by reference successfully; index_data = TestManifest.artifact.get_reference_info( TestManifest.project_name, TestManifest.index_name, TestManifest.index_tag, **TestManifest.USER_CLIENT) print("index_data:", index_data) manifests_sha256_harbor_ret = [ index_data.references[1].child_digest, index_data.references[0].child_digest ] #7. Verify harbor index is index(IA) pushed by docker manifest CLI; self.assertEqual(index_data.digest, TestManifest.index_sha256_cli_ret) self.assertEqual( manifests_sha256_harbor_ret.count( TestManifest.manifests_sha256_cli_ret[0]), 1) self.assertEqual( manifests_sha256_harbor_ret.count( TestManifest.manifests_sha256_cli_ret[1]), 1) #8.1 Verify harbor index(IA) can be pulled by docker CLI successfully; pull_harbor_image( harbor_server, TestManifest.user_name, TestManifest.user_push_index_password, TestManifest.project_name + "/" + TestManifest.index_name, TestManifest.index_tag) #8.2 Verify harbor index(IA) can be pulled by ctr successfully; oci_ref = harbor_server + "/" + TestManifest.project_name + "/" + TestManifest.index_name + ":" + TestManifest.index_tag library.containerd.ctr_images_pull( TestManifest.user_name, TestManifest.user_push_index_password, oci_ref) library.containerd.ctr_images_list(oci_ref=oci_ref) #9. Get addition successfully; addition_v = TestManifest.artifact.get_addition( TestManifest.project_name, TestManifest.index_name, TestManifest.index_tag, "vulnerabilities", **TestManifest.USER_CLIENT) self.assertEqual(addition_v[0], '{}') #This artifact has no build history addition_v = TestManifest.artifact.get_addition( TestManifest.project_name, TestManifest.index_name, TestManifest.manifests_sha256_cli_ret[0], "vulnerabilities", **TestManifest.USER_CLIENT) self.assertEqual(addition_v[0], '{}') addition_b = TestManifest.artifact.get_addition( TestManifest.project_name, TestManifest.index_name, TestManifest.manifests_sha256_cli_ret[0], "build_history", **TestManifest.USER_CLIENT) self.assertIn("ADD file:", addition_b[0]) image_data = TestManifest.artifact.get_reference_info( TestManifest.project_name, TestManifest.index_name, TestManifest.manifests_sha256_cli_ret[0], **TestManifest.USER_CLIENT) addition_v = TestManifest.artifact.get_addition( TestManifest.project_name, TestManifest.index_name, TestManifest.manifests_sha256_cli_ret[1], "vulnerabilities", **TestManifest.USER_CLIENT) self.assertEqual(addition_v[0], '{}') addition_b = TestManifest.artifact.get_addition( TestManifest.project_name, TestManifest.index_name, TestManifest.manifests_sha256_cli_ret[1], "build_history", **TestManifest.USER_CLIENT) self.assertIn("ADD file:", addition_b[0]) image_data = TestManifest.artifact.get_reference_info( TestManifest.project_name, TestManifest.index_name, TestManifest.manifests_sha256_cli_ret[0], **TestManifest.USER_CLIENT)
def test_01_ProjectlevelRobotAccount(self): """ Test case: Robot Account Test step and expected result: 1. Create user(UA); 2. Create private project(PA), private project(PB) and public project(PC) by user(UA); 3. Push image(ImagePA) to project(PA), image(ImagePB) to project(PB) and image(ImagePC) to project(PC) by user(UA); 4. Create a new robot account(RA) with pull and push privilige in project(PA) by user(UA); 5. Check robot account info, it should has both pull and push priviliges; 6. Pull image(ImagePA) from project(PA) by robot account(RA), it must be successful; 7. Push image(ImageRA) to project(PA) by robot account(RA), it must be successful; 8. Push image(ImageRA) to project(PB) by robot account(RA), it must be not successful; 9. Pull image(ImagePB) from project(PB) by robot account(RA), it must be not successful; 10. Pull image from project(PC), it must be successful; 11. Push image(ImageRA) to project(PC) by robot account(RA), it must be not successful; 12. Update action property of robot account(RA); 13. Pull image(ImagePA) from project(PA) by robot account(RA), it must be not successful; 14. Push image(ImageRA) to project(PA) by robot account(RA), it must be not successful; 15. Delete robot account(RA). 16. Create user(UB), Create public project(PD) by user(UB), user(UA) can't create robot account for project(PD). Tear down: 1. Delete repository(RA) by user(UA); 2. Delete project(PA); 3. Delete user(UA). """ image_project_a = "haproxy" image_project_b = "image_project_b" image_project_c = "httpd" image_robot_account = "alpine" tag = "latest" #1. Create user(UA);" self.user_ra_id, user_ra_name = self.user.create_user( user_password=TestRobotAccount.user_ra_password, **ADMIN_CLIENT) self.USER_RA_CLIENT = dict(endpoint=TestRobotAccount.url, username=user_ra_name, password=TestRobotAccount.user_ra_password) #2. Create private project(PA), private project(PB) and public project(PC) by user(UA); self.project_ra_id_a, self.project_ra_name_a = self.project.create_project( metadata={"public": "false"}, **self.USER_RA_CLIENT) self.project_ra_id_b, self.project_ra_name_b = self.project.create_project( metadata={"public": "false"}, **self.USER_RA_CLIENT) self.project_ra_id_c, self.project_ra_name_c = self.project.create_project( metadata={"public": "true"}, **self.USER_RA_CLIENT) #3. Push image(ImagePA) to project(PA), image(ImagePB) to project(PB) and image(ImagePC) to project(PC) by user(UA); self.repo_name_in_project_a, tag_a = push_self_build_image_to_project( self.project_ra_name_a, harbor_server, user_ra_name, TestRobotAccount.user_ra_password, image_project_a, tag) self.repo_name_in_project_b, tag_b = push_self_build_image_to_project( self.project_ra_name_b, harbor_server, user_ra_name, TestRobotAccount.user_ra_password, image_project_b, tag) self.repo_name_in_project_c, tag_c = push_self_build_image_to_project( self.project_ra_name_c, harbor_server, user_ra_name, TestRobotAccount.user_ra_password, image_project_c, tag) #4. Create a new robot account(RA) with pull and push privilege in project(PA) by user(UA); robot_id_a, robot_account_a = self.robot.create_project_robot( self.project_ra_name_a, 30, **self.USER_RA_CLIENT) robot_id_b, robot_account_b = self.robot.create_project_robot( self.project_ra_name_b, 30, **self.USER_RA_CLIENT) #5. Check robot account info, it should has both pull and push privilege; data = self.robot.get_robot_account_by_id(robot_id_a, **self.USER_RA_CLIENT) _assert_status_code(robot_account_a.name, data.name) #6. Pull image(ImagePA) from project(PA) by robot account(RA), it must be successful; pull_harbor_image(harbor_server, robot_account_a.name, robot_account_a.secret, self.repo_name_in_project_a, tag_a) #7. Push image(ImageRA) to project(PA) by robot account(RA), it must be successful; self.repo_name_pa, _ = push_self_build_image_to_project( self.project_ra_name_a, harbor_server, robot_account_a.name, robot_account_a.secret, image_robot_account, tag) #8. Push image(ImageRA) to project(PB) by robot account(RA), it must be not successful; push_self_build_image_to_project( self.project_ra_name_b, harbor_server, robot_account_a.name, robot_account_a.secret, image_robot_account, tag, expected_error_message="unauthorized to access repository") #9. Pull image(ImagePB) from project(PB) by robot account(RA), it must be not successful; pull_harbor_image( harbor_server, robot_account_a.name, robot_account_a.secret, self.repo_name_in_project_b, tag_b, expected_error_message="unauthorized to access repository") #10. Pull image from project(PC), it must be successful; pull_harbor_image(harbor_server, robot_account_a.name, robot_account_a.secret, self.repo_name_in_project_c, tag_c) #11. Push image(ImageRA) to project(PC) by robot account(RA), it must be not successful; push_self_build_image_to_project( self.project_ra_name_c, harbor_server, robot_account_a.name, robot_account_a.secret, image_robot_account, tag, expected_error_message="unauthorized to access repository") #12. Update action property of robot account(RA);" self.robot.disable_robot_account(robot_id_a, True, **self.USER_RA_CLIENT) #13. Pull image(ImagePA) from project(PA) by robot account(RA), it must be not successful; pull_harbor_image( harbor_server, robot_account_a.name, robot_account_a.secret, self.repo_name_in_project_a, tag_a, expected_login_error_message="unauthorized: authentication required" ) #14. Push image(ImageRA) to project(PA) by robot account(RA), it must be not successful; push_self_build_image_to_project( self.project_ra_name_a, harbor_server, robot_account_a.name, robot_account_a.secret, image_robot_account, tag, expected_login_error_message="unauthorized: authentication required" ) #15. Delete robot account(RA). self.robot.delete_robot_account(robot_id_a, **self.USER_RA_CLIENT) #16. Create user(UB), Create public project(PD) by user(UB), user(UA) can't create robot account for project(PD). self.user_ra_id_b, user_ra_name_b = self.user.create_user( user_password=TestRobotAccount.user_ra_password, **ADMIN_CLIENT) self.USER_RA_CLIENT_B = dict( endpoint=TestRobotAccount.url, username=user_ra_name_b, password=TestRobotAccount.user_ra_password) self.project_ra_id_d, self.project_ra_name_d = self.project.create_project( metadata={"public": "true"}, **self.USER_RA_CLIENT_B) self.robot.create_project_robot(self.project_ra_name_d, 30, expect_status_code=403, **self.USER_RA_CLIENT) self.do_01_tearDown()