Skip to content

Use API Key in module artifactory download #58936

@jvimont

Description

@jvimont

Description of Issue

I try to download an artifact from artifactory but i got an "ValueError: Invalid header value".
It seems there is a format issue regarding the base64 encoding of the Authorisation header. The "\n" included by the hashutil.base64_encodestring, as said in salt.modules.hashutil.base64_encodestring doc, are not removed.

Setup

Create api key in artifactory
Target an existing artifact_id using username and api_key

Steps to Reproduce Issue

salt minion_id artifactory.get_release artifactory_url=my_artifactory_url repository=my_repo group_id=my_group artifact_id=my_artifact packaging=jar version=0.1 target_dir='/tmp' username=foo, password=my_artifactory_api_key

minion_id:
    The minion function caused an exception: Traceback (most recent call last):
      File "/usr/lib/python3/dist-packages/salt/minion.py", line 1889, in _thread_return
        function_name, function_args, executors, opts, data
      File "/usr/lib/python3/dist-packages/salt/minion.py", line 1845, in _execute_job_function
        return_data = self.executors[fname](opts, data, func, args, kwargs)
      File "/usr/lib/python3/dist-packages/salt/executors/direct_call.py", line 12, in execute
        return func(*args, **kwargs)
      File "/usr/lib/python3/dist-packages/salt/modules/artifactory.py", line 354, in get_release
        return __save_artifact(release_url, target_file, headers)
      File "/usr/lib/python3/dist-packages/salt/modules/artifactory.py", line 744, in __save_artifact
        f = urllib.request.urlopen(request)
      File "/usr/lib/python3.7/urllib/request.py", line 222, in urlopen
        return opener.open(url, data, timeout)
      File "/usr/lib/python3.7/urllib/request.py", line 525, in open
        response = self._open(req, data)
      File "/usr/lib/python3.7/urllib/request.py", line 543, in _open
        '_open', req)
      File "/usr/lib/python3.7/urllib/request.py", line 503, in _call_chain
        result = func(*args)
      File "/usr/lib/python3.7/urllib/request.py", line 1367, in https_open
        context=self._context, check_hostname=self._check_hostname)
      File "/usr/lib/python3.7/urllib/request.py", line 1324, in do_open
        encode_chunked=req.has_header('Transfer-encoding'))
      File "/usr/lib/python3.7/http/client.py", line 1244, in request
        self._send_request(method, url, body, headers, encode_chunked)
      File "/usr/lib/python3.7/http/client.py", line 1285, in _send_request
        self.putheader(hdr, value)
      File "/usr/lib/python3.7/http/client.py", line 1222, in putheader
        raise ValueError('Invalid header value %r' % (values[i],))
    ValueError: Invalid header value b'Basic XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX**\n**XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX**\n**'

It can be fixed with this workaround:

if username and password:
        headers["Authorization"] = "Basic {0}".format(
            salt.utils.hashutils.base64_encodestring(
                "{0}:{1}".format(username.replace("\n", ""), password.replace("\n", ""))
            ).replace("\n", "")

Versions Report

Same version on master and minion.

Salt Version:
           Salt: 3002.1
 
Dependency Versions:
           cffi: 1.14.3
       cherrypy: unknown
       dateutil: 2.7.3
      docker-py: Not Installed
          gitdb: 2.0.5
      gitpython: 2.1.11
         Jinja2: 2.10
        libgit2: Not Installed
       M2Crypto: 0.31.0
           Mako: Not Installed
   msgpack-pure: Not Installed
 msgpack-python: 0.5.6
   mysql-python: Not Installed
      pycparser: 2.20
       pycrypto: Not Installed
   pycryptodome: 3.6.1
         pygit2: Not Installed
         Python: 3.7.3 (default, Jul 25 2020, 13:03:44)
   python-gnupg: Not Installed
         PyYAML: 3.13
          PyZMQ: 17.1.2
          smmap: 2.0.5
        timelib: Not Installed
        Tornado: 4.5.3
            ZMQ: 4.3.1
 
System Versions:
           dist: debian 10 buster
         locale: UTF-8
        machine: x86_64
        release: 4.19.0-12-amd64
         system: Linux
        version: Debian GNU/Linux 10 buster

Metadata

Metadata

Assignees

No one assigned

    Labels

    Bugbroken, incorrect, or confusing behaviorseverity-medium3rd level, incorrect or bad functionality, confusing and lacks a work around

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions