Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove and cleanup impersonation #42

Merged
merged 2 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 4 additions & 43 deletions src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ def create_app() -> Quart:

app.config.from_mapping(
SECRET_KEY=secrets.token_hex(16),
DATABASE=firestore.AsyncClient(
project=constants.FIREBASE_PROJECT_ID,
database=constants.FIRESTORE_DATABASE,
),
DATABASE=firestore.AsyncClient(database=constants.FIRESTORE_DATABASE),
)

app.register_blueprint(status.bp)
Expand All @@ -50,48 +47,12 @@ def create_app() -> Quart:

def initialize_firebase_app() -> None:
key: str = ".serviceAccountKey.json"
options = {
'projectId': constants.FIREBASE_PROJECT_ID,
}
if os.path.exists(key): # local testing
firebase_admin.initialize_app(credential=firebase_admin.credentials.Certificate(key),
options=options)
firebase_admin.initialize_app(credential=firebase_admin.credentials.Certificate(key))
else:
logger.info("No service account key found, using default for firebase_admin")
cred = firebase_admin.credentials.ApplicationDefault()
token = cred.get_access_token().access_token
logger.info(f'Firebase admin project_id: {cred.project_id}, token: {token}')

if constants.TARGET_SERVICE_ACCOUNT:
gcred, _ = google.auth.default()

# for testing
aud = "https://iam.googleapis.com"
token = google.oauth2.id_token.fetch_id_token(Request(), aud)
logger.info(f'Default Google token: {".".join(token.split(".")[:2])}')

gcred = impersonated_credentials.Credentials(
source_credentials=gcred,
target_principal=constants.TARGET_SERVICE_ACCOUNT,
target_scopes=["https://www.googleapis.com/auth/cloud-platform"],
lifetime=500)

# for testing
token = google.oauth2.id_token.fetch_id_token(Request(), aud)
logger.info(f'Impersonated Google token: {".".join(token.split(".")[:2])}')

# https://github.com/firebase/firebase-admin-python/issues/698
cred._g_credential = gcred
cred._project_id = constants.FIREBASE_PROJECT_ID
options['serviceAccountId'] = constants.TARGET_SERVICE_ACCOUNT

# for testing
token = cred.get_access_token().access_token
logger.info(f'Impersonated Firebase project_id: {cred.project_id}, token: {token}')

firebase_admin.initialize_app(credential=cred, options=options)
firebase_admin.initialize_app()

# test firestore connection
db = firestore.Client(project=constants.FIREBASE_PROJECT_ID,
database=constants.FIRESTORE_DATABASE)
db = firestore.Client(database=constants.FIRESTORE_DATABASE)
logger.info(f'Firestore test: {db.collection("test").document("test").get().exists}')
4 changes: 1 addition & 3 deletions src/utils/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
"419003787216-rcif34r976a9qm3818qgeqed7c582od6.apps.googleusercontent.com"
)
MICROSOFT_CLIENT_ID = "a605ffae-592a-4096-b029-78ba66b6d614" # public; used for authentication
FIREBASE_PROJECT_ID = os.getenv("FIREBASE_PROJECT_ID", SERVER_GCP_PROJECT)
FIRESTORE_DATABASE = os.getenv("FIRESTORE_DATABASE", "(default)")
TARGET_SERVICE_ACCOUNT = os.getenv("TARGET_SERVICE_ACCOUNT")
FIRESTORE_DATABASE = os.getenv("FIRESTORE_DATABASE")

MPCGWAS_SHARED_PARAMETERS = {
"NUM_SNPS": {
Expand Down
Loading