From 4960c908048320105643aa32eae80bb4e02f51fc Mon Sep 17 00:00:00 2001 From: Dmitry Kuzmenko Date: Tue, 23 Jun 2020 13:55:30 +0300 Subject: [PATCH] Use hashutils and stringutils for bytes to unicode conversion. --- salt/modules/artifactory.py | 40 ++++++++++++++----------------------- 1 file changed, 15 insertions(+), 25 deletions(-) diff --git a/salt/modules/artifactory.py b/salt/modules/artifactory.py index 1794f7c00057..af08b285a394 100644 --- a/salt/modules/artifactory.py +++ b/salt/modules/artifactory.py @@ -6,14 +6,13 @@ # Import python libs from __future__ import absolute_import, print_function, unicode_literals -import base64 import logging import os -import salt.ext.six.moves.http_client # pylint: disable=import-error,redefined-builtin,no-name-in-module - # Import Salt libs +import salt.ext.six.moves.http_client # pylint: disable=import-error,redefined-builtin,no-name-in-module import salt.utils.files +import salt.utils.hashutils import salt.utils.stringutils from salt.exceptions import CommandExecutionError from salt.ext.six.moves import urllib # pylint: disable=no-name-in-module @@ -100,12 +99,10 @@ def get_latest_snapshot( headers = {} if username and password: - data_string = "{0}:{1}".format( - username.replace("\n", ""), password.replace("\n", "") - ) - data_bytes = data_string.encode("utf-8") headers["Authorization"] = "Basic {0}".format( - base64.b64encode(data_bytes).decode("utf-8") + salt.utils.hashutils.base64_encodestring( + "{0}:{1}".format(username.replace("\n", ""), password.replace("\n", "")) + ) ) artifact_metadata = _get_artifact_metadata( artifactory_url=artifactory_url, @@ -186,12 +183,10 @@ def get_snapshot( ) headers = {} if username and password: - data_string = "{0}:{1}".format( - username.replace("\n", ""), password.replace("\n", "") - ) - data_bytes = data_string.encode("utf-8") headers["Authorization"] = "Basic {0}".format( - base64.b64encode(data_bytes).decode("utf-8") + salt.utils.hashutils.base64_encodestring( + "{0}:{1}".format(username.replace("\n", ""), password.replace("\n", "")) + ) ) snapshot_url, file_name = _get_snapshot_url( artifactory_url=artifactory_url, @@ -259,12 +254,10 @@ def get_latest_release( ) headers = {} if username and password: - data_string = "{0}:{1}".format( - username.replace("\n", ""), password.replace("\n", "") - ) - data_bytes = data_string.encode("utf-8") headers["Authorization"] = "Basic {0}".format( - base64.b64encode(data_bytes).decode("utf-8") + salt.utils.hashutils.base64_encodestring( + "{0}:{1}".format(username.replace("\n", ""), password.replace("\n", "")) + ) ) version = __find_latest_version( artifactory_url=artifactory_url, @@ -341,12 +334,10 @@ def get_release( ) headers = {} if username and password: - data_string = "{0}:{1}".format( - username.replace("\n", ""), password.replace("\n", "") - ) - data_bytes = data_string.encode("utf-8") headers["Authorization"] = "Basic {0}".format( - base64.b64encode(data_bytes).decode("utf-8") + salt.utils.hashutils.base64_encodestring( + "{0}:{1}".format(username.replace("\n", ""), password.replace("\n", "")) + ) ) release_url, file_name = _get_release_url( repository, @@ -722,8 +713,7 @@ def __save_artifact(artifact_url, target_file, headers): checksum_url, headers ) if checksum_success: - if isinstance(artifact_sum, bytes): - artifact_sum = artifact_sum.decode() + artifact_sum = salt.utils.stringutils.to_unicode(artifact_sum) log.debug("Downloaded SHA1 SUM: %s", artifact_sum) file_sum = __salt__["file.get_hash"](path=target_file, form="sha1") log.debug("Target file (%s) SHA1 SUM: %s", target_file, file_sum)