Beispiel #1
0
def test_stacks_exists(cfngin_context: CfnginContext) -> None:
    """Test CloudFormation Stacks exists."""
    client = cfngin_context.get_session(region="us-east-1").client("cloudformation")
    assert cfngin_context.stacks, "no stacks found in context/config"
    for stack in cfngin_context.stacks:
        assert client.describe_stacks(StackName=stack.fqn)[
            "Stacks"
        ], f"unable to descrive stack: {stack.fqn}"
Beispiel #2
0
def get_principal_arn(context: CfnginContext) -> str:
    """Return ARN of current session principle."""
    # looking up caller identity
    session = context.get_session()
    sts_client = session.client("sts")
    caller_identity_arn = sts_client.get_caller_identity()["Arn"]
    if caller_identity_arn.split(":")[2] == "iam" and (
        caller_identity_arn.split(":")[5].startswith("user/")
    ):
        return caller_identity_arn  # user arn
    return assumed_role_to_principle(caller_identity_arn)
Beispiel #3
0
def test_stacks_not_exists(cfngin_context: CfnginContext) -> None:
    """Test CloudFormation Stacks don't exists."""
    client = cfngin_context.get_session(region="us-east-1").client("cloudformation")
    assert cfngin_context.stacks, "no stacks found in context/config"
    for stack in cfngin_context.stacks:
        try:
            assert not client.describe_stacks(StackName=stack.fqn)[
                "Stacks"
            ], f"stack exists: {stack.fqn}"
        except client.exceptions.ClientError as exc:
            assert "does not exist" in str(exc)
Beispiel #4
0
def invoke(
    context: CfnginContext,
    *,
    expected_status_code: int = 200,
    function_name: str,
    **_: Any,
) -> bool:
    """Invoke AWS Lambda Function and check the response."""
    LOGGER.info("invoking %s", function_name)
    assert (context.get_session().client("lambda").invoke(
        FunctionName=function_name,
        InvocationType="RequestResponse")["StatusCode"] == expected_status_code
            )
    LOGGER.info("%s returned %s", function_name, expected_status_code)
    return True
Beispiel #5
0
def delete_prefix(
    context: CfnginContext,
    *,
    bucket_name: str,
    delimiter: str = "/",
    prefix: str,
    **_: Any,
) -> bool:
    """Delete all objects with prefix."""
    if not Bucket(context, bucket_name):
        LOGGER.warning("bucket '%s' does not exist or you do not have access to it")
        return True
    bucket = context.get_session().resource("s3").Bucket(bucket_name)
    LOGGER.info(
        "deleting objects from s3://%s%s%s...",
        bucket_name,
        delimiter,
        prefix,
    )
    bucket.object_versions.filter(Delimiter=delimiter, Prefix=prefix).delete()
    return True