Skip to content

Commit

Permalink
Add v1alpha samples for Get, Patch (Add, Delete, Replace) Reference L…
Browse files Browse the repository at this point in the history
…ist.

PiperOrigin-RevId: 604743778
  • Loading branch information
dandye authored and copybara-github committed Feb 7, 2024
1 parent 1302d85 commit fd3b7e8
Show file tree
Hide file tree
Showing 5 changed files with 429 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,11 @@ python3 -m detect.v2.<sample_name> -h
```shell
python3 -m lists.<sample_name> -h
```

### Lists API v1alpha

```
python -m lists.v1alpha.create_list -h
python -m lists.v1alpha.get_list -h
python -m lists.v1alpha.patch_list -h
```
7 changes: 7 additions & 0 deletions lists/example_input/coldriver_sha256.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
0f6b9d2ada67cebc8c0f03786c442c61c05cef5b92641ec4c1bdd8f5baeb2ee1
A949ec428116489f5e77cefc67fea475017e0f50d2289e17c3eb053072adcf24
C97acea1a6ef59d58a498f1e1f0e0648d6979c4325de3ee726038df1fc2e831d
Ac270310b5410e7430fe7e36a079525cd8724b002b38e13a6ee6e09b326f4847
84523ddad722e205e2d52eedfb682026928b63f919a7bf1ce6f1ad4180d0f507
37c52481711631a5c73a6341bd8bea302ad57f02199db7624b580058547fb5a9
C97acea1a6ef59d58a498f1e1f0e0648d6979c4325de3ee726038df1fc2e831d
6 changes: 6 additions & 0 deletions lists/example_input/foo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
foo
bar
baz
foo
bar
foo
115 changes: 115 additions & 0 deletions lists/v1alpha/get_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#!/usr/bin/env python3

# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# pylint: disable=line-too-long
"""Executable and reusable sample for getting a Reference List.
Requires the following IAM permission on the parent resource:
chronicle.referenceLists.get
https://cloud.google.com/chronicle/docs/reference/rest/v1alpha/projects.locations.instances.referenceLists/get
"""
# pylint: enable=line-too-long

import argparse
import json
from typing import Dict

from google.auth.transport import requests

from .common import chronicle_auth
from .common import project_id
from .common import project_instance
from .common import regions

SCOPES = [
"https://www.googleapis.com/auth/cloud-platform",
]


def get_list(
http_session: requests.AuthorizedSession,
proj_id: str,
proj_instance: str,
proj_region: str,
name: str,
) -> Dict[str, any]:
"""Gets a Reference List.
Args:
http_session: Authorized session for HTTP requests.
proj_id: GCP project id or number to which the target instance belongs.
proj_instance: Customer ID (uuid with dashes) for the Chronicle instance.
proj_region: region in which the target project is located.
name: name that identifies the list to get.
Returns:
Dictionary representation of the Reference List
Raises:
requests.exceptions.HTTPError: HTTP request resulted in an error
(response.status_code >= 400).
"""
# pylint: disable=line-too-long
parent = f"projects/{proj_id}/locations/{proj_region}/instances/{proj_instance}"
url = f"https://{proj_region}-chronicle.googleapis.com/v1alpha/{parent}/referenceLists/{name}"
# pylint: enable=line-too-long

response = http_session.request("GET", url)
# Expected server response:
# { # pylint: disable-next=line-too-long
# "name": "projects/<proj>/locations/us/instances/<cust-id>/referenceLists/<str>",
# "displayName": <str>,
# "revisionCreateTime": "yyyy-MM-ddThh:mm:ss.ssssssZ",
# "description": <str>,
# "entries": [
# {
# "value": <str>
# },
# ...
# ],
# "syntaxType": <enum>
# }
if response.status_code >= 400:
print(response.text)
response.raise_for_status()
return response.json()


if __name__ == "__main__":
parser = argparse.ArgumentParser()
chronicle_auth.add_argument_credentials_file(parser)
project_instance.add_argument_project_instance(parser)
project_id.add_argument_project_id(parser)
regions.add_argument_region(parser)
parser.add_argument(
"-n", "--name", type=str, required=True,
help="name that identifies the list to get"
)
args = parser.parse_args()

auth_session = chronicle_auth.initialize_http_session(
args.credentials_file,
SCOPES,
)
a_list = get_list(
auth_session,
args.project_id,
args.project_instance,
args.region,
args.name,
)
print(json.dumps(a_list, indent=2))
Loading

0 comments on commit fd3b7e8

Please sign in to comment.