def test_create(shared_client, cleanup, scan_host, src_type): """Create a Source using a single credential. :id: db459fc2-d34c-45cf-915a-1535406a9320 :description: Create {network, vcenter} source of single host and credential. Any valid IPv4 or dns name should be allowed to create a source. :steps: 1) Create host credential 2) Send POST with data to create the source using the credential to the source endpoint. :expectedresults: A new source entry is created with the data. """ cred = Credential(cred_type=src_type, client=shared_client, password=uuid4()) cred.create() src = Source( source_type=src_type, client=shared_client, hosts=[scan_host], credential_ids=[cred._id], ) src.create() # add the ids to the lists to destroy after the test is done cleanup.extend([cred, src]) assert_matches_server(src)
def test_delete(src_type, cleanup, shared_client): """After creating several {network, vcenter} sources, delete one. :id: 24d811b1-655d-4278-ab9f-64ca46a7121b :description: Test that we can delete an individual {network, vcenter} source by id :steps: 1) Create collection of {network, vcenter} sources, saving the information. 2) Send a DELETE request to destroy individual source 3) Confirm that all sources are on the server except the deleted one. 4) Repeat until all sources are deleted. :expectedresults: All sources are present on the server except the deleted source. """ created_srcs = [] num_srcs = random.randint(2, 20) echo_client = api.Client(response_handler=api.echo_handler) for i in range(num_srcs): # gen_valid_srcs will take care of cleanup created_srcs.append(gen_valid_source(cleanup, src_type, "localhost")) for i in range(num_srcs): delete_src = created_srcs.pop() delete_src.delete() delete_src.client = echo_client delete_response = delete_src.read() assert delete_response.status_code == 404 for p in created_srcs: assert_matches_server(p)
def test_update_sshkey_to_password(shared_client, cleanup, isolated_filesystem): """Create a network credential using password and switch it to use sshkey. :id: d24a54b5-3d8c-44e4-a0ae-61584a15b127 :description: Create a network credential with a sshkey, then update it to use a password. :steps: 1) Create a network credential with a username and sshkey. 2) Update the network credential deleting sshkey and updating password. 3) Confirm network credential has been updated. :expectedresults: The network credential is updated. """ sshkeyfile_name = utils.uuid4() tmp_dir = os.path.basename(os.getcwd()) sshkeyfile = Path(sshkeyfile_name) sshkeyfile.touch() cred = Credential(cred_type="network", ssh_keyfile=f"/sshkeys/{tmp_dir}/{sshkeyfile_name}") cred.create() # add the id to the list to destroy after the test is done cleanup.append(cred) assert_matches_server(cred) cred.client.response_handler = api.echo_handler cred.password = uuid4() cred.ssh_keyfile = None cred.update() assert_matches_server(cred)
def test_update(shared_client, cleanup, scan_host, src_type): """Create a {network, vcenter} source and then update it. :id: 900dda70-6208-44f5-b64d-f6ca4db7dfa4 :description: Create {network, vcenter} source of single host and credential :steps: 1) Create host credential 2) Send POST with data to create {network, vcenter} source using the host credential to the source endpoint. 3) Add a host and a new credential and send and PUT to the server with the data :expectedresults: The source entry is created and updated. """ cred = Credential(cred_type=src_type, client=shared_client, password=uuid4()) cred.create() cleanup.append(cred) src = Source( source_type=src_type, client=shared_client, hosts=[scan_host], credential_ids=[cred._id], ) src.create() cleanup.append(src) assert_matches_server(src) src.hosts = ["example.com"] cred2 = Credential(cred_type=src_type, password=uuid4()) cred2.create() cleanup.append(cred2) src.credentials = [cred2._id] src.update() assert_matches_server(src)
def test_create_multiple_hosts(shared_client, cleanup, scan_host): """Create a Network Source using multiple hosts. :id: 248f701c-b4d4-408a-80b0-c4863a8007e1 :description: Create a network source with multiple hosts :steps: 1) Create credential 2) Send POST with data to create network source using the credential to the source endpoint with multiple hosts, (list, IPv4 range, CIDR notation, or other ansible pattern) :expectedresults: The source is created. """ cred = Credential(cred_type=NETWORK_TYPE, client=shared_client, password=uuid4()) cred.create() src = Source( source_type=NETWORK_TYPE, client=shared_client, hosts=scan_host, credential_ids=[cred._id], ) src.create() # add the ids to the lists to destroy after the test is done cleanup.extend([cred, src]) src.hosts = RESULTING_HOST_FORMAT_DATA assert_matches_server(src)
def test_update_password_to_sshkeyfile(shared_client, cleanup, isolated_filesystem): """Create a network credential using password and switch it to use sshkey. :id: 6e557092-192b-4f75-babc-abc5774fe965 :description: Create a network credential with password, then update it to use a sshkey. :steps: 1) Create a network credential with a username and password. 2) Update the network credential deleting password and adding sshkey. 3) Confirm network credential has been updated. :expectedresults: The network credential is updated. """ cred = Credential(cred_type="network", client=shared_client, password=uuid4()) cred.create() # add the id to the list to destroy after the test is done cleanup.append(cred) assert_matches_server(cred) sshkeyfile_name = utils.uuid4() tmp_dir = os.path.basename(os.getcwd()) sshkeyfile = Path(sshkeyfile_name) sshkeyfile.touch() cred.ssh_keyfile = f"/sshkeys/{tmp_dir}/{sshkeyfile_name}" cred.password = None cred.update() assert_matches_server(cred)
def test_create_multiple_creds_and_sources( shared_client, cleanup, scan_host, isolated_filesystem ): """Create a Network Source using multiple credentials. :id: 07f49731-0162-4eb1-b89a-3c95fddad428 :description: Create a network source with multiple credentials :steps: 1) Create multiple credentials using both sshkey and passwords 2) Send POST with data to create network source using the credentials using a list of sources using multiple formats (alphabetical name, CIDR, individual IPv4 address, etc.) :expectedresults: The source is created. """ sshkeyfile_name = utils.uuid4() tmp_dir = os.path.basename(os.getcwd()) sshkeyfile = Path(sshkeyfile_name) sshkeyfile.touch() ssh_key_cred = Credential( cred_type=NETWORK_TYPE, client=shared_client, ssh_keyfile=f"/sshkeys/{tmp_dir}/{sshkeyfile_name}", ) ssh_key_cred.create() pwd_cred = Credential( cred_type=NETWORK_TYPE, client=shared_client, password=uuid4() ) pwd_cred.create() input_hosts = [scan_host] if isinstance(scan_host, list): input_hosts = scan_host src = Source( source_type=NETWORK_TYPE, client=shared_client, hosts=input_hosts, credential_ids=[ssh_key_cred._id, pwd_cred._id], ) src.create() # add the ids to the lists to destroy after the test is done cleanup.extend([ssh_key_cred, pwd_cred, src]) if isinstance(scan_host, list): src.hosts = RESULTING_HOST_FORMAT_DATA assert_matches_server(src)
def test_create_become_method(cleanup, shared_client, method): """Create a network credential that uses become options. :id: e49e497d-abb7-4d6a-8366-3409e297062a :description: Create a network credential with username, password and uses a become method. :steps: Send a POST to the credential endpoint with data. :expectedresults: A new network credential is created. """ cred = Credential( cred_type="network", client=shared_client, password=uuid4(), become_method=method, become_password=uuid4(), become_user=uuid4(), ) cred.create() # add the id to the list to destroy after the test is done cleanup.append(cred) assert_matches_server(cred)
def test_create_with_sshkey(shared_client, cleanup, isolated_filesystem): """Create a network credential with username and sshkey. :id: ab6fd574-2e9f-46b8-847d-17b23c19fdd2 :description: Create a network credential with a user name and sshkey :steps: Send POST with necessary data to documented api endpoint. :expectedresults: A new network credential entry is created with the data. """ sshkeyfile_name = utils.uuid4() tmp_dir = os.path.basename(os.getcwd()) sshkeyfile = Path(sshkeyfile_name) sshkeyfile.touch() cred = Credential( cred_type="network", client=shared_client, ssh_keyfile=f"/sshkeys/{tmp_dir}/{sshkeyfile_name}", ) cred.create() # add the id to the list to destroy after the test is done cleanup.append(cred) assert_matches_server(cred)
def test_create_multiple_creds(shared_client, cleanup, scan_host, isolated_filesystem): """Create a Network Source using multiple credentials. :id: dcf6ea99-c6b1-493d-9db8-3ec0ea09b5e0 :description: Create a network source with multiple credentials :steps: 1) Create multiple credentials using both sshkey and passwords 2) Send POST with data to create network source using the credentials :expectedresults: The source is created. """ sshkeyfile_name = utils.uuid4() tmp_dir = os.path.basename(os.getcwd()) sshkeyfile = Path(sshkeyfile_name) sshkeyfile.touch() ssh_key_cred = Credential( cred_type=NETWORK_TYPE, client=shared_client, ssh_keyfile=f"/sshkeys/{tmp_dir}/{sshkeyfile_name}", ) ssh_key_cred.create() pwd_cred = Credential( cred_type=NETWORK_TYPE, client=shared_client, password=uuid4() ) pwd_cred.create() src = Source( source_type=NETWORK_TYPE, client=shared_client, hosts=[scan_host], credential_ids=[ssh_key_cred._id, pwd_cred._id], ) src.create() # add the ids to the lists to destroy after the test is done cleanup.extend([ssh_key_cred, pwd_cred, src]) assert_matches_server(src)
def test_negative_update_to_invalid(shared_client, cleanup, isolated_filesystem): """Attempt to update valid credential with invalid data. :id: c34ea917-ee36-4b93-8907-24a5f87bbed3 :description: Create valid network credentials, then attempt to update to be invalid. :steps: 1) Create valid credentials with passwords or sshkey. 2) Update the network credentials: a) using both password and sshkey b) missing both password and sshkey :expectedresults: Error codes are returned and the network credentials are not updated. """ sshkeyfile_name = utils.uuid4() tmp_dir = os.path.basename(os.getcwd()) sshkeyfile = Path(sshkeyfile_name) sshkeyfile.touch() cred = Credential( cred_type="network", client=shared_client, ssh_keyfile=f"/sshkeys/{tmp_dir}/{sshkeyfile_name}", ) cred.create() # add the id to the list to destroy after the test is done cleanup.append(cred) assert_matches_server(cred) cred.client = api.Client(api.echo_handler) # Try to update with both sshkeyfile and password cred.password = uuid4() response = cred.update() assert response.status_code == 400 assert "either a password or an ssh_keyfile, not both" in response.text cred.password = None assert_matches_server(cred) # Try to update with both sshkeyfile and password missing old = cred.ssh_keyfile del cred.ssh_keyfile response = cred.update() assert response.status_code == 400 assert "must have either a password or an ssh_keyfile" in response.text cred.ssh_keyfile = old assert_matches_server(cred)