datacenter / acitoolkit

A basic toolkit for accessing the Cisco APIC
Other
347 stars 266 forks source link

TypeError tripped By acisession cookie object when app runs in docker container #375

Closed seansica closed 3 years ago

seansica commented 3 years ago

I are using the acitoolkit in a Flask app to fetch data from the APIC. A TypeError trips when I run the app in a container. Here is the traceback:

(dev) ssica@computer % docker container attach 043c25cb0000
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/gevent/pywsgi.py", line 999, in handle_one_response
    self.run_application()
  File "/usr/local/lib/python3.9/site-packages/gevent/pywsgi.py", line 945, in run_application
    self.result = self.application(self.environ, self.start_response)
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 2069, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 2054, in wsgi_app
    response = self.handle_exception(e)
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 2051, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1501, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1499, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1485, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
  File "/acinow/leaf_blueprint.py", line 12, in show_leaf_nodes
    apic_response = apic.leaf.get_leaf_nodes()
  File "/acinow/apic_handler/leaf.py", line 15, in get_leaf_nodes
    nodes = ACI.Session.get(_session, query_url)
  File "/usr/local/lib/python3.9/site-packages/acitoolkit/acisession.py", line 816, in get
    resp = self.session.get(get_url, timeout=timeout, verify=self.verify_ssl, proxies=self._proxies, cookies=cookies)
  File "/usr/local/lib/python3.9/site-packages/requests/sessions.py", line 555, in get
    return self.request('GET', url, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/requests/sessions.py", line 528, in request
    prep = self.prepare_request(req)
  File "/usr/local/lib/python3.9/site-packages/requests/sessions.py", line 444, in prepare_request
    cookies = cookiejar_from_dict(cookies)
  File "/usr/local/lib/python3.9/site-packages/requests/cookies.py", line 524, in cookiejar_from_dict
    cookiejar.set_cookie(create_cookie(name, cookie_dict[name]))
  File "/usr/local/lib/python3.9/site-packages/requests/cookies.py", line 344, in set_cookie
    if hasattr(cookie.value, 'startswith') and cookie.value.startswith('"') and cookie.value.endswith('"'):
TypeError: startswith first arg must be bytes or a tuple of bytes, not str
2021-05-12T16:54:39Z {'REMOTE_ADDR': '172.17.0.1', 'REMOTE_PORT': '57456', 'HTTP_HOST': 'localhost:5000', (hidden keys: 25)} failed with TypeError

We are using certificate based authentication to login to APIC. We are using an unmodified version of the acitoolkit library. We simply imported via pip.

_creds = ACI.Credentials()
_args = _creds.get()

_session = ACI.Session(_args.url, _args.login, subscription_enabled=False, cert_name='apiadmin.crt', key='apiadmin.key')

I am exposing only the Flask port (TCP/5000) that clients connect to.

CONTAINER ID   IMAGE                 COMMAND            CREATED         STATUS         PORTS                    NAMES
043c25cb0000   acinow_flask_buster   "python wsgi.py"   3 minutes ago   Up 3 minutes   0.0.0.0:5000->5000/tcp   nice_rhodes

I prefer to keep the acitoolkit unmodified so I can simply import it rather than maintain a local modified version. Does anyone have any suggestions on how to resolve the TypeError?