Repository: Netflix/consoleme
File: consoleme/celery/celery_tasks.py (Python)

Code before:
                region=config.region,
                filter="User",
            )

            for user in iam_users:
                kd = get_user_access_keys(account_number=account_id,
                                   assume_role=config.get("policies.role_name"),
                                   region=config.region, user=user)
                for key_details in kd:
                    key_data.append(key_details.get("AccessKeyId"))
        put_object(
            Bucket=config.get("get_inventory_of_iam_keys.Bucket"),
            assume_role=config.get("get_inventory_of_iam_keys.assume_role"),
            account_number=config.get("get_inventory_of_iam_keys.account_number"),
            region=config.get("get_inventory_of_iam_keys.region"),
            Key=config.get("get_inventory_of_iam_keys.Key"),
            Body=json.dumps(key_data),
            session_name='consoleme_dirtylaundry'
        )
    log_data = {
        "function": function,
        "message": "Successfully written to the S3 bucket"
    }
    log.debug(log_data)
    stats.count(f"{function}.success")
    return True


@rate_limited()
@sts_conn('s3')
def put_object(client=None, **kwargs):
    """Create an S3 object -- calls wrapped with CloudAux."""
    client.put_object(**kwargs)


schedule = {
    "alert_on_group_changes": {
        "task": "consoleme.celery.celery_tasks.alert_on_group_changes",
        "options": {"expires": 600},
        "schedule": schedule_30_minute,
    },
    "cache_roles_across_accounts": {
        "task": "consoleme.celery.celery_tasks.cache_roles_across_accounts",
        "options": {"expires": 1800},
        "schedule": schedule_45_minute,
    },
    "clear_old_redis_iam_cache": {
        "task": "consoleme.celery.celery_tasks.clear_old_redis_iam_cache",
        "options": {"expires": 1000},
        "schedule": schedule_6_hours,
    },

Review comment (suggestion):
Instead of returning True, let's return `log_data`. That way, we can see relevant metadata at ConsoleMe flower.

Code after:
                        for key_details in kd:
                            key_data.append(key_details.get("AccessKeyId"))
                except Exception as e:
                    log_data["error"] = e
                    log.error(log_data, exc_info=True)
            put_object(
                Bucket=config.get("get_inventory_of_iam_keys.bucket"),
                assume_role=config.get("get_inventory_of_iam_keys.assume_role"),
                account_number=config.get("get_inventory_of_iam_keys.account_number"),
                region=config.get("get_inventory_of_iam_keys.region"),
                Key=config.get("get_inventory_of_iam_keys.key"),
                Body=json.dumps(key_data),
                session_name=config.get("get_inventory_of_iam_keys.session_name")
            )
    log_data["total_iam_access_key_id"] = len(key_data)
    log.debug(log_data)
    stats.count(f"{function}.success")
    return log_data


schedule_30_minute = timedelta(seconds=1800)
schedule_45_minute = timedelta(seconds=2700)
schedule_6_hours = timedelta(hours=6)
schedule_minute = timedelta(minutes=1)
schedule_5_minutes = timedelta(minutes=5)
schedule_24_hours = timedelta(hours=24)

if config.get("development", False):
    # If debug mode, we will set up the schedule to run the next minute after the job starts
    time_to_start = datetime.utcnow() + timedelta(minutes=1)
    dev_schedule = crontab(hour=time_to_start.hour, minute=time_to_start.minute)
    schedule_30_minute = dev_schedule
    schedule_45_minute = dev_schedule
    schedule_6_hours = dev_schedule

schedule = {
    "alert_on_group_changes": {
        "task": "consoleme.celery.celery_tasks.alert_on_group_changes",
        "options": {"expires": 600},
        "schedule": schedule_30_minute,
    },
    "cache_roles_across_accounts": {
        "task": "consoleme.celery.celery_tasks.cache_roles_across_accounts",
        "options": {"expires": 1800},
        "schedule": schedule_45_minute,
    },
    "clear_old_redis_iam_cache": {
        "task": "consoleme.celery.celery_tasks.clear_old_redis_iam_cache",
        "options": {"expires": 1000},
        "schedule": schedule_6_hours,
    },