Ejemplo n.º 1
0
def create_cloudformation_template_init_buckets3():
    # Create Header for the YML
    stack = dict(AWSTemplateFormatVersion='2010-09-09',
                 Description='Bonfire project S3 bucket',
                 Resources={
                     "S3Bucket": {
                         "Type": "AWS::S3::Bucket",
                         "Properties": {
                             "BucketName":
                             "bonfire-bucket-{}".format(
                                 Utils.generate_random_word(10))
                         }
                     }
                 })

    return stack
Ejemplo n.º 2
0
def check_sns_topic(topic_arn_list, category):
    # Dict of the topic resources
    topics = []
    topic_resources = []

    for topic_arn in topic_arn_list:

        subscriptions = None

        # Check if the topic arn has any suscription
        if type(topic_arn) is dict:
            topic = list(topic_arn.keys())[0]
            subscriptions = list(topic_arn.values())[0]
            topic_arn = topic

        if type(topic_arn) is str:
            topic_arn = topic_arn

        # If arn resource exist query
        # logger.info('Checking if the SNS topic exist: {}'.format(topic_arn))
        if topic_arn.startswith('arn'):
            _sns_topic_ref = topic_arn
        else:
            # logger.info('SNS topic "{}" doesn\'t exist and will be create'.format(topic_arn))
            _sns_topic_name = "{0}-sns-topic-{1}".format(
                category, Utils.generate_random_word(5))
            # Update topic name with the correct
            _sns_topic_name_ref = _sns_topic_name.replace("-", "")
            _sns_topic_ref = "!Ref {}".format(_sns_topic_name_ref)

            sns_topic_resource = {
                _sns_topic_name_ref: {
                    "Type": "AWS::SNS::Topic",
                    "Properties": {
                        "DisplayName": topic_arn,
                        "TopicName": _sns_topic_name
                    }
                }
            }

            # Add resources
            topic_resources.append(sns_topic_resource)
            SetupLogger.logger.debug(
                'SNS Topic {} added to Cloudformation Template.'.format(
                    _sns_topic_name))
        # Add topic ref
        topics.append(_sns_topic_ref)

        # Check if any suscriptions exist
        if subscriptions is not None:
            for subscription in subscriptions:
                _suscription_name = "{0}-suscription-{1}".format(
                    category, Utils.generate_random_word(5))

                # Update topic name with the correct
                _suscription_name_ref = _suscription_name.replace("-", "")

                subscription_resource = {
                    _suscription_name_ref: {
                        "Type": "AWS::SNS::Subscription",
                        "Properties": {
                            "Endpoint": subscription["Endpoint"],
                            "Protocol": subscription["Protocol"],
                            "TopicArn": _sns_topic_ref
                        }
                    }
                }

                # Add resources
                topic_resources.append(subscription_resource)

    return {'topics': topics, 'resources': topic_resources}
Ejemplo n.º 3
0
def create_cloudformation_template_alerts(alert_yml_data=None):
    print("[plugin: AWS] Creating cloudformation alerts template")
    """    
    _aws_account = None
    _category = None
    _sub_category = None
    _monitoring_system = None
    _environment = None
    sns_topic = None
    _namespace = None
    _alarm_name = None
    _alarm_description = None   
    _threshold = None
    _evaluation_period = None
    _period = None
    _metric_name = None
    _statistic = None
    _ok_action = None
    _alarm_action = None
    _dimensions = None
    _treat_missing_data = None
    _severity = None
    """
    _action_enabled = True
    _comparison_operator = None

    # Create Header for the YML
    stack = dict(AWSTemplateFormatVersion='2010-09-09',
                 Description='Monitoring alert stack',
                 Resources={})

    # Query all categories inside the yml alert file

    for category in alert_yml_data:
        # Must use
        _category = str(category)

        # Get Monitoring System
        for monitoring_system in alert_yml_data.get(_category):
            # print 'Monitoring System:',  monitoring_system
            _monitoring_system = str(monitoring_system)

            # Check if the monitor tool is AWS
            if _monitoring_system == "AWS":
                _account = str(
                    alert_yml_data[_category][_monitoring_system]['Account'])

                # Add the description of the stack

                stack[
                    'Description'] += " for {} category, generated by bonfire project".format(
                        _category)

                # Check if the SNS Topic exist
                _sns_topic_list = check_sns_topic(
                    alert_yml_data[_category][_monitoring_system]['SNS'],
                    category)

                for sns_resource in _sns_topic_list['resources']:
                    for key, val in sns_resource.items():
                        stack['Resources'][key] = val

                # Loop for each cloudwatch alert definition
                for name, alert in alert_yml_data[_category][
                        _monitoring_system]['Cloudwatch'].items():
                    _namespace = alert['Namespace']
                    _sub_category = alert['Subcategory']
                    _environment = alert['Env']
                    _metric_name = alert['MetricName']
                    _alarm_description = alert['Desc']
                    _severity = alert['Severity']
                    _threshold = alert['Threshold']
                    _evaluation_period = alert['EvaluationPeriods']

                    _period = alert['Period']
                    _statistic = alert['Statistic']

                    # _ok_action = list(SNS_TOPICS_LIST['topics'])
                    _ok_action = list(_sns_topic_list['topics'])
                    _alarm_action = list(_sns_topic_list['topics'])
                    _dimensions = alert['Dimensions']
                    _insufficient_data = list(_sns_topic_list['topics'])

                    if str(alert['Comparator']) == '>':
                        _comparison_operator = 'GreaterThanThreshold'
                    elif str(alert['Comparator']) == '<':
                        _comparison_operator = 'LessThanThreshold'
                    elif str(alert['Comparator']) == '>=':
                        _comparison_operator = 'GreaterThanOrEqualToThreshold'
                    elif str(alert['Comparator']) == '<=':
                        _comparison_operator = 'LessThanOrEqualToThreshold'

                    _alarm_name = "-".join([
                        _category, _sub_category, _environment, _metric_name,
                        _comparison_operator,
                        str(_threshold),
                        str(_evaluation_period), _severity
                    ])

                    # Check if the variable TreatMissingData exist
                    try:
                        _treat_missing_data = alert['TreatMissingData']
                    # I should improve this ....
                    except Exception as e:
                        _treat_missing_data = 'missing'

                    _alarm_name_cloudformation = (_alarm_name.replace(
                        '-', '').replace('_', '').replace(
                            '.', '')) + Utils.generate_random_word(4)

                    stack['Resources'][_alarm_name_cloudformation] = \
                        {"Type": "AWS::CloudWatch::Alarm",
                         "Properties": {"ActionsEnabled": _action_enabled,
                                        "AlarmActions": _alarm_action,
                                        "AlarmDescription": _alarm_description,
                                        "AlarmName": _alarm_name,
                                        "ComparisonOperator": _comparison_operator,
                                        "Dimensions": _dimensions,
                                        "EvaluationPeriods": _evaluation_period,
                                        "Period": _period,
                                        # ExtendedStatistic = 'String',
                                        "InsufficientDataActions": _insufficient_data,
                                        "MetricName": _metric_name,
                                        "Namespace": _namespace,
                                        "OKActions": _ok_action,
                                        "Statistic": _statistic,
                                        "Threshold": _threshold,
                                        "TreatMissingData": _treat_missing_data
                                        }
                         }
                    SetupLogger.logger.debug(
                        ('Alarm {} added to Cloudformation Template.'.format(
                            _alarm_name)))

                # Write template to file
                stack_name = "{0}-{1}-alerts-stack".format(_account, category)
                stack_file_path = os.path.join(_path_deployment_plugin,
                                               stack_name + ".yml")
                print("[plugin: AWS] '{0}' stack created in directory '{1}'".
                      format(stack_name, stack_file_path))
                write_cloudformation_template_to_file(stack, stack_file_path)