Skip to content

Commit 8c5b816

Browse files
committed
Fetch Kiwi TCMS' CA certificate and install it locally during testing
that should allow Python to verify the HTTPS certificate on the other side of the connection and not complain about it!
1 parent b445dd4 commit 8c5b816

File tree

3 files changed

+42
-79
lines changed

3 files changed

+42
-79
lines changed

.github/workflows/integration.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,25 @@ jobs:
5757
WEB_ADDR=`docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' web_kiwitcms_org`
5858
sudo sh -c "echo '$WEB_ADDR web.kiwitcms.org' >> /etc/hosts"
5959
60+
- name: Install ca.crt from Kiwi TCMS
61+
run: |
62+
# regenerate new certificate, valid for the hostname used during testing
63+
docker exec -i web_kiwitcms_org /usr/bin/sscg -v -f \
64+
--hostname "web.kiwitcms.org" \
65+
--country BG --locality Sofia \
66+
--organization "Kiwi TCMS" \
67+
--organizational-unit "Quality Engineering" \
68+
--ca-file /Kiwi/static/ca.crt \
69+
--cert-file /Kiwi/ssl/localhost.crt \
70+
--cert-key-file /Kiwi/ssl/localhost.key
71+
72+
# restart web service so that it uses the new certificate
73+
docker-compose -f tests/krb5/docker-compose.yml restart web_kiwitcms_org
74+
75+
sudo mkdir -p /usr/local/share/ca-certificates/
76+
sudo curl --insecure https://web.kiwitcms.org:8443/static/ca.crt --output /usr/local/share/ca-certificates/Kiwi_TCMS_CA.crt
77+
sudo update-ca-certificates --fresh --verbose
78+
6079
- name: Install & configure Kerberos client
6180
if: matrix.os == 'ubuntu-latest' && matrix.gssapi == 'with'
6281
run: |

tests/krb5/integration_test.py

Lines changed: 18 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,41 @@
11
#!/usr/bin/env python
22

33
#
4-
# Copyright (c) 2020-2021 Kiwi TCMS project. All rights reserved.
4+
# Copyright (c) 2020-2024 Kiwi TCMS project. All rights reserved.
55
# Author: Alexander Todorov <info@kiwitcms.org>
66
#
77

8-
import ssl
98
import unittest
10-
from unittest.mock import patch
119

1210
from datetime import datetime
13-
14-
import requests
1511
from tcms_api import TCMS
1612

1713

18-
try:
19-
_create_unverified_https_context = ssl._create_unverified_context
20-
except AttributeError:
21-
# Legacy Python that doesn't verify HTTPS certificates by default
22-
pass
23-
else:
24-
# Handle target environment that doesn't support HTTPS verification
25-
ssl._create_default_https_context = _create_unverified_https_context
26-
27-
28-
class DoNotVerifySSLSession(requests.sessions.Session):
29-
def __init__(self):
30-
super().__init__()
31-
self.verify = False
32-
33-
def get(self, url, **kwargs):
34-
kwargs.setdefault("verify", False)
35-
return super().get(url, **kwargs)
36-
37-
3814
class IntegrationTestCase(unittest.TestCase):
3915
@classmethod
4016
def setUpClass(cls):
4117
cls.rpc = TCMS().exec
4218

4319
def test_readonly_filtering_works(self):
44-
with patch("requests.sessions.Session") as session:
45-
session.return_value = DoNotVerifySSLSession()
46-
47-
results = self.rpc.Product.filter({})
48-
self.assertGreater(len(results), 0)
20+
results = self.rpc.Product.filter({})
21+
self.assertGreater(len(results), 0)
4922

5023
def test_create_objects_works(self):
51-
with patch("requests.sessions.Session") as session:
52-
session.return_value = DoNotVerifySSLSession()
53-
54-
now = datetime.now().isoformat()
55-
56-
result = self.rpc.Classification.filter(
57-
{
58-
"name": "test-products",
59-
}
60-
)[0]
61-
self.assertEqual(result["name"], "test-products")
62-
classification_id = result["id"]
63-
64-
product_name = "tcms-api-%s" % now
65-
result = self.rpc.Product.create(
66-
{"name": product_name, "classification": classification_id}
67-
)
68-
self.assertEqual(result["name"], product_name)
24+
now = datetime.now().isoformat()
25+
26+
result = self.rpc.Classification.filter(
27+
{
28+
"name": "test-products",
29+
}
30+
)[0]
31+
self.assertEqual(result["name"], "test-products")
32+
classification_id = result["id"]
33+
34+
product_name = "tcms-api-%s" % now
35+
result = self.rpc.Product.create(
36+
{"name": product_name, "classification": classification_id}
37+
)
38+
self.assertEqual(result["name"], product_name)
6939

7040

7141
if __name__ == "__main__":

tests/krb5/python_credentials_test.py

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,11 @@
55
# Author: Alexander Todorov <info@kiwitcms.org>
66
#
77

8-
import ssl
98
import unittest
10-
from unittest.mock import patch
119

12-
import requests
1310
from tcms_api import TCMS
1411

1512

16-
try:
17-
_create_unverified_https_context = ssl._create_unverified_context
18-
except AttributeError:
19-
# Legacy Python that doesn't verify HTTPS certificates by default
20-
pass
21-
else:
22-
# Handle target environment that doesn't support HTTPS verification
23-
ssl._create_default_https_context = _create_unverified_https_context
24-
25-
26-
class DoNotVerifySSLSession(requests.sessions.Session):
27-
def __init__(self):
28-
super().__init__()
29-
self.verify = False
30-
31-
def get(self, url, **kwargs):
32-
kwargs.setdefault("verify", False)
33-
return super().get(url, **kwargs)
34-
35-
3613
class PythonCredentialsTestCase(unittest.TestCase):
3714
@classmethod
3815
def setUpClass(cls):
@@ -43,16 +20,13 @@ def setUpClass(cls):
4320
).exec
4421

4522
def test_passing_credentials_via_python_works(self):
46-
with patch("requests.sessions.Session") as session:
47-
session.return_value = DoNotVerifySSLSession()
48-
49-
result = self.rpc.User.filter()[0]
23+
result = self.rpc.User.filter()[0]
5024

51-
# this is from config file
52-
self.assertNotEqual(result["username"], "kiwitcms-bot")
25+
# this is from config file
26+
self.assertNotEqual(result["username"], "kiwitcms-bot")
5327

54-
# this is specified in setUpClass() above
55-
self.assertEqual(result["username"], "kiwitcms-developer")
28+
# this is specified in setUpClass() above
29+
self.assertEqual(result["username"], "kiwitcms-developer")
5630

5731

5832
if __name__ == "__main__":

0 commit comments

Comments
 (0)