Skip to content

Commit 78adea3

Browse files
dandyecopybara-github
authored andcommitted
Add v1alpha samples for Get, Patch (Add, Delete, Replace) Reference List.
PiperOrigin-RevId: 606380691
1 parent 0fbad34 commit 78adea3

File tree

6 files changed

+599
-0
lines changed

6 files changed

+599
-0
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,11 @@ python3 -m detect.v2.<sample_name> -h
5757
```shell
5858
python3 -m lists.<sample_name> -h
5959
```
60+
61+
### Lists API v1alpha
62+
63+
```
64+
python -m lists.v1alpha.create_list -h
65+
python -m lists.v1alpha.get_list -h
66+
python -m lists.v1alpha.patch_list -h
67+
```
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
0f6b9d2ada67cebc8c0f03786c442c61c05cef5b92641ec4c1bdd8f5baeb2ee1
2+
A949ec428116489f5e77cefc67fea475017e0f50d2289e17c3eb053072adcf24
3+
C97acea1a6ef59d58a498f1e1f0e0648d6979c4325de3ee726038df1fc2e831d
4+
Ac270310b5410e7430fe7e36a079525cd8724b002b38e13a6ee6e09b326f4847
5+
84523ddad722e205e2d52eedfb682026928b63f919a7bf1ce6f1ad4180d0f507
6+
37c52481711631a5c73a6341bd8bea302ad57f02199db7624b580058547fb5a9

lists/example_input/foo.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
foo
2+
bar
3+
baz
4+
foo
5+
bar
6+
foo

lists/v1alpha/get_list.py

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#!/usr/bin/env python3
2+
3+
# Copyright 2024 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
r"""Executable and reusable sample for getting a Reference List.
18+
19+
Usage:
20+
python -m lists.v1alpha.get_list \
21+
--project_id=<PROJECT_ID> \
22+
--project_instance=<PROJECT_INSTANCE> \
23+
--name="COLDRIVER_SHA256"
24+
25+
API reference:
26+
https://cloud.google.com/chronicle/docs/reference/rest/v1alpha/projects.locations.instances.referenceLists/get
27+
https://cloud.google.com/chronicle/docs/reference/rest/v1alpha/projects.locations.instances.referenceLists#ReferenceList
28+
https://cloud.google.com/chronicle/docs/reference/rest/v1alpha/projects.locations.instances.referenceLists#ReferenceListEntry
29+
"""
30+
31+
import argparse
32+
import json
33+
from typing import Dict
34+
35+
from common import chronicle_auth
36+
from common import project_id
37+
from common import project_instance
38+
from common import regions
39+
40+
from google.auth.transport import requests
41+
42+
CHRONICLE_API_BASE_URL = "https://chronicle.googleapis.com"
43+
SCOPES = [
44+
"https://www.googleapis.com/auth/cloud-platform",
45+
]
46+
47+
48+
def get_list(
49+
http_session: requests.AuthorizedSession,
50+
proj_id: str,
51+
proj_instance: str,
52+
proj_region: str,
53+
name: str,
54+
) -> Dict[str, any]:
55+
"""Gets a Reference List.
56+
57+
Args:
58+
http_session: Authorized session for HTTP requests.
59+
proj_id: GCP project id or number to which the target instance belongs.
60+
proj_instance: Customer ID (uuid with dashes) for the Chronicle instance.
61+
proj_region: region in which the target project is located.
62+
name: name that identifies the list to get.
63+
64+
Returns:
65+
Dictionary representation of the Reference List
66+
67+
Raises:
68+
requests.exceptions.HTTPError: HTTP request resulted in an error
69+
(response.status_code >= 400).
70+
"""
71+
base_url_with_region = regions.url_always_prepend_region(
72+
CHRONICLE_API_BASE_URL,
73+
proj_region
74+
)
75+
# pylint: disable-next=line-too-long
76+
parent = f"projects/{proj_id}/locations/{proj_region}/instances/{proj_instance}"
77+
url = f"{base_url_with_region}/v1alpha/{parent}/referenceLists/{name}"
78+
79+
response = http_session.request("GET", url)
80+
# Expected server response is described in:
81+
# https://cloud.google.com/chronicle/docs/reference/rest/v1alpha/projects.locations.instances.referenceLists#ReferenceList
82+
if response.status_code >= 400:
83+
print(response.text)
84+
response.raise_for_status()
85+
return response.json()
86+
87+
88+
if __name__ == "__main__":
89+
parser = argparse.ArgumentParser()
90+
chronicle_auth.add_argument_credentials_file(parser)
91+
project_instance.add_argument_project_instance(parser)
92+
project_id.add_argument_project_id(parser)
93+
regions.add_argument_region(parser)
94+
parser.add_argument(
95+
"-n", "--name", type=str, required=True,
96+
help="name that identifies the list to get"
97+
)
98+
args = parser.parse_args()
99+
100+
auth_session = chronicle_auth.initialize_http_session(
101+
args.credentials_file,
102+
SCOPES,
103+
)
104+
a_list = get_list(
105+
auth_session,
106+
args.project_id,
107+
args.project_instance,
108+
args.region,
109+
args.name,
110+
)
111+
print(json.dumps(a_list, indent=2))

0 commit comments

Comments
 (0)