Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _handle_remote_ip_request(self, req):
remote_address = req.remote_addr
if CONF.api.use_forwarded_for:
remote_address = req.headers.get('X-Forwarded-For', remote_address)
try:
meta_data = self.get_metadata_by_remote_address(remote_address)
except Exception:
LOG.exception('Failed to get metadata for IP %s',
remote_address)
msg = _('An unknown error has occurred. '
'Please try your request again.')
raise webob.exc.HTTPInternalServerError(
explanation=six.text_type(msg))
if meta_data is None:
LOG.error('Failed to get metadata for IP %s: no metadata',
remote_address)
return meta_data
def __call__(self, req):
try:
response = req.get_response(self.application)
except Exception:
LOG.exception(_LE('An error occurred during '
'processing the request: %s'))
response = webob.exc.HTTPInternalServerError()
return response
request_token = OAuthToken.gql('WHERE token_key=:key', key=oauth_token).get()
if request_token is None:
raise exc.HTTPBadRequest('Invalid oauth_token: %s' % oauth_token)
# Rebuild the auth handler
auth = tweepy.OAuthHandler(appengine_config.TWITTER_APP_KEY,
appengine_config.TWITTER_APP_SECRET)
auth.set_request_token(request_token.token_key, request_token.token_secret)
# Fetch the access token
try:
access_token = auth.get_access_token(oauth_verifier)
except tweepy.TweepError, e:
msg = 'Twitter OAuth error, could not get access token: '
logging.exception(msg)
raise exc.HTTPInternalServerError(msg + `e`)
params = {'access_token_key': access_token.key,
'access_token_secret': access_token.secret,
}
self.redirect('/?%s' % urllib.urlencode(params))
def _internal_error(self, exception):
"""Last resource error for :meth:`__call__`."""
logging.exception(exception)
if self.debug:
lines = ''.join(traceback.format_exception(*sys.exc_info()))
html = _debug_template % (cgi.escape(lines, quote=True))
return Response(body=html, status=500)
return exc.HTTPInternalServerError()
def abort(status_code=exc.HTTPInternalServerError.code, message='Unhandled exception'):
raise exc.status_map[status_code](message)
def delete(self, req, namespace, tag_name):
meta_repo = self.gateway.get_metadef_tag_repo(req.context)
try:
metadef_tag = meta_repo.get(namespace, tag_name)
metadef_tag.delete()
meta_repo.remove(metadef_tag)
except exception.Forbidden as e:
LOG.debug("User not permitted to delete metadata tag '%s' "
"within '%s' namespace", tag_name, namespace)
raise webob.exc.HTTPForbidden(explanation=e.msg)
except exception.NotFound as e:
raise webob.exc.HTTPNotFound(explanation=e.msg)
except Exception as e:
LOG.error(encodeutils.exception_to_unicode(e))
raise webob.exc.HTTPInternalServerError()
def __call__(self, req):
try:
resp = req.get_response(self.application)
if resp.status_int in Fault.resp_codes:
for (header, value) in resp._headerlist:
if header == "Content-Type" and \
value == "text/plain; charset=UTF-8":
return Fault(Fault.code_wrapper[resp.status_int]())
return resp
return resp
except Exception as ex:
LOG.exception(_("Caught error: %s."),
encodeutils.exception_to_unicode(ex))
exc = webob.exc.HTTPInternalServerError()
return Fault(exc)
% {'property_name': property_name,
'prefix': prefix})
raise exception.NotFound(msg)
prop_repo = self.gateway.get_metadef_property_repo(req.context)
db_property = prop_repo.get(namespace, property_name)
property = self._to_model(db_property)
except exception.Forbidden as e:
LOG.debug("User not permitted to show metadata property '%s' "
"within '%s' namespace", property_name, namespace)
raise webob.exc.HTTPForbidden(explanation=e.msg)
except exception.NotFound as e:
raise webob.exc.HTTPNotFound(explanation=e.msg)
except Exception as e:
LOG.error(encodeutils.exception_to_unicode(e))
raise webob.exc.HTTPInternalServerError()
return property
type_and_rbac_filters = []
for resource_type in resource_types:
plugin = self.plugins[resource_type].obj
try:
plugin_filter = plugin.get_query_filters(
context, ignore_rbac=ignore_rbac)
type_and_rbac_filters.append(plugin_filter)
except Exception as e:
msg = _("Error processing %s RBAC filter") % resource_type
LOG.error("Failed to retrieve RBAC filters "
"from search plugin "
"%(ext)s: %(e)s" %
{'ext': plugin.name, 'e': e})
raise webob.exc.HTTPInternalServerError(explanation=msg)
role_filter = {'term': {searchlight.elasticsearch.ROLE_USER_FIELD:
context.user_role_filter}}
# Create a filter query for the role filter; RBAC filters are added
# in the next step
es_query = {
'bool': {
'filter': {
'bool': {
'must': role_filter
}
},
'must': query
}
}
def __call__(self, request):
action = request.urlvars['action']
try:
method = getattr(self, action) #pylint: disable=R0921
result = method(request, **request.urlvars)
except Exception:
log.exception('An unrecoverable error was detected')
raise webob.exc.HTTPInternalServerError()
if result is None:
result = webob.exc.HTTPNoContent()
elif isinstance(result, dict):
# serialize body based on response content type
if 'body' in result:
content_type = result.get('content_type')
result['body'] = dumps(result['body'], content_type)
result.setdefault('status', HTTP_STATUS_OK)
result.setdefault('content_type', CONTENT_TYPE_HTML)
result = self.response(**result) #pylint: disable=W0142
elif not isinstance(result, webob.Response) and \