Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support AUTH_TYPE_CERT_PEM and AUTH_TYPE_NONE #305

Merged
merged 9 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ All notable changes to the Zowe Client Python SDK will be documented in this fil

### Enhancements

- 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)

### Bug Fixes

- 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)
Expand Down
2 changes: 2 additions & 0 deletions src/core/zowe/core_for_zowe_sdk/sdk_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ def __init__(self, profile, default_url, logger_name=__name__):
self._default_headers["Authorization"] = f"Bearer {self.session.tokenValue}"
elif self.session.type == session_constants.AUTH_TYPE_TOKEN:
self._default_headers["Cookie"] = f"{self.session.tokenType}={self.session.tokenValue}"
elif self.session.type == session_constants.AUTH_TYPE_CERT_PEM:
self.__session_arguments["cert"] = self.session.cert

def __enter__(self):
return self
Expand Down
16 changes: 13 additions & 3 deletions src/core/zowe/core_for_zowe_sdk/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class ISession:
type: Optional[str] = None
tokenType: Optional[str] = None
tokenValue: Optional[str] = None
cert: Optional[str] = None


class Session:
Expand Down Expand Up @@ -64,15 +65,24 @@ def __init__(self, props: dict) -> None:
elif props.get("tokenValue") is not None:
self.session.tokenValue = props.get("tokenValue")
self.session.type = session_constants.AUTH_TYPE_BEARER
elif props.get("certFile") is not None:
if props.get("certKeyFile"):
self.session.cert = (props.get("certFile"), props.get("certKeyFile"))
else:
self.__logger.error("A cert key must be provided")
raise Exception("A cert key must be provided")
self.session.rejectUnauthorized = props.get("rejectUnauthorized")
self.session.type = session_constants.AUTH_TYPE_CERT_PEM
else:
self.__logger.error("Authentication method not supplied")
raise Exception("An authentication method must be supplied")
self.session.type = session_constants.AUTH_TYPE_NONE
self.__logger.info("Authentication method not supplied")
# raise Exception("An authentication method must be supplied")

# set additional parameters
self.session.basePath = props.get("basePath")
self.session.port = props.get("port", self.session.port)
self.session.protocol = props.get("protocol", self.session.protocol)
self.session.rejectUnauthorized = props.get("rejectUnauthorized", self.session.rejectUnauthorized)
self.session.rejectUnauthorized = False if props.get("rejectUnauthorized") == False else True

def load(self) -> ISession:
return self.session
Expand Down
29 changes: 18 additions & 11 deletions tests/unit/core/test_sdk_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,18 @@ def setUp(self):
common_props = {"host": "mock-url.com", "port": 443, "protocol": "https", "rejectUnauthorized": True}
self.basic_props = {**common_props, "user": "Username", "password": "Password"}
self.bearer_props = {**common_props, "tokenValue": "BearerToken"}
self.token_props = {
**common_props,
"tokenType": "MyToken",
"tokenValue": "TokenValue",
}
self.token_props = {**common_props, "tokenType": "MyToken", "tokenValue": "TokenValue"}
self.cert_props = {**common_props, "rejectUnauthorized": False, "certFile": "cert", "certKeyFile": "certKey"}
self.default_url = "https://default-api.com/"

def test_object_should_be_instance_of_class(self):
"""Created object should be instance of SdkApi class."""
sdk_api = SdkApi(self.basic_props, self.default_url)
self.assertIsInstance(sdk_api, SdkApi)
@mock.patch('requests.Session.close')

@mock.patch("requests.Session.close")
def test_context_manager_closes_session(self, mock_close_request):

mock_close_request.return_value = mock.Mock(headers={"Content-Type": "application/json"}, status_code=200)
with SdkApi(self.basic_props, self.default_url) as api:
pass
Expand All @@ -47,13 +44,23 @@ def test_session_no_host_logger(self, mock_logger_error: mock.MagicMock):
self.assertIn("Host", mock_logger_error.call_args[0][0])

@mock.patch("logging.Logger.error")
def test_session_no_authentication_logger(self, mock_logger_error: mock.MagicMock):
props = {"host": "test"}
def test_session_combined_cert_logger(self, mock_logger_error: mock.MagicMock):
props = {"host": "test", "certFile": "test"}
try:
sdk_api = SdkApi(props, self.default_url)
except Exception:
mock_logger_error.assert_called()
self.assertIn("Authentication", mock_logger_error.call_args[0][0])
self.assertIn("cert key", mock_logger_error.call_args[0][0])

def test_should_handle_none_auth(self):
props = {"host": "test"}
sdk_api = SdkApi(props, self.default_url)
self.assertEqual(sdk_api.session.password, None)

def test_should_handle_cert_auth(self):
props = self.cert_props
sdk_api = SdkApi(props, self.default_url)
self.assertEqual(sdk_api.session.cert, (self.cert_props["certFile"], self.cert_props["certKeyFile"]))

def test_should_handle_basic_auth(self):
"""Created object should handle basic authentication."""
Expand Down
Loading