Skip to content

Commit

Permalink
[core] Moved pymsteams to package.py
Browse files Browse the repository at this point in the history
[docs] Corrected docstring for teams and added teams to outputs
[core] Added Alert section to card (didn't have the alert_id which made
it confusing previously)
[testing] re-wrote the tests

Signed-off-by: jack1902 <[email protected]>
  • Loading branch information
jack1902 committed Jan 27, 2020
1 parent cef9a9a commit 63b3142
Show file tree
Hide file tree
Showing 5 changed files with 415 additions and 175 deletions.
3 changes: 0 additions & 3 deletions conf/lambda.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@
"threshold": 0
}
},
"third_party_libraries": [
"pymsteams"
],
"timeout": 60,
"vpc_config": {
"security_group_ids": [],
Expand Down
2 changes: 1 addition & 1 deletion docs/source/outputs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Adding a new configuration for a currently supported service is handled using ``
- ``<SERVICE_NAME>`` above should be one of the following supported service identifiers:
``aws-cloudwatch-log``, ``aws-firehose``, ``aws-lambda``, ``aws-s3``, ``aws-sns``, ``aws-sqs``,
``carbonblack``, ``github``, ``jira``, ``komand``, ``pagerduty``, ``pagerduty-incident``,
``pagerduty-v2``, ``phantom``, ``slack``
``pagerduty-v2``, ``phantom``, ``slack``, ``teams``

For example:
- ``python manage.py output slack``
Expand Down
67 changes: 55 additions & 12 deletions streamalert/alert_processor/outputs/teams.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def get_user_defined_properties(cls):
Every output should return a dict that contains a 'descriptor' with a description of the
integration being configured.
Microsoft Teams also requires a user provided 'webhook' url that is comprised of the Teams
Microsoft Teams also requires a user provided 'webhook' url that is composed of the Team's
api url and the unique integration key for this output. This value should be should be
masked during input and is a credential requirement.
Expand Down Expand Up @@ -78,6 +78,7 @@ def _format_message(cls, alert, publication, webhook_url):
Args:
alert (Alert): The alert
publication (dict): Alert relevant to the triggered rule
webhook_url (str): The webhook_url to send the card too
Returns:
pymsteams.connectorcard: The message to be sent to Teams
Expand Down Expand Up @@ -110,18 +111,12 @@ def _format_message(cls, alert, publication, webhook_url):
teams_card.text(description)
teams_card.color(card_color)

if with_record:
# Instantiate the card section
record_section = pymsteams.cardsection()

# Set the title
record_section.activityTitle("StreamAlert Alert Record")
# Add the Alert Section
teams_card.addSection(cls._generate_alert_section(alert))

# Add the raw alert as key/value pairs
for key, value in alert.record.items():
record_section.addFact(key, str(value))

teams_card.addSection(record_section)
if with_record:
# Add the record Section
teams_card.addSection(cls._generate_record_section(alert.record))

if "@teams.additional_card_sections" in publication:
teams_card = cls._add_additional_sections(
Expand All @@ -130,6 +125,51 @@ def _format_message(cls, alert, publication, webhook_url):

return teams_card

@classmethod
def _generate_record_section(cls, record):
"""Generate the record section
This adds the entire record to a section as key/value pairs
Args:
record (dict): asd
Returns:
record_section (pymsteams.cardsection): record section for the outgoing card
"""
# Instantiate the card section
record_section = pymsteams.cardsection()

# Set the title
record_section.activityTitle("StreamAlert Alert Record")

# Add the record as key/value pairs
for key, value in record.items():
record_section.addFact(key, str(value))

return record_section

@classmethod
def _generate_alert_section(cls, alert):
"""Generate the alert section
Args:
alert (Alert): The alert
Returns:
alert_section (pymsteams.cardsection): alert section for the outgoing card
"""

# Instantiate the card
alert_section = pymsteams.cardsection()

# Set the title
alert_section.activityTitle("Alert Info")

# Add basic information to the alert section
alert_section.addFact("rule_name", alert.rule_name)
alert_section.addFact("alert_id", alert.alert_id)

return alert_section

@staticmethod
def _add_additional_sections(teams_card, additional_sections):
"""Add additional card sections to the teams card
Expand Down Expand Up @@ -202,10 +242,13 @@ def _dispatch(self, alert, descriptor):
"""
creds = self._load_creds(descriptor)
if not creds:
LOGGER.error("No credentials found for descriptor: %s", descriptor)
return False

# Create the publication
publication = compose_alert(alert, self, descriptor)

# Format the message
teams_card = self._format_message(alert, publication, creds["url"])

try:
Expand Down
3 changes: 2 additions & 1 deletion streamalert_cli/manage_lambda/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class LambdaPackage:
'netaddr': 'netaddr==0.7.19',
'policyuniverse': 'policyuniverse==1.3.2.1',
'requests': 'requests==2.22.0',
'pymsteams': 'pymsteams==0.1.12'
}

def __init__(self, config):
Expand Down Expand Up @@ -226,7 +227,7 @@ class AlertProcessorPackage(LambdaPackage):
'streamalert/shared'
}
package_name = 'alert_processor'
package_libs = {'cbapi', 'netaddr', 'requests'}
package_libs = {'cbapi', 'netaddr', 'pymsteams', 'requests'}


class AlertMergerPackage(LambdaPackage):
Expand Down
Loading

0 comments on commit 63b3142

Please sign in to comment.