forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirebase_admin_runtime.py
More file actions
114 lines (90 loc) · 4 KB
/
Copy pathfirebase_admin_runtime.py
File metadata and controls
114 lines (90 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
"""Runtime fences for Firebase Admin in isolated local QA stacks."""
from __future__ import annotations
import os
from typing import Any, Mapping
_AUTH_MUTATORS = frozenset(
{
"create_custom_token",
"create_oidc_provider_config",
"create_saml_provider_config",
"create_session_cookie",
"create_user",
"delete_oidc_provider_config",
"delete_saml_provider_config",
"delete_user",
"delete_users",
"generate_email_verification_link",
"generate_password_reset_link",
"generate_sign_in_with_email_link",
"import_users",
"revoke_refresh_tokens",
"set_custom_user_claims",
"update_oidc_provider_config",
"update_saml_provider_config",
"update_user",
}
)
def firebase_verify_only_enabled(environ: Mapping[str, str] | None = None) -> bool:
source = os.environ if environ is None else environ
return source.get("OMI_JIT_QA_LOCAL_STACK", "").strip() == "1" or (
source.get("OMI_JIT_QA_AUTH_ONLY", "").strip().casefold() in {"1", "true", "yes", "on"}
and (source.get("OMI_ENV_STAGE") or "").strip().casefold() == "dev"
and (source.get("GOOGLE_CLOUD_PROJECT") or "").strip() == "based-hardware-dev"
)
def firebase_verify_only_credential(environ: Mapping[str, str] | None = None) -> Any | None:
"""Return an anonymous Admin credential for ID-token verification only.
Firebase ID-token verification downloads public certificates and uses the
explicit project ID; it does not need an OAuth access token. Returning
AnonymousCredentials prevents the Admin client from borrowing development
ADC for an Auth mutation.
"""
if not firebase_verify_only_enabled(environ):
return None
from firebase_admin import credentials
from google.auth.credentials import AnonymousCredentials
class VerifyOnlyCredential(credentials.Base):
def get_credential(self) -> AnonymousCredentials:
return AnonymousCredentials()
return VerifyOnlyCredential()
def install_firebase_auth_mutation_guard(
environ: Mapping[str, str] | None = None, *, auth_module: Any | None = None
) -> bool:
"""Mechanically deny every Firebase Auth mutation in local JIT QA."""
if not firebase_verify_only_enabled(environ):
return False
if auth_module is None:
from firebase_admin import auth as auth_module
def blocked(*_args: Any, **_kwargs: Any) -> Any:
raise RuntimeError("Firebase Auth mutations are disabled in local JIT QA")
missing = [name for name in _AUTH_MUTATORS if not hasattr(auth_module, name)]
if missing:
raise RuntimeError("Firebase Auth mutation guard is incomplete: " + ", ".join(sorted(missing)))
for name in _AUTH_MUTATORS:
setattr(auth_module, name, blocked)
return True
def install_google_adc_guard(
environ: Mapping[str, str] | None = None, *, google_auth_module: Any | None = None
) -> bool:
"""Deny ADC discovery in general local-JIT backend processes.
The separate loopback Vertex broker deliberately does not set
``OMI_JIT_QA_LOCAL_STACK`` and is therefore the only child that can use the
host's development ADC.
"""
source = os.environ if environ is None else environ
# The local hermetic stack must deny all ADC discovery because it uses the
# loopback Vertex broker. Cloud QA still needs Firestore ADC for its
# isolated data plane, so its auth-only fence is intentionally narrower.
if source.get("OMI_JIT_QA_LOCAL_STACK", "").strip() != "1":
return False
if google_auth_module is None:
import google.auth as google_auth_module
def blocked(*_args: Any, **_kwargs: Any) -> Any:
raise RuntimeError("Google ADC is disabled in local JIT QA; use the loopback Vertex gateway")
google_auth_module.default = blocked
return True
__all__ = [
"firebase_verify_only_credential",
"firebase_verify_only_enabled",
"install_firebase_auth_mutation_guard",
"install_google_adc_guard",
]