def stat(self, path): obsAdapter = None try: _path = self._realpath(path) if ObsSftpUtil.isRoot(_path): self._ar.update({'obs_key': _path}) self._logClient.log(logging.DEBUG, 'Stat root path successfully, return a sftp folder SFTPAttributes.') return self._buildSftpFolder() if ObsSftpUtil.isBucket(_path): bucketName = ObsSftpUtil.getBucketName(_path) auditDict = {'obs_bucket': bucketName, 'obs_key': '-'} self._ar.update(auditDict) if self._bucketExists(bucketName): self._logClient.log(logging.DEBUG, 'Stat obs bucket [%s] successfully, return a sftp folder SFTPAttributes.', bucketName) return self._buildSftpFolder() self._logClient.log(logging.ERROR, 'Stat obs bucket [%s] failed, error message [bucket is not exists].', bucketName) return SFTP_NO_SUCH_FILE bucketName, key = ObsSftpUtil.getBucketAndKey(_path) obsAdapter = obsadpter.ObsAdapter(client=self._client, bucketName=bucketName, key=key, logClient=self._logClient, ar=self._ar) pathInfo = obsAdapter.pathInfo() return SFTP_NO_SUCH_FILE if not pathInfo else pathInfo finally: del obsAdapter
def wrapper(*args, **kwargs): start = time.time() sftpServer = args[0] if isinstance(args[0], ObsSftpServer) else None result = None _path = '' try: if sftpServer and sftpServer._logClient: sftpServer._ar = {} # record active time for clear connection sftpServer._activeTime = time.time() if len(args) < 1: raise Exception('Function must has a path for operation.') path = args[1] _path = sftpServer._realpath(path) funcName = func.__name__ if funcName in ['rename']: newPath = args[2] sftpServer._logClient.log(logging.INFO, 'Entering [%s] for old path [%s] to new path [%s] ...' % ( func.__name__, _path, newPath)) else: sftpServer._logClient.log(logging.INFO, 'Entering [%s] for path [%s] ...', func.__name__, _path) if funcName in ['open', 'rename', 'mkdir', 'rmdir', 'remove']: if ObsSftpUtil.isRoot(_path) or ObsSftpUtil.isBucket(_path): sftpServer._ar.update({'obs_key': _path}) sftpServer._logClient.log(logging.ERROR, 'Sftp operation [%s] is not supported for path [%s]' , func.__name__, _path) result = SFTP_OP_UNSUPPORTED return SFTP_OP_UNSUPPORTED result = func(*args, **kwargs) except Exception as e: result = SFTP_FAILURE if sftpServer and sftpServer._logClient: sftpServer._logClient.log(logging.ERROR, "Operation [%s] failed, error message [%s] - %s.", func.__name__, str(e), traceback.format_exc()) finally: end = time.time() totalTimeMs = int((end - start) * 1000) operationResult = 'success' if result not in [SFTP_FAILURE, SFTP_OP_UNSUPPORTED, SFTP_NO_SUCH_FILE] else 'failed' auditDict = {'session_id': sftpServer._session, 'client_ip_port': sftpServer._connHost, 'server_ip_port': sftpServer._listenHost, 'user': sftpServer._userName, 'obs_endpoint': sftpServer._endpoint, 'sftp_opt': func.__name__, 'result': operationResult, 'client_ak': sftpServer._ak, 'sftp_start_time': ObsSftpUtil.utcFormater(start), 'sftp_end_time': ObsSftpUtil.utcFormater(end), 'sftp_total_time': totalTimeMs} sftpServer._ar.update(auditDict) if sftpServer and sftpServer._logClient: sftpServer._logClient.log(logging.INFO, 'End operation [%s] for path [%s] - result [%s]%s - cost [%s] ms', _path, func.__name__, operationResult, (' - count [%d]' % len(result)) if all( [operationResult == 'success', func.__name__ == 'list_folder']) else '', totalTimeMs) try: # record active time for clear connection auditLogger.log(sftpServer._ar) sftpServer._activeTime = time.time() except Exception as e: sftpServer._logClient.log(logging.ERROR, 'Log audit failed. error message [%s] - %s', str(e), traceback.format_exc()) sftpServer._ar = {} return result