def _validate_connection(authority_url, username, password, totp=None): try: return py42.sdk.from_local_account(authority_url, username, password, totp=totp) except SSLError as err: logger.log_error(err) raise LoggedCLIError( f"Problem connecting to {authority_url}, SSL certificate verification failed.\nUpdate profile with --disable-ssl-errors to bypass certificate checks (not recommended!)." ) except ConnectionError as err: logger.log_error(err) raise LoggedCLIError(f"Problem connecting to {authority_url}.") except Py42UnauthorizedError as err: logger.log_error(err) if "LoginConfig: LOCAL_2FA" in str(err): if totp is None: totp = prompt( "Multi-factor authentication required. Enter TOTP", type=TOTP() ) return _validate_connection(authority_url, username, password, totp) else: raise Code42CLIError( f"Invalid credentials or TOTP token for user {username}." ) else: raise Code42CLIError(f"Invalid credentials for user {username}.") except Exception as err: logger.log_error(err) raise LoggedCLIError("Unknown problem validating connection.")
def _validate_connection(authority_url, username, password, totp=None): try: return py42.sdk.from_local_account(authority_url, username, password, totp=totp) except SSLError as err: logger.log_error(err) raise LoggedCLIError( f"Problem connecting to {authority_url}, SSL certificate verification failed.\nUpdate profile with --disable-ssl-errors to bypass certificate checks (not recommended!)." ) except ConnectionError as err: logger.log_error(err) raise LoggedCLIError(f"Problem connecting to {authority_url}.") except Py42MFARequiredError: totp = prompt("Multi-factor authentication required. Enter TOTP", type=int) return _validate_connection(authority_url, username, password, totp) except Py42UnauthorizedError as err: logger.log_error(err) if "INVALID_TIME_BASED_ONE_TIME_PASSWORD" in err.response.text: raise Code42CLIError(f"Invalid TOTP token for user {username}.") else: raise Code42CLIError(f"Invalid credentials for user {username}.") except Exception as err: logger.log_error(err) raise LoggedCLIError("Unknown problem validating connection.")
def validate_connection(authority_url, username, password): try: return py42.sdk.from_local_account(authority_url, username, password) except ConnectionError as err: logger.log_error(str(err)) raise LoggedCLIError("Problem connecting to {}".format(authority_url)) except Py42UnauthorizedError as err: logger.log_error(str(err)) raise Code42CLIError("Invalid credentials for user {}".format(username)) except Exception as err: logger.log_error(str(err)) raise LoggedCLIError("Unknown problem validating connection.")
def invoke(self, ctx): try: return super().invoke(ctx) except click.UsageError as err: self._suggest_cmd(err) except LoggedCLIError: raise except Code42CLIError as err: self.logger.log_error(str(err)) raise except click.ClickException: raise except click.exceptions.Exit: raise except ( UserDoesNotExistError, Py42UserAlreadyAddedError, Py42UserNotOnListError, Py42InvalidRuleOperationError, Py42LegalHoldNotFoundOrPermissionDeniedError, SyslogServerNetworkConnectionError, Py42CaseNameExistsError, Py42DescriptionLimitExceededError, Py42CaseAlreadyHasEventError, Py42UpdateClosedCaseError, ) as err: self.logger.log_error(err) raise Code42CLIError(str(err)) except Py42ForbiddenError as err: self.logger.log_verbose_error(self._original_args, err.response.request) raise LoggedCLIError( "You do not have the necessary permissions to perform this task. " "Try using or creating a different profile.") except Py42HTTPError as err: self.logger.log_verbose_error(self._original_args, err.response.request) raise LoggedCLIError("Problem making request to server.") except OSError: raise except Exception: self.logger.log_verbose_error() raise LoggedCLIError("Unknown problem occurred.")
def _handle_if_errors(self): click.echo("") if self._stats.total_errors and self._raise_global_error: raise LoggedCLIError( "Some problems occurred during bulk processing.")
def invalid_connection(mock_verify): mock_verify.side_effect = LoggedCLIError("Problem connecting to server") return mock_verify
def _print_results(self): click.echo("") if self._stats.total_errors: raise LoggedCLIError( "Some problems occurred during bulk processing.")
def invoke(self, ctx): try: return super().invoke(ctx) except click.UsageError as err: self._suggest_cmd(err) except LoggedCLIError: raise except Code42CLIError as err: self.logger.log_error(str(err)) raise except click.ClickException: raise except click.exceptions.Exit: raise except ( UserDoesNotExistError, Py42UserAlreadyAddedError, Py42UserNotOnListError, Py42InvalidRuleOperationError, Py42LegalHoldNotFoundOrPermissionDeniedError, SyslogServerNetworkConnectionError, Py42CaseNameExistsError, Py42DescriptionLimitExceededError, Py42CaseAlreadyHasEventError, Py42UpdateClosedCaseError, Py42UsernameMustBeEmailError, Py42InvalidEmailError, Py42InvalidPasswordError, Py42InvalidUsernameError, Py42ActiveLegalHoldError, Py42OrgNotFoundError, Py42TrustedActivityConflictError, Py42TrustedActivityInvalidCharacterError, Py42TrustedActivityIdNotFound, Py42CloudAliasLimitExceededError, Py42CloudAliasCharacterLimitExceededError, ) as err: msg = err.args[0] self.logger.log_error(msg) raise Code42CLIError(msg) except Py42ForbiddenError as err: self.logger.log_verbose_error(self._original_args, err.response.request) raise LoggedCLIError( "You do not have the necessary permissions to perform this task. " "Try using or creating a different profile.") except Py42HTTPError as err: self.logger.log_verbose_error(self._original_args, err.response.request) raise LoggedCLIError("Problem making request to server.") except UnicodeEncodeError: if platform.system() == "Windows": cmd = 'if using powershell: $ENV:PYTHONIOENCODING="utf-16"\nif using cmd.exe: SET PYTHONIOENCODING="utf-16"' else: cmd = 'export PYTHONIOENCODING="utf-8"' raise Code42CLIError( f"Failed to handle unicode character using environment's detected encoding, try running the following:\n\n{cmd}\n\nand then re-run your `code42` command." ) except OSError: raise except Exception: self.logger.log_verbose_error() raise LoggedCLIError("Unknown problem occurred.")