예제 #1
0
    def setup_outputs(self, alert):
        """Helper function to handler any output setup

        Args:
            alert (Alert): The Alert instance containing outputs to be mocked out
        """
        # Patch requests.get and requests.post
        self._setup_api_mocks()

        alert_outputs = resources.get_required_outputs()
        alert_outputs.update(alert.outputs)

        for output in alert_outputs:
            try:
                service, descriptor = output.split(':')
            except ValueError:
                LOGGER_CLI.error(
                    'Outputs should be declared in the format <SERVICE>:<DESCRIPTOR>'
                )
                continue

            if service == 'aws-s3':
                bucket = self.outputs_config[service][descriptor]
                client = boto3.client('s3', region_name=self.region)
                try:
                    # Check if the bucket exists before creating it
                    client.head_bucket(Bucket=bucket)
                except ClientError:
                    client.create_bucket(Bucket=bucket)

            elif service == 'aws-firehose':
                stream_name = self.outputs_config[service][descriptor]
                helpers.create_delivery_stream(self.region, stream_name)

            elif service == 'aws-lambda':
                lambda_function = self.outputs_config[service][descriptor]
                parts = lambda_function.split(':')
                if len(parts) == 2 or len(parts) == 8:
                    lambda_function = parts[-2]
                else:
                    lambda_function = parts[-1]
                helpers.create_lambda_function(lambda_function, self.region)

            elif service == 'aws-sns':
                topic_name = self.outputs_config[service][descriptor]
                boto3.client(
                    'sns',
                    region_name=self.region).create_topic(Name=topic_name)

            elif service == 'aws-sqs':
                queue_name = self.outputs_config[service][descriptor]
                boto3.client(
                    'sqs',
                    region_name=self.region).create_queue(QueueName=queue_name)

            elif service == 'carbonblack':
                output_name = '{}/{}'.format(service, descriptor)
                creds = {
                    'token': 'e51273c7c8e0fd9fae431cc019ab244112345678',
                    'url': 'cb.foo.bar'
                }
                helpers.put_mock_creds(output_name, creds, self.secrets_bucket,
                                       self.region, self.kms_alias)

            elif service == 'komand':
                output_name = '{}/{}'.format(service, descriptor)
                creds = {
                    'komand_auth_token':
                    '00000000-0000-0000-0000-000000000000',
                    'url': 'komand.foo.bar'
                }
                helpers.put_mock_creds(output_name, creds, self.secrets_bucket,
                                       self.region, self.kms_alias)

            elif service == 'pagerduty':
                output_name = '{}/{}'.format(service, descriptor)
                creds = {'service_key': '247b97499078a015cc6c586bc0a92de6'}
                helpers.put_mock_creds(output_name, creds, self.secrets_bucket,
                                       self.region, self.kms_alias)

            elif service == 'pagerduty-v2':
                output_name = '{}/{}'.format(service, descriptor)
                creds = {'routing_key': '247b97499078a015cc6c586bc0a92de6'}
                helpers.put_mock_creds(output_name, creds, self.secrets_bucket,
                                       self.region, self.kms_alias)

            elif service == 'pagerduty-incident':
                output_name = '{}/{}'.format(service, descriptor)
                creds = {
                    'token': '247b97499078a015cc6c586bc0a92de6',
                    'service_name': '247b97499078a015cc6c586bc0a92de6',
                    'service_id': 'SERVICEID123',
                    'escalation_policy': '247b97499078a015cc6c586bc0a92de6',
                    'escalation_policy_id': 'POLICYID123',
                    'email_from': '*****@*****.**',
                    'integration_key': '247b97499078a015cc6c586bc0a92de6'
                }
                helpers.put_mock_creds(output_name, creds, self.secrets_bucket,
                                       'us-east-1', self.kms_alias)

            elif service == 'phantom':
                output_name = '{}/{}'.format(service, descriptor)
                creds = {
                    'ph_auth_token': '6c586bc047b9749a92de29078a015cc6',
                    'url': 'phantom.foo.bar'
                }
                helpers.put_mock_creds(output_name, creds, self.secrets_bucket,
                                       self.region, self.kms_alias)

            elif service == 'slack':
                output_name = '{}/{}'.format(service, descriptor)
                creds = {'url': 'https://api.slack.com/web-hook-key'}
                helpers.put_mock_creds(output_name, creds, self.secrets_bucket,
                                       self.region, self.kms_alias)

            elif service == 'jira':
                output_name = '{}/{}'.format(service, descriptor)
                creds = {
                    'username': '******',
                    'password': '******',
                    'url': 'jira.foo.bar',
                    'project_key': 'foobar',
                    'issue_type': 'Task',
                    'aggregate': 'no'
                }
                helpers.put_mock_creds(output_name, creds, self.secrets_bucket,
                                       'us-east-1', self.kms_alias)

            elif service == 'github':
                output_name = '{}/{}'.format(service, descriptor)
                creds = {
                    'username': '******',
                    'repository': 'github-user/github-repository',
                    'access_token': 'foobar',
                    'labels': 'test-label'
                }
                helpers.put_mock_creds(output_name, creds, self.secrets_bucket,
                                       'us-east-1', self.kms_alias)
예제 #2
0
파일: test.py 프로젝트: zackpar/streamalert
    def setup_outputs(self, alert):
        """Helper function to handler any output setup

        Args:
            alert (dict): The alert dictionary containing outputs the need mocking out
        """
        # Patch requests.get and requests.post
        self._setup_requests_mocks()

        for output in alert.get('outputs', []):
            try:
                service, descriptor = output.split(':')
            except ValueError:
                LOGGER_CLI.error(
                    'Outputs should be declared in the format <SERVICE>:<DESCRIPTOR>'
                )
                continue

            if service == 'aws-s3':
                bucket = self.outputs_config[service][descriptor]
                client = boto3.client('s3', region_name=self.region)
                try:
                    # Check if the bucket exists before creating it
                    client.head_bucket(Bucket=bucket)
                except ClientError:
                    client.create_bucket(Bucket=bucket)

            elif service == 'aws-firehose':
                stream_name = self.outputs_config[service][descriptor]
                helpers.create_delivery_stream(self.region, stream_name)

            elif service == 'aws-lambda':
                lambda_function = self.outputs_config[service][descriptor]
                parts = lambda_function.split(':')
                if len(parts) == 2 or len(parts) == 8:
                    lambda_function = parts[-2]
                else:
                    lambda_function = parts[-1]
                helpers.create_lambda_function(lambda_function, self.region)
            elif service == 'pagerduty':
                output_name = '{}/{}'.format(service, descriptor)
                creds = {'service_key': '247b97499078a015cc6c586bc0a92de6'}
                helpers.put_mock_creds(output_name, creds, self.secrets_bucket,
                                       self.region, self.kms_alias)

            elif service == 'pagerduty-v2':
                output_name = '{}/{}'.format(service, descriptor)
                creds = {'routing_key': '247b97499078a015cc6c586bc0a92de6'}
                helpers.put_mock_creds(output_name, creds, self.secrets_bucket,
                                       self.region, self.kms_alias)

            elif service == 'pagerduty-incident':
                output_name = '{}/{}'.format(service, descriptor)
                creds = {
                    'token': '247b97499078a015cc6c586bc0a92de6',
                    'service_name': '247b97499078a015cc6c586bc0a92de6',
                    'escalation_policy': '247b97499078a015cc6c586bc0a92de6',
                    'email_from': '*****@*****.**',
                    'integration_key': '247b97499078a015cc6c586bc0a92de6'
                }
                helpers.put_mock_creds(output_name, creds, self.secrets_bucket,
                                       'us-east-1', self.kms_alias)

            elif service == 'phantom':
                output_name = '{}/{}'.format(service, descriptor)
                creds = {
                    'ph_auth_token': '6c586bc047b9749a92de29078a015cc6',
                    'url': 'phantom.foo.bar'
                }
                helpers.put_mock_creds(output_name, creds, self.secrets_bucket,
                                       self.region, self.kms_alias)

            elif service == 'slack':
                output_name = '{}/{}'.format(service, descriptor)
                creds = {'url': 'https://api.slack.com/web-hook-key'}
                helpers.put_mock_creds(output_name, creds, self.secrets_bucket,
                                       self.region, self.kms_alias)

            elif service == 'jira':
                output_name = '{}/{}'.format(service, descriptor)
                creds = {
                    'username': '******',
                    'password': '******',
                    'url': 'jira.foo.bar',
                    'project_key': 'foobar',
                    'issue_type': 'Task',
                    'aggregate': 'no'
                }
                helpers.put_mock_creds(output_name, creds, self.secrets_bucket,
                                       'us-east-1', self.kms_alias)

            elif service == 'github':
                output_name = '{}/{}'.format(service, descriptor)
                creds = {
                    'username': '******',
                    'repository': 'github-user/github-repository',
                    'access_token': 'foobar',
                    'labels': 'test-label'
                }
                helpers.put_mock_creds(output_name, creds, self.secrets_bucket,
                                       'us-east-1', self.kms_alias)