def from_local_account(cls, host_address, username, password, totp=None): """Creates a :class:`~py42.sdk.SDKClient` object for accessing the Code42 REST APIs using the supplied credentials. This method supports only accounts created within the Code42 console or using the APIs (including py42). Username/passwords that are based on Active Directory, Okta, or other Identity providers should use the `from_jwt_provider` method. Args: host_address (str): The domain name of the Code42 instance being authenticated to, e.g. console.us.code42.com username (str): The username of the authenticating account. password (str): The password of the authenticating account. totp (callable or str, optional): The time-based one-time password of the authenticating account. Include only if the account uses Code42's two-factor authentication. Defaults to None. Returns: :class:`py42.sdk.SDKClient` """ basic_auth = None if username and password: basic_auth = HTTPBasicAuth(username, password) auth_connection = Connection.from_host_address(host_address, auth=basic_auth) bearer_auth = BearerAuth(auth_connection, totp) main_connection = Connection.from_host_address(host_address, auth=bearer_auth) return cls(main_connection, bearer_auth)
def test_get_for_user_calls_session_get_with_expected_uri_and_params( self, mock_session): connection = Connection.from_host_address(HOST_ADDRESS, session=mock_session) loginconfig = LoginConfigurationClient(connection) loginconfig.get_for_user("*****@*****.**") expected_uri = f"https://{HOST_ADDRESS}/c42api/v3/LoginConfiguration" expected_params = {"username": "******"} mock_session.get.assert_called_once_with(expected_uri, params=expected_params)
def test_get_for_user_does_not_use_py42_connection_get_method( self, mocker, mock_session): """Because the loginConfiguration endpoint is unauthenticated, we want to make sure we don't force the Connection's C42RenewableAuth object to make any authentication requests before making the loginConfig request. """ mock_get = mocker.patch("py42.services._connection.Connection.get") connection = Connection.from_host_address(HOST_ADDRESS, session=mock_session) loginconfig = LoginConfigurationClient(connection) loginconfig.get_for_user("*****@*****.**") assert mock_get.call_count == 0 assert mock_session.get.call_count == 1
def get_storage_connection(self, tmp_auth): try: url = tmp_auth.get_storage_url() connection = self.get_saved_connection_for_url(url) if connection is None: with self._list_update_lock: connection = self.get_saved_connection_for_url(url) if connection is None: connection = Connection.from_host_address( url, auth=tmp_auth) self._session_cache[url.lower()] = connection except Exception as ex: message = u"Failed to create or retrieve connection, caused by: {}".format( str(ex)) raise Py42StorageSessionInitializationError(ex, message) return connection
def test_from_local_account_when_unauthorized_calls_loginConfig_and_returns_config_value_on_raised_exception_text( self, mocker, mock_session, mock_auth, unauthorized_response): login_type = "LOCAL_2FA" mock_session.send.return_value = unauthorized_response mock_session.get.return_value = create_mock_response( mocker, f'{{"loginType": "{login_type}"}}') connection = Connection.from_host_address(HOST_ADDRESS, session=mock_session) client = SDKClient(connection, mock_auth) mocker.patch("py42.sdk.SDKClient.from_local_account", return_value=client) with pytest.raises(Py42UnauthorizedError) as err: from_local_account(HOST_ADDRESS, TEST_USERNAME, TEST_PASSWORD) assert f"User LoginConfig: {login_type}" in str(err)
def from_jwt_provider(cls, host_address, jwt_provider): """Creates a :class:`~py42.sdk.SDKClient` object for accessing the Code42 REST APIs using a custom auth mechanism. User can use any authentication mechanism like that returns a JSON Web token on authentication which would then be used for all subsequent requests. Args: host_address (str): The domain name of the Code42 instance being authenticated to, e.g. console.us.code42.com jwt_provider (function): A function that accepts no parameters and on execution returns a JSON web token string. Returns: :class:`py42.sdk.SDKClient` """ custom_auth = CustomJWTAuth(jwt_provider) main_connection = Connection.from_host_address(host_address, auth=custom_auth) return cls(main_connection, custom_auth)
def _get_new_storage_connection(storage_url): return Connection.from_host_address(storage_url)
def create_preservation_data_service(self, host_address): main_connection = self._connection.clone(host_address) streaming_connection = Connection.from_host_address(host_address) return StoragePreservationDataService(main_connection, streaming_connection)
def create_exfiltrated_data_service(self, host_address): main_connection = self._connection.clone(host_address) streaming_connection = Connection.from_host_address(host_address) return ExfiltratedDataService(main_connection, streaming_connection)