Skip to content

Capture specific environment variables on write #721

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

Merged
merged 3 commits into from
Apr 3, 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
28 changes: 28 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,19 @@ 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:
"""Get a JSON encoded dictionary from the 'sm-extra-values' header if it exists"""
headers = request.headers.get('sm-extra-values')
if not headers:
return None

try:
return json.loads(headers)
except json.JSONDecodeError:
logging.error(f'Could not parse sm-extra-values: {headers}')
return None


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 +83,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 +107,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
Loading