This repository has been archived by the owner on Dec 22, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from alphagov/pmi-753-env-var-for-google-sheet
PMI-753: Read Google auth credentials from environment variables
- Loading branch information
Showing
4 changed files
with
46 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
AWS_PROFILE=verify-audit-billing-dev | ||
RP_REPORT_OUTPUT_BUCKET=govukverify-hub-integration-billing-reports | ||
PIWIK_API_TOKEN=<your-Piwik-API-token> | ||
GOOGLE_AUTH_PRIVATE_KEY_ID=<your-Google-auth-private-key-id> | ||
# GOOGLE_AUTH_PRIVATE_KEY=(cannot be set in `.env.`, must be defined in shell) | ||
GOOGLE_AUTH_CLIENT_EMAIL=<your-Google-auth-client-email> | ||
GOOGLE_AUTH_CLIENT_ID=<your-Google-auth-client-ID> | ||
GOOGLE_AUTH_CLIENT_CERT_URL=<your-Google-auth-client-cert-URL> |
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import json | ||
import os | ||
import pygsheets | ||
import tempfile | ||
|
||
from performance.env import check_get_env | ||
from performance import prod_config as config | ||
|
||
|
||
def get_pygsheets_client(): | ||
google_auth_private_key_id = os.getenv('GOOGLE_AUTH_PRIVATE_KEY_ID') | ||
if not google_auth_private_key_id: | ||
return pygsheets.authorize(service_file=config.GSHEETS_CREDENTIALS_FILE) | ||
|
||
creds = { | ||
"type": "service_account", | ||
"project_id": "verify-performance", | ||
"private_key_id": google_auth_private_key_id, | ||
# Private key cannot be set in `.env` at the moment and must therefore be defined in the | ||
# shell when running locally. | ||
"private_key": check_get_env('GOOGLE_AUTH_PRIVATE_KEY'), | ||
"client_email": check_get_env('GOOGLE_AUTH_CLIENT_EMAIL'), | ||
"client_id": check_get_env('GOOGLE_AUTH_CLIENT_ID'), | ||
"auth_uri": "https://accounts.google.com/o/oauth2/auth", | ||
"token_uri": "https://oauth2.googleapis.com/token", | ||
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", | ||
"client_x509_cert_url": check_get_env('GOOGLE_AUTH_CLIENT_CERT_URL') | ||
} | ||
temp = tempfile.NamedTemporaryFile(delete=False, mode="w+t") | ||
temp.write(json.dumps(creds)) | ||
temp.close() | ||
try: | ||
return pygsheets.authorize(service_file=temp.name) | ||
finally: | ||
os.unlink(temp.name) |
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