def test_update_organization_settings(self): # Setup Expected Response name = 'name3373707' enable_asset_discovery = False expected_response = { 'name': name, 'enable_asset_discovery': enable_asset_discovery } expected_response = organization_settings_pb2.OrganizationSettings( **expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) client = securitycenter_v1beta1.SecurityCenterClient(channel=channel) # Setup Request organization_settings = {} response = client.update_organization_settings(organization_settings) assert expected_response == response assert len(channel.requests) == 1 expected_request = securitycenter_service_pb2.UpdateOrganizationSettingsRequest( organization_settings=organization_settings) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_list_sources(self): # Setup Expected Response next_page_token = '' sources_element = {} sources = [sources_element] expected_response = { 'next_page_token': next_page_token, 'sources': sources } expected_response = securitycenter_service_pb2.ListSourcesResponse( **expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) client = securitycenter_v1beta1.SecurityCenterClient(channel=channel) # Setup Request parent = client.organization_path('[ORGANIZATION]') paged_list_response = client.list_sources(parent) resources = list(paged_list_response) assert len(resources) == 1 assert expected_response.sources[0] == resources[0] assert len(channel.requests) == 1 expected_request = securitycenter_service_pb2.ListSourcesRequest( parent=parent) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_set_finding_state(self): # Setup Expected Response name_2 = 'name2-1052831874' parent = 'parent-995424086' resource_name = 'resourceName979421212' category = 'category50511102' external_uri = 'externalUri-1385596168' expected_response = { 'name': name_2, 'parent': parent, 'resource_name': resource_name, 'category': category, 'external_uri': external_uri } expected_response = finding_pb2.Finding(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) client = securitycenter_v1beta1.SecurityCenterClient(channel=channel) # Setup Request name = client.finding_path('[ORGANIZATION]', '[SOURCE]', '[FINDING]') state = enums.Finding.State.STATE_UNSPECIFIED start_time = {} response = client.set_finding_state(name, state, start_time) assert expected_response == response assert len(channel.requests) == 1 expected_request = securitycenter_service_pb2.SetFindingStateRequest( name=name, state=state, start_time=start_time) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_list_findings(self): # Setup Expected Response next_page_token = '' total_size = 705419236 findings_element = {} findings = [findings_element] expected_response = { 'next_page_token': next_page_token, 'total_size': total_size, 'findings': findings } expected_response = securitycenter_service_pb2.ListFindingsResponse( **expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) client = securitycenter_v1beta1.SecurityCenterClient(channel=channel) # Setup Request parent = client.source_path('[ORGANIZATION]', '[SOURCE]') paged_list_response = client.list_findings(parent) resources = list(paged_list_response) assert len(resources) == 1 assert expected_response.findings[0] == resources[0] assert len(channel.requests) == 1 expected_request = securitycenter_service_pb2.ListFindingsRequest( parent=parent) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_group_findings(self): # Setup Expected Response next_page_token = '' group_by_results_element = {} group_by_results = [group_by_results_element] expected_response = { 'next_page_token': next_page_token, 'group_by_results': group_by_results } expected_response = securitycenter_service_pb2.GroupFindingsResponse( **expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) client = securitycenter_v1beta1.SecurityCenterClient(channel=channel) # Setup Request parent = client.source_path('[ORGANIZATION]', '[SOURCE]') group_by = 'groupBy506361367' paged_list_response = client.group_findings(parent, group_by) resources = list(paged_list_response) assert len(resources) == 1 assert expected_response.group_by_results[0] == resources[0] assert len(channel.requests) == 1 expected_request = securitycenter_service_pb2.GroupFindingsRequest( parent=parent, group_by=group_by) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_get_source(self): # Setup Expected Response name_2 = 'name2-1052831874' display_name = 'displayName1615086568' description = 'description-1724546052' expected_response = { 'name': name_2, 'display_name': display_name, 'description': description } expected_response = source_pb2.Source(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) client = securitycenter_v1beta1.SecurityCenterClient(channel=channel) # Setup Request name = client.source_path('[ORGANIZATION]', '[SOURCE]') response = client.get_source(name) assert expected_response == response assert len(channel.requests) == 1 expected_request = securitycenter_service_pb2.GetSourceRequest( name=name) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_update_source(self): # Setup Expected Response name = 'name3373707' display_name = 'displayName1615086568' description = 'description-1724546052' expected_response = { 'name': name, 'display_name': display_name, 'description': description } expected_response = source_pb2.Source(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) client = securitycenter_v1beta1.SecurityCenterClient(channel=channel) # Setup Request source = {} response = client.update_source(source) assert expected_response == response assert len(channel.requests) == 1 expected_request = securitycenter_service_pb2.UpdateSourceRequest( source=source) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_list_assets(self): # Setup Expected Response next_page_token = '' total_size = 705419236 list_assets_results_element = {} list_assets_results = [list_assets_results_element] expected_response = { 'next_page_token': next_page_token, 'total_size': total_size, 'list_assets_results': list_assets_results } expected_response = securitycenter_service_pb2.ListAssetsResponse( **expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch('google.api_core.grpc_helpers.create_channel') with patch as create_channel: create_channel.return_value = channel client = securitycenter_v1beta1.SecurityCenterClient() # Setup Request parent = client.organization_path('[ORGANIZATION]') paged_list_response = client.list_assets(parent) resources = list(paged_list_response) assert len(resources) == 1 assert expected_response.list_assets_results[0] == resources[0] assert len(channel.requests) == 1 expected_request = securitycenter_service_pb2.ListAssetsRequest( parent=parent) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_update_security_marks(self): # Setup Expected Response name = "name3373707" expected_response = {"name": name} expected_response = security_marks_pb2.SecurityMarks( **expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = securitycenter_v1beta1.SecurityCenterClient() # Setup Request security_marks = {} response = client.update_security_marks(security_marks) assert expected_response == response assert len(channel.requests) == 1 expected_request = securitycenter_service_pb2.UpdateSecurityMarksRequest( security_marks=security_marks) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_list_sources(self): # Setup Expected Response next_page_token = "" sources_element = {} sources = [sources_element] expected_response = { "next_page_token": next_page_token, "sources": sources } expected_response = securitycenter_service_pb2.ListSourcesResponse( **expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = securitycenter_v1beta1.SecurityCenterClient() # Setup Request parent = client.organization_path("[ORGANIZATION]") paged_list_response = client.list_sources(parent) resources = list(paged_list_response) assert len(resources) == 1 assert expected_response.sources[0] == resources[0] assert len(channel.requests) == 1 expected_request = securitycenter_service_pb2.ListSourcesRequest( parent=parent) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_run_asset_discovery(self): # Setup Expected Response expected_response = {} expected_response = empty_pb2.Empty(**expected_response) operation = operations_pb2.Operation( name="operations/test_run_asset_discovery", done=True) operation.response.Pack(expected_response) # Mock the API response channel = ChannelStub(responses=[operation]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = securitycenter_v1beta1.SecurityCenterClient() # Setup Request parent = client.organization_path("[ORGANIZATION]") response = client.run_asset_discovery(parent) result = response.result() assert expected_response == result assert len(channel.requests) == 1 expected_request = securitycenter_service_pb2.RunAssetDiscoveryRequest( parent=parent) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_group_findings(self): # Setup Expected Response next_page_token = "" group_by_results_element = {} group_by_results = [group_by_results_element] expected_response = { "next_page_token": next_page_token, "group_by_results": group_by_results, } expected_response = securitycenter_service_pb2.GroupFindingsResponse( **expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = securitycenter_v1beta1.SecurityCenterClient() # Setup Request parent = client.source_path("[ORGANIZATION]", "[SOURCE]") group_by = "groupBy506361367" paged_list_response = client.group_findings(parent, group_by) resources = list(paged_list_response) assert len(resources) == 1 assert expected_response.group_by_results[0] == resources[0] assert len(channel.requests) == 1 expected_request = securitycenter_service_pb2.GroupFindingsRequest( parent=parent, group_by=group_by) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_list_findings(self): # Setup Expected Response next_page_token = "" total_size = 705419236 findings_element = {} findings = [findings_element] expected_response = { "next_page_token": next_page_token, "total_size": total_size, "findings": findings, } expected_response = securitycenter_service_pb2.ListFindingsResponse( **expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = securitycenter_v1beta1.SecurityCenterClient() # Setup Request parent = client.source_path("[ORGANIZATION]", "[SOURCE]") paged_list_response = client.list_findings(parent) resources = list(paged_list_response) assert len(resources) == 1 assert expected_response.findings[0] == resources[0] assert len(channel.requests) == 1 expected_request = securitycenter_service_pb2.ListFindingsRequest( parent=parent) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_update_source(self): # Setup Expected Response name = "name3373707" display_name = "displayName1615086568" description = "description-1724546052" expected_response = { "name": name, "display_name": display_name, "description": description, } expected_response = source_pb2.Source(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = securitycenter_v1beta1.SecurityCenterClient() # Setup Request source = {} response = client.update_source(source) assert expected_response == response assert len(channel.requests) == 1 expected_request = securitycenter_service_pb2.UpdateSourceRequest( source=source) actual_request = channel.requests[0][1] assert expected_request == actual_request
def _security_client(self): credentials = service_account.Credentials \ .from_service_account_info(self._credentials.security_service_account_info()) scoped_credentials = credentials.with_scopes(self._CREDENTIAL_SCOPES) return \ securitycenter.SecurityCenterClient(credentials=scoped_credentials)
def test_update_finding(self): # Setup Expected Response name = 'name3373707' parent = 'parent-995424086' resource_name = 'resourceName979421212' category = 'category50511102' external_uri = 'externalUri-1385596168' expected_response = { 'name': name, 'parent': parent, 'resource_name': resource_name, 'category': category, 'external_uri': external_uri } expected_response = finding_pb2.Finding(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch('google.api_core.grpc_helpers.create_channel') with patch as create_channel: create_channel.return_value = channel client = securitycenter_v1beta1.SecurityCenterClient() # Setup Request finding = {} response = client.update_finding(finding) assert expected_response == response assert len(channel.requests) == 1 expected_request = securitycenter_service_pb2.UpdateFindingRequest( finding=finding) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_update_finding(self): # Setup Expected Response name = "name3373707" parent = "parent-995424086" resource_name = "resourceName979421212" category = "category50511102" external_uri = "externalUri-1385596168" expected_response = { "name": name, "parent": parent, "resource_name": resource_name, "category": category, "external_uri": external_uri, } expected_response = finding_pb2.Finding(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = securitycenter_v1beta1.SecurityCenterClient() # Setup Request finding = {} response = client.update_finding(finding) assert expected_response == response assert len(channel.requests) == 1 expected_request = securitycenter_service_pb2.UpdateFindingRequest( finding=finding) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_group_assets(self): # Setup Expected Response next_page_token = '' group_by_results_element = {} group_by_results = [group_by_results_element] expected_response = { 'next_page_token': next_page_token, 'group_by_results': group_by_results } expected_response = securitycenter_service_pb2.GroupAssetsResponse( **expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch('google.api_core.grpc_helpers.create_channel') with patch as create_channel: create_channel.return_value = channel client = securitycenter_v1beta1.SecurityCenterClient() # Setup Request parent = client.organization_path('[ORGANIZATION]') group_by = 'groupBy506361367' paged_list_response = client.group_assets(parent, group_by) resources = list(paged_list_response) assert len(resources) == 1 assert expected_response.group_by_results[0] == resources[0] assert len(channel.requests) == 1 expected_request = securitycenter_service_pb2.GroupAssetsRequest( parent=parent, group_by=group_by) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_get_organization_settings(self): # Setup Expected Response name_2 = 'name2-1052831874' enable_asset_discovery = False expected_response = { 'name': name_2, 'enable_asset_discovery': enable_asset_discovery } expected_response = organization_settings_pb2.OrganizationSettings( **expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) client = securitycenter_v1beta1.SecurityCenterClient(channel=channel) # Setup Request name = client.organization_settings_path('[ORGANIZATION]') response = client.get_organization_settings(name) assert expected_response == response assert len(channel.requests) == 1 expected_request = securitycenter_service_pb2.GetOrganizationSettingsRequest( name=name) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_get_iam_policy(self): # Setup Expected Response version = 351608024 etag = b'21' expected_response = {'version': version, 'etag': etag} expected_response = policy_pb2.Policy(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch('google.api_core.grpc_helpers.create_channel') with patch as create_channel: create_channel.return_value = channel client = securitycenter_v1beta1.SecurityCenterClient() # Setup Request resource = client.source_path('[ORGANIZATION]', '[SOURCE]') response = client.get_iam_policy(resource) assert expected_response == response assert len(channel.requests) == 1 expected_request = iam_policy_pb2.GetIamPolicyRequest( resource=resource) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_list_point_in_time_changes(): """Demonstrate listing assets along with their state changes.""" from google.cloud import securitycenter_v1beta1 as securitycenter from google.protobuf.duration_pb2 import Duration from datetime import timedelta # [START demo_list_assets_changes] client = securitycenter.SecurityCenterClient() # ORGANIZATION_ID is the numeric ID of the organization (e.g. 123213123121) org_name = "organizations/{org_id}".format(org_id=ORGANIZATION_ID) project_filter = ("security_center_properties.resource_type=" + '"google.cloud.resourcemanager.Project"') # List assets and their state change the last 30 days compare_delta = timedelta(days=30) # Convert the timedelta to a Duration duration_proto = Duration() duration_proto.FromTimedelta(compare_delta) # Call the API and print results. asset_iterator = client.list_assets(org_name, filter_=project_filter, compare_duration=duration_proto) for i, asset in enumerate(asset_iterator): print(i, asset) # [END demo_list_assets_changes] assert i > 0
def test_list_assets_with_filters_and_read_time(): """Demonstrate listing assets with a filter.""" from datetime import datetime, timedelta from google.cloud import securitycenter_v1beta1 as securitycenter from google.protobuf.timestamp_pb2 import Timestamp # [START demo_list_assets_with_filter_and_time] client = securitycenter.SecurityCenterClient() # ORGANIZATION_ID is the numeric ID of the organization (e.g. 123213123121) org_name = "organizations/{org_id}".format(org_id=ORGANIZATION_ID) project_filter = ("security_center_properties.resource_type=" + '"google.cloud.resourcemanager.Project"') # Lists assets as of yesterday. read_time = datetime.utcnow() - timedelta(days=1) timestamp_proto = Timestamp() timestamp_proto.FromDatetime(read_time) # Call the API and print results. asset_iterator = client.list_assets(org_name, filter_=project_filter, read_time=timestamp_proto) for i, asset_result in enumerate(asset_iterator): print(i, asset_result) # [END demo_list_assets_with_filter_and_time] assert i > 0
def test_set_iam_policy(self): # Setup Expected Response version = 351608024 etag = b"21" expected_response = {"version": version, "etag": etag} expected_response = policy_pb2.Policy(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = securitycenter_v1beta1.SecurityCenterClient() # Setup Request resource = client.source_path("[ORGANIZATION]", "[SOURCE]") policy = {} response = client.set_iam_policy(resource, policy) assert expected_response == response assert len(channel.requests) == 1 expected_request = iam_policy_pb2.SetIamPolicyRequest( resource=resource, policy=policy) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_create_source(self): # Setup Expected Response name = 'name3373707' display_name = 'displayName1615086568' description = 'description-1724546052' expected_response = { 'name': name, 'display_name': display_name, 'description': description } expected_response = source_pb2.Source(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch('google.api_core.grpc_helpers.create_channel') with patch as create_channel: create_channel.return_value = channel client = securitycenter_v1beta1.SecurityCenterClient() # Setup Request parent = client.organization_path('[ORGANIZATION]') source = {} response = client.create_source(parent, source) assert expected_response == response assert len(channel.requests) == 1 expected_request = securitycenter_service_pb2.CreateSourceRequest( parent=parent, source=source) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_test_iam_permissions(self): # Setup Expected Response expected_response = {} expected_response = iam_policy_pb2.TestIamPermissionsResponse( **expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = securitycenter_v1beta1.SecurityCenterClient() # Setup Request resource = client.source_path("[ORGANIZATION]", "[SOURCE]") permissions = [] response = client.test_iam_permissions(resource, permissions) assert expected_response == response assert len(channel.requests) == 1 expected_request = iam_policy_pb2.TestIamPermissionsRequest( resource=resource, permissions=permissions) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_get_organization_settings(self): # Setup Expected Response name_2 = "name2-1052831874" enable_asset_discovery = False expected_response = { "name": name_2, "enable_asset_discovery": enable_asset_discovery, } expected_response = organization_settings_pb2.OrganizationSettings( **expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = securitycenter_v1beta1.SecurityCenterClient() # Setup Request name = client.organization_settings_path("[ORGANIZATION]") response = client.get_organization_settings(name) assert expected_response == response assert len(channel.requests) == 1 expected_request = securitycenter_service_pb2.GetOrganizationSettingsRequest( name=name) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_update_organization_settings(self): # Setup Expected Response name = "name3373707" enable_asset_discovery = False expected_response = { "name": name, "enable_asset_discovery": enable_asset_discovery, } expected_response = organization_settings_pb2.OrganizationSettings( **expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = securitycenter_v1beta1.SecurityCenterClient() # Setup Request organization_settings = {} response = client.update_organization_settings(organization_settings) assert expected_response == response assert len(channel.requests) == 1 expected_request = securitycenter_service_pb2.UpdateOrganizationSettingsRequest( organization_settings=organization_settings) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_get_source(self): # Setup Expected Response name_2 = "name2-1052831874" display_name = "displayName1615086568" description = "description-1724546052" expected_response = { "name": name_2, "display_name": display_name, "description": description, } expected_response = source_pb2.Source(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = securitycenter_v1beta1.SecurityCenterClient() # Setup Request name = client.source_path("[ORGANIZATION]", "[SOURCE]") response = client.get_source(name) assert expected_response == response assert len(channel.requests) == 1 expected_request = securitycenter_service_pb2.GetSourceRequest( name=name) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_create_finding(self): # Setup Expected Response name = 'name3373707' parent_2 = 'parent21175163357' resource_name = 'resourceName979421212' category = 'category50511102' external_uri = 'externalUri-1385596168' expected_response = { 'name': name, 'parent': parent_2, 'resource_name': resource_name, 'category': category, 'external_uri': external_uri } expected_response = finding_pb2.Finding(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) client = securitycenter_v1beta1.SecurityCenterClient(channel=channel) # Setup Request parent = client.source_path('[ORGANIZATION]', '[SOURCE]') finding_id = 'findingId728776081' finding = {} response = client.create_finding(parent, finding_id, finding) assert expected_response == response assert len(channel.requests) == 1 expected_request = securitycenter_service_pb2.CreateFindingRequest( parent=parent, finding_id=finding_id, finding=finding) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_update_security_marks_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) client = securitycenter_v1beta1.SecurityCenterClient(channel=channel) # Setup request security_marks = {} with pytest.raises(CustomException): client.update_security_marks(security_marks)