infobloxopen/infoblox-client

session cookies are not maintained when downloading a file via the connector

Opened this issue · 3 comments

version = 0.6.0
os = mac

There is an issue of NOT carrying the cookie jar contents of the ibapauth cookie to conn.download_file() method call. This results in a 401 Authorization Required result.

#!/usr/bin/env python3
import sys
from infoblox_client import connector
ibx_gm_host = 'infoblox.local'
username = 'admin'
password = 'infoblox'

# Setup basic connection object
opts = {
    "host": ibx_gm_host,
    "username": username,
    "password": password,
    "ssl_verify": False,
    "silent_ssl_warnings": True,
}
conn = connector.Connector(opts)

r = conn.call_func('getmemberdata', 'fileop', {"member": "ibx01.example.com", "type": "DNS_CFG"})

token = r.get('token')
url = r.get('url')

print(r)

r = conn.download_file(url)
print(r)
with open('named.tar.gz', 'wb') as fh:
    for blk in r.iter_content(1024):
        if not blk:
            break
        fh.write(blk)
data = dict(token=token)
r = conn.call_func('downloadcomplete', 'fileop', data)
print(r)

The workaround is to use the IP Address of the Grid Manager instead of the FQDN, b/c the resulting download_url is always returned by the GM using it's IP address - this is a change to the session.

Unable to download file from 'https://192.168.1.2/http_direct_file_io/req_id-DOWNLOAD-0412215901103461/dnsConf.tar.gz'

here's a diff for a fix:

diff --git a/infoblox_client/connector.py b/infoblox_client/connector.py
index 7880015..5b2742c 100644
--- a/infoblox_client/connector.py
+++ b/infoblox_client/connector.py
@@ -374,8 +374,10 @@ class Connector(object):
     def download_file(self, url):
         if self.session.cookies:
             self.session.auth = None
+        ibapauth_cookie = self.session.cookies.get('ibapauth')
+        req_cookies = {'ibapauth': ibapauth_cookie}
         headers = {'content-type': 'application/force-download'}
-        r = self.session.get(url, headers=headers)
+        r = self.session.get(url, headers=headers, cookies=req_cookies)
         if r.status_code != requests.codes.ok:
             response = utils.safe_json_load(r.content)
             raise ib_ex.InfobloxFileDownloadFailed(

with the above change(s) to the connector - the script now works when using an FQDN for the Grid Manager:

./ibxc-test.py
{'token': 'eJytjk0LgjAAhv+K7Jybm2s4b5YFQShE0HGImzZQZ3NBH/Tfc4e6dun6vJ9PoG6jtnfhdK9AGmCW\n4JjGMcGQUbLkZBGAq+1mBZydG6cUIcwJnG2QRpBFyFMhtVW1E43ulNAGWXURWoZ5eSr2ZZaHEcWE\nEJpQzGLOOUNymNZmaKCrLGwfYN6QlauEGmoj9dD6tdWu+PLeSP8N5NkxE4fN9iN4hiZnbNUq5Prx\nX2e09M2/guD1Br3oXfc=\n', 'url': 'https://192.168.40.60/http_direct_file_io/req_id-DOWNLOAD-0412224841639996/dnsConf.tar.gz'}
<Response [200]>
{}

You can alternatively workaround this issue by replacing the IP address in the returned URL with the host value from the opts object originally passed to the Connector constructor. For example:

url = r.get('url')
url = url.replace(url[8:url.index('/',8)], opts['host'])

r = conn.download_file(url)