-
Notifications
You must be signed in to change notification settings - Fork 260
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
Added support for webhook_url dynamic overriding using annotations (issue 1083) #1476
Merged
+324
−35
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
dab1777
* Introduced a new attribute, webhook_override, to the MsSink class
itisallgood d61e2d4
* Added tests for MsTeamsWebhookUrlTransformer class based on tests f…
itisallgood 70dd675
Merge branch 'master' into issue-1083-dynamic-ms-teams-sink
itisallgood 9386244
* Fixed MsTeamsSinkParams's validate_webhook_override method
itisallgood eb079d3
Merge branch 'issue-1083-dynamic-ms-teams-sink' of github.com:robusta…
itisallgood d561722
* Added possibility to override webhook_url value for ms_teams_sink f…
itisallgood 33c793b
* Added docs on how to use webhook_override attribute
itisallgood 960e942
Merge branch 'master' into issue-1083-dynamic-ms-teams-sink
itisallgood 86b4d85
* Updated the docs section mentioning webhook overriding
itisallgood 09f0dda
* Removed redundant poetry virtualenv file
itisallgood 82901a0
* Updated Dynamically Route MS Teams Alerts docs section
itisallgood 3a9f117
*Added logging to MsTeamsWebhookUrlTransformer's validate_url_or_get_…
itisallgood 1660918
Merge branch 'master' into issue-1083-dynamic-ms-teams-sink
itisallgood File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
src/robusta/core/sinks/msteams/msteams_webhook_tranformer.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import logging | ||
import os | ||
from typing import Dict, Optional | ||
|
||
import regex | ||
|
||
from robusta.core.sinks.common.channel_transformer import ANNOTATIONS_PREF, MISSING, BaseChannelTransformer | ||
|
||
ANNOTATIONS_COMPOSITE_PATTERN = r".*\$({?annotations.[^$]+).*" | ||
ANNOTATIONS_ONLY_VALUE_PATTERN = r"^(annotations.[^$]+)$" | ||
URL_PATTERN = regex.compile( | ||
r"^(https?)://" | ||
r"(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|" | ||
r"localhost|" | ||
r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|" | ||
r"\[?[A-F0-9]*:[A-F0-9:]+\]?)" | ||
r"(?::\d+)?" | ||
r"(?:/?|[/?]\S+)$", | ||
regex.IGNORECASE, | ||
) | ||
|
||
|
||
# This class supports overriding the webhook_url only using annotations from yaml files. | ||
# Annotations are used instead of labels because urls can be passed to annotations contrary to labels. | ||
# Labels must be an empty string or consist of alphanumeric characters, '-', '_', or '.', | ||
# and must start and end with an alphanumeric character (e.g., 'MyValue', 'my_value', or '12345'). | ||
# The regex used for label validation is '(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])?'. | ||
class MsTeamsWebhookUrlTransformer(BaseChannelTransformer): | ||
@classmethod | ||
def validate_webhook_override(cls, v: Optional[str]) -> Optional[str]: | ||
if v: | ||
if regex.match(ANNOTATIONS_ONLY_VALUE_PATTERN, v): | ||
return "$" + v | ||
if not regex.match(ANNOTATIONS_COMPOSITE_PATTERN, v): | ||
err_msg = f"webhook_override must be '{ANNOTATIONS_PREF}foo' or contain patterns like: '${ANNOTATIONS_PREF}foo'" | ||
raise ValueError(err_msg) | ||
return v | ||
|
||
@classmethod | ||
def validate_url_or_get_env(cls, webhook_url: str, default_webhook_url: str) -> str: | ||
if URL_PATTERN.match(webhook_url): | ||
return webhook_url | ||
logging.info(f"URL matching failed for: {webhook_url}. Trying to get environment variable.") | ||
|
||
env_value = os.getenv(webhook_url) | ||
if env_value: | ||
return env_value | ||
logging.info(f"Environment variable not found for: {webhook_url}. Using default webhook URL.") | ||
|
||
return default_webhook_url | ||
|
||
@classmethod | ||
def template( | ||
cls, | ||
webhook_override: Optional[str], | ||
default_webhook_url: str, | ||
annotations: Dict[str, str], | ||
) -> str: | ||
if not webhook_override: | ||
return default_webhook_url | ||
|
||
webhook_url = webhook_override | ||
|
||
webhook_url = cls.process_template_annotations(webhook_url, annotations) | ||
if MISSING in webhook_url: | ||
return default_webhook_url | ||
webhook_url = cls.validate_url_or_get_env(webhook_url, default_webhook_url) | ||
|
||
return webhook_url |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Can you add info logs here, in case the url matching is failed?
Also is there any reference to this url pattern ?
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.
@RoiGlinik There is no reference to this url pattern as I composed it from different resources, but I added pytests with different valid and invalid urls.