remove error handler
This commit is contained in:
parent
bd769f9e3f
commit
582f8757b3
1 changed files with 70 additions and 81 deletions
|
@ -53,65 +53,6 @@ DIOS_MIO = """<?xml version="1.0"?>
|
||||||
</d:propfind>
|
</d:propfind>
|
||||||
"""
|
"""
|
||||||
|
|
||||||
class NextcloudErrorHandler:
|
|
||||||
'''Handle errors in Nextcloud'''
|
|
||||||
def __init__(self):
|
|
||||||
self.config = {}
|
|
||||||
self._init_log()
|
|
||||||
|
|
||||||
def _init_log(self):
|
|
||||||
''' Initialize log object '''
|
|
||||||
self._log = logging.getLogger("nc_password_client")
|
|
||||||
self._log.setLevel(logging.DEBUG)
|
|
||||||
|
|
||||||
sysloghandler = SysLogHandler()
|
|
||||||
sysloghandler.setLevel(logging.DEBUG)
|
|
||||||
self._log.addHandler(sysloghandler)
|
|
||||||
|
|
||||||
streamhandler = logging.StreamHandler(sys.stdout)
|
|
||||||
streamhandler.setLevel(
|
|
||||||
logging.getLevelName(self.config.get("debug_level", 'INFO'))
|
|
||||||
)
|
|
||||||
self._log.addHandler(streamhandler)
|
|
||||||
|
|
||||||
if 'log_file' in self.config:
|
|
||||||
log_file = self.config['log_file']
|
|
||||||
else:
|
|
||||||
home_folder = os.environ.get(
|
|
||||||
'HOME', os.environ.get('USERPROFILE', '')
|
|
||||||
)
|
|
||||||
log_folder = os.path.join(home_folder, "log")
|
|
||||||
log_file = os.path.join(log_folder, "nc_password_client.log")
|
|
||||||
|
|
||||||
if not os.path.exists(os.path.dirname(log_file)):
|
|
||||||
os.mkdir(os.path.dirname(log_file))
|
|
||||||
|
|
||||||
filehandler = logging.handlers.RotatingFileHandler(
|
|
||||||
log_file, maxBytes=102400000
|
|
||||||
)
|
|
||||||
# create formatter
|
|
||||||
formatter = logging.Formatter(
|
|
||||||
'%(asctime)s %(name)-12s %(levelname)-8s %(message)s'
|
|
||||||
)
|
|
||||||
filehandler.setFormatter(formatter)
|
|
||||||
filehandler.setLevel(logging.DEBUG)
|
|
||||||
self._log.addHandler(filehandler)
|
|
||||||
return True
|
|
||||||
|
|
||||||
def status_code_error(self, status):
|
|
||||||
'''Process status code error???'''
|
|
||||||
if status > 399:
|
|
||||||
self._log.error(
|
|
||||||
'Nextcloud returned status code %s',
|
|
||||||
status
|
|
||||||
)
|
|
||||||
sys.exit(1)
|
|
||||||
else:
|
|
||||||
self._log.debug(
|
|
||||||
'Nextcloud returned status %s',
|
|
||||||
status
|
|
||||||
)
|
|
||||||
|
|
||||||
def parameter_spects(spec_arguments):
|
def parameter_spects(spec_arguments):
|
||||||
'''Specification of parameters???'''
|
'''Specification of parameters???'''
|
||||||
argument_spec = dict(
|
argument_spec = dict(
|
||||||
|
@ -143,7 +84,6 @@ class NextcloudHandler:
|
||||||
'''Handle Nextcloud Password API'''
|
'''Handle Nextcloud Password API'''
|
||||||
def __init__(self, params):
|
def __init__(self, params):
|
||||||
self._init_log(params)
|
self._init_log(params)
|
||||||
self.exit = NextcloudErrorHandler()
|
|
||||||
self.http = 'https'
|
self.http = 'https'
|
||||||
self.timeout = params.get('timeout', 3)
|
self.timeout = params.get('timeout', 3)
|
||||||
self.ssl = True
|
self.ssl = True
|
||||||
|
@ -161,15 +101,18 @@ class NextcloudHandler:
|
||||||
|
|
||||||
self.host = params.get('host') or os.environ.get('NEXTCLOUD_HOST')
|
self.host = params.get('host') or os.environ.get('NEXTCLOUD_HOST')
|
||||||
if self.host is None:
|
if self.host is None:
|
||||||
self.exit.status_code_error('Unable to continue. No Nextcloud Host is given.')
|
self._log.error('Unable to continue. No Nextcloud Host is given.')
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
self.user = params.get('user') or os.environ.get('NEXTCLOUD_USER')
|
self.user = params.get('user') or os.environ.get('NEXTCLOUD_USER')
|
||||||
if self.user is None:
|
if self.user is None:
|
||||||
self.exit.status_code_error('Unable to continue. No Nextcloud User is given.')
|
self._log.error('Unable to continue. No Nextcloud User is given.')
|
||||||
|
sys.exit(2)
|
||||||
|
|
||||||
self.token = params.get('api_token') or os.environ.get('NEXTCLOUD_TOKEN')
|
self.token = params.get('api_token') or os.environ.get('NEXTCLOUD_TOKEN')
|
||||||
if self.token is None:
|
if self.token is None:
|
||||||
self.exit.status_code_error('Unable to continue. No Nextcloud Token is given.')
|
self._log.error('Unable to continue. No Nextcloud Token is given.')
|
||||||
|
sys.exit(3)
|
||||||
|
|
||||||
self.e2e_password = False
|
self.e2e_password = False
|
||||||
if params.get('cse_password') or os.environ.get('NEXTCLOUD_CSE_PASSWORD'):
|
if params.get('cse_password') or os.environ.get('NEXTCLOUD_CSE_PASSWORD'):
|
||||||
|
@ -270,9 +213,11 @@ class NextcloudHandler:
|
||||||
if r.status_code == 200:
|
if r.status_code == 200:
|
||||||
return r
|
return r
|
||||||
elif r.status_code == 404:
|
elif r.status_code == 404:
|
||||||
self.exit.status_code_error(f'File {path} does not exist')
|
self._log.error(f'File {path} does not exist')
|
||||||
|
sys.exit(3)
|
||||||
else:
|
else:
|
||||||
self.exit.status_code_error(r.status_code)
|
self._log.error(r.status_code)
|
||||||
|
sys.exit(4)
|
||||||
|
|
||||||
def propfind(self, path):
|
def propfind(self, path):
|
||||||
'''Do a PROPFIND request'''
|
'''Do a PROPFIND request'''
|
||||||
|
@ -338,9 +283,11 @@ class NextcloudHandler:
|
||||||
|
|
||||||
elif r.status_code == 404:
|
elif r.status_code == 404:
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
else:
|
else:
|
||||||
self.exit.status_code_error(r.status_code)
|
self._log.error(
|
||||||
|
"Nextcloud instance returned status code: %s",
|
||||||
|
r.status_code
|
||||||
|
)
|
||||||
|
|
||||||
def put(self, path, src=None):
|
def put(self, path, src=None):
|
||||||
'''Do a PUT request'''
|
'''Do a PUT request'''
|
||||||
|
@ -358,7 +305,10 @@ class NextcloudHandler:
|
||||||
if r.status_code in [200, 201, 204]:
|
if r.status_code in [200, 201, 204]:
|
||||||
return r, True
|
return r, True
|
||||||
else:
|
else:
|
||||||
self.exit.status_code_error(r.status_code)
|
self._log.error(
|
||||||
|
"Nextcloud instance returned status code: %s",
|
||||||
|
r.status_code
|
||||||
|
)
|
||||||
|
|
||||||
def delete(self, path):
|
def delete(self, path):
|
||||||
'''Do a DELETE request'''
|
'''Do a DELETE request'''
|
||||||
|
@ -372,7 +322,10 @@ class NextcloudHandler:
|
||||||
elif r.status_code == 404:
|
elif r.status_code == 404:
|
||||||
return r, False
|
return r, False
|
||||||
else:
|
else:
|
||||||
self.exit.status_code_error(r.status_code)
|
self._log.error(
|
||||||
|
"Nextcloud instance returned status code: %s",
|
||||||
|
r.status_code
|
||||||
|
)
|
||||||
|
|
||||||
def talk(self, message, channel):
|
def talk(self, message, channel):
|
||||||
'''Post in Talk/Chat'''
|
'''Post in Talk/Chat'''
|
||||||
|
@ -394,7 +347,10 @@ class NextcloudHandler:
|
||||||
if r.status_code == 201:
|
if r.status_code == 201:
|
||||||
return r, True
|
return r, True
|
||||||
else:
|
else:
|
||||||
self.exit.status_code_error(r.status_code)
|
self._log.error(
|
||||||
|
"Nextcloud instance returned status code: %s",
|
||||||
|
r.status_code
|
||||||
|
)
|
||||||
|
|
||||||
def request_passwords_session(self):
|
def request_passwords_session(self):
|
||||||
'''Request a Passwords API session'''
|
'''Request a Passwords API session'''
|
||||||
|
@ -402,7 +358,10 @@ class NextcloudHandler:
|
||||||
if r.status_code == 200:
|
if r.status_code == 200:
|
||||||
return r.json()
|
return r.json()
|
||||||
else:
|
else:
|
||||||
self.exit.status_code_error(r.status_code)
|
self._log.error(
|
||||||
|
"Nextcloud instance returned status code: %s",
|
||||||
|
r.status_code
|
||||||
|
)
|
||||||
|
|
||||||
def open_passwords_session(self, password_hash):
|
def open_passwords_session(self, password_hash):
|
||||||
'''Open a Passwords API session'''
|
'''Open a Passwords API session'''
|
||||||
|
@ -421,7 +380,10 @@ class NextcloudHandler:
|
||||||
self.x_api_session = r.headers.get('X-API-SESSION')
|
self.x_api_session = r.headers.get('X-API-SESSION')
|
||||||
return r.json()
|
return r.json()
|
||||||
else:
|
else:
|
||||||
self.exit.status_code_error(r.status_code)
|
self._log.error(
|
||||||
|
"Nextcloud instance returned status code: %s",
|
||||||
|
r.status_code
|
||||||
|
)
|
||||||
|
|
||||||
def close_passwords_session(self):
|
def close_passwords_session(self):
|
||||||
'''Close Passwords API session'''
|
'''Close Passwords API session'''
|
||||||
|
@ -429,7 +391,10 @@ class NextcloudHandler:
|
||||||
if r.status_code == 200:
|
if r.status_code == 200:
|
||||||
return r.json()
|
return r.json()
|
||||||
else:
|
else:
|
||||||
self.exit.status_code_error(r.status_code)
|
self._log.error(
|
||||||
|
"Nextcloud instance returned status code: %s",
|
||||||
|
r.status_code
|
||||||
|
)
|
||||||
|
|
||||||
def list_passwords(self):
|
def list_passwords(self):
|
||||||
'''List all passwords'''
|
'''List all passwords'''
|
||||||
|
@ -437,7 +402,10 @@ class NextcloudHandler:
|
||||||
if r.status_code == 200:
|
if r.status_code == 200:
|
||||||
return r.json()
|
return r.json()
|
||||||
else:
|
else:
|
||||||
self.exit.status_code_error(r.status_code)
|
self._log.error(
|
||||||
|
"Nextcloud instance returned status code: %s",
|
||||||
|
r.status_code
|
||||||
|
)
|
||||||
|
|
||||||
def list_passwords_folders(self):
|
def list_passwords_folders(self):
|
||||||
'''List passwords folders'''
|
'''List passwords folders'''
|
||||||
|
@ -445,7 +413,10 @@ class NextcloudHandler:
|
||||||
if r.status_code == 200:
|
if r.status_code == 200:
|
||||||
return r.json()
|
return r.json()
|
||||||
else:
|
else:
|
||||||
self.exit.status_code_error(r.status_code)
|
self._log.error(
|
||||||
|
"Nextcloud instance returned status code: %s",
|
||||||
|
r.status_code
|
||||||
|
)
|
||||||
|
|
||||||
def delete_passwords_folder(self, name):
|
def delete_passwords_folder(self, name):
|
||||||
'''Delete a passwords folder'''
|
'''Delete a passwords folder'''
|
||||||
|
@ -473,7 +444,10 @@ class NextcloudHandler:
|
||||||
if r.status_code == 201:
|
if r.status_code == 201:
|
||||||
return r.json()
|
return r.json()
|
||||||
else:
|
else:
|
||||||
self.exit.status_code_error(r.status_code)
|
self._log.error(
|
||||||
|
"Nextcloud instance returned status code: %s",
|
||||||
|
r.status_code
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
self._log.error(
|
self._log.error(
|
||||||
"Fodler '{name}' not found."
|
"Fodler '{name}' not found."
|
||||||
|
@ -505,7 +479,10 @@ class NextcloudHandler:
|
||||||
if r.status_code == 201:
|
if r.status_code == 201:
|
||||||
return r.json()
|
return r.json()
|
||||||
else:
|
else:
|
||||||
self.exit.status_code_error(r.status_code)
|
self._log.error(
|
||||||
|
"Nextcloud instance returned status code: %s",
|
||||||
|
r.status_code
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
self._log.warning(
|
self._log.warning(
|
||||||
"Password folder '%s' already exists",
|
"Password folder '%s' already exists",
|
||||||
|
@ -552,7 +529,10 @@ class NextcloudHandler:
|
||||||
if r.status_code == 200:
|
if r.status_code == 200:
|
||||||
return [r.json().get('password')]
|
return [r.json().get('password')]
|
||||||
else:
|
else:
|
||||||
self.exit.status_code_error(r.status_code)
|
self._log.error(
|
||||||
|
"Nextcloud instance returned status code: %s",
|
||||||
|
r.status_code
|
||||||
|
)
|
||||||
|
|
||||||
def exists_password(self, obj):
|
def exists_password(self, obj):
|
||||||
'''Test if a password exist with the same name'''
|
'''Test if a password exist with the same name'''
|
||||||
|
@ -578,7 +558,10 @@ class NextcloudHandler:
|
||||||
return r.json()
|
return r.json()
|
||||||
else:
|
else:
|
||||||
self._log.error(r.json())
|
self._log.error(r.json())
|
||||||
self.exit.status_code_error(r.status_code)
|
self._log.error(
|
||||||
|
"Nextcloud instance returned status code: %s",
|
||||||
|
r.status_code
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
self._log.warning(
|
self._log.warning(
|
||||||
"Password with name '%s' already exists",
|
"Password with name '%s' already exists",
|
||||||
|
@ -598,7 +581,10 @@ class NextcloudHandler:
|
||||||
if r.status_code == 200:
|
if r.status_code == 200:
|
||||||
return r.json()
|
return r.json()
|
||||||
else:
|
else:
|
||||||
self.exit.status_code_error(r.status_code)
|
self._log.error(
|
||||||
|
"Nextcloud instance returned status code: %s",
|
||||||
|
r.status_code
|
||||||
|
)
|
||||||
|
|
||||||
def update_password(self, post_obj):
|
def update_password(self, post_obj):
|
||||||
'''Update a password'''
|
'''Update a password'''
|
||||||
|
@ -613,7 +599,10 @@ class NextcloudHandler:
|
||||||
if r.status_code == 200:
|
if r.status_code == 200:
|
||||||
return r.json()
|
return r.json()
|
||||||
else:
|
else:
|
||||||
self.exit.status_code_error(r.status_code)
|
self._log.error(
|
||||||
|
"Nextcloud instance returned status code: %s",
|
||||||
|
r.status_code
|
||||||
|
)
|
||||||
|
|
||||||
def is_same_key(self, key, dict1, dict2):
|
def is_same_key(self, key, dict1, dict2):
|
||||||
'''Test if two dictionaries have the same key with the same value'''
|
'''Test if two dictionaries have the same key with the same value'''
|
||||||
|
|
Loading…
Reference in a new issue