-
Notifications
You must be signed in to change notification settings - Fork 92
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
Changes from 6 commits
9c3fa49
7f6a63e
6c559d6
4b4ef06
18c47c0
dfc5a8b
afef363
9ce571f
e5bdf45
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,9 +56,7 @@ | |
|
||
Returns: | ||
API key if it exists, otherwise an empty string. | ||
|
||
TODO: use this method everywhere else in this file | ||
""" | ||
""" | ||
# Try to get the key from the environment | ||
for k in env_keys: | ||
if os.environ.get(k): | ||
|
@@ -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}') | ||
github-advanced-security[bot] marked this conversation as resolved.
Fixed
Show resolved
Hide resolved
|
||
return '' | ||
|
||
# If key is not found, return an empty string | ||
logging.warning(f'No key found for project: {gcp_project}; path:{gcp_path}') | ||
|
||
return '' | ||
|
||
|
||
|
@@ -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') | ||
|
||
|
@@ -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') | ||
|
@@ -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( | ||
) | ||
|
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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!