##// END OF EJS Templates
init: fix license type
ergo -
Show More
@@ -1,217 +1,217 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2
2
3 # Copyright 2010 - 2017 RhodeCode GmbH and the AppEnlight project authors
3 # Copyright 2010 - 2017 RhodeCode GmbH and the AppEnlight project authors
4 #
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
7 # You may obtain a copy of the License at
8 #
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
10 #
11 # Unless required by applicable law or agreed to in writing, software
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
15 # limitations under the License.
16
16
17 import datetime
17 import datetime
18 import logging
18 import logging
19 import pyelasticsearch
19 import pyelasticsearch
20 import redis
20 import redis
21 import os
21 import os
22 from pkg_resources import iter_entry_points
22 from pkg_resources import iter_entry_points
23
23
24 import appenlight.lib.jinja2_filters as jinja2_filters
24 import appenlight.lib.jinja2_filters as jinja2_filters
25 import appenlight.lib.encryption as encryption
25 import appenlight.lib.encryption as encryption
26
26
27 from pyramid.config import PHASE3_CONFIG
27 from pyramid.config import PHASE3_CONFIG
28 from pyramid.authentication import AuthTktAuthenticationPolicy
28 from pyramid.authentication import AuthTktAuthenticationPolicy
29 from pyramid.authorization import ACLAuthorizationPolicy
29 from pyramid.authorization import ACLAuthorizationPolicy
30 from pyramid_mailer.mailer import Mailer
30 from pyramid_mailer.mailer import Mailer
31 from pyramid.renderers import JSON
31 from pyramid.renderers import JSON
32 from pyramid_redis_sessions import session_factory_from_settings
32 from pyramid_redis_sessions import session_factory_from_settings
33 from pyramid.settings import asbool, aslist
33 from pyramid.settings import asbool, aslist
34 from pyramid.security import AllPermissionsList
34 from pyramid.security import AllPermissionsList
35 from pyramid_authstack import AuthenticationStackPolicy
35 from pyramid_authstack import AuthenticationStackPolicy
36 from redlock import Redlock
36 from redlock import Redlock
37 from sqlalchemy import engine_from_config
37 from sqlalchemy import engine_from_config
38
38
39 from appenlight.celery import configure_celery
39 from appenlight.celery import configure_celery
40 from appenlight.lib.configurator import (CythonCompatConfigurator,
40 from appenlight.lib.configurator import (CythonCompatConfigurator,
41 register_appenlight_plugin)
41 register_appenlight_plugin)
42 from appenlight.lib import cache_regions
42 from appenlight.lib import cache_regions
43 from appenlight.lib.ext_json import json
43 from appenlight.lib.ext_json import json
44 from appenlight.security import groupfinder, AuthTokenAuthenticationPolicy
44 from appenlight.security import groupfinder, AuthTokenAuthenticationPolicy
45
45
46 __license__ = 'AGPLv3, and Commercial License'
46 __license__ = 'Apache 2.0'
47 __author__ = 'RhodeCode GmbH'
47 __author__ = 'RhodeCode GmbH'
48 __url__ = 'http://rhodecode.com'
48 __url__ = 'http://rhodecode.com'
49
49
50 json_renderer = JSON(serializer=json.dumps, indent=4)
50 json_renderer = JSON(serializer=json.dumps, indent=4)
51
51
52 log = logging.getLogger(__name__)
52 log = logging.getLogger(__name__)
53
53
54
54
55 def datetime_adapter(obj, request):
55 def datetime_adapter(obj, request):
56 return obj.isoformat()
56 return obj.isoformat()
57
57
58
58
59 def all_permissions_adapter(obj, request):
59 def all_permissions_adapter(obj, request):
60 return '__all_permissions__'
60 return '__all_permissions__'
61
61
62
62
63 json_renderer.add_adapter(datetime.datetime, datetime_adapter)
63 json_renderer.add_adapter(datetime.datetime, datetime_adapter)
64 json_renderer.add_adapter(AllPermissionsList, all_permissions_adapter)
64 json_renderer.add_adapter(AllPermissionsList, all_permissions_adapter)
65
65
66
66
67 def main(global_config, **settings):
67 def main(global_config, **settings):
68 """ This function returns a Pyramid WSGI application.
68 """ This function returns a Pyramid WSGI application.
69 """
69 """
70 auth_tkt_policy = AuthTktAuthenticationPolicy(
70 auth_tkt_policy = AuthTktAuthenticationPolicy(
71 settings['authtkt.secret'],
71 settings['authtkt.secret'],
72 hashalg='sha512',
72 hashalg='sha512',
73 callback=groupfinder,
73 callback=groupfinder,
74 max_age=2592000,
74 max_age=2592000,
75 secure=asbool(settings.get('authtkt.secure', 'false')))
75 secure=asbool(settings.get('authtkt.secure', 'false')))
76 auth_token_policy = AuthTokenAuthenticationPolicy(
76 auth_token_policy = AuthTokenAuthenticationPolicy(
77 callback=groupfinder
77 callback=groupfinder
78 )
78 )
79 authorization_policy = ACLAuthorizationPolicy()
79 authorization_policy = ACLAuthorizationPolicy()
80 authentication_policy = AuthenticationStackPolicy()
80 authentication_policy = AuthenticationStackPolicy()
81 authentication_policy.add_policy('auth_tkt', auth_tkt_policy)
81 authentication_policy.add_policy('auth_tkt', auth_tkt_policy)
82 authentication_policy.add_policy('auth_token', auth_token_policy)
82 authentication_policy.add_policy('auth_token', auth_token_policy)
83 # set crypto key
83 # set crypto key
84 encryption.ENCRYPTION_SECRET = settings.get('encryption_secret')
84 encryption.ENCRYPTION_SECRET = settings.get('encryption_secret')
85 # import this later so encyption key can be monkeypatched
85 # import this later so encyption key can be monkeypatched
86 from appenlight.models import DBSession, register_datastores
86 from appenlight.models import DBSession, register_datastores
87 # update config with cometd info
87 # update config with cometd info
88 settings['cometd_servers'] = {'server': settings['cometd.server'],
88 settings['cometd_servers'] = {'server': settings['cometd.server'],
89 'secret': settings['cometd.secret']}
89 'secret': settings['cometd.secret']}
90
90
91 # Create the Pyramid Configurator.
91 # Create the Pyramid Configurator.
92 settings['_mail_url'] = settings['mailing.app_url']
92 settings['_mail_url'] = settings['mailing.app_url']
93 config = CythonCompatConfigurator(
93 config = CythonCompatConfigurator(
94 settings=settings,
94 settings=settings,
95 authentication_policy=authentication_policy,
95 authentication_policy=authentication_policy,
96 authorization_policy=authorization_policy,
96 authorization_policy=authorization_policy,
97 root_factory='appenlight.security.RootFactory',
97 root_factory='appenlight.security.RootFactory',
98 default_permission='view')
98 default_permission='view')
99 # custom registry variables
99 # custom registry variables
100
100
101 # resource type information
101 # resource type information
102 config.registry.resource_types = ['resource', 'application']
102 config.registry.resource_types = ['resource', 'application']
103 # plugin information
103 # plugin information
104 config.registry.appenlight_plugins = {}
104 config.registry.appenlight_plugins = {}
105
105
106 config.set_default_csrf_options(require_csrf=True, header='X-XSRF-TOKEN')
106 config.set_default_csrf_options(require_csrf=True, header='X-XSRF-TOKEN')
107 config.add_view_deriver('appenlight.predicates.csrf_view',
107 config.add_view_deriver('appenlight.predicates.csrf_view',
108 name='csrf_view')
108 name='csrf_view')
109
109
110 # later, when config is available
110 # later, when config is available
111 dogpile_config = {'url': settings['redis.url'],
111 dogpile_config = {'url': settings['redis.url'],
112 "redis_expiration_time": 86400,
112 "redis_expiration_time": 86400,
113 "redis_distributed_lock": True}
113 "redis_distributed_lock": True}
114 cache_regions.regions = cache_regions.CacheRegions(dogpile_config)
114 cache_regions.regions = cache_regions.CacheRegions(dogpile_config)
115 config.registry.cache_regions = cache_regions.regions
115 config.registry.cache_regions = cache_regions.regions
116 engine = engine_from_config(settings, 'sqlalchemy.',
116 engine = engine_from_config(settings, 'sqlalchemy.',
117 json_serializer=json.dumps)
117 json_serializer=json.dumps)
118 DBSession.configure(bind=engine)
118 DBSession.configure(bind=engine)
119
119
120 # json rederer that serializes datetime
120 # json rederer that serializes datetime
121 config.add_renderer('json', json_renderer)
121 config.add_renderer('json', json_renderer)
122 config.set_request_property('appenlight.lib.request.es_conn', 'es_conn')
122 config.set_request_property('appenlight.lib.request.es_conn', 'es_conn')
123 config.set_request_property('appenlight.lib.request.get_user', 'user',
123 config.set_request_property('appenlight.lib.request.get_user', 'user',
124 reify=True)
124 reify=True)
125 config.set_request_property('appenlight.lib.request.get_csrf_token',
125 config.set_request_property('appenlight.lib.request.get_csrf_token',
126 'csrf_token', reify=True)
126 'csrf_token', reify=True)
127 config.set_request_property('appenlight.lib.request.safe_json_body',
127 config.set_request_property('appenlight.lib.request.safe_json_body',
128 'safe_json_body', reify=True)
128 'safe_json_body', reify=True)
129 config.set_request_property('appenlight.lib.request.unsafe_json_body',
129 config.set_request_property('appenlight.lib.request.unsafe_json_body',
130 'unsafe_json_body', reify=True)
130 'unsafe_json_body', reify=True)
131 config.add_request_method('appenlight.lib.request.add_flash_to_headers',
131 config.add_request_method('appenlight.lib.request.add_flash_to_headers',
132 'add_flash_to_headers')
132 'add_flash_to_headers')
133 config.add_request_method('appenlight.lib.request.get_authomatic',
133 config.add_request_method('appenlight.lib.request.get_authomatic',
134 'authomatic', reify=True)
134 'authomatic', reify=True)
135
135
136 config.include('pyramid_redis_sessions')
136 config.include('pyramid_redis_sessions')
137 config.include('pyramid_tm')
137 config.include('pyramid_tm')
138 config.include('pyramid_jinja2')
138 config.include('pyramid_jinja2')
139 config.include('appenlight_client.ext.pyramid_tween')
139 config.include('appenlight_client.ext.pyramid_tween')
140 config.include('ziggurat_foundations.ext.pyramid.sign_in')
140 config.include('ziggurat_foundations.ext.pyramid.sign_in')
141 es_server_list = aslist(settings['elasticsearch.nodes'])
141 es_server_list = aslist(settings['elasticsearch.nodes'])
142 redis_url = settings['redis.url']
142 redis_url = settings['redis.url']
143 log.warning('Elasticsearch server list: {}'.format(es_server_list))
143 log.warning('Elasticsearch server list: {}'.format(es_server_list))
144 log.warning('Redis server: {}'.format(redis_url))
144 log.warning('Redis server: {}'.format(redis_url))
145 config.registry.es_conn = pyelasticsearch.ElasticSearch(es_server_list)
145 config.registry.es_conn = pyelasticsearch.ElasticSearch(es_server_list)
146 config.registry.redis_conn = redis.StrictRedis.from_url(redis_url)
146 config.registry.redis_conn = redis.StrictRedis.from_url(redis_url)
147
147
148 config.registry.redis_lockmgr = Redlock([settings['redis.redlock.url'], ],
148 config.registry.redis_lockmgr = Redlock([settings['redis.redlock.url'], ],
149 retry_count=0, retry_delay=0)
149 retry_count=0, retry_delay=0)
150 # mailer
150 # mailer
151 config.registry.mailer = Mailer.from_settings(settings)
151 config.registry.mailer = Mailer.from_settings(settings)
152
152
153 # Configure sessions
153 # Configure sessions
154 session_factory = session_factory_from_settings(settings)
154 session_factory = session_factory_from_settings(settings)
155 config.set_session_factory(session_factory)
155 config.set_session_factory(session_factory)
156
156
157 # Configure renderers and event subscribers
157 # Configure renderers and event subscribers
158 config.add_jinja2_extension('jinja2.ext.loopcontrols')
158 config.add_jinja2_extension('jinja2.ext.loopcontrols')
159 config.add_jinja2_search_path('appenlight:templates')
159 config.add_jinja2_search_path('appenlight:templates')
160 # event subscribers
160 # event subscribers
161 config.add_subscriber("appenlight.subscribers.application_created",
161 config.add_subscriber("appenlight.subscribers.application_created",
162 "pyramid.events.ApplicationCreated")
162 "pyramid.events.ApplicationCreated")
163 config.add_subscriber("appenlight.subscribers.add_renderer_globals",
163 config.add_subscriber("appenlight.subscribers.add_renderer_globals",
164 "pyramid.events.BeforeRender")
164 "pyramid.events.BeforeRender")
165 config.add_subscriber('appenlight.subscribers.new_request',
165 config.add_subscriber('appenlight.subscribers.new_request',
166 'pyramid.events.NewRequest')
166 'pyramid.events.NewRequest')
167 config.add_view_predicate('context_type_class',
167 config.add_view_predicate('context_type_class',
168 'appenlight.predicates.contextTypeClass')
168 'appenlight.predicates.contextTypeClass')
169
169
170 register_datastores(es_conn=config.registry.es_conn,
170 register_datastores(es_conn=config.registry.es_conn,
171 redis_conn=config.registry.redis_conn,
171 redis_conn=config.registry.redis_conn,
172 redis_lockmgr=config.registry.redis_lockmgr)
172 redis_lockmgr=config.registry.redis_lockmgr)
173
173
174 # base stuff and scan
174 # base stuff and scan
175
175
176 # need to ensure webassets exists otherwise config.override_asset()
176 # need to ensure webassets exists otherwise config.override_asset()
177 # throws exception
177 # throws exception
178 if not os.path.exists(settings['webassets.dir']):
178 if not os.path.exists(settings['webassets.dir']):
179 os.mkdir(settings['webassets.dir'])
179 os.mkdir(settings['webassets.dir'])
180 config.add_static_view(path='appenlight:webassets',
180 config.add_static_view(path='appenlight:webassets',
181 name='static', cache_max_age=3600)
181 name='static', cache_max_age=3600)
182 config.override_asset(to_override='appenlight:webassets/',
182 config.override_asset(to_override='appenlight:webassets/',
183 override_with=settings['webassets.dir'])
183 override_with=settings['webassets.dir'])
184
184
185 config.include('appenlight.views')
185 config.include('appenlight.views')
186 config.include('appenlight.views.admin')
186 config.include('appenlight.views.admin')
187 config.scan(ignore=['appenlight.migrations', 'appenlight.scripts',
187 config.scan(ignore=['appenlight.migrations', 'appenlight.scripts',
188 'appenlight.tests'])
188 'appenlight.tests'])
189
189
190 config.add_directive('register_appenlight_plugin',
190 config.add_directive('register_appenlight_plugin',
191 register_appenlight_plugin)
191 register_appenlight_plugin)
192
192
193 for entry_point in iter_entry_points(group='appenlight.plugins'):
193 for entry_point in iter_entry_points(group='appenlight.plugins'):
194 plugin = entry_point.load()
194 plugin = entry_point.load()
195 plugin.includeme(config)
195 plugin.includeme(config)
196
196
197 # include other appenlight plugins explictly if needed
197 # include other appenlight plugins explictly if needed
198 includes = aslist(settings.get('appenlight.includes', []))
198 includes = aslist(settings.get('appenlight.includes', []))
199 for inc in includes:
199 for inc in includes:
200 config.include(inc)
200 config.include(inc)
201
201
202 # run this after everything registers in configurator
202 # run this after everything registers in configurator
203
203
204 def pre_commit():
204 def pre_commit():
205 jinja_env = config.get_jinja2_environment()
205 jinja_env = config.get_jinja2_environment()
206 jinja_env.filters['tojson'] = json.dumps
206 jinja_env.filters['tojson'] = json.dumps
207 jinja_env.filters['toJSONUnsafe'] = jinja2_filters.toJSONUnsafe
207 jinja_env.filters['toJSONUnsafe'] = jinja2_filters.toJSONUnsafe
208
208
209 config.action(None, pre_commit, order=PHASE3_CONFIG + 999)
209 config.action(None, pre_commit, order=PHASE3_CONFIG + 999)
210
210
211 def wrap_config_celery():
211 def wrap_config_celery():
212 configure_celery(config.registry)
212 configure_celery(config.registry)
213
213
214 config.action(None, wrap_config_celery, order=PHASE3_CONFIG + 999)
214 config.action(None, wrap_config_celery, order=PHASE3_CONFIG + 999)
215
215
216 app = config.make_wsgi_app()
216 app = config.make_wsgi_app()
217 return app
217 return app
General Comments 2
Under Review
author

Auto status change to "Under Review"

Rejected

Please use: https://github.com/Appenlight/appenlight to contribute :) Thanks !

You need to be logged in to leave comments. Login now