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

Use api key helper during app creation. #4969

Merged
merged 9 commits into from
Feb 21, 2025
50 changes: 14 additions & 36 deletions server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@

Returns:
API key if it exists, otherwise an empty string.

TODO: use this method everywhere else in this file
"""
"""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ultra nit: might be nice to take this helper method into a helper file, to keep this a bit emptier.
Obviously you're not introducing this so no pressure to make the change now :) but at least a TODO would be good!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that send_email.py (link) and runner.py (link) reuse the same logic!

Do you have an idea for where this helper file should live and whether I should have send_email and runner use the same function? Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Chatted offline, will move to website/shared/lib/utils.py in follow up PR today!

# Try to get the key from the environment
for k in env_keys:
if os.environ.get(k):
Expand All @@ -73,9 +71,11 @@
secret_response = secret_client.access_secret_version(name=secret_name)
return secret_response.payload.data.decode('UTF-8').replace('\n', '')
except NotFound:
logging.warning(f'No key found at {gcp_project}:{gcp_path}')
return ''

# If key is not found, return an empty string
logging.warning(f'No key found for project: {gcp_project}; path:{gcp_path}')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not necessarily true because this is also a case of the key not found in the environment

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the log to include the environment variables that were also checked.

return ''


Expand Down Expand Up @@ -315,7 +315,8 @@
app.config.from_object(cfg)

# Check DC_API_KEY is set for local dev.
if cfg.CUSTOM and cfg.LOCAL and not os.environ.get('DC_API_KEY'):
if (cfg.LITE or
(cfg.CUSTOM and cfg.LOCAL)) and not os.environ.get('DC_API_KEY'):
raise Exception(
'Set environment variable DC_API_KEY for local custom DC development')

Expand Down Expand Up @@ -377,34 +378,17 @@
app.config['MAPS_API_KEY'] = ''
else:
# Get the API key from environment first.
if os.environ.get('MAPS_API_KEY'):
app.config['MAPS_API_KEY'] = os.environ.get('MAPS_API_KEY')
elif os.environ.get('maps_api_key'):
app.config['MAPS_API_KEY'] = os.environ.get('maps_api_key')
else:
secret_client = secretmanager.SecretManagerServiceClient()
secret_name = secret_client.secret_version_path(cfg.SECRET_PROJECT,
'maps-api-key', 'latest')
secret_response = secret_client.access_secret_version(name=secret_name)
app.config['MAPS_API_KEY'] = secret_response.payload.data.decode('UTF-8')
app.config['MAPS_API_KEY'] = _get_api_key(['MAPS_API_KEY', 'maps_api_key'],
cfg.SECRET_PROJECT,
'maps-api-key')

if cfg.LOCAL:
app.config['LOCAL'] = True

# Need to fetch the API key for non gcp environment.
if cfg.LOCAL or cfg.WEBDRIVER or cfg.INTEGRATION:
# Get the API key from environment first.
if os.environ.get('DC_API_KEY'):
app.config['DC_API_KEY'] = os.environ.get('DC_API_KEY')
elif os.environ.get('dc_api_key'):
app.config['DC_API_KEY'] = os.environ.get('dc_api_key')
else:
secret_client = secretmanager.SecretManagerServiceClient()
secret_name = secret_client.secret_version_path(cfg.SECRET_PROJECT,
'mixer-api-key', 'latest')
secret_response = secret_client.access_secret_version(name=secret_name)
app.config['DC_API_KEY'] = secret_response.payload.data.decode(
'UTF-8').replace('\n', '')
app.config['DC_API_KEY'] = _get_api_key(['DC_API_KEY', 'dc_api_key'],
cfg.SECRET_PROJECT, 'mixer-api-key')

# Initialize translations
babel = Babel(app, default_domain='all')
Expand All @@ -422,18 +406,12 @@
else:
app.config['NL_TABLE'] = None

# Get the API key from environment first.
if cfg.USE_LLM:
app.config['LLM_PROMPT_TEXT'] = llm_prompt.get_prompts()
if os.environ.get('LLM_API_KEY'):
app.config['LLM_API_KEY'] = os.environ.get('LLM_API_KEY')
else:
secret_client = secretmanager.SecretManagerServiceClient()
secret_name = secret_client.secret_version_path(cfg.SECRET_PROJECT,
'palm-api-key',
'latest')
secret_response = secret_client.access_secret_version(name=secret_name)
app.config['LLM_API_KEY'] = secret_response.payload.data.decode('UTF-8')
app.config['LLM_API_KEY'] = _get_api_key(['LLM_API_KEY'],
cfg.SECRET_PROJECT,
'palm-api-key')

app.config[
'NL_BAD_WORDS'] = EMPTY_BANNED_WORDS if cfg.CUSTOM else load_bad_words(
)
Expand Down