Skip to content

Commit caa28b2

Browse files
authored
Update Pyxis module to also verify that cert paths exist (#201)
* Update Pyxis module to also verify that cert paths exist With some upcoming changes for ISV-1278, the cert+key filepaths could be set but not point to existent file. This will detect the case and provide a clearer error message.
1 parent 57cad48 commit caa28b2

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

operator-pipeline-images/operatorcert/pyxis.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,26 @@ def _get_session() -> requests.Session:
4747
"https": "http://squid.corp.redhat.com:3128",
4848
}
4949

50-
# API key or cert + key need to be provided using env variable
51-
if not api_key and (not cert or not key):
52-
raise Exception(
53-
"No auth details provided for Pyxis. "
54-
"Either define PYXIS_API_KEY or PYXIS_CERT_PATH + PYXIS_KEY_PATH"
55-
)
56-
5750
session = requests.Session()
5851

5952
if api_key:
6053
LOGGER.debug("Pyxis session using API key is created")
6154
session.headers.update({"X-API-KEY": api_key})
55+
elif cert and key:
56+
if os.path.exists(cert) and os.path.exists(key):
57+
LOGGER.debug("Pyxis session using cert + key is created")
58+
session.cert = (cert, key)
59+
else:
60+
raise Exception(
61+
"PYXIS_CERT_PATH or PYXIS_KEY_PATH does not point to a file that "
62+
"exists."
63+
)
6264
else:
63-
LOGGER.debug("Pyxis session using cert + key is created")
64-
session.cert = (cert, key)
65+
# API key or cert + key need to be provided using env variable
66+
raise Exception(
67+
"No auth details provided for Pyxis. "
68+
"Either define PYXIS_API_KEY or PYXIS_CERT_PATH + PYXIS_KEY_PATH"
69+
)
6570

6671
if proxies:
6772
LOGGER.debug(

operator-pipeline-images/tests/test_pyxis.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,28 @@ def test_get_session_api_key(monkeypatch: Any) -> None:
2222
assert session.headers["X-API-KEY"] == "123"
2323

2424

25-
def test_get_session_cert(monkeypatch: Any) -> None:
25+
@patch("os.path.exists")
26+
def test_get_session_cert(mock_path_exists: MagicMock, monkeypatch: Any) -> None:
27+
mock_path_exists.return_value = True
2628
monkeypatch.setenv("PYXIS_CERT_PATH", "/path/to/cert.pem")
2729
monkeypatch.setenv("PYXIS_KEY_PATH", "/path/to/key.key")
2830
session = pyxis._get_session()
2931

3032
assert session.cert == ("/path/to/cert.pem", "/path/to/key.key")
3133

3234

35+
@patch("os.path.exists")
36+
def test_get_session_cert_not_exist(
37+
mock_path_exists: MagicMock, monkeypatch: Any
38+
) -> None:
39+
mock_path_exists.return_value = False
40+
monkeypatch.setenv("PYXIS_CERT_PATH", "/path/to/cert.pem")
41+
monkeypatch.setenv("PYXIS_KEY_PATH", "/path/to/key.key")
42+
43+
with pytest.raises(Exception):
44+
pyxis._get_session()
45+
46+
3347
def test_get_session_no_auth(monkeypatch: Any) -> None:
3448
with pytest.raises(Exception):
3549
pyxis._get_session()

0 commit comments

Comments
 (0)