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

feat: inject env vars into provider provision json #1910

Merged
merged 1 commit into from
Sep 12, 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
46 changes: 42 additions & 4 deletions keep/parser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,9 @@ def _parse_providers_from_env(self, context_manager: ContextManager):
self.logger.debug(
"Parsing providers from KEEP_PROVIDERS environment variable"
)
context_manager.providers_context.update(json.loads(providers_json))
providers_dict = json.loads(providers_json)
self._inject_env_variables(providers_dict)
context_manager.providers_context.update(providers_dict)
self.logger.debug(
"Providers parsed successfully from KEEP_PROVIDERS environment variable"
)
Expand All @@ -275,6 +277,7 @@ def _parse_providers_from_env(self, context_manager: ContextManager):
self.logger.debug(f"Parsing provider {provider_name} from {env}")
# {'authentication': {'webhook_url': 'https://hooks.slack.com/services/...'}}
provider_config = json.loads(os.environ.get(env))
self._inject_env_variables(provider_config)
context_manager.providers_context[provider_name] = provider_config
self.logger.debug(
f"Provider {provider_name} parsed successfully from {env}"
Expand All @@ -284,6 +287,28 @@ def _parse_providers_from_env(self, context_manager: ContextManager):
f"Error parsing provider config from environment variable {env}"
)

def _inject_env_variables(self, config):
"""
Recursively inject environment variables into the config.
"""
if isinstance(config, dict):
for key, value in config.items():
config[key] = self._inject_env_variables(value)
elif isinstance(config, list):
return [self._inject_env_variables(item) for item in config]
elif (
isinstance(config, str) and config.startswith("$(") and config.endswith(")")
):
env_var = config[2:-1]
env_var_val = os.environ.get(env_var)
if not env_var_val:
self.logger.warning(
f"Environment variable {env_var} not found while injecting into config"
)
return config
return env_var_val
return config

def _parse_providers_from_workflow(
self, context_manager: ContextManager, workflow: dict
) -> typing.List[BaseProvider]:
Expand Down Expand Up @@ -328,7 +353,14 @@ def parse_interval(self, workflow) -> int:
@staticmethod
def parse_disabled(workflow_dict: dict) -> bool:
workflow_is_disabled_in_yml = workflow_dict.get("disabled")
return True if (workflow_is_disabled_in_yml == "true" or workflow_is_disabled_in_yml is True) else False
return (
True
if (
workflow_is_disabled_in_yml == "true"
or workflow_is_disabled_in_yml is True
)
else False
)

@staticmethod
def parse_provider_parameters(provider_parameters: dict) -> dict:
Expand Down Expand Up @@ -538,7 +570,9 @@ def _merge_action_by_use(
extended_action = actions_context.get(action.get("use"), {})
yield ParserUtils.deep_merge(action, extended_action)

def _get_on_failure_action(self, context_manager: ContextManager, workflow: dict) -> Step | None:
def _get_on_failure_action(
self, context_manager: ContextManager, workflow: dict
) -> Step | None:
"""
Parse the on-failure action

Expand All @@ -552,7 +586,11 @@ def _get_on_failure_action(self, context_manager: ContextManager, workflow: dict
self.logger.debug("Parsing on-failure")
workflow_on_failure = workflow.get("on-failure", {})
if workflow_on_failure:
parsed_action = self._get_action(context_manager=context_manager, action=workflow_on_failure, action_name="on-failure")
parsed_action = self._get_action(
context_manager=context_manager,
action=workflow_on_failure,
action_name="on-failure",
)
self.logger.debug("Parsed on-failure successfully")
return parsed_action
self.logger.debug("No on-failure action")
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "keep"
version = "0.24.2"
version = "0.24.3"
description = "Alerting. for developers, by developers."
authors = ["Keep Alerting LTD"]
readme = "README.md"
Expand Down
Loading