<commit_before># Copyright 2017-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You may
# not use this file except in compliance with the License. A copy of the License is located at
#
#        http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for
# the specific language governing permissions and limitations under the License.
'''
Rule Name:
  ELASTICACHE_REDIS_CLUSTER_AUTOMATIC_BACKUP_CHECK

Description:
  Check whether the Amazon ElastiCache Redis clusters have automatic backup turned on. The rule is NON_COMPLIANT if the SnapshotRetentionLimit of an Amazon Elasticache Redis cluster is 0.

Trigger:
  Periodic

Reports on:
  AWS::ElastiCache::CacheCluster

Rule Parameters:
  SnapshotRetentionPeriod
   (Optional) Minimum snapshot retention period in days for Amazon ElastiCache Redis cluster. Default is 15 days.

Scenarios:
  Scenario: 1
     Given: No Amazon ElastiCache Redis cluster in the AWS Account
      Then: Return "NOT_APPLICABLE"
  Scenario: 2
     Given: Parameter SnapshotRetentionPeriod is configured
       And: It is not a positive integer greater then 0
      Then: Return an error
  Scenario: 3
     Given: At least 1 Amazon Elasticache Redis cluster is present
       And: The SnapshotRetentionLimit is set to 0
      Then: Return NON_COMPLIANT with Annotation "Automatic backup not enabled for Amazon ElastiCache cluster {Cluster_ID}"
  Scenario: 4
     Given: At least 1 Amazon Elasticache Redis cluster is present
       And: The SnapshotRetentionLimit is less than SnapshotRetentionPeriod
      Then: Return NON_COMPLAINT with Annotation "Automatic backup retention period for Amazon ElastiCache cluster {Cluster_ID} is less then {SnapshotRetentionPeriod} day(s)."
  Scenario: 5
     Given: At least 1 Amazon Elasticache Redis cluster is present
       And: The SnapshotRetentionLimit is greater than or equal to SnapshotRetentionPeriod
      Then: Return COMPLAINT
'''
import json
import sys
import datetime
import boto3
import botocore

try:
    import liblogging
except ImportError:
    pass

##############
# Parameters #
##############

# Define the default resource to report to Config Rules
DEFAULT_RESOURCE_TYPE = 'AWS::ElastiCache::CacheCluster'
DEFAULT_PARAMETER_VALUE = 15

# Set to True to get the lambda to assume the Role attached on the Config Service (useful for cross-account).
ASSUME_ROLE_MODE = False

# Other parameters (no change needed)
CONFIG_ROLE_TIMEOUT_SECONDS = 900

#############
# Main Code #
#############

def get_replication_groups(ec_client):
    replication_groups = []
    marker = None
    replication_groups_result = {}
    while True:
        if not marker:
            replication_groups_result = ec_client.describe_replication_groups(MaxRecords=100)
        else:
            replication_groups_result = ec_client.describe_replication_groups(Marker=marker, MaxRecords=100)
        replication_groups.extend(replication_groups_result['ReplicationGroups'])
        if 'Marker' in replication_groups_result:
            marker = replication_groups_result['Marker']
        else:
            return replication_groups


def get_cache_clusters(ec_client):
    cache_clusters = []
    marker = None
    cache_clusters_result = {}
    while True:
        if not marker:
            cache_clusters_result = ec_client.describe_cache_clusters(MaxRecords=100, ShowCacheNodeInfo=False, ShowCacheClustersNotInReplicationGroups=True)
        else:
            cache_clusters_result = ec_client.describe_cache_clusters(Marker=marker, MaxRecords=100, ShowCacheNodeInfo=False, ShowCacheClustersNotInReplicationGroups=True)
        for cluster in cache_clusters_result['CacheClusters']:
            cache_clusters.append(cluster) if cluster['Engine'] == 'redis' else None
        if 'Marker' in cache_clusters_result:
            marker = cache_clusters_result['Marker']
        else:
            return cache_clusters


def generate_evaluations(evalList, key, snapshot_retention_period, event):
    evaluations = []
    for cluster in evalList:
        if cluster['SnapshotRetentionLimit'] == 0:
            evaluations.append(build_evaluation(cluster[key], 'NON_COMPLIANT', event, annotation="Automatic backup not enabled for Amazon ElastiCache cluster: {}".format(cluster[key])))
        elif cluster['SnapshotRetentionLimit'] < snapshot_retention_period:
            evaluations.append(build_evaluation(cluster[key], 'NON_COMPLIANT', event, annotation='Automatic backup retention period for Amazon ElastiCache cluster {} is less then {} day(s).'.format(cluster[key], snapshot_retention_period)))
        else:
            evaluations.append(build_evaluation(cluster[key], 'COMPLIANT', event))
    return evaluations


def evaluate_compliance(event, configuration_item, valid_rule_parameters):
    ec_client = get_client('elasticache', event)
    cache_clusters = get_cache_clusters(ec_client)
    evaluations = []
    replication_groups = get_replication_groups(ec_client)<commit_msg>modify var to eval_list<commit_after># Copyright 2017-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You may
# not use this file except in compliance with the License. A copy of the License is located at
#
#        http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for
# the specific language governing permissions and limitations under the License.
'''
Rule Name:
  ELASTICACHE_REDIS_CLUSTER_AUTOMATIC_BACKUP_CHECK

Description:
  Check whether the Amazon ElastiCache Redis clusters have automatic backup turned on. The rule is NON_COMPLIANT if the SnapshotRetentionLimit of an Amazon Elasticache Redis cluster is 0.

Trigger:
  Periodic

Reports on:
  AWS::ElastiCache::CacheCluster

Rule Parameters:
  SnapshotRetentionPeriod
   (Optional) Minimum snapshot retention period in days for Amazon ElastiCache Redis cluster. Default is 15 days.

Scenarios:
  Scenario: 1
     Given: No Amazon ElastiCache Redis cluster in the AWS Account
      Then: Return "NOT_APPLICABLE"
  Scenario: 2
     Given: Parameter SnapshotRetentionPeriod is configured
       And: It is not a positive integer greater then 0
      Then: Return an error
  Scenario: 3
     Given: At least 1 Amazon Elasticache Redis cluster is present
       And: The SnapshotRetentionLimit is set to 0
      Then: Return NON_COMPLIANT with Annotation "Automatic backup not enabled for Amazon ElastiCache cluster {Cluster_ID}"
  Scenario: 4
     Given: At least 1 Amazon Elasticache Redis cluster is present
       And: The SnapshotRetentionLimit is less than SnapshotRetentionPeriod
      Then: Return NON_COMPLAINT with Annotation "Automatic backup retention period for Amazon ElastiCache cluster {Cluster_ID} is less then {SnapshotRetentionPeriod} day(s)."
  Scenario: 5
     Given: At least 1 Amazon Elasticache Redis cluster is present
       And: The SnapshotRetentionLimit is greater than or equal to SnapshotRetentionPeriod
      Then: Return COMPLAINT
'''
import json
import sys
import datetime
import boto3
import botocore

try:
    import liblogging
except ImportError:
    pass

##############
# Parameters #
##############

# Define the default resource to report to Config Rules
DEFAULT_RESOURCE_TYPE = 'AWS::ElastiCache::CacheCluster'
DEFAULT_PARAMETER_VALUE = 15

# Set to True to get the lambda to assume the Role attached on the Config Service (useful for cross-account).
ASSUME_ROLE_MODE = False

# Other parameters (no change needed)
CONFIG_ROLE_TIMEOUT_SECONDS = 900

#############
# Main Code #
#############

def get_replication_groups(ec_client):
    replication_groups = []
    marker = None
    replication_groups_result = {}
    while True:
        if not marker:
            replication_groups_result = ec_client.describe_replication_groups(MaxRecords=100)
        else:
            replication_groups_result = ec_client.describe_replication_groups(Marker=marker, MaxRecords=100)
        replication_groups.extend(replication_groups_result['ReplicationGroups'])
        if 'Marker' in replication_groups_result:
            marker = replication_groups_result['Marker']
        else:
            return replication_groups

def get_cache_clusters(ec_client):
    cache_clusters = []
    marker = None
    cache_clusters_result = {}
    while True:
        if not marker:
            cache_clusters_result = ec_client.describe_cache_clusters(MaxRecords=100, ShowCacheNodeInfo=False, ShowCacheClustersNotInReplicationGroups=True)
        else:
            cache_clusters_result = ec_client.describe_cache_clusters(Marker=marker, MaxRecords=100, ShowCacheNodeInfo=False, ShowCacheClustersNotInReplicationGroups=True)
        for cluster in cache_clusters_result['CacheClusters']:
            cache_clusters.append(cluster) if cluster['Engine'] == 'redis' else None
        if 'Marker' in cache_clusters_result:
            marker = cache_clusters_result['Marker']
        else:
            return cache_clusters

def generate_evaluations(eval_list, key, snapshot_retention_period, event):
    evaluations = []
    for cluster in eval_list:
        if cluster['SnapshotRetentionLimit'] == 0:
            evaluations.append(build_evaluation(cluster[key], 'NON_COMPLIANT', event, annotation="Automatic backup not enabled for Amazon ElastiCache cluster: {}".format(cluster[key])))
        elif cluster['SnapshotRetentionLimit'] < snapshot_retention_period:
            evaluations.append(build_evaluation(cluster[key], 'NON_COMPLIANT', event, annotation='Automatic backup retention period for Amazon ElastiCache cluster {} is less then {} day(s).'.format(cluster[key], snapshot_retention_period)))
        else:
            evaluations.append(build_evaluation(cluster[key], 'COMPLIANT', event))
    return evaluations

def evaluate_compliance(event, configuration_item, valid_rule_parameters):
    ec_client = get_client('elasticache', event)
    cache_clusters = get_cache_clusters(ec_client)
    evaluations = []
    replication_groups = get_replication_groups(ec_client)
{'fraction_non_alphanumeric': 0.07381066794810187, 'fraction_numerical': 0.00845747236905334, 'mean_word_length': 4.216040100250627, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5112067', 'n_tokens_mistral': 2932, 'n_tokens_neox': 2811, 'n_words': 922}