Skip to content

Commit

Permalink
Use Base64-encoded google service account creds
Browse files Browse the repository at this point in the history
  • Loading branch information
mazursasha1990 committed Jan 9, 2025
1 parent d9317d5 commit 7f90392
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 7 deletions.
2 changes: 1 addition & 1 deletion deployment/terraform/batch.tf
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ data "template_file" "export_csv_job_definition" {
batch_job_queue_name = "queue${local.short}ExportCsv"
batch_job_def_name = "job${local.short}ExportCsv"
log_group_name = "log${local.short}Batch"
google_service_account_creds = var.google_service_account_creds
google_service_account_creds_base64 = var.google_service_account_creds_base64
google_drive_shared_directory_id = var.google_drive_shared_directory_id
}
}
Expand Down
2 changes: 1 addition & 1 deletion deployment/terraform/job-definitions/export_csv.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
{ "name": "BATCH_JOB_QUEUE_NAME", "value": "${batch_job_queue_name}" },
{ "name": "BATCH_JOB_DEF_NAME", "value": "${batch_job_def_name}" },
{ "name": "BATCH_MODE", "value": "True" },
{ "name": "GOOGLE_SERVICE_ACCOUNT_CREDS", "value": "${google_service_account_creds}" },
{ "name": "GOOGLE_SERVICE_ACCOUNT_CREDS_BASE64", "value": "${google_service_account_creds_base64}" },
{ "name": "GOOGLE_DRIVE_SHARED_DIRECTORY_ID", "value": "${google_drive_shared_directory_id}" }
],
"logConfiguration": {
Expand Down
4 changes: 2 additions & 2 deletions deployment/terraform/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -669,9 +669,9 @@ variable "export_csv_schedule_expression" {
description = "The schedule expression for the export csv job"
}

variable "google_service_account_creds" {
variable "google_service_account_creds_base64" {
type = string
description = "Credentials for the Google service account"
description = "Base64-encoded Google service account key"
}

variable "google_drive_shared_directory_id" {
Expand Down
11 changes: 8 additions & 3 deletions src/django/api/management/commands/export_csv.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import csv
import logging
import os
import base64
import json
from datetime import datetime
from django.core.management.base import BaseCommand
Expand Down Expand Up @@ -29,13 +30,17 @@ def upload_file_to_google_drive(filename):
in the environment variables.
"""

gdrive_creds = os.getenv("GOOGLE_SERVICE_ACCOUNT_CREDS")
base64_gdrive_creds = os.getenv("GOOGLE_SERVICE_ACCOUNT_CREDS_BASE64")
print('base64_gdrive_creds >>>', base64_gdrive_creds)

if gdrive_creds is None:
if base64_gdrive_creds is None:
raise ValueError("Google Service Account credentials not found!")

decoded_creds = base64.b64decode(base64_gdrive_creds).decode("utf-8")
print('decoded_creds >>>', decoded_creds)

credentials = service_account.Credentials.from_service_account_info(
info=json.loads(gdrive_creds),
info=json.loads(decoded_creds),
)
logger.info("Initialized Google Drive service account credentials")

Expand Down

0 comments on commit 7f90392

Please sign in to comment.