Example #1
0
    def setUp(self):

        self.mock_context = mock.MagicMock('context')

        self.target_swagger = {
            "swagger": "2.0",
            "info": {
                "version": "1.0.0",
                "title": ""
           },
           "paths": {
            }
        }

        self.target_swagger_navigator = SwaggerNavigator(self.target_swagger)

        self.interface_swagger = {
            "swagger": "2.0",
            "info": {
                "version": "1.0.0",
                "title": ""
           },
           "paths": {
            }
        }

        self.interface_swagger_navigator = SwaggerNavigator(self.interface_swagger)

        self.test_interface_id = 'test_interface_1_2_3'
        self.test_path = '/test_path'
        self.test_definition_name = 'test-definition-name'
        self.test_definition_value = { 'test-definition-property': 'test-definition-value' }
        self.test_mappings = {}
        self.test_lambda_dispatch_for_paths = {}
        self.test_external_parameters = None
Example #2
0
def process_swagger(context, swagger):

    # make sure we are starting with a valid document
    validate_swagger(swagger)

    # process the swagger, order matters
    swagger_navigator = SwaggerNavigator(swagger)
    interface.process_interface_implementation_objects(context,
                                                       swagger_navigator)
    lambda_dispatch.process_lambda_dispatch_objects(context, swagger_navigator)

    # make sure we produce a valid document
    validate_swagger(swagger)
    def generate_component_json(self):
        self.add_def_to_struct_type_conversions()
        self._component_json["namespace"] = self.get_namespace()
        self._component_json["componentClass"] = "{}ClientComponent".format(self._resource_group_name)
        self._component_json["resourceGroup"] = self._resource_group_name
        self._component_json["redefinitions"] = []
        self._component_json["otherClasses"] = []
        self._component_json["functions"] = []

        base_navigator = SwaggerNavigator(self._swagger)
        self.__read_functions(base_navigator.get("paths"))

        return self._component_json
Example #4
0
def process_lambda_dispatch_objects(swagger):

    swagger_navigator = SwaggerNavigator(swagger)

    # The x-amazon-cloud-canvas-lambda-dispatch objects can appear in the swagger object (root)
    # a path object or an operation object. We use these to generate x-amazon-apigateway-integration
    # objects for each operation that doesn't have one already.
    #
    # For the lambda, module, and function properties, the value specified by the "lowest" object
    # override any values provided by the "higher" object (so you can set a defaults at the top
    # and override then for individual paths or operations).
    #
    # For additional_properties, addnitional_request_template_cntent, and additional_response_template_content
    # properties, the aggregate values are injected into the x-amazon-apigateway-integration.
    #
    # We keep track of the lambda dispatch objects that are "in scope" in the displatch_object_stack.
    # This starts with the one in the swagger object, then the one for a "current" path, then
    # one for the "current" operation (the stack will never have more than three entries).
    #
    # As part of this processing, we need to know an operation's current parameters. Swagger
    # allows parameters to be defined in the path or operation object, with parameters in the
    # operation object overriding those in the path object. Swagger lets you put paramter
    # definitions in the swagger object, but these are not used for any path/operation unless
    # the path/operation parameters use $ref to identify one of these definitions. We use the
    # parameter_object_stack to keep track of the "current" path and operation parameters.

    dispatch_object_stack = []
    parameters_object_stack = []

    dispatch_object_stack.append(
        swagger_navigator.remove_object(LAMBDA_DISPATCH_OBJECT_NAME, {}))

    missing_security_warnings = []

    global_security_object = swagger_navigator.get_object('security',
                                                          default=None)

    for path, path_object in swagger_navigator.get_object('paths').items():

        dispatch_object_stack.append(
            path_object.remove_object(LAMBDA_DISPATCH_OBJECT_NAME, {}))
        parameters_object_stack.append(path_object.get_array('parameters', []))

        options_operation_needed_for_cors = False
        options_operation_found = False

        for operation, operation_object in path_object.items():

            # Swagger allows a path object to have properties that don't represent an
            # operation. Only the following properties define operations.

            if operation not in [
                    'get', 'put', 'post', 'delete', 'options', 'head', 'patch'
            ]:
                continue

            if operation == 'options':
                options_operation_found = True

            dispatch_object_stack.append(
                operation_object.remove_object(LAMBDA_DISPATCH_OBJECT_NAME,
                                               {}))
            parameters_object_stack.append(
                operation_object.get_array('parameters', []))

            # Create an x-amazon-apigateway-intergration object only if the operation object
            # doesn't have one already.

            if not operation_object.contains(
                    API_GATEWAY_INTEGRATION_OBJECT_NAME):

                # We assume executing a lambda can result in both internal server errors and
                # client errors, in addition to success. We add definitions for these
                # responses to the operation object if they aren't already present.
                if _add_error_response(operation_object):
                    _ensure_error_definition(swagger_navigator)

                # By default we want all APIs to be callable only when using valid AWS IAM
                # credentails. If no security object is present, add add one.
                if _determine_if_iam_security_enabled(dispatch_object_stack):
                    if _add_iam_security_to_operation(operation_object):
                        _ensure_iam_security_definition(swagger_navigator)

                # Construct the x-amazon-apigateway-intergration object using the information
                # we have in the x-amazon-cloud-canvas-lambda-dispatch objects that are currently
                # in scope.
                integration_object = _make_integration_object(
                    dispatch_object_stack, parameters_object_stack, path,
                    operation)
                operation_object.value[
                    API_GATEWAY_INTEGRATION_OBJECT_NAME] = integration_object

                if _determine_if_cors_enabled(dispatch_object_stack):
                    options_operation_needed_for_cors = True
                    __add_cores_response_headers_to_operation(operation_object)

            # If no security has been declared or inserted above, API Gateway will make the operation public.
            if global_security_object.is_none:
                security_array = operation_object.get_array('security',
                                                            default=None)
                if security_array.is_none:
                    missing_security_warnings.append('    {:<7} {}'.format(
                        operation, path))

            dispatch_object_stack.pop()  # operation scope
            parameters_object_stack.pop()

        if options_operation_needed_for_cors and not options_operation_found:
            __add_options_operation_for_cors(path, path_object)

        dispatch_object_stack.pop()  # path scope
        parameters_object_stack.pop()

    dispatch_object_stack.pop()  # swagger scope

    if missing_security_warnings:
        print ''
        print 'WARNING: the following operations do not specify a swagger "security" object.'
        print 'The Service APIs for these operations will be publically accessible.'
        print ''
        for warning in missing_security_warnings:
            print warning
        print ''