aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBryn M. Reeves <bmr@redhat.com>2015-11-19 18:46:36 +0000
committerBryn M. Reeves <bmr@redhat.com>2015-12-04 15:43:15 +0000
commit6038fdf8617319a13b0b42f3283ec2066d54b283 (patch)
tree302bab5062039e705b875a90a0de6551afe1aceb
parentb279dcdb1ddd36d5ea254503ae056e484d48b7c2 (diff)
downloadsos-6038fdf8617319a13b0b42f3283ec2066d54b283.tar.gz
[policies] move hash determination to policies
It's crazy having the Policy classes call a function in the utilities module only to have that function then load the Policy module, call policy.get_preferred_hash_algorithm() and then test the result. Get rid of the get_hash_name() function in the utilities module and simplify the calls in the policies module to obtain the preferred hash name. Signed-off-by: Bryn M. Reeves <bmr@redhat.com>
-rw-r--r--sos/policies/__init__.py22
-rw-r--r--sos/utilities.py12
-rw-r--r--tests/utilities_tests.py2
3 files changed, 16 insertions, 20 deletions
diff --git a/sos/policies/__init__.py b/sos/policies/__init__.py
index a403bb92..bf9afde0 100644
--- a/sos/policies/__init__.py
+++ b/sos/policies/__init__.py
@@ -10,7 +10,6 @@ from os import environ
from sos.utilities import (ImporterHelper,
import_module,
- get_hash_name,
shell_out)
from sos.plugins import IndependentPlugin, ExperimentalPlugin
from sos import _sos as _
@@ -286,17 +285,17 @@ No changes will be made to system configuration.
considered to be a superuser"""
return (os.getuid() == 0)
- def _create_checksum(self, final_filename=None):
+ def _create_checksum(self, hash_name, final_filename=None):
if not final_filename:
return False
archive_fp = open(final_filename, 'rb')
- digest = hashlib.new(get_hash_name())
+ digest = hashlib.new(hash_name)
digest.update(archive_fp.read())
archive_fp.close()
return digest.hexdigest()
- def get_preferred_hash_algorithm(self):
+ def get_preferred_hash_name(self):
"""Returns the string name of the hashlib-supported checksum algorithm
to use"""
return "md5"
@@ -309,10 +308,11 @@ No changes will be made to system configuration.
self._print()
+ hash_name = self.get_preferred_hash_name()
if not build:
# store checksum into file
- fp = open(final_filename + "." + get_hash_name(), "w")
- checksum = self._create_checksum(final_filename)
+ fp = open(final_filename + "." + hash_name, "w")
+ checksum = self._create_checksum(hash_name, final_filename)
if checksum:
fp.write(checksum + "\n")
fp.close()
@@ -373,20 +373,28 @@ class LinuxPolicy(Policy):
vendor = "None"
PATH = "/bin:/sbin:/usr/bin:/usr/sbin"
+ _preferred_hash_name = None
+
def __init__(self, sysroot=None):
super(LinuxPolicy, self).__init__(sysroot=sysroot)
- def get_preferred_hash_algorithm(self):
+ def get_preferred_hash_name(self):
+
+ if self._preferred_hash_name:
+ return self._preferred_hash_name
+
checksum = "md5"
try:
fp = open("/proc/sys/crypto/fips_enabled", "r")
except:
+ self._preferred_hash_name = checksum
return checksum
fips_enabled = fp.read()
if fips_enabled.find("1") >= 0:
checksum = "sha256"
fp.close()
+ self._preferred_hash_name = checksum
return checksum
def default_runlevel(self):
diff --git a/sos/utilities.py b/sos/utilities.py
index d3a10480..63e7ee42 100644
--- a/sos/utilities.py
+++ b/sos/utilities.py
@@ -52,18 +52,6 @@ def fileobj(path_or_file, mode='r'):
return closing(path_or_file)
-def get_hash_name():
- """Returns the algorithm used when computing a hash"""
- import sos.policies
- policy = sos.policies.load()
- try:
- name = policy.get_preferred_hash_algorithm()
- hashlib.new(name)
- return name
- except:
- return 'sha256'
-
-
def convert_bytes(bytes_, K=1 << 10, M=1 << 20, G=1 << 30, T=1 << 40):
"""Converts a number of bytes to a shorter, more human friendly format"""
fn = float(bytes_)
diff --git a/tests/utilities_tests.py b/tests/utilities_tests.py
index c4646926..f1b60e2b 100644
--- a/tests/utilities_tests.py
+++ b/tests/utilities_tests.py
@@ -5,7 +5,7 @@ import unittest
import six
from six import StringIO
-from sos.utilities import grep, get_hash_name, is_executable, sos_get_command_output, find, tail, shell_out
+from sos.utilities import grep, is_executable, sos_get_command_output, find, tail, shell_out
import sos
TEST_DIR = os.path.dirname(__file__)