Skip to content

Commit 19297ff

Browse files
authored
Merge pull request #305 from zowe/Auth_Type
Support AUTH_TYPE_CERT_PEM and AUTH_TYPE_NONE
2 parents af53e8f + 8fdf3b3 commit 19297ff

File tree

4 files changed

+35
-14
lines changed

4 files changed

+35
-14
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ All notable changes to the Zowe Client Python SDK will be documented in this fil
66

77
### Enhancements
88

9+
- Included support for `AUTH_TYPE_CERT_PEM` and `AUTH_TYPE_NONE` in `session` [#291] (https://github.com/zowe/zowe-client-python-sdk/issues/291) and [#296] (https://github.com/zowe/zowe-client-python-sdk/issues/296)
10+
911
### Bug Fixes
1012

1113
- Fixed a bug on `create` in `Datasets` where the target dataset gets created with a different block size when `like` is specified [#295] (https://github.com/zowe/zowe-client-python-sdk/issues/295)

src/core/zowe/core_for_zowe_sdk/sdk_api.py

+2
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ def __init__(self, profile, default_url, logger_name=__name__):
5454
self._default_headers["Authorization"] = f"Bearer {self.session.tokenValue}"
5555
elif self.session.type == session_constants.AUTH_TYPE_TOKEN:
5656
self._default_headers["Cookie"] = f"{self.session.tokenType}={self.session.tokenValue}"
57+
elif self.session.type == session_constants.AUTH_TYPE_CERT_PEM:
58+
self.__session_arguments["cert"] = self.session.cert
5759

5860
def __enter__(self):
5961
return self

src/core/zowe/core_for_zowe_sdk/session.py

+13-3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class ISession:
3333
type: Optional[str] = None
3434
tokenType: Optional[str] = None
3535
tokenValue: Optional[str] = None
36+
cert: Optional[str] = None
3637

3738

3839
class Session:
@@ -64,15 +65,24 @@ def __init__(self, props: dict) -> None:
6465
elif props.get("tokenValue") is not None:
6566
self.session.tokenValue = props.get("tokenValue")
6667
self.session.type = session_constants.AUTH_TYPE_BEARER
68+
elif props.get("certFile") is not None:
69+
if props.get("certKeyFile"):
70+
self.session.cert = (props.get("certFile"), props.get("certKeyFile"))
71+
else:
72+
self.__logger.error("A cert key must be provided")
73+
raise Exception("A cert key must be provided")
74+
self.session.rejectUnauthorized = props.get("rejectUnauthorized")
75+
self.session.type = session_constants.AUTH_TYPE_CERT_PEM
6776
else:
68-
self.__logger.error("Authentication method not supplied")
69-
raise Exception("An authentication method must be supplied")
77+
self.session.type = session_constants.AUTH_TYPE_NONE
78+
self.__logger.info("Authentication method not supplied")
79+
# raise Exception("An authentication method must be supplied")
7080

7181
# set additional parameters
7282
self.session.basePath = props.get("basePath")
7383
self.session.port = props.get("port", self.session.port)
7484
self.session.protocol = props.get("protocol", self.session.protocol)
75-
self.session.rejectUnauthorized = props.get("rejectUnauthorized", self.session.rejectUnauthorized)
85+
self.session.rejectUnauthorized = False if props.get("rejectUnauthorized") == False else True
7686

7787
def load(self) -> ISession:
7888
return self.session

tests/unit/core/test_sdk_api.py

+18-11
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,18 @@ def setUp(self):
1616
common_props = {"host": "mock-url.com", "port": 443, "protocol": "https", "rejectUnauthorized": True}
1717
self.basic_props = {**common_props, "user": "Username", "password": "Password"}
1818
self.bearer_props = {**common_props, "tokenValue": "BearerToken"}
19-
self.token_props = {
20-
**common_props,
21-
"tokenType": "MyToken",
22-
"tokenValue": "TokenValue",
23-
}
19+
self.token_props = {**common_props, "tokenType": "MyToken", "tokenValue": "TokenValue"}
20+
self.cert_props = {**common_props, "rejectUnauthorized": False, "certFile": "cert", "certKeyFile": "certKey"}
2421
self.default_url = "https://default-api.com/"
2522

2623
def test_object_should_be_instance_of_class(self):
2724
"""Created object should be instance of SdkApi class."""
2825
sdk_api = SdkApi(self.basic_props, self.default_url)
2926
self.assertIsInstance(sdk_api, SdkApi)
30-
31-
@mock.patch('requests.Session.close')
27+
28+
@mock.patch("requests.Session.close")
3229
def test_context_manager_closes_session(self, mock_close_request):
33-
30+
3431
mock_close_request.return_value = mock.Mock(headers={"Content-Type": "application/json"}, status_code=200)
3532
with SdkApi(self.basic_props, self.default_url) as api:
3633
pass
@@ -47,13 +44,23 @@ def test_session_no_host_logger(self, mock_logger_error: mock.MagicMock):
4744
self.assertIn("Host", mock_logger_error.call_args[0][0])
4845

4946
@mock.patch("logging.Logger.error")
50-
def test_session_no_authentication_logger(self, mock_logger_error: mock.MagicMock):
51-
props = {"host": "test"}
47+
def test_session_combined_cert_logger(self, mock_logger_error: mock.MagicMock):
48+
props = {"host": "test", "certFile": "test"}
5249
try:
5350
sdk_api = SdkApi(props, self.default_url)
5451
except Exception:
5552
mock_logger_error.assert_called()
56-
self.assertIn("Authentication", mock_logger_error.call_args[0][0])
53+
self.assertIn("cert key", mock_logger_error.call_args[0][0])
54+
55+
def test_should_handle_none_auth(self):
56+
props = {"host": "test"}
57+
sdk_api = SdkApi(props, self.default_url)
58+
self.assertEqual(sdk_api.session.password, None)
59+
60+
def test_should_handle_cert_auth(self):
61+
props = self.cert_props
62+
sdk_api = SdkApi(props, self.default_url)
63+
self.assertEqual(sdk_api.session.cert, (self.cert_props["certFile"], self.cert_props["certKeyFile"]))
5764

5865
def test_should_handle_basic_auth(self):
5966
"""Created object should handle basic authentication."""

0 commit comments

Comments
 (0)