|
@@ -3,6 +3,7 @@ import argparse
|
|
|
import json
|
|
|
|
|
|
import requests
|
|
|
+from requests.auth import HTTPBasicAuth, HTTPDigestAuth
|
|
|
|
|
|
try:
|
|
|
import urllib.parse as urlparse
|
|
@@ -12,11 +13,17 @@ except ImportError:
|
|
|
|
|
|
class SFTPGoApiRequests:
|
|
|
|
|
|
- def __init__(self, debug, baseUrl):
|
|
|
+ def __init__(self, debug, baseUrl, authType, authUser, authPassword):
|
|
|
self.userPath = urlparse.urljoin(baseUrl, "/api/v1/user")
|
|
|
self.quotaScanPath = urlparse.urljoin(baseUrl, "/api/v1/quota_scan")
|
|
|
self.activeConnectionsPath = urlparse.urljoin(baseUrl, "/api/v1/sftp_connection")
|
|
|
self.debug = debug
|
|
|
+ if authType == "basic":
|
|
|
+ self.auth = HTTPBasicAuth(authUser, authPassword)
|
|
|
+ elif authType == "digest":
|
|
|
+ self.auth = HTTPDigestAuth(authUser, authPassword)
|
|
|
+ else:
|
|
|
+ self.auth = None
|
|
|
|
|
|
def formatAsJSON(self, text):
|
|
|
if not text:
|
|
@@ -48,18 +55,18 @@ class SFTPGoApiRequests:
|
|
|
|
|
|
def getUsers(self, limit=100, offset=0, order="ASC", username=""):
|
|
|
r = requests.get(self.userPath, params={"limit":limit, "offset":offset, "order":order,
|
|
|
- "username":username})
|
|
|
+ "username":username}, auth=self.auth)
|
|
|
self.printResponse(r)
|
|
|
|
|
|
def getUserByID(self, user_id):
|
|
|
- r = requests.get(urlparse.urljoin(self.userPath, "user/" + str(user_id)))
|
|
|
+ r = requests.get(urlparse.urljoin(self.userPath, "user/" + str(user_id)), auth=self.auth)
|
|
|
self.printResponse(r)
|
|
|
|
|
|
def addUser(self, username="", password="", public_key="", home_dir="", uid=0, gid=0, max_sessions=0,
|
|
|
quota_size=0, quota_files=0, permissions=[], upload_bandwidth=0, download_bandwidth=0):
|
|
|
u = self.buildUserObject(0, username, password, public_key, home_dir, uid, gid, max_sessions,
|
|
|
quota_size, quota_files, permissions, upload_bandwidth, download_bandwidth)
|
|
|
- r = requests.post(self.userPath, json=u)
|
|
|
+ r = requests.post(self.userPath, json=u, auth=self.auth)
|
|
|
self.printResponse(r)
|
|
|
|
|
|
def updateUser(self, user_id, username="", password="", public_key="", home_dir="", uid=0, gid=0,
|
|
@@ -67,28 +74,28 @@ class SFTPGoApiRequests:
|
|
|
download_bandwidth=0):
|
|
|
u = self.buildUserObject(user_id, username, password, public_key, home_dir, uid, gid, max_sessions,
|
|
|
quota_size, quota_files, permissions, upload_bandwidth, download_bandwidth)
|
|
|
- r = requests.put(urlparse.urljoin(self.userPath, "user/" + str(user_id)), json=u)
|
|
|
+ r = requests.put(urlparse.urljoin(self.userPath, "user/" + str(user_id)), json=u, auth=self.auth)
|
|
|
self.printResponse(r)
|
|
|
|
|
|
def deleteUser(self, user_id):
|
|
|
- r = requests.delete(urlparse.urljoin(self.userPath, "user/" + str(user_id)))
|
|
|
+ r = requests.delete(urlparse.urljoin(self.userPath, "user/" + str(user_id)), auth=self.auth)
|
|
|
self.printResponse(r)
|
|
|
|
|
|
def getSFTPConnections(self):
|
|
|
- r = requests.get(self.activeConnectionsPath)
|
|
|
+ r = requests.get(self.activeConnectionsPath, auth=self.auth)
|
|
|
self.printResponse(r)
|
|
|
|
|
|
def closeSFTPConnection(self, connectionID):
|
|
|
- r = requests.delete(urlparse.urljoin(self.userPath, "sftp_connection/" + str(connectionID)))
|
|
|
+ r = requests.delete(urlparse.urljoin(self.userPath, "sftp_connection/" + str(connectionID)), auth=self.auth)
|
|
|
self.printResponse(r)
|
|
|
|
|
|
def getQuotaScans(self):
|
|
|
- r = requests.get(self.quotaScanPath)
|
|
|
+ r = requests.get(self.quotaScanPath, auth=self.auth)
|
|
|
self.printResponse(r)
|
|
|
|
|
|
def startQuotaScan(self, username):
|
|
|
u = self.buildUserObject(0, username)
|
|
|
- r = requests.post(self.quotaScanPath, json=u)
|
|
|
+ r = requests.post(self.quotaScanPath, json=u, auth=self.auth)
|
|
|
self.printResponse(r)
|
|
|
|
|
|
|
|
@@ -113,6 +120,12 @@ if __name__ == '__main__':
|
|
|
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
|
|
|
parser.add_argument("--base_url", type=str, default="http://127.0.0.1:8080",
|
|
|
help="Base URL for SFTPGo REST API. Default: %(default)s")
|
|
|
+ parser.add_argument("--auth_type", type=str, default="", choices=["", "basic", "digest"],
|
|
|
+ help="Authentication type to use. Default: %(default)s")
|
|
|
+ parser.add_argument("--auth_user", type=str, default="",
|
|
|
+ help="User to use for authentication. Default: %(default)s")
|
|
|
+ parser.add_argument("--auth_password", type=str, default="",
|
|
|
+ help="Password to use for authentication. Default: %(default)s")
|
|
|
parser.add_argument("--debug", dest='debug', action='store_true')
|
|
|
parser.set_defaults(debug=False)
|
|
|
|
|
@@ -151,7 +164,7 @@ if __name__ == '__main__':
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
- api = SFTPGoApiRequests(args.debug, args.base_url)
|
|
|
+ api = SFTPGoApiRequests(args.debug, args.base_url, args.auth_type, args.auth_user, args.auth_password)
|
|
|
|
|
|
if args.command == "add_user":
|
|
|
api.addUser(args.username, args.password, args.public_key, args.home_dir,
|