Skip to content

Commit 848f28e

Browse files
strasburgcopybara-github
authored andcommitted
Update chronicle_auth to support alternative OAuth scopes. Add a library to
specify the Chronicle customer region and to hold region-specific configuration. PiperOrigin-RevId: 384741287
1 parent 7d553c4 commit 848f28e

2 files changed

Lines changed: 31 additions & 7 deletions

File tree

common/chronicle_auth.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
import argparse
2929
import pathlib
30-
from typing import Optional, Union
30+
from typing import Optional, Sequence, Union
3131

3232
from google.auth.transport import requests
3333
from google.oauth2 import service_account
@@ -38,15 +38,18 @@
3838

3939

4040
def initialize_http_session(
41-
credentials_file_path: Optional[Union[str, pathlib.Path]]
42-
) -> requests.AuthorizedSession:
41+
credentials_file_path: Optional[Union[str, pathlib.Path]],
42+
scopes: Optional[Sequence[str]] = None) -> requests.AuthorizedSession:
4343
"""Initializes an authorized HTTP session, based on the given credentials.
4444
4545
Args:
4646
credentials_file_path: Absolute or relative path to a JSON file containing
4747
the private OAuth 2.0 credentials of a Google Cloud Platform service
4848
account. Optional - the default is ".chronicle_credentials.json" in the
4949
user's home directory. Keep it secret, keep it safe.
50+
scopes: A list of OAuth scopes (https://oauth.net/2/scope/) that are
51+
associated with the end points to be accessed. The default is the
52+
Chronicle API scope.
5053
5154
Returns:
5255
HTTP session object to send authorized requests and receive responses.
@@ -56,10 +59,9 @@ def initialize_http_session(
5659
(https://docs.python.org/library/exceptions.html#os-exceptions).
5760
ValueError: Invalid file contents.
5861
"""
59-
if not credentials_file_path:
60-
credentials_file_path = DEFAULT_CREDENTIALS_FILE
6162
credentials = service_account.Credentials.from_service_account_file(
62-
str(credentials_file_path), scopes=AUTHORIZATION_SCOPES)
63+
str(credentials_file_path or DEFAULT_CREDENTIALS_FILE),
64+
scopes=scopes or AUTHORIZATION_SCOPES)
6365
return requests.AuthorizedSession(credentials)
6466

6567

common/chronicle_auth_test.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616

1717
import os
1818
import tempfile
19+
1920
import unittest
21+
from unittest import mock
22+
from google.oauth2 import service_account
2023

2124
from . import chronicle_auth
2225

@@ -65,8 +68,27 @@ def setUp(self):
6568
os.write(fd, fake_json_credentials.strip() + fake_private_key + b'"\n}\n')
6669
os.close(fd)
6770

68-
def test_initialize_http_session_with_custom_json_credentials(self):
71+
@mock.patch.object(service_account.Credentials, "from_service_account_file")
72+
def test_initialize_http_session(self, mock_from_service_account_file):
73+
chronicle_auth.initialize_http_session("")
74+
mock_from_service_account_file.assert_called_once_with(
75+
str(chronicle_auth.DEFAULT_CREDENTIALS_FILE),
76+
scopes=chronicle_auth.AUTHORIZATION_SCOPES)
77+
78+
@mock.patch.object(service_account.Credentials, "from_service_account_file")
79+
def test_initialize_http_session_with_custom_json_credentials(
80+
self, mock_from_service_account_file):
6981
chronicle_auth.initialize_http_session(self.path)
82+
mock_from_service_account_file.assert_called_once_with(
83+
self.path, scopes=chronicle_auth.AUTHORIZATION_SCOPES)
84+
85+
@mock.patch.object(service_account.Credentials, "from_service_account_file")
86+
def test_initialize_http_session_with_custom_creds_and_scopes(
87+
self, mock_from_service_account_file):
88+
scopes = ["https://www.googleapis.com/auth/malachite-ingestion"]
89+
chronicle_auth.initialize_http_session(self.path, scopes=scopes)
90+
mock_from_service_account_file.assert_called_once_with(
91+
self.path, scopes=scopes)
7092

7193
def tearDown(self):
7294
os.remove(self.path)

0 commit comments

Comments
 (0)