Skip to content

Commit

Permalink
Capture specific environment variables on write
Browse files Browse the repository at this point in the history
  • Loading branch information
illusional committed Apr 3, 2024
1 parent e098d13 commit 11c371c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
23 changes: 23 additions & 0 deletions api/utils/db.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import logging
from os import getenv

Expand Down Expand Up @@ -29,6 +30,14 @@ def get_ar_guid(request: Request) -> str | None:
return request.headers.get('sm-ar-guid')


def get_extra_audit_log_values(request: Request) -> dict | None:
headers = request.headers.get('sm-extra-values')
if not headers:
return None

return json.loads(headers)


def get_on_behalf_of(request: Request) -> str | None:
"""
Get sm-on-behalf-of if there are requests that were performed on behalf of
Expand Down Expand Up @@ -69,12 +78,16 @@ async def dependable_get_write_project_connection(
request: Request,
author: str = Depends(authenticate),
ar_guid: str = Depends(get_ar_guid),
extra_values: dict | None = Depends(get_extra_audit_log_values),
on_behalf_of: str | None = Depends(get_on_behalf_of),
) -> Connection:
"""FastAPI handler for getting connection WITH project"""
meta = {'path': request.url.path}
if request.client:
meta['ip'] = request.client.host
if extra_values:
meta.update(extra_values)

return await ProjectPermissionsTable.get_project_connection(
project_name=project,
author=author,
Expand All @@ -89,27 +102,37 @@ async def dependable_get_readonly_project_connection(
project: str,
author: str = Depends(authenticate),
ar_guid: str = Depends(get_ar_guid),
extra_values: dict | None = Depends(get_extra_audit_log_values),
) -> Connection:
"""FastAPI handler for getting connection WITH project"""
meta = {}
if extra_values:
meta.update(extra_values)

return await ProjectPermissionsTable.get_project_connection(
project_name=project,
author=author,
readonly=True,
on_behalf_of=None,
ar_guid=ar_guid,
meta=meta,
)


async def dependable_get_connection(
request: Request,
author: str = Depends(authenticate),
ar_guid: str = Depends(get_ar_guid),
extra_values: dict | None = Depends(get_extra_audit_log_values),
):
"""FastAPI handler for getting connection withOUT project"""
meta = {'path': request.url.path}
if request.client:
meta['ip'] = request.client.host

if extra_values:
meta.update(extra_values)

return await SMConnections.get_connection_no_project(
author, ar_guid=ar_guid, meta=meta
)
Expand Down
19 changes: 19 additions & 0 deletions openapi-templates/api_client.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@ from {{packageName}}.model_utils import (
validate_and_convert_types
)

def get_select_env_values():
env_values = {
'HAIL_ATTEMPT_ID': 'HAIL_ATTEMPT_ID',
'HAIL_BATCH_ID': 'HAIL_BATCH_ID',
'HAIL_JOB_ID': 'HAIL_JOB_ID',
}

as_map = {}
for env_key, dict_key in env_values.items():
value = os.getenv(env_key)
if value:
as_map[dict_key] = value

return as_map


class ApiClient(object):
"""Generic API client for OpenAPI client library builds.
Expand Down Expand Up @@ -74,6 +89,10 @@ class ApiClient(object):
self.default_headers['sm-ar-guid'] = ar_guid
if header_name is not None:
self.default_headers[header_name] = header_value
extra_values = get_select_env_values()
if extra_values:
self.default_headers['sm-extra-values'] = json.dumps(extra_values)

self.cookie = cookie
# Set default User-Agent.
self.user_agent = '{{{httpUserAgent}}}{{^httpUserAgent}}OpenAPI-Generator/{{{packageVersion}}}/python{{/httpUserAgent}}'
Expand Down

0 comments on commit 11c371c

Please sign in to comment.