Skip to content

Commit 56dd04c

Browse files
committed
add samples for using a regional endpoint with SCC v2 API
The SCC API can be accessed via Regional Endpoints to meet customer data residency requirements. This sample shows how to override the endpoint for an API client. See https://cloud.google.com/security-command-center/docs/data-residency-support.md#regional-urls for more information on data residency Combine rep sample with findings samples Fixed lint issues Update regional_endpoint_snippet_test.py Update regional_endpoint_snippet_test.py
1 parent 362c9e7 commit 56dd04c

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

securitycenter/snippets/noxfile_config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
# A dictionary you want to inject into your test. Don't put any
3333
# secrets here. These values will override predefined values.
3434
"envs": {
35+
"DRZ_SA_ORGANIZATION": "172173830708",
3536
"GCLOUD_ORGANIZATION": "1081635000895",
3637
"GCLOUD_PROJECT": "project-a-id",
3738
"GCLOUD_PUBSUB_TOPIC": "projects/project-a-id/topics/notifications-sample-topic",

securitycenter/snippets_v2/snippets_findings_v2.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,37 @@ def list_all_findings(organization_id, source_name, location_id) -> int:
5555
# [END securitycenter_list_all_findings_v2]
5656

5757

58+
# [START securitycenter_regional_endpoint_list_findings]
59+
def rep_list_finding(parent, endpoint) -> int:
60+
"""
61+
lists all findings for a parent
62+
Args:
63+
parent: Parent resource for which findings to be listed. Must be in one of the following formats:
64+
"organizations/{organization_id}/sources/{sources}/locations/{location}"
65+
"projects/{project_id}/sources/{sources}/locations/{location}"
66+
"folders/{folder_id}/sources/{sources}/locations/{location}"
67+
endpoint: Endpoint for this request. For example "securitycenter.googleapis.com", "securitycenter.me-central2.rep.googleapis.com"
68+
Returns:
69+
int: return the count of all findings for a source
70+
"""
71+
from google.cloud import securitycenter_v2 as securitycenter
72+
from google.api_core.client_options import ClientOptions
73+
# Override endpoint and create a client.
74+
options = ClientOptions(api_endpoint=endpoint)
75+
client = securitycenter.SecurityCenterClient(client_options=options)
76+
77+
finding_result_iterator = client.list_findings(request={"parent": parent})
78+
for count, finding_result in enumerate(finding_result_iterator):
79+
print(
80+
"{}: name: {} resource: {}".format(
81+
count, finding_result.finding.name, finding_result.finding.resource_name
82+
)
83+
)
84+
return count
85+
86+
# [END securitycenter_regional_endpoint_list_findings]
87+
88+
5889
# [START securitycenter_list_filtered_findings_v2]
5990
def list_filtered_findings(organization_id, source_name, location_id) -> int:
6091
"""

securitycenter/snippets_v2/snippets_findings_v2_test.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,15 @@ def finding_name(source_name):
8585
return finding.name
8686

8787

88+
@pytest.fixture(scope="module")
89+
def rep_parent():
90+
return f"{(os.environ['DRZ_SA_ORGANIZATION'])}/sources/-/locations/sa"
91+
92+
93+
def endpoint():
94+
return "securitycenter.me-central2.rep.googleapis.com"
95+
96+
8897
def test_list_all_findings(organization_id, finding_name, source_name):
8998
finding_result_iterator = snippets_findings_v2.list_all_findings(
9099
organization_id, source_name.split("/")[-1], "global"
@@ -96,6 +105,11 @@ def test_list_all_findings(organization_id, finding_name, source_name):
96105
assert finding_name in names
97106

98107

108+
def test_rep_list_finding():
109+
count = snippets_findings_v2.rep_list_finding(rep_parent, endpoint)
110+
assert count > 0
111+
112+
99113
def test_list_filtered_findings(organization_id):
100114
count = snippets_findings_v2.list_filtered_findings(organization_id, "-", "global")
101115
assert count > 0

0 commit comments

Comments
 (0)