1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
|
#!/usr/bin/env python2 -tt
# -*- coding: utf-8 -*-
#
# Copyright (C) 2013-2014 Red Hat, Inc.
# This file is part of python-fedora
#
# python-fedora is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# python-fedora is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with python-fedora; if not, see <http://www.gnu.org/licenses/>
#
"""Implement a class that sets up simple communication to a Fedora Service.
.. moduleauthor:: Pierre-Yves Chibon <pingou@fedoraproject.org>
.. moduleauthor:: Toshio Kuratomi <toshio@fedoraproject.org>
.. versionadded: 0.3.35
"""
import copy
import logging
import re
# For handling an exception that's coming from requests:
import ssl
import time
from six.moves import http_client as httplib
from six.moves.urllib.parse import quote, parse_qs, urljoin, urlparse
# Hack, hack, hack around
# the horror that is logging!
# Verily, verliy, verily, verily
# We should all use something better
try:
# Python 2.7+
from logging import NullHandler
except ImportError:
class NullHandler(logging.Handler):
def emit(self, *args):
pass
import requests
#from munch import munchify
from kitchen.text.converters import to_bytes
# For handling an exception that's coming from requests:
import urllib3
from fedora import __version__
from fedora.client import AuthError, ServerError, FedoraServiceError
log = logging.getLogger(__name__)
log.addHandler(NullHandler())
OPENID_SESSION_NAME = 'FAS_OPENID'
FEDORA_OPENID_API = 'https://id.fedoraproject.org/api/v1/'
FEDORA_OPENID_RE = re.compile(r'^http(s)?:\/\/id\.(|stg.|dev.)?fedoraproject\.org(/)?')
def _parse_response_history(response):
""" Retrieve the attributes from the response history. """
data = {}
for r in response.history:
if FEDORA_OPENID_RE.match(r.url):
parsed = parse_qs(urlparse(r.url).query)
for key, value in parsed.items():
data[key] = value[0]
break
return data
def openid_login(session, login_url, username, password, otp=None,
openid_insecure=False):
""" Open a session for the user.
Log in the user with the specified username and password
against the FAS OpenID server.
:arg session: Requests session object required to persist the cookies
that are created during redirects on the openid provider.
:arg login_url: The url to the login endpoint of the application.
:arg username: the FAS username of the user that wants to log in
:arg password: the FAS password of the user that wants to log in
:kwarg otp: currently unused. Eventually a way to send an otp to the
API that the API can use.
:kwarg openid_insecure: If True, do not check the openid server
certificates against their CA's. This means that man-in-the-middle
attacks are possible against the `BaseClient`. You might turn this
option on for testing against a local version of a server with a
self-signed certificate but it should be off in production.
"""
# Log into the service
response = session.get(
login_url, headers={'Accept': 'application/json'})
try:
data = response.json()
openid_url = data.get('server_url', None)
if not FEDORA_OPENID_RE.match(openid_url):
raise FedoraServiceError(
'Un-expected openid provider asked: %s' % openid_url)
except:
# Some consumers (like pyramid_openid) return redirects with the
# openid attributes encoded in the url
if not FEDORA_OPENID_RE.match(response.url):
raise FedoraServiceError(
'Un-expected openid provider asked: %s' % response.url)
data = _parse_response_history(response)
# Contact openid provider
data['username'] = username
data['password'] = password
# Let's precise to FedOAuth that we want to authenticate with FAS
data['auth_module'] = 'fedoauth.auth.fas.Auth_FAS'
data['auth_flow'] = 'fedora'
if not 'openid.mode' in data:
data['openid.mode'] = 'checkid_setup'
response = session.post(
FEDORA_OPENID_API, data=data, verify=not openid_insecure)
if not bool(response):
raise ServerError(FEDORA_OPENID_API, response.status_code,
'Error returned from our POST to ipsilon.')
output = response.json()
if not output['success']:
raise AuthError(output['message'])
response = session.post(output['response']['openid.return_to'],
data=output['response'])
return response
def absolute_url(beginning, end):
""" Join two urls parts if the last part does not start with the first
part specified """
if not end.startswith(beginning):
end = urljoin(beginning, end)
return end
class OpenIdProxyClient(object):
# pylint: disable-msg=R0903
"""
A client to a Fedora Service. This class is optimized to proxy multiple
users to a service. OpenIdProxyClient is designed to be usable by code
that creates a single instance of this class and uses it in multiple
threads. However it is not completely threadsafe. See the information
on setting attributes below.
If you want something that can manage one user's connection to a Fedora
Service, then look into using :class:`~fedora.client.OpenIdBaseClient`
instead.
This class has several attributes. These may be changed after
instantiation. Please note, however, that changing these values when
another thread is utilizing the same instance may affect more than just
the thread that you are making the change in. (For instance, changing
the debug option could cause other threads to start logging debug
messages in the middle of a method.)
.. attribute:: base_url
Initial portion of the url to contact the server. It is highly
recommended not to change this value unless you know that no other
threads are accessing this :class:`OpenIdProxyClient` instance.
.. attribute:: useragent
Changes the useragent string that is reported to the web server.
.. attribute:: session_name
Name of the cookie that holds the authentication value.
.. attribute:: debug
If :data:`True`, then more verbose logging is performed to aid in
debugging issues.
.. attribute:: insecure
If :data:`True` then the connection to the server is not checked to be
sure that any SSL certificate information is valid. That means that
a remote host can lie about who it is. Useful for development but
should not be used in production code.
.. attribute:: retries
Setting this to a positive integer will retry failed requests to the
web server this many times. Setting to a negative integer will retry
forever.
.. attribute:: timeout
A float describing the timeout of the connection. The timeout only
affects the connection process itself, not the downloading of the
response body. Defaults to 120 seconds.
"""
def __init__(self, base_url, login_url=None, useragent=None,
session_name='session', debug=False, insecure=False,
openid_insecure=False, retries=None, timeout=None):
"""Create a client configured for a particular service.
:arg base_url: Base of every URL used to contact the server
:kwarg login_url: The url to the login endpoint of the application.
If none are specified, it uses the default `/login`.
:kwarg useragent: useragent string to use. If not given, default
to "Fedora ProxyClient/VERSION"
:kwarg session_name: name of the cookie to use with session handling
:kwarg debug: If True, log debug information
:kwarg insecure: If True, do not check server certificates against
their CA's. This means that man-in-the-middle attacks are
possible against the `BaseClient`. You might turn this option
on for testing against a local version of a server with a
self-signed certificate but it should be off in production.
:kwarg openid_insecure: If True, do not check the openid server
certificates against their CA's. This means that man-in-the-
middle attacks are possible against the `BaseClient`. You might
turn this option on for testing against a local version of a
server with a self-signed certificate but it should be off in
production.
:kwarg retries: if we get an unknown or possibly transient error
from the server, retry this many times. Setting this to a
negative number makes it try forever. Defaults to zero, no
retries.
:kwarg timeout: A float describing the timeout of the connection.
The timeout only affects the connection process itself, not the
downloading of the response body. Defaults to 120 seconds.
"""
self.debug = debug
log.debug('proxyclient.__init__:entered')
# When we are instantiated, go ahead and silence the python-requests
# log. It is kind of noisy in our app server logs.
if not debug:
requests_log = logging.getLogger("requests")
requests_log.setLevel(logging.WARN)
if base_url[-1] != '/':
base_url = base_url + '/'
self.base_url = base_url
self.login_url = login_url or urljoin(self.base_url, '/login')
self.domain = urlparse(self.base_url).netloc
self.useragent = useragent or 'Fedora ProxyClient/%(version)s' % {
'version': __version__}
self.session_name = session_name
self.insecure = insecure
self.openid_insecure = openid_insecure
# Have to do retries and timeout default values this way as BaseClient
# sends None for these values if not overridden by the user.
if retries is None:
self.retries = 0
else:
self.retries = retries
if timeout is None:
self.timeout = 120.0
else:
self.timeout = timeout
log.debug('proxyclient.__init__:exited')
def __get_debug(self):
"""Return whether we have debug logging turned on.
:Returns: True if debugging is on, False otherwise.
"""
if log.level <= logging.DEBUG:
return True
return False
def __set_debug(self, debug=False):
"""Change debug level.
:kwarg debug: A true value to turn debugging on, false value to turn it
off.
"""
if debug:
log.setLevel(logging.DEBUG)
else:
log.setLevel(logging.ERROR)
debug = property(__get_debug, __set_debug, doc="""
When True, we log extra debugging statements. When False, we only log
errors.
""")
def login(self, username, password, otp=None):
""" Open a session for the user.
Log in the user with the specified username and password
against the FAS OpenID server.
:arg username: the FAS username of the user that wants to log in
:arg password: the FAS password of the user that wants to log in
:kwarg otp: currently unused. Eventually a way to send an otp to the
API that the API can use.
:return: a tuple containing both the response from the OpenID
provider and the session used to by this provider.
"""
session = requests.session()
response = openid_login(
session=session,
login_url=self.login_url,
username=username,
password=password,
otp=otp,
openid_insecure=self.openid_insecure)
return (response, session)
def send_request(self, method, verb='POST', req_params=None,
auth_params=None, file_params=None, retries=None,
timeout=None, headers=None):
"""Make an HTTP request to a server method.
The given method is called with any parameters set in ``req_params``.
If auth is True, then the request is made with an authenticated
session cookie. Note that path parameters should be set by adding
onto the method, not via ``req_params``.
:arg method: Method to call on the server. It's a url fragment that
comes after the base_url set in __init__(). Note that any
parameters set as extra path information should be listed here,
not in ``req_params``.
:kwarg req_params: dict containing extra parameters to send to the
server
:kwarg auth_params: dict containing one or more means of
authenticating to the server. Valid entries in this dict are:
:cookie: **Deprecated** Use ``session_id`` instead. If both
``cookie`` and ``session_id`` are set, only ``session_id``
will be used. A ``Cookie.SimpleCookie`` to send as a
session cookie to the server
:session_id: Session id to put in a cookie to construct an
identity for the server
:username: Username to send to the server
:password: Password to use with username to send to the server
:httpauth: If set to ``basic`` then use HTTP Basic Authentication
to send the username and password to the server. This may
be extended in the future to support other httpauth types
than ``basic``.
Note that cookie can be sent alone but if one of username or
password is set the other must as well. Code can set all of
these if it wants and all of them will be sent to the server.
Be careful of sending cookies that do not match with the
username in this case as the server can decide what to do in
this case.
:kwarg file_params: dict of files where the key is the name of the
file field used in the remote method and the value is the local
path of the file to be uploaded. If you want to pass multiple
files to a single file field, pass the paths as a list of paths.
:kwarg retries: if we get an unknown or possibly transient error
from the server, retry this many times. Setting this to a
negative number makes it try forever. Default to use the
:attr:`retries` value set on the instance or in :meth:`__init__`.
:kwarg timeout: A float describing the timeout of the connection.
The timeout only affects the connection process itself, not the
downloading of the response body. Defaults to the :attr:`timeout`
value set on the instance or in :meth:`__init__`.
:kwarg headers: A dictionary containing specific headers to add to
the request made.
:returns: A tuple of session_id and data.
:rtype: tuple of session information and data from server
"""
log.debug('openidproxyclient.send_request: entered')
# parameter mangling
file_params = file_params or {}
# Check whether we need to authenticate for this request
session_id = None
username = None
password = None
if auth_params:
if 'session_id' in auth_params:
session_id = auth_params['session_id']
if 'username' in auth_params and 'password' in auth_params:
username = auth_params['username']
password = auth_params['password']
elif 'username' in auth_params or 'password' in auth_params:
raise AuthError(
'username and password must both be set in auth_params'
)
if not (session_id or username):
raise AuthError(
'No known authentication methods specified: set '
'"cookie" in auth_params or set both username and '
'password in auth_params')
# urljoin is slightly different than os.path.join(). Make sure
# method will work with it.
method = method.lstrip('/')
# And join to make our url.
url = urljoin(self.base_url, quote(method))
# Set standard headers
if headers:
if not 'User-agent' in headers:
headers['User-agent'] = self.useragent
if not 'Accept' in headers:
headers['Accept'] = 'application/json'
else:
headers = {
'User-agent': self.useragent,
'Accept': 'application/json',
}
# Files to upload
for field_name, local_file_name in file_params:
file_params[field_name] = open(local_file_name, 'rb')
cookies = requests.cookies.RequestsCookieJar()
# If we have a session_id, send it
if session_id:
# Anytime the session_id exists, send it so that visit tracking
# works. Will also authenticate us if there's a need. Note
# that there's no need to set other cookie attributes because
# this is a cookie generated client-side.
cookies.set(self.session_name, session_id)
complete_params = req_params or {}
auth = None
if username and password:
# OpenID login
resp, session = self.login(username, password, otp=None)
cookies = session.cookies
# If debug, give people our debug info
log.debug('Creating request %s', to_bytes(url))
log.debug('Headers: %s', to_bytes(headers, nonstring='simplerepr'))
if self.debug and complete_params:
debug_data = copy.deepcopy(complete_params)
if 'password' in debug_data:
debug_data['password'] = 'xxxxxxx'
log.debug('Data: %r', debug_data)
if retries is None:
retries = self.retries
if timeout is None:
timeout = self.timeout
num_tries = 0
while True:
try:
response = session.request(
method=verb,
url=url,
data=complete_params,
cookies=cookies,
headers=headers,
auth=auth,
verify=not self.insecure,
timeout=timeout,
)
except (requests.Timeout, requests.exceptions.SSLError) as err:
if isinstance(err, requests.exceptions.SSLError):
# And now we know how not to code a library exception
# hierarchy... We're expecting that requests is raising
# the following stupidity:
# requests.exceptions.SSLError(
# urllib3.exceptions.SSLError(
# ssl.SSLError('The read operation timed out')))
# If we weren't interested in reraising the exception with
# full traceback we could use a try: except instead of
# this gross conditional. But we need to code defensively
# because we don't want to raise an unrelated exception
# here and if requests/urllib3 can do this sort of
# nonsense, they may change the nonsense in the future
if not (err.args and isinstance(
err.args[0], urllib3.exceptions.SSLError)
and err.args[0].args
and isinstance(err.args[0].args[0], ssl.SSLError)
and err.args[0].args[0].args
and 'timed out' in err.args[0].args[0].args[0]):
# We're only interested in timeouts here
raise
log.debug('Request timed out')
if retries < 0 or num_tries < retries:
num_tries += 1
log.debug('Attempt #%s failed', num_tries)
time.sleep(0.5)
continue
# Fail and raise an error
# Raising our own exception protects the user from the
# implementation detail of requests vs pycurl vs urllib
raise ServerError(
url, -1, 'Request timed out after %s seconds' % timeout)
# When the python-requests module gets a response, it attempts to
# guess the encoding using chardet (or a fork)
# That process can take an extraordinarily long time for long
# response.text strings.. upwards of 30 minutes for FAS queries to
# /accounts/user/list JSON api! Therefore, we cut that codepath
# off at the pass by assuming that the response is 'utf-8'. We can
# make that assumption because we're only interfacing with servers
# that we run (and we know that they all return responses
# encoded 'utf-8').
response.encoding = 'utf-8'
# Check for auth failures
# Note: old TG apps returned 403 Forbidden on authentication
# failures.
# Updated apps return 401 Unauthorized
# We need to accept both until all apps are updated to return 401.
http_status = response.status_code
if http_status in (401, 403):
# Wrong username or password
log.debug('Authentication failed logging in')
raise AuthError(
'Unable to log into server. Invalid '
'authentication tokens. Send new username and password'
)
elif http_status >= 400:
if retries < 0 or num_tries < retries:
# Retry the request
num_tries += 1
log.debug('Attempt #%s failed', num_tries)
time.sleep(0.5)
continue
# Fail and raise an error
try:
msg = httplib.responses[http_status]
except (KeyError, AttributeError):
msg = 'Unknown HTTP Server Response'
raise ServerError(url, http_status, msg)
# Successfully returned data
break
# In case the server returned a new session cookie to us
new_session = session.cookies.get(self.session_name, '')
log.debug('openidproxyclient.send_request: exited')
#data = munchify(data)
return new_session, response
__all__ = (OpenIdProxyClient,)
|