X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2Funtangle%2Funtangle.py;h=c66cd67b45dbbe56c67d3ed8d1654d2e7cb7f5fd;hb=b5f616878bac3673de90e9c47bf8dedbae790dfd;hp=09c5723c9c11a08a98dc250973716ec081ecdd3a;hpb=1b7c4afc84bf55acef350fdc5479e7213c78eaa4;p=untangle-https-backup.git diff --git a/src/untangle/untangle.py b/src/untangle/untangle.py index 09c5723..c66cd67 100644 --- a/src/untangle/untangle.py +++ b/src/untangle/untangle.py @@ -5,7 +5,11 @@ import urllib.parse import urllib.request class Untangle: - + """ + This class wraps one instance of Untangle. It gets initialized with + some configuration information, and then provides the methods to + retreive a backup. + """ def __init__(self, s): """ Initialize this Untangle object with a ConfigParser section. @@ -14,11 +18,11 @@ class Untangle: self.host = s['host'] self.username = s.get('username', 'admin') self.password = s['password'] - self.version = int(s.get('version', '11')) + self.version = int(s.get('version', '12')) self.base_url = 'https://' + self.host + '/' # This never changes # Sanity check the numerical version. - if self.version not in [9, 11]: + if self.version not in [9, 10, 11, 12]: msg = 'Invalid version "' + str(self.version) + '" ' msg += 'in section "' + s.name + '"' raise configparser.ParsingError(msg) @@ -34,7 +38,6 @@ class Untangle: msg += 'in section "' + s.name + '"' raise configparser.ParsingError(msg) - # # Finally, create a URL opener to make HTTPS requests. # # First, create a cookie jar that we'll attach to our URL @@ -57,6 +60,10 @@ class Untangle: def login(self): + """ + Perform the HTTPS request to log in to the Untangle web admin + UI. The resulting session cookie is stored by our ``self.opener``. + """ login_path = 'auth/login?url=/setup/welcome.do&realm=Administrator' url = self.base_url + login_path post_vars = {'username': self.username, 'password': self.password } @@ -65,13 +72,25 @@ class Untangle: def get_backup(self): + """ + Version-agnostic get-me-a-backup method. Dispatches to the + actual implementation based on ``self.version``. + """ if self.version == 9: return self.get_backup_v9() - elif self.version == 11: - return self.get_backup_v11() + elif self.version in [10, 11, 12]: + # The procedure for v11 or v12 is the same as for v10. + return self.get_backup_v10() def get_backup_v9(self): + """ + Retrieve a backup from Untangle version 9. This requires two + requests; the first just hits the page, and the second actually + retrieves the backup file. + + Returns the binary HTTPS response (i.e. the file). + """ url = self.base_url + '/webui/backup' post_vars = {'action': 'requestBackup'} post_data = urllib.parse.urlencode(post_vars).encode('ascii') @@ -82,8 +101,13 @@ class Untangle: return response.read() - def get_backup_v11(self): - url = self.base_url + '/webui/download?type=backup' + def get_backup_v10(self): + """ + Retrieve a backup from Untangle version 10. + + Returns the binary HTTPS response (i.e. the file). + """ + url = self.base_url + '/webui/download' post_vars = {'type': 'backup'} post_data = urllib.parse.urlencode(post_vars).encode('ascii') with self.opener.open(url, post_data) as response: