Skip to content

Commit aec459a

Browse files
committed
feat(auth): support inline credentials for ADC
1 parent d966bc7 commit aec459a

File tree

3 files changed

+65
-4
lines changed

3 files changed

+65
-4
lines changed

src/models/rewards_eligibility_oracle.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,9 @@ def main(run_date_override: date = None):
3333
"""
3434
Main entry point for the Rewards Eligibility Oracle.
3535
This function:
36-
1. Sets up Google credentials (if not already set up by scheduler)
37-
2. Fetches and processes indexer eligibility data
38-
3. Submits eligible indexers to the blockchain
39-
4. Sends Slack notifications about the run status
36+
1. Fetches and processes indexer eligibility data
37+
2. Submits eligible indexers to the blockchain
38+
3. Sends Slack notifications about the run status
4039
4140
Args:
4241
run_date_override: If provided, use this date for the run instead of today.

src/models/scheduler.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,9 @@ def initialize(self):
159159
try:
160160
validate_all_required_env_vars()
161161

162+
# Prepare credentials using inline JSON or file paths
163+
credential_manager.prepare_credentials_for_adc()
164+
162165
# Validate credentials early (Fail Fast)
163166
try:
164167
credential_manager.get_google_credentials()

src/utils/configuration.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,65 @@ def _setup_service_account_credentials_from_dict(self, creds_data: dict) -> None
448448
raise ValueError(f"Invalid service account credentials: {e}") from e
449449

450450

451+
def prepare_credentials_for_adc(self) -> None:
452+
"""
453+
Prepare Google credentials for Application Default Credentials (ADC).
454+
455+
Supports both inline JSON and file paths:
456+
- Inline JSON: Writes temp file and updates env var
457+
- File path: Validates existence, logs warning if not found
458+
459+
This enables google.auth.default() to work with both credential sources while
460+
maintaining official API usage.
461+
462+
Raises:
463+
ValueError: If inline JSON is invalid or incomplete
464+
"""
465+
creds_env = os.environ.get("GOOGLE_APPLICATION_CREDENTIALS")
466+
467+
# If not set, log warning and return
468+
if not creds_env:
469+
logger.warning("GOOGLE_APPLICATION_CREDENTIALS not set. Will fall back to ADC.")
470+
return
471+
472+
# Inline JSON pattern
473+
if creds_env.strip().startswith("{"):
474+
creds_data = None
475+
try:
476+
# Validate JSON structure
477+
creds_data = self._parse_and_validate_credentials_json(creds_env)
478+
479+
# Write to temp file
480+
temp_path = Path("/tmp/gcp-credentials.json")
481+
with open(temp_path, "w", encoding="utf-8") as f:
482+
json.dump(creds_data, f)
483+
484+
# Set restrictive permissions
485+
temp_path.chmod(0o600)
486+
487+
# Update env var
488+
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = str(temp_path)
489+
490+
logger.info("Prepared inline JSON credentials for ADC")
491+
492+
except ValueError:
493+
# Re-raise validation errors
494+
raise
495+
496+
except Exception as e:
497+
raise ValueError(f"Failed to prepare inline credentials: {e}") from e
498+
499+
finally:
500+
# Clear data from memory
501+
if creds_data:
502+
creds_data.clear()
503+
504+
# File path pattern
505+
elif not Path(creds_env).exists():
506+
logger.warning(f"Credentials file not found: {creds_env}")
507+
logger.warning("Will attempt to use gcloud CLI or other ADC sources")
508+
509+
451510
def setup_google_credentials(self) -> None:
452511
"""
453512
Set up Google credentials directly in memory from environment variable.

0 commit comments

Comments
 (0)