9 This class wraps one instance of Untangle. It gets initialized with
10 some configuration information, and then provides the methods to
13 def __init__(self
, s
):
15 Initialize this Untangle object with a ConfigParser section.
19 self
.username
= s
.get('username', 'admin')
20 self
.password
= s
['password']
21 self
.version
= int(s
.get('version', '12'))
22 self
.base_url
= 'https://' + self
.host
+ '/' # This never changes
24 # Sanity check the numerical version.
25 if self
.version
not in [9, 10, 11, 12]:
26 msg
= 'Invalid version "' + str(self
.version
) + '" '
27 msg
+= 'in section "' + s
.name
+ '"'
28 raise configparser
.ParsingError(msg
)
30 # Sanity check the boolean verify_cert parameter.
31 vc
= s
.get('verify_cert', 'False')
33 self
.verify_cert
= True
35 self
.verify_cert
= False
37 msg
= 'Invalid value "' + vc
+ '" for verify_cert '
38 msg
+= 'in section "' + s
.name
+ '"'
39 raise configparser
.ParsingError(msg
)
42 # Finally, create a URL opener to make HTTPS requests.
44 # First, create a cookie jar that we'll attach to our URL
46 cj
= http
.cookiejar
.CookieJar()
47 cookie_proc
= urllib
.request
.HTTPCookieProcessor(cj
)
49 # SSL mumbo jumbo to make it ignore the certificate's hostname
50 # when verify_cert = False.
52 ssl_ctx
= ssl
.create_default_context()
54 ssl_ctx
= ssl
._create
_unverified
_context
()
56 https_handler
= urllib
.request
.HTTPSHandler(context
=ssl_ctx
)
58 # Now Create a URL opener, and tell it to use our cookie jar
59 # and SSL context. We keep this around for future requests.
60 self
.opener
= urllib
.request
.build_opener(https_handler
, cookie_proc
)
65 Perform the HTTPS request to log in to the Untangle web admin
66 UI. The resulting session cookie is stored by our ``self.opener``.
68 login_path
= 'auth/login?url=/setup/welcome.do&realm=Administrator'
69 url
= self
.base_url
+ login_path
70 post_vars
= {'username': self.username, 'password': self.password }
71 post_data
= urllib
.parse
.urlencode(post_vars
).encode('ascii')
72 self
.opener
.open(url
, post_data
)
77 Version-agnostic get-me-a-backup method. Dispatches to the
78 actual implementation based on ``self.version``.
81 return self
.get_backup_v9()
82 elif self
.version
in [10, 11, 12]:
83 # The procedure for v11 or v12 is the same as for v10.
84 return self
.get_backup_v10()
87 def get_backup_v9(self
):
89 Retrieve a backup from Untangle version 9. This requires two
90 requests; the first just hits the page, and the second actually
91 retrieves the backup file.
93 Returns the binary HTTPS response (i.e. the file).
95 url
= self
.base_url
+ '/webui/backup'
96 post_vars
= {'action': 'requestBackup'}
97 post_data
= urllib
.parse
.urlencode(post_vars
).encode('ascii')
98 self
.opener
.open(url
, post_data
)
100 url
= self
.base_url
+ 'webui/backup?action=initiateDownload'
101 with self
.opener
.open(url
) as response
:
102 return response
.read()
105 def get_backup_v10(self
):
107 Retrieve a backup from Untangle version 10.
109 Returns the binary HTTPS response (i.e. the file).
111 url
= self
.base_url
+ '/webui/download?type=backup'
112 post_vars
= {'type': 'backup'}
113 post_data
= urllib
.parse
.urlencode(post_vars
).encode('ascii')
114 with self
.opener
.open(url
, post_data
) as response
:
115 return response
.read()