def bucket_conditions_guarantee_min(conditions, min_retention): """Check if other conditions can guarantee minimum retention. Args: conditions (dict): the condition dict of the bucket min_retention (int): the value of minimum retention. Returns: bool: True: min is guaranteed even if age is too small. """ age = conditions.get('age') if age is not None and age >= min_retention: return True # if createdBefore is old enough, it's OK. if 'createdBefore' in conditions: created_before = conditions['createdBefore'] dt_cfg = dt.get_datetime_from_string(created_before, '%Y-%m-%d') dt_now = dt.get_utc_now_datetime() day_diff = (dt_now - dt_cfg).days if day_diff >= min_retention: return True # if number of new version is larger than 0, OK. if conditions.get('numNewerVersions', 0) >= 1: return True return False
def _is_more_than_max_age(self, created_time, scan_time): """Check if the key has been rotated: is the key creation time older than max_age in the policy Args: created_time (str): The time at which the key was created (this is the validAfterTime in the key API response (in string_formats.DEFAULT_FORSETI_TIMESTAMP) format scan_time (datetime): Snapshot timestamp. Returns: bool: Returns true if un_rotated """ created_time = date_time.get_datetime_from_string( created_time, string_formats.DEFAULT_FORSETI_TIMESTAMP) if (scan_time - created_time).days > self.key_max_age: return True return False
def test_get_datetime_from_string(self): test_string = "2018-01-01T00:00:00Z" result = date_time.get_datetime_from_string( test_string, string_formats.TIMESTAMP_TIMEZONE) expected_result = datetime(2018, 1, 1, 0, 0, 0) self.assertEqual(expected_result, result)