Skip to content

Commit 5e4bce4

Browse files
authored
GCP / GSuite fixes (#436)
* GCP / GSuite fixes * Ignore noisy GCP warnings * Include additional filter for "this service is not enabled" errors within the GCP Client * Swap out oauth libs - Fixes #353 * Remove sum() call from datasets fetching. * Move warning filtering back into gcp/client.py
1 parent e6e9d14 commit 5e4bce4

8 files changed

Lines changed: 23 additions & 64 deletions

File tree

bin/__init__.py

Whitespace-only changes.

bin/auth/__init__.py

Whitespace-only changes.

bin/auth/setup_gsuite.py

Lines changed: 0 additions & 38 deletions
This file was deleted.

gcp/bigquery/resources.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def datasets():
1515
get_dataset(d["datasetReference"]["datasetId"], project_id)
1616
for d in datasets
1717
]
18-
return sum(results, [])
18+
return results
1919

2020

2121
def get_dataset(dataset_id, project_id):

gcp/client.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
import warnings
2+
import logging
23

34
from apiclient.discovery import build as build_service
45
from apiclient.errors import HttpError
56

7+
8+
# Filters out the warning about using end user credentials.
69
warnings.filterwarnings(
710
"ignore", "Your application has authenticated using end user credentials"
811
)
912

13+
# Filters out a warning around a feature we don't use - https://github.com/googleapis/google-api-python-client/issues/299
14+
logging.getLogger("googleapiclient.discovery_cache").setLevel(logging.ERROR)
15+
16+
# Filters out a warning about not have a default GCP Project ID. Not required for Frost, no need to display.
17+
logging.getLogger("google.auth._default").setLevel(logging.ERROR)
18+
1019

1120
def cache_key(project_id, version, product, subproduct, call="list", id_value="na"):
1221
"""Returns the fullname (directory and filename) for a cached GCP API call.
@@ -276,6 +285,8 @@ def _list_all_items(self, api_entity, call_kwargs, results_key):
276285
# This will be thrown if an API is disabled.
277286
if "has not been used in project" in e._get_reason():
278287
return []
288+
if "has not enabled" in e._get_reason():
289+
return []
279290
raise e
280291
items = sum([items], resp.get(results_key, []))
281292
try:

gsuite/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
### EXPERIMENTAL - GSuite tests
2+
3+
These tests and the gsuite client are experimental. The client currently only supports the "application default" google auth credentials.

gsuite/client.py

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,14 @@
22
import httplib2
33

44
from apiclient import discovery
5-
from oauth2client.file import Storage
5+
import google.auth
66

7-
CREDS_NAME = "frost-gsuite-readonly"
87
SCOPES = [
98
"https://www.googleapis.com/auth/admin.directory.user.readonly",
109
"https://www.googleapis.com/auth/admin.directory.group.readonly",
1110
]
1211

1312

14-
def get_credential_dir():
15-
home_dir = os.path.expanduser("~")
16-
credential_dir = os.path.join(home_dir, ".credentials")
17-
if not os.path.exists(credential_dir):
18-
os.makedirs(credential_dir)
19-
return credential_dir
20-
21-
22-
def get_credential_path(credential_name):
23-
return os.path.join(get_credential_dir(), credential_name + ".json")
24-
25-
26-
def get_credentials(credential_name):
27-
store = Storage(get_credential_path(credential_name))
28-
return store.get()
29-
30-
3113
class GsuiteClient:
3214
def __init__(self, domain, offline):
3315
self.domain = domain
@@ -37,10 +19,10 @@ def __init__(self, domain, offline):
3719
self.directory_client = self.build_directory_client()
3820

3921
def build_directory_client(self):
40-
# TODO: Support passing creds name as config option
41-
credentials = get_credentials(CREDS_NAME)
42-
http = credentials.authorize(httplib2.Http())
43-
return discovery.build("admin", "directory_v1", http=http)
22+
# TODO: Support service accounts:
23+
# https://googleapis.github.io/google-api-python-client/docs/oauth-server.html#examples
24+
credentials, _ = google.auth.default(scopes=SCOPES)
25+
return discovery.build("admin", "directory_v1", credentials=credentials)
4426

4527
def list_users(self):
4628
"""

requirements.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
botocore==1.12.75
22
click==7.1.2
33
coverage==4.5.3
4-
google_api_python_client==1.12.3
5-
oauth2client==4.1.3
4+
google-api-python-client==1.12.8
5+
google-auth==1.24.0
6+
google-auth-httplib2==0.0.4
67
pre-commit==1.17.0
78
pytest-cov==2.10.0
89
pytest-json==0.4.0

0 commit comments

Comments
 (0)