Exemple #1
0
def test_can_auto_resource_policy_with_cfn(sample_app):
    swagger_gen = CFNSwaggerGenerator()
    rest_api = RestAPI(
        resource_name='dev',
        swagger_doc={},
        lambda_function=None,
        minimum_compression="",
        api_gateway_stage="xyz",
        endpoint_type="PRIVATE",
        policy=IAMPolicy({
            'Statement': [{
                "Effect": "Allow",
                "Principal": "*",
                "Action": "execute-api:Invoke",
                "Resource": "arn:aws:execute-api:*:*:*/*",
                "Condition": {
                    "StringEquals": {
                        "aws:SourceVpce": "vpce-abc123"
                    }
                }
            }]
        })
    )

    doc = swagger_gen.generate_swagger(sample_app, rest_api)
    assert doc['x-amazon-apigateway-policy'] == {
        'Statement': [{
            'Action': 'execute-api:Invoke',
            'Condition': {'StringEquals': {
                'aws:SourceVpce': 'vpce-abc123'}},
            'Effect': 'Allow',
            'Principal': '*',
            'Resource': 'arn:aws:execute-api:*:*:*/*',
            }]
    }
Exemple #2
0
def test_will_custom_auth_with_cfn(sample_app):
    swagger_gen = CFNSwaggerGenerator()

    # No "name=" kwarg provided should default
    # to a name of "auth".
    @sample_app.authorizer(ttl_seconds=10, execution_role='arn:role')
    def auth(auth_request):
        pass

    @sample_app.route('/auth', authorizer=auth)
    def foo():
        pass

    doc = swagger_gen.generate_swagger(sample_app)
    assert 'securityDefinitions' in doc
    assert doc['securityDefinitions']['auth'] == {
        'in': 'header',
        'name': 'Authorization',
        'type': 'apiKey',
        'x-amazon-apigateway-authtype': 'custom',
        'x-amazon-apigateway-authorizer': {
            'type': 'token',
            'authorizerCredentials': 'arn:role',
            'authorizerResultTtlInSeconds': 10,
            'authorizerUri': {
                'Fn::Sub': ('arn:aws:apigateway:${AWS::Region}:lambda:path'
                            '/2015-03-31/functions/${Auth.Arn}/invocations')
            }
        }
    }
Exemple #3
0
def test_will_custom_auth_with_cfn(sample_app):
    swagger_gen = CFNSwaggerGenerator()

    # No "name=" kwarg provided should default
    # to a name of "auth".
    @sample_app.authorizer(ttl_seconds=10, execution_role='arn:role')
    def auth(auth_request):
        pass

    @sample_app.route('/auth', authorizer=auth)
    def foo():
        pass

    doc = swagger_gen.generate_swagger(sample_app)
    assert 'securityDefinitions' in doc
    assert doc['securityDefinitions']['auth'] == {
        'in': 'header',
        'name': 'Authorization',
        'type': 'apiKey',
        'x-amazon-apigateway-authtype': 'custom',
        'x-amazon-apigateway-authorizer': {
            'type': 'token',
            'authorizerCredentials': 'arn:role',
            'authorizerResultTtlInSeconds': 10,
            'authorizerUri': {
                'Fn::Sub': (
                    'arn:aws:apigateway:${AWS::Region}:lambda:path'
                    '/2015-03-31/functions/${Auth.Arn}/invocations'
                )
            }
        }
    }
Exemple #4
0
def create_app_packager(config,
                        package_format='cloudformation',
                        merge_template=None):
    # type: (Config, str, Optional[str]) -> AppPackager
    osutils = OSUtils()
    ui = UI()
    application_builder = ApplicationGraphBuilder()
    deps_builder = DependencyBuilder()
    post_processors = []  # type: List[TemplatePostProcessor]
    generator = None  # type: Union[None, TemplateGenerator]

    if package_format == 'cloudformation':
        build_stage = create_build_stage(osutils, ui, CFNSwaggerGenerator())
        post_processors.extend([
            SAMCodeLocationPostProcessor(osutils=osutils),
            TemplateMergePostProcessor(osutils=osutils,
                                       merger=TemplateDeepMerger(),
                                       merge_template=merge_template)
        ])
        generator = SAMTemplateGenerator(config)
    else:
        build_stage = create_build_stage(osutils, ui,
                                         TerraformSwaggerGenerator())
        generator = TerraformGenerator(config)
        post_processors.append(
            TerraformCodeLocationPostProcessor(osutils=osutils))

    resource_builder = ResourceBuilder(application_builder, deps_builder,
                                       build_stage)

    return AppPackager(generator, resource_builder,
                       CompositePostProcessor(post_processors), osutils)
Exemple #5
0
def create_app_packager(config):
    # type: (Config) -> AppPackager
    osutils = OSUtils()
    ui = UI()
    application_builder = ApplicationGraphBuilder()
    deps_builder = DependencyBuilder()
    build_stage = create_build_stage(osutils, ui, CFNSwaggerGenerator())
    resource_builder = ResourceBuilder(application_builder, deps_builder,
                                       build_stage)
    return AppPackager(
        SAMTemplateGenerator(),
        resource_builder,
        TemplatePostProcessor(osutils=osutils),
        osutils,
    )
Exemple #6
0
def create_app_packager(config):
    # type: (Config) -> AppPackager
    osutils = OSUtils()
    # The config object does not handle a default value
    # for autogen'ing a policy so we need to handle this here.
    return AppPackager(
        # We're add place holder values that will be filled in once the
        # lambda function is deployed.
        SAMTemplateGenerator(
            CFNSwaggerGenerator('{region}', {}),
            PreconfiguredPolicyGenerator(
                config,
                ApplicationPolicyHandler(osutils,
                                         AppPolicyGenerator(osutils)))),
        LambdaDeploymentPackager())
Exemple #7
0
def create_app_packager(
        config, package_format='cloudformation',
        template_format='json', merge_template=None):
    # type: (Config, str, str, Optional[str]) -> AppPackager
    osutils = OSUtils()
    ui = UI()
    application_builder = ApplicationGraphBuilder()
    deps_builder = DependencyBuilder()
    post_processors = []  # type: List[TemplatePostProcessor]
    generator = None  # type: Union[None, TemplateGenerator]

    template_serializer = cast(TemplateSerializer, JSONTemplateSerializer())
    if package_format == 'cloudformation':
        build_stage = create_build_stage(
            osutils, ui, CFNSwaggerGenerator())
        use_yaml_serializer = template_format == 'yaml'
        if merge_template is not None and \
                YAMLTemplateSerializer.is_yaml_template(merge_template):
            # Automatically switch the serializer to yaml if they specify
            # a yaml template to merge, regardless of what template format
            # they specify.
            use_yaml_serializer = True
        if use_yaml_serializer:
            template_serializer = YAMLTemplateSerializer()
        post_processors.extend([
            SAMCodeLocationPostProcessor(osutils=osutils),
            TemplateMergePostProcessor(
                osutils=osutils,
                merger=TemplateDeepMerger(),
                template_serializer=template_serializer,
                merge_template=merge_template)])
        generator = SAMTemplateGenerator(config)
    else:
        build_stage = create_build_stage(
            osutils, ui, TerraformSwaggerGenerator())
        generator = TerraformGenerator(config)
        post_processors.append(
            TerraformCodeLocationPostProcessor(osutils=osutils))

    resource_builder = ResourceBuilder(
        application_builder, deps_builder, build_stage)

    return AppPackager(
        generator,
        resource_builder,
        CompositePostProcessor(post_processors),
        template_serializer,
        osutils)
Exemple #8
0
def create_app_packager(config, merge_template=None):
    # type: (Config, Optional[str]) -> AppPackager
    osutils = OSUtils()
    ui = UI()
    application_builder = ApplicationGraphBuilder()
    deps_builder = DependencyBuilder()
    build_stage = create_build_stage(osutils, ui, CFNSwaggerGenerator())
    resource_builder = ResourceBuilder(application_builder, deps_builder,
                                       build_stage)
    processors = [
        ReplaceCodeLocationPostProcessor(osutils=osutils),
        TemplateMergePostProcessor(
            osutils=osutils,
            merger=TemplateDeepMerger(),
            merge_template=merge_template,
        ),
    ]
    return AppPackager(
        SAMTemplateGenerator(),
        resource_builder,
        CompositePostProcessor(processors),
        osutils,
    )