-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pass credentials directly to GCP connectors rather than through envir…
…onment variable (#1040) * Fix typo in filename utitities > utilities * New method to pass credentials directly to GCP clients * Pass credentials explicitly to Google clients Avoids issue documented in #1039 where credentials for all GCP clients are stored in the same environment variable, leading to overwrites if multiple clients are initialized in the same environment. * Mock credential parsing in tests Avoids mock credentials needing to match Google Service Account credential parsing * Refactor Google Admin using new authed request session * Fix tests on GoogleAdmin, expect new response structure * Small changes to get BigQuery tests working
- Loading branch information
1 parent
118d744
commit 6dfaaec
Showing
8 changed files
with
234 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,14 @@ | ||
import os | ||
import json | ||
import logging | ||
|
||
from parsons.etl.table import Table | ||
from parsons.google.utitities import setup_google_application_credentials, hexavigesimal | ||
import uuid | ||
|
||
import gspread | ||
from google.oauth2.service_account import Credentials | ||
|
||
from parsons.etl.table import Table | ||
from parsons.google.utilities import ( | ||
load_google_application_credentials, | ||
setup_google_application_credentials, | ||
hexavigesimal, | ||
) | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
@@ -32,12 +34,15 @@ def __init__(self, google_keyfile_dict=None, subject=None): | |
"https://www.googleapis.com/auth/drive", | ||
] | ||
|
||
setup_google_application_credentials(google_keyfile_dict, "GOOGLE_DRIVE_CREDENTIALS") | ||
google_credential_file = open(os.environ["GOOGLE_DRIVE_CREDENTIALS"]) | ||
credentials_dict = json.load(google_credential_file) | ||
env_credentials_path = str(uuid.uuid4()) | ||
setup_google_application_credentials( | ||
google_keyfile_dict, | ||
"GOOGLE_DRIVE_CREDENTIAL", | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
austinweisgrau
Author
Collaborator
|
||
target_env_var_name=env_credentials_path, | ||
) | ||
|
||
credentials = Credentials.from_service_account_info( | ||
credentials_dict, scopes=scope, subject=subject | ||
credentials = load_google_application_credentials( | ||
env_credentials_path, scopes=scope, subject=subject | ||
) | ||
|
||
self.gspread_client = gspread.authorize(credentials) | ||
|
@@ -47,12 +52,16 @@ def _get_worksheet(self, spreadsheet_id, worksheet=0): | |
|
||
# Check if the worksheet is an integer, if so find the sheet by index | ||
if isinstance(worksheet, int): | ||
return self.gspread_client.open_by_key(spreadsheet_id).get_worksheet(worksheet) | ||
return self.gspread_client.open_by_key(spreadsheet_id).get_worksheet( | ||
worksheet | ||
) | ||
|
||
elif isinstance(worksheet, str): | ||
idx = self.list_worksheets(spreadsheet_id).index(worksheet) | ||
try: | ||
return self.gspread_client.open_by_key(spreadsheet_id).get_worksheet(idx) | ||
return self.gspread_client.open_by_key(spreadsheet_id).get_worksheet( | ||
idx | ||
) | ||
except: # noqa: E722 | ||
raise ValueError(f"Couldn't find worksheet {worksheet}") | ||
|
||
|
@@ -280,7 +289,9 @@ def append_to_sheet( | |
|
||
# If the existing sheet is blank, then just overwrite the table. | ||
if existing_table.num_rows == 0: | ||
return self.overwrite_sheet(spreadsheet_id, table, worksheet, user_entered_value) | ||
return self.overwrite_sheet( | ||
spreadsheet_id, table, worksheet, user_entered_value | ||
) | ||
|
||
cells = [] | ||
for row_num, row in enumerate(table.data): | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
I think this should be
GOOGLE_DRIVE_CREDENTIALS
. Was this intended @austinweisgrau ?