Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
yeti9990 opened this issue Apr 12, 2023 · 3 comments · May be fixed by #368
Open

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

yeti9990 opened this issue Apr 12, 2023 · 3 comments · May be fixed by #368

Comments

@yeti9990
Copy link

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'
@yeti9990
Copy link
Author

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(

@yeti9990
Copy link
Author

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]>
{}

yeti9990 pushed a commit to yeti9990/infoblox-client that referenced this issue Apr 12, 2023
@yeti9990 yeti9990 linked a pull request May 19, 2023 that will close this issue
@achernevskii achernevskii linked a pull request Jun 16, 2023 that will close this issue
@rmbolger
Copy link

rmbolger commented Dec 7, 2023

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants