Projet

Général

Profil

0001-misc-allow-client-side-cert-on-http_requests-59039.patch

Thomas Noël, 21 janvier 2022 18:01

Télécharger (4,5 ko)

Voir les différences:

Subject: [PATCH] misc: allow client side cert on http_requests (#59039)

 tests/test_misc.py | 62 ++++++++++++++++++++++++++++++++++++++++++++++
 wcs/qommon/misc.py |  6 +++++
 wcs/settings.py    |  5 ++++
 3 files changed, 73 insertions(+)
tests/test_misc.py
20 20
from wcs.qommon.http_request import HTTPRequest
21 21
from wcs.qommon.humantime import humanduration2seconds, seconds2humanduration
22 22
from wcs.qommon.misc import (
23
    _http_request,
23 24
    date_format,
24 25
    ellipsize,
25 26
    format_time,
......
533 534
def test_uwsgi_spooler_import():
534 535
    with pytest.raises(ImportError):
535 536
        import wcs.qommon.spooler  # noqa pylint: disable=unused-import
537

  
538

  
539
@mock.patch('requests.Session.request')
540
def test_http_request_global_settings(mock_request):
541
    response = {'err': 0, 'data': []}
542
    mock_json = mock.Mock(status_code=200)
543
    mock_json.json.return_value = response
544
    mock_request.return_value = mock_json
545
    from django.conf import settings
546

  
547
    response, status, data, auth_header = _http_request('https://example.com/')
548
    mock_request.assert_called_once_with(
549
        'GET', 'https://example.com/', cert=None, data=None, headers={}, proxies=None, timeout=28
550
    )
551
    assert status == 200
552

  
553
    settings.REQUESTS_TIMEOUT = 42
554
    mock_request.reset_mock()
555
    response, status, data, auth_header = _http_request('https://example.com/')
556
    mock_request.assert_called_once_with(
557
        'GET', 'https://example.com/', cert=None, data=None, headers={}, proxies=None, timeout=42
558
    )
559
    assert status == 200
560

  
561
    settings.REQUESTS_PROXIES = {
562
        'http': 'http://10.10.1.10:3128',
563
        'https': 'http://10.10.1.10:1080',
564
    }
565
    mock_request.reset_mock()
566
    response, status, data, auth_header = _http_request('https://example.com/')
567
    mock_request.assert_called_once_with(
568
        'GET',
569
        'https://example.com/',
570
        cert=None,
571
        data=None,
572
        headers={},
573
        proxies=settings.REQUESTS_PROXIES,
574
        timeout=42,
575
    )
576
    assert status == 200
577
    settings.REQUESTS_PROXIES = None
578

  
579
    settings.REQUESTS_CERT = {
580
        'https://example.com/ssl': '/path/client.pem',
581
    }
582
    mock_request.reset_mock()
583
    response, status, data, auth_header = _http_request('https://example.com/ssl/')
584
    mock_request.assert_called_once_with(
585
        'GET',
586
        'https://example.com/ssl/',
587
        cert='/path/client.pem',
588
        data=None,
589
        headers={},
590
        proxies=None,
591
        timeout=42,
592
    )
593
    mock_request.reset_mock()
594
    response, status, data, auth_header = _http_request('https://example.com/')
595
    mock_request.assert_called_once_with(
596
        'GET', 'https://example.com/', cert=None, data=None, headers={}, proxies=None, timeout=42
597
    )
wcs/qommon/misc.py
359 359
    hostname = splitted_url.netloc
360 360
    timeout = timeout or settings.REQUESTS_TIMEOUT
361 361

  
362
    if cert_file is None:
363
        for url_prefix, cert in settings.REQUESTS_CERT.items():
364
            if url.startswith(url_prefix):
365
                cert_file = cert
366
                break
367

  
362 368
    # re-use HTTP adapter to get connection pooling and keep-alive.
363 369
    adapter = getattr(get_publisher(), '_http_adapter', None)
364 370
    if adapter is None:
wcs/settings.py
187 187
# we use 28s by default: timeout just before web server, which is usually 30s
188 188
REQUESTS_TIMEOUT = 28
189 189

  
190
# REQUESTS_CERT is a dict of 'url_prefix': cert. cert is used in python-requests call
191
# https://docs.python-requests.org/en/master/user/advanced/#client-side-certificates
192
# example : REQUESTS_CERT = {'https://example.net/ssl-auth/': '/path/client.pem'}
193
REQUESTS_CERT = {}
194

  
190 195
# For high availability installations with multiple instances of w.c.s.
191 196
# components, one should disable cron jobs execution on secondary servers;
192 197
# set the following variable True disables "cron" management command
193
-