Test Failed
Push — master ( e380d0...f5671d )
by W
02:58
created

st2auth/st2auth/app.py (1 issue)

1
# Licensed to the StackStorm, Inc ('StackStorm') under one or more
2
# contributor license agreements.  See the NOTICE file distributed with
3
# this work for additional information regarding copyright ownership.
4
# The ASF licenses this file to You under the Apache License, Version 2.0
5
# (the "License"); you may not use this file except in compliance with
6
# the License.  You may obtain a copy of the License at
7
#
8
#     http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15
16
from oslo_config import cfg
17
18
from st2common import log as logging
19
from st2common.middleware.error_handling import ErrorHandlingMiddleware
20
from st2common.middleware.cors import CorsMiddleware
21
from st2common.middleware.request_id import RequestIDMiddleware
22
from st2common.middleware.logging import LoggingMiddleware
23
from st2common.middleware.instrumentation import RequestInstrumentationMiddleware
24
from st2common.middleware.instrumentation import ResponseInstrumentationMiddleware
25
from st2common.router import Router
26
from st2common.util.monkey_patch import monkey_patch
27
from st2common.constants.system import VERSION_STRING
28
from st2common.service_setup import setup as common_setup
29
from st2common.util import spec_loader
30
from st2auth import config as st2auth_config
31
from st2auth.validation import validate_auth_backend_is_correctly_configured
32
33
LOG = logging.getLogger(__name__)
34
35
36
def setup_app(config={}):
0 ignored issues
show
Bug Best Practice introduced by
The default value {} might cause unintended side-effects.

Objects as default values are only created once in Python and not on each invocation of the function. If the default object is modified, this modification is carried over to the next invocation of the method.

# Bad:
# If array_param is modified inside the function, the next invocation will
# receive the modified object.
def some_function(array_param=[]):
    # ...

# Better: Create an array on each invocation
def some_function(array_param=None):
    array_param = array_param or []
    # ...
Loading history...
37
    LOG.info('Creating st2auth: %s as OpenAPI app.', VERSION_STRING)
38
39
    is_gunicorn = config.get('is_gunicorn', False)
40
    if is_gunicorn:
41
        # Note: We need to perform monkey patching in the worker. If we do it in
42
        # the master process (gunicorn_config.py), it breaks tons of things
43
        # including shutdown
44
        monkey_patch()
45
46
        # This should be called in gunicorn case because we only want
47
        # workers to connect to db, rabbbitmq etc. In standalone HTTP
48
        # server case, this setup would have already occurred.
49
        st2auth_config.register_opts()
50
        common_setup(service='auth', config=st2auth_config, setup_db=True,
51
                     register_mq_exchanges=False,
52
                     register_signal_handlers=True,
53
                     register_internal_trigger_types=False,
54
                     run_migrations=False,
55
                     config_args=config.get('config_args', None))
56
57
    # Additional pre-run time checks
58
    validate_auth_backend_is_correctly_configured()
59
60
    router = Router(debug=cfg.CONF.auth.debug, is_gunicorn=is_gunicorn)
61
62
    spec = spec_loader.load_spec('st2common', 'openapi.yaml.j2')
63
    transforms = {
64
        '^/auth/v1/': ['/', '/v1/']
65
    }
66
    router.add_spec(spec, transforms=transforms)
67
68
    app = router.as_wsgi
69
70
    # Order is important. Check middleware for detailed explanation.
71
    app = ErrorHandlingMiddleware(app)
72
    app = CorsMiddleware(app)
73
    app = LoggingMiddleware(app, router)
74
    app = ResponseInstrumentationMiddleware(app, service_name='auth')
75
    app = RequestIDMiddleware(app)
76
    app = RequestInstrumentationMiddleware(app, service_name='auth')
77
78
    return app
79