__init__.py
301 lines
| 9.6 KiB
| text/x-python
|
PythonLexer
r0 | # -*- coding: utf-8 -*- | |||
r112 | # Copyright 2010 - 2017 RhodeCode GmbH and the AppEnlight project authors | |||
r0 | # | |||
r112 | # Licensed under the Apache License, Version 2.0 (the "License"); | |||
# you may not use this file except in compliance with the License. | ||||
# You may obtain a copy of the License at | ||||
r0 | # | |||
r112 | # http://www.apache.org/licenses/LICENSE-2.0 | |||
r0 | # | |||
r112 | # Unless required by applicable law or agreed to in writing, software | |||
# distributed under the License is distributed on an "AS IS" BASIS, | ||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||
# See the License for the specific language governing permissions and | ||||
# limitations under the License. | ||||
r0 | ||||
"""View handlers package. | ||||
""" | ||||
from pyramid.response import Response | ||||
import logging | ||||
import simplejson as json | ||||
from appenlight.lib import helpers | ||||
log = logging.getLogger(__name__) | ||||
def includeme(config): | ||||
"""Add the application's view handlers. | ||||
""" | ||||
r153 | config.add_route("/", "/") | |||
config.add_route("angular_app_ui_ix", "/ui") | ||||
config.add_route("angular_app_ui", "/ui/*remainder") | ||||
r0 | ||||
# applications API | ||||
r153 | config.add_route("applications_no_id", "/applications") | |||
r0 | config.add_route( | |||
r153 | "applications", | |||
"/applications/{resource_id}", | ||||
factory="appenlight.security.ResourceFactory", | ||||
) | ||||
config.add_route( | ||||
"applications_property", | ||||
"/applications/{resource_id}/{key}", | ||||
factory="appenlight.security.ResourceFactory", | ||||
) | ||||
config.add_route( | ||||
"integrations_id", | ||||
"/applications/{resource_id}/integrations/{integration}/{action}", | ||||
factory="appenlight.security.ResourceFactory", | ||||
) | ||||
r0 | ||||
# users API | ||||
r153 | config.add_route("users_self", "/users/self") | |||
config.add_route("users_self_property", "/users/self/{key}") | ||||
config.add_route("users_no_id", "/users") | ||||
config.add_route("users", "/users/{user_id}") | ||||
config.add_route("users_property", "/users/{user_id}/{key}") | ||||
r0 | ||||
# events | ||||
r153 | config.add_route("events_no_id", "/events") | |||
config.add_route("events", "/events/{event_id}") | ||||
config.add_route("events_property", "/events/{event_id}/{key}") | ||||
r0 | ||||
# groups | ||||
r153 | config.add_route("groups_no_id", "/groups") | |||
config.add_route("groups", "/groups/{group_id}") | ||||
config.add_route("groups_property", "/groups/{group_id}/{key}") | ||||
r0 | ||||
# reports API | ||||
r153 | config.add_route("reports", "/reports") | |||
config.add_route("slow_reports", "/slow_reports") | ||||
config.add_route( | ||||
"report_groups", | ||||
"/report_groups/{group_id}", | ||||
factory="appenlight.security.ResourceReportFactory", | ||||
) | ||||
config.add_route( | ||||
"report_groups_property", | ||||
"/report_groups/{group_id}/{key}", | ||||
factory="appenlight.security.ResourceReportFactory", | ||||
) | ||||
# generic resource API | ||||
config.add_route( | ||||
"resources_property", | ||||
"/resources/{resource_id}/{key}", | ||||
factory="appenlight.security.ResourceFactory", | ||||
) | ||||
r0 | ||||
# plugin configs API | ||||
r153 | config.add_route( | |||
"plugin_configs", | ||||
"/plugin_configs/{plugin_name}", | ||||
factory="appenlight.security.ResourcePluginMixedFactory", | ||||
) | ||||
config.add_route( | ||||
"plugin_config", | ||||
"/plugin_configs/{plugin_name}/{id}", | ||||
factory="appenlight.security.ResourcePluginConfigFactory", | ||||
) | ||||
r0 | ||||
# client endpoints API | ||||
r153 | config.add_route( | |||
"api_reports", "/api/reports", factory="appenlight.security.APIFactory" | ||||
) | ||||
config.add_route( | ||||
"api_report", "/api/report", factory="appenlight.security.APIFactory" | ||||
) | ||||
config.add_route("api_logs", "/api/logs", factory="appenlight.security.APIFactory") | ||||
config.add_route("api_log", "/api/log", factory="appenlight.security.APIFactory") | ||||
config.add_route( | ||||
"api_slow_reports", | ||||
"/api/slow_reports", | ||||
factory="appenlight.security.APIFactory", | ||||
) | ||||
config.add_route( | ||||
"api_request_stats", | ||||
"/api/request_stats", | ||||
factory="appenlight.security.APIFactory", | ||||
) | ||||
config.add_route( | ||||
"api_metrics", "/api/metrics", factory="appenlight.security.APIFactory" | ||||
) | ||||
config.add_route( | ||||
"api_general_metrics", | ||||
"/api/general_metrics", | ||||
factory="appenlight.security.APIFactory", | ||||
) | ||||
config.add_route( | ||||
"api_general_metric", | ||||
"/api/general_metric", | ||||
factory="appenlight.security.APIFactory", | ||||
) | ||||
config.add_route( | ||||
"api_airbrake", | ||||
"/notifier_api/v2/{action}", | ||||
factory="appenlight.security.AirbrakeV2APIFactory", | ||||
) | ||||
config.add_route( | ||||
"api_sentry", | ||||
"/api/{project}/store", | ||||
factory="appenlight.security.SentryAPIFactory", | ||||
) | ||||
config.add_route( | ||||
"api_sentry_slash", | ||||
"/api/{project}/store/", | ||||
factory="appenlight.security.SentryAPIFactory", | ||||
) | ||||
r0 | ||||
# other | ||||
r153 | config.add_route("register", "/register") | |||
config.add_route("register_ajax", "/register_ajax") | ||||
config.add_route("lost_password", "/lost_password") | ||||
config.add_route("lost_password_generate", "/lost_password_generate") | ||||
config.add_route("logs_no_id", "/logs") | ||||
config.add_route("forbidden", "/forbidden") | ||||
config.add_route("test", "/test/{action}") | ||||
config.add_route("section_view", "/sections/{section}/{view}") | ||||
config.add_view( | ||||
"appenlight.views.forbidden_view", | ||||
context="pyramid.exceptions.Forbidden", | ||||
renderer="appenlight:templates/forbidden.jinja2", | ||||
permission="__no_permission_required__", | ||||
) | ||||
config.add_view( | ||||
"appenlight.views.not_found_view", | ||||
context="pyramid.exceptions.NotFound", | ||||
renderer="appenlight:templates/not_found.jinja2", | ||||
permission="__no_permission_required__", | ||||
) | ||||
config.add_view( | ||||
"appenlight.views.csrf_view", | ||||
context="appenlight.lib.request.CSRFException", | ||||
renderer="appenlight:templates/forbidden.jinja2", | ||||
permission="__no_permission_required__", | ||||
) | ||||
config.add_view( | ||||
"appenlight.views.csrf_view", | ||||
context="appenlight.forms.CSRFException", | ||||
renderer="appenlight:templates/forbidden.jinja2", | ||||
permission="__no_permission_required__", | ||||
) | ||||
config.add_view( | ||||
"appenlight.views.colander_invalid_view", | ||||
context="colander.Invalid", | ||||
renderer="json", | ||||
permission="__no_permission_required__", | ||||
) | ||||
config.add_view( | ||||
"appenlight.views.bad_json_view", | ||||
context="appenlight.lib.request.JSONException", | ||||
renderer="json", | ||||
permission="__no_permission_required__", | ||||
) | ||||
r0 | ||||
# handle authomatic | ||||
r153 | config.add_route("social_auth", "/social_auth/{provider}") | |||
config.add_route("social_auth_abort", "/social_auth/{provider}/abort") | ||||
r0 | ||||
# only use in production | ||||
r153 | if ( | |||
config.registry.settings.get("pyramid.reload_templates") is False | ||||
and config.registry.settings.get("pyramid.debug_templates") is False | ||||
): | ||||
config.add_view( | ||||
"appenlight.views.error_view", | ||||
context=Exception, | ||||
renderer="appenlight:templates/error.jinja2", | ||||
permission="__no_permission_required__", | ||||
) | ||||
r0 | ||||
def bad_json_view(exc, request): | ||||
r153 | request.environ["appenlight.ignore_error"] = 1 | |||
request.response.headers.add("X-AppEnlight-Error", "Incorrect JSON") | ||||
r0 | request.response.status_int = 400 | |||
return "Incorrect JSON" | ||||
def colander_invalid_view(exc, request): | ||||
r153 | request.environ["appenlight.ignore_error"] = 1 | |||
log.warning( | ||||
"API version %s, %s" | ||||
% (request.params.get("protocol_version"), request.context.resource) | ||||
) | ||||
log.warning("Invalid payload sent") | ||||
r0 | errors = exc.asdict() | |||
r153 | request.response.headers.add("X-AppEnlight-Error", "Invalid payload sent") | |||
r0 | request.response.status_int = 422 | |||
return errors | ||||
def csrf_view(exc, request): | ||||
request.response.status = 403 | ||||
from ..models import DBSession | ||||
r153 | ||||
r0 | request.environ["appenlight.ignore_error"] = 1 | |||
r153 | request.response.headers.add("X-AppEnlight-Error", str(exc)) | |||
r0 | if request.user: | |||
request.user = DBSession.merge(request.user) | ||||
r153 | return {"forbidden_view": True, "csrf": True} | |||
r0 | ||||
def not_found_view(exc, request): | ||||
request.response.status = 404 | ||||
from ..models import DBSession | ||||
if request.user: | ||||
request.user = DBSession.merge(request.user) | ||||
if request.user: | ||||
r153 | request.response.headers["x-appenlight-uid"] = "%s" % request.user.id | |||
request.response.headers["x-appenlight-flash"] = json.dumps( | ||||
helpers.get_flash(request) | ||||
) | ||||
r0 | ||||
return {} | ||||
def forbidden_view(exc, request): | ||||
# dont serve html for api requests | ||||
from ..models import DBSession | ||||
if request.user: | ||||
request.user = DBSession.merge(request.user) | ||||
r153 | if request.path.startswith("/api"): | |||
logging.warning("Wrong API Key sent") | ||||
r0 | logging.info(request.url) | |||
r153 | logging.info("\n".join(["%s:%s" % (k, v) for k, v in request.headers.items()])) | |||
r0 | resp = Response( | |||
r153 | "Wrong api key", headers=(("X-AppEnlight-Error", "Incorrect API key"),) | |||
) | ||||
r0 | resp.status_int = 403 | |||
return resp | ||||
if request.user: | ||||
r153 | request.response.headers["x-appenlight-uid"] = "%s" % request.user.id | |||
request.response.headers["x-appenlight-flash"] = json.dumps( | ||||
helpers.get_flash(request) | ||||
) | ||||
r0 | request.response.status = 403 | |||
r153 | return {"forbidden_view": True} | |||
r0 | ||||
def error_view(exc, request): | ||||
from ..models import DBSession | ||||
r153 | ||||
r0 | if request.user: | |||
request.user = DBSession.merge(request.user) | ||||
r153 | if request.path.startswith("/api"): | |||
r0 | resp = Response( | |||
"There was a problem handling your request please try again", | ||||
r153 | headers=(("X-AppEnlight-Error", "Problem handling request"),), | |||
r0 | ) | |||
resp.status_int = 500 | ||||
return resp | ||||
log.error(exc) | ||||
request.response.status = 500 | ||||
return {} | ||||