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