2019-08-05 09:11:29 +00:00
|
|
|
#!/usr/bin/env python
|
2019-08-01 22:00:48 +00:00
|
|
|
import argparse
|
|
|
|
import json
|
|
|
|
|
|
|
|
import requests
|
|
|
|
|
2019-08-05 09:11:29 +00:00
|
|
|
try:
|
|
|
|
import urllib.parse as urlparse
|
|
|
|
except ImportError:
|
|
|
|
import urlparse
|
2019-08-01 22:00:48 +00:00
|
|
|
|
2019-08-08 12:20:42 +00:00
|
|
|
try:
|
|
|
|
import pygments
|
|
|
|
from pygments.lexers import JsonLexer
|
|
|
|
from pygments.formatters import TerminalFormatter
|
|
|
|
except ImportError:
|
|
|
|
pygments = None
|
|
|
|
|
2019-08-01 22:00:48 +00:00
|
|
|
|
|
|
|
class SFTPGoApiRequests:
|
|
|
|
|
2019-08-08 12:20:42 +00:00
|
|
|
def __init__(self, debug, baseUrl, authType, authUser, authPassword, secure, no_color):
|
2019-08-08 10:03:24 +00:00
|
|
|
self.userPath = urlparse.urljoin(baseUrl, '/api/v1/user')
|
|
|
|
self.quotaScanPath = urlparse.urljoin(baseUrl, '/api/v1/quota_scan')
|
2019-08-24 12:41:15 +00:00
|
|
|
self.activeConnectionsPath = urlparse.urljoin(baseUrl, '/api/v1/connection')
|
2019-08-08 10:03:24 +00:00
|
|
|
self.versionPath = urlparse.urljoin(baseUrl, '/api/v1/version')
|
2019-08-01 22:00:48 +00:00
|
|
|
self.debug = debug
|
2019-08-08 10:03:24 +00:00
|
|
|
if authType == 'basic':
|
2019-08-07 21:06:26 +00:00
|
|
|
self.auth = requests.auth.HTTPBasicAuth(authUser, authPassword)
|
2019-08-08 10:03:24 +00:00
|
|
|
elif authType == 'digest':
|
2019-08-07 21:06:26 +00:00
|
|
|
self.auth = requests.auth.HTTPDigestAuth(authUser, authPassword)
|
2019-08-07 06:59:46 +00:00
|
|
|
else:
|
|
|
|
self.auth = None
|
2019-08-08 10:03:24 +00:00
|
|
|
self.verify = secure
|
2019-08-08 12:20:42 +00:00
|
|
|
self.no_color = no_color
|
2019-08-01 22:00:48 +00:00
|
|
|
|
|
|
|
def formatAsJSON(self, text):
|
|
|
|
if not text:
|
|
|
|
return ""
|
2019-08-08 12:20:42 +00:00
|
|
|
json_string = json.dumps(json.loads(text), sort_keys=True, indent=2)
|
|
|
|
if not self.no_color and pygments:
|
|
|
|
return pygments.highlight(json_string, JsonLexer(), TerminalFormatter())
|
|
|
|
return json_string
|
2019-08-01 22:00:48 +00:00
|
|
|
|
|
|
|
def printResponse(self, r):
|
|
|
|
if "content-type" in r.headers and "application/json" in r.headers["content-type"]:
|
|
|
|
if self.debug:
|
2019-08-08 12:20:42 +00:00
|
|
|
if pygments is None:
|
|
|
|
print('')
|
|
|
|
print('Response color highlight is not available: you need pygments 1.5 or above.')
|
|
|
|
print('')
|
|
|
|
print("Executed request: {} {} - request body: {}".format(
|
|
|
|
r.request.method, r.url, self.formatAsJSON(r.request.body)))
|
|
|
|
print('')
|
|
|
|
print("Got response, status code: {} body:".format(r.status_code))
|
2019-08-01 22:00:48 +00:00
|
|
|
print(self.formatAsJSON(r.text))
|
|
|
|
else:
|
|
|
|
print(r.text)
|
|
|
|
|
2019-08-07 21:41:10 +00:00
|
|
|
def buildUserObject(self, user_id=0, username="", password="", public_keys="", home_dir="", uid=0,
|
2019-08-01 22:00:48 +00:00
|
|
|
gid=0, max_sessions=0, quota_size=0, quota_files=0, permissions=[], upload_bandwidth=0,
|
|
|
|
download_bandwidth=0):
|
2019-08-17 13:20:49 +00:00
|
|
|
user = {"id":user_id, "username":username, "uid":uid, "gid":gid,
|
2019-08-01 22:00:48 +00:00
|
|
|
"max_sessions":max_sessions, "quota_size":quota_size, "quota_files":quota_files,
|
2019-09-17 06:53:45 +00:00
|
|
|
"upload_bandwidth":upload_bandwidth, "download_bandwidth":download_bandwidth}
|
2019-08-01 22:00:48 +00:00
|
|
|
if password:
|
|
|
|
user.update({"password":password})
|
2019-08-07 21:41:10 +00:00
|
|
|
if public_keys:
|
|
|
|
user.update({"public_keys":public_keys})
|
2019-08-17 13:20:49 +00:00
|
|
|
if home_dir:
|
|
|
|
user.update({"home_dir":home_dir})
|
|
|
|
if permissions:
|
|
|
|
user.update({"permissions":permissions})
|
2019-08-01 22:00:48 +00:00
|
|
|
return user
|
|
|
|
|
|
|
|
def getUsers(self, limit=100, offset=0, order="ASC", username=""):
|
|
|
|
r = requests.get(self.userPath, params={"limit":limit, "offset":offset, "order":order,
|
2019-08-07 21:06:26 +00:00
|
|
|
"username":username}, auth=self.auth, verify=self.verify)
|
2019-08-01 22:00:48 +00:00
|
|
|
self.printResponse(r)
|
|
|
|
|
|
|
|
def getUserByID(self, user_id):
|
2019-08-07 21:06:26 +00:00
|
|
|
r = requests.get(urlparse.urljoin(self.userPath, "user/" + str(user_id)), auth=self.auth, verify=self.verify)
|
2019-08-01 22:00:48 +00:00
|
|
|
self.printResponse(r)
|
|
|
|
|
2019-08-07 21:41:10 +00:00
|
|
|
def addUser(self, username="", password="", public_keys="", home_dir="", uid=0, gid=0, max_sessions=0,
|
2019-08-01 22:00:48 +00:00
|
|
|
quota_size=0, quota_files=0, permissions=[], upload_bandwidth=0, download_bandwidth=0):
|
2019-08-07 21:41:10 +00:00
|
|
|
u = self.buildUserObject(0, username, password, public_keys, home_dir, uid, gid, max_sessions,
|
2019-08-01 22:00:48 +00:00
|
|
|
quota_size, quota_files, permissions, upload_bandwidth, download_bandwidth)
|
2019-08-07 21:06:26 +00:00
|
|
|
r = requests.post(self.userPath, json=u, auth=self.auth, verify=self.verify)
|
2019-08-01 22:00:48 +00:00
|
|
|
self.printResponse(r)
|
|
|
|
|
2019-08-07 21:41:10 +00:00
|
|
|
def updateUser(self, user_id, username="", password="", public_keys="", home_dir="", uid=0, gid=0,
|
2019-08-01 22:00:48 +00:00
|
|
|
max_sessions=0, quota_size=0, quota_files=0, permissions=[], upload_bandwidth=0,
|
|
|
|
download_bandwidth=0):
|
2019-08-07 21:41:10 +00:00
|
|
|
u = self.buildUserObject(user_id, username, password, public_keys, home_dir, uid, gid, max_sessions,
|
2019-08-01 22:00:48 +00:00
|
|
|
quota_size, quota_files, permissions, upload_bandwidth, download_bandwidth)
|
2019-08-07 21:06:26 +00:00
|
|
|
r = requests.put(urlparse.urljoin(self.userPath, "user/" + str(user_id)), json=u, auth=self.auth, verify=self.verify)
|
2019-08-01 22:00:48 +00:00
|
|
|
self.printResponse(r)
|
|
|
|
|
|
|
|
def deleteUser(self, user_id):
|
2019-08-07 21:06:26 +00:00
|
|
|
r = requests.delete(urlparse.urljoin(self.userPath, "user/" + str(user_id)), auth=self.auth, verify=self.verify)
|
2019-08-01 22:00:48 +00:00
|
|
|
self.printResponse(r)
|
|
|
|
|
2019-08-24 12:41:15 +00:00
|
|
|
def getConnections(self):
|
2019-08-07 21:06:26 +00:00
|
|
|
r = requests.get(self.activeConnectionsPath, auth=self.auth, verify=self.verify)
|
2019-08-01 22:00:48 +00:00
|
|
|
self.printResponse(r)
|
|
|
|
|
2019-08-24 12:41:15 +00:00
|
|
|
def closeConnection(self, connectionID):
|
|
|
|
r = requests.delete(urlparse.urljoin(self.activeConnectionsPath, "connection/" + str(connectionID)), auth=self.auth)
|
2019-08-01 22:00:48 +00:00
|
|
|
self.printResponse(r)
|
|
|
|
|
|
|
|
def getQuotaScans(self):
|
2019-08-07 21:06:26 +00:00
|
|
|
r = requests.get(self.quotaScanPath, auth=self.auth, verify=self.verify)
|
2019-08-01 22:00:48 +00:00
|
|
|
self.printResponse(r)
|
|
|
|
|
|
|
|
def startQuotaScan(self, username):
|
|
|
|
u = self.buildUserObject(0, username)
|
2019-08-07 21:06:26 +00:00
|
|
|
r = requests.post(self.quotaScanPath, json=u, auth=self.auth, verify=self.verify)
|
2019-08-01 22:00:48 +00:00
|
|
|
self.printResponse(r)
|
|
|
|
|
2019-08-08 08:01:33 +00:00
|
|
|
def getVersion(self):
|
|
|
|
r = requests.get(self.versionPath, auth=self.auth, verify=self.verify)
|
|
|
|
self.printResponse(r)
|
|
|
|
|
2019-08-01 22:00:48 +00:00
|
|
|
|
|
|
|
def addCommonUserArguments(parser):
|
|
|
|
parser.add_argument('username', type=str)
|
2019-08-08 10:03:24 +00:00
|
|
|
parser.add_argument('-P', '--password', type=str, default="", help='Default: %(default)s')
|
|
|
|
parser.add_argument('-K', '--public-keys', type=str, nargs='+', default=[], help='Default: %(default)s')
|
|
|
|
parser.add_argument('-H', '--home-dir', type=str, default="", help='Default: %(default)s')
|
|
|
|
parser.add_argument('--uid', type=int, default=0, help='Default: %(default)s')
|
|
|
|
parser.add_argument('--gid', type=int, default=0, help='Default: %(default)s')
|
|
|
|
parser.add_argument('-C', '--max-sessions', type=int, default=0,
|
|
|
|
help='Maximum concurrent sessions. 0 means unlimited. Default: %(default)s')
|
|
|
|
parser.add_argument('-S', '--quota-size', type=int, default=0,
|
|
|
|
help='Maximum size allowed as bytes. 0 means unlimited. Default: %(default)s')
|
|
|
|
parser.add_argument('-F', '--quota-files', type=int, default=0, help="default: %(default)s")
|
|
|
|
parser.add_argument('-G', '--permissions', type=str, nargs='+', default=[],
|
2019-09-17 06:53:45 +00:00
|
|
|
choices=['*', 'list', 'download', 'upload', 'overwrite', 'delete', 'rename', 'create_dirs',
|
2019-08-08 10:03:24 +00:00
|
|
|
'create_symlinks'], help='Default: %(default)s')
|
|
|
|
parser.add_argument('-U', '--upload-bandwidth', type=int, default=0,
|
|
|
|
help='Maximum upload bandwidth as KB/s, 0 means unlimited. Default: %(default)s')
|
|
|
|
parser.add_argument('-D', '--download-bandwidth', type=int, default=0,
|
|
|
|
help='Maximum download bandwidth as KB/s, 0 means unlimited. Default: %(default)s')
|
2019-08-01 22:00:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
|
2019-08-08 10:03:24 +00:00
|
|
|
parser.add_argument('-b', '--base-url', type=str, default='http://127.0.0.1:8080',
|
|
|
|
help='Base URL for SFTPGo REST API. Default: %(default)s')
|
|
|
|
parser.add_argument('-a', '--auth-type', type=str, default=None, choices=['basic', 'digest'],
|
|
|
|
help='HTTP authentication type. Default: %(default)s')
|
|
|
|
parser.add_argument("-u", "--auth-user", type=str, default="",
|
2019-08-08 12:20:42 +00:00
|
|
|
help='User for HTTP authentication. Default: %(default)s')
|
2019-08-08 10:03:24 +00:00
|
|
|
parser.add_argument('-p', '--auth-password', type=str, default='',
|
2019-08-08 12:20:42 +00:00
|
|
|
help='Password for HTTP authentication. Default: %(default)s')
|
2019-08-08 10:03:24 +00:00
|
|
|
parser.add_argument('-d', '--debug', dest='debug', action='store_true')
|
2019-08-01 22:00:48 +00:00
|
|
|
parser.set_defaults(debug=False)
|
2019-08-08 12:20:42 +00:00
|
|
|
parser.add_argument('-i', '--insecure', dest='secure', action='store_false',
|
2019-08-08 10:03:24 +00:00
|
|
|
help='Set to false to ignore verifying the SSL certificate')
|
|
|
|
parser.set_defaults(secure=True)
|
2019-08-08 12:20:42 +00:00
|
|
|
parser.add_argument('-t', '--no-color', dest='no_color', action='store_true',
|
|
|
|
help='Disable color highlight for JSON responses. You need python pygments module 1.5 or above to have highlighted output')
|
|
|
|
parser.set_defaults(no_color=(pygments is None))
|
2019-08-01 22:00:48 +00:00
|
|
|
|
2019-08-08 10:03:24 +00:00
|
|
|
subparsers = parser.add_subparsers(dest='command', help='sub-command --help')
|
2019-08-01 22:00:48 +00:00
|
|
|
subparsers.required = True
|
|
|
|
|
2019-08-08 10:03:24 +00:00
|
|
|
parserAddUser = subparsers.add_parser('add-user', help='Add a new SFTP user')
|
2019-08-01 22:00:48 +00:00
|
|
|
addCommonUserArguments(parserAddUser)
|
|
|
|
|
2019-08-08 10:03:24 +00:00
|
|
|
parserUpdateUser = subparsers.add_parser('update-user', help='Update an existing user')
|
|
|
|
parserUpdateUser.add_argument('id', type=int, help='User\'s ID to update')
|
2019-08-01 22:00:48 +00:00
|
|
|
addCommonUserArguments(parserUpdateUser)
|
|
|
|
|
2019-08-08 10:03:24 +00:00
|
|
|
parserDeleteUser = subparsers.add_parser('delete-user', help='Delete an existing user')
|
|
|
|
parserDeleteUser.add_argument('id', type=int, help='User\'s ID to delete')
|
2019-08-01 22:00:48 +00:00
|
|
|
|
2019-08-08 10:03:24 +00:00
|
|
|
parserGetUsers = subparsers.add_parser('get-users', help='Returns an array with one or more SFTP users')
|
|
|
|
parserGetUsers.add_argument('-L', '--limit', type=int, default=100, choices=range(1, 501),
|
|
|
|
help='Maximum allowed value is 500. Default: %(default)s', metavar='[1...500]')
|
|
|
|
parserGetUsers.add_argument('-O', '--offset', type=int, default=0, help='Default: %(default)s')
|
|
|
|
parserGetUsers.add_argument('-U', '--username', type=str, default='', help='Default: %(default)s')
|
|
|
|
parserGetUsers.add_argument('-S', '--order', type=str, choices=['ASC', 'DESC'], default='ASC',
|
|
|
|
help='default: %(default)s')
|
2019-08-01 22:00:48 +00:00
|
|
|
|
2019-08-08 10:03:24 +00:00
|
|
|
parserGetUserByID = subparsers.add_parser('get-user-by-id', help='Find user by ID')
|
2019-08-01 22:00:48 +00:00
|
|
|
parserGetUserByID.add_argument('id', type=int)
|
|
|
|
|
2019-08-24 12:41:15 +00:00
|
|
|
parserGetConnections = subparsers.add_parser('get-connections',
|
|
|
|
help='Get the active users and info about their uploads/downloads')
|
2019-08-01 22:00:48 +00:00
|
|
|
|
2019-08-24 12:41:15 +00:00
|
|
|
parserCloseConnection = subparsers.add_parser('close-connection', help='Terminate an active SFTP/SCP connection')
|
|
|
|
parserCloseConnection.add_argument('connectionID', type=str)
|
2019-08-01 22:00:48 +00:00
|
|
|
|
2019-08-08 10:03:24 +00:00
|
|
|
parserGetQuotaScans = subparsers.add_parser('get-quota-scans', help='Get the active quota scans')
|
2019-08-01 22:00:48 +00:00
|
|
|
|
2019-08-08 10:03:24 +00:00
|
|
|
parserStartQuotaScans = subparsers.add_parser('start-quota-scan', help='Start a new quota scan')
|
2019-08-01 22:00:48 +00:00
|
|
|
addCommonUserArguments(parserStartQuotaScans)
|
|
|
|
|
2019-08-08 10:03:24 +00:00
|
|
|
parserGetVersion = subparsers.add_parser('get-version', help='Get version details')
|
2019-08-08 08:01:33 +00:00
|
|
|
|
2019-08-01 22:00:48 +00:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
2019-08-08 12:20:42 +00:00
|
|
|
api = SFTPGoApiRequests(args.debug, args.base_url, args.auth_type, args.auth_user, args.auth_password, args.secure,
|
|
|
|
args.no_color)
|
2019-08-01 22:00:48 +00:00
|
|
|
|
2019-08-08 10:03:24 +00:00
|
|
|
if args.command == 'add-user':
|
2019-08-07 21:41:10 +00:00
|
|
|
api.addUser(args.username, args.password, args.public_keys, args.home_dir,
|
2019-08-01 22:00:48 +00:00
|
|
|
args.uid, args.gid, args.max_sessions, args.quota_size, args.quota_files,
|
|
|
|
args.permissions, args.upload_bandwidth, args.download_bandwidth)
|
2019-08-08 10:03:24 +00:00
|
|
|
elif args.command == 'update-user':
|
2019-08-07 21:41:10 +00:00
|
|
|
api.updateUser(args.id, args.username, args.password, args.public_keys, args.home_dir,
|
2019-08-01 22:00:48 +00:00
|
|
|
args.uid, args.gid, args.max_sessions, args.quota_size, args.quota_files,
|
|
|
|
args.permissions, args.upload_bandwidth, args.download_bandwidth)
|
2019-08-08 10:03:24 +00:00
|
|
|
elif args.command == 'delete-user':
|
2019-08-01 22:00:48 +00:00
|
|
|
api.deleteUser(args.id)
|
2019-08-08 10:03:24 +00:00
|
|
|
elif args.command == 'get-users':
|
2019-08-01 22:00:48 +00:00
|
|
|
api.getUsers(args.limit, args.offset, args.order, args.username)
|
2019-08-08 10:03:24 +00:00
|
|
|
elif args.command == 'get-user-by-id':
|
2019-08-01 22:00:48 +00:00
|
|
|
api.getUserByID(args.id)
|
2019-08-24 12:41:15 +00:00
|
|
|
elif args.command == 'get-connections':
|
|
|
|
api.getConnections()
|
|
|
|
elif args.command == 'close-connection':
|
|
|
|
api.closeConnection(args.connectionID)
|
2019-08-08 10:03:24 +00:00
|
|
|
elif args.command == 'get-quota-scans':
|
2019-08-01 22:00:48 +00:00
|
|
|
api.getQuotaScans()
|
2019-08-08 10:03:24 +00:00
|
|
|
elif args.command == 'start-quota-scan':
|
2019-08-01 22:00:48 +00:00
|
|
|
api.startQuotaScan(args.username)
|
2019-08-08 10:03:24 +00:00
|
|
|
elif args.command == 'get-version':
|
2019-08-08 08:01:33 +00:00
|
|
|
api.getVersion()
|
2019-08-01 22:00:48 +00:00
|
|
|
|