Browse Source

Stop using md5 for __repr__ of objects

The md5 function is not available on systems in FIPS mode, just
use the callable name (which is closer to how repr usually looks).

Change-Id: I4319ce2f42f35251595306b9a77ae1f8bc55595c
changes/35/739735/2
Dmitry Tantsur 1 year ago
parent
commit
8aecabcfe4
  1. 7
      ironic/objects/fields.py
  2. 9
      ironic/tests/unit/objects/test_fields.py
  3. 1
      tools/bandit.yml

7
ironic/objects/fields.py

@ -14,8 +14,6 @@
# under the License.
import ast
import hashlib
import inspect
from oslo_versionedobjects import fields as object_fields
@ -57,10 +55,7 @@ class StringFieldThatAcceptsCallable(object_fields.StringField):
default = self._default
if (self._default != object_fields.UnspecifiedDefault
and callable(self._default)):
default = "%s-%s" % (
self._default.__name__,
hashlib.md5(inspect.getsource(
self._default).encode()).hexdigest())
default = '<function %s>' % default.__name__
return '%s(default=%s,nullable=%s)' % (self._type.__class__.__name__,
default, self._nullable)

9
ironic/tests/unit/objects/test_fields.py

@ -13,9 +13,6 @@
# License for the specific language governing permissions and limitations
# under the License.
import hashlib
import inspect
from ironic.common import exception
from ironic.objects import fields
from ironic.tests import base as test_base
@ -73,8 +70,6 @@ class TestStringFieldThatAcceptsCallable(test_base.TestCase):
def test_default_function():
return "default value"
self.test_default_function_hash = hashlib.md5(
inspect.getsource(test_default_function).encode()).hexdigest()
self.field = fields.StringFieldThatAcceptsCallable(
default=test_default_function)
@ -102,8 +97,8 @@ class TestStringFieldThatAcceptsCallable(test_base.TestCase):
self.field.coerce('obj', 'attr', None))
def test__repr__includes_default_function_name_and_source_hash(self):
expected = ('StringAcceptsCallable(default=test_default_function-%s,'
'nullable=False)' % self.test_default_function_hash)
expected = ('StringAcceptsCallable(default=<function '
'test_default_function>,nullable=False)')
self.assertEqual(expected, repr(self.field))

1
tools/bandit.yml

@ -89,7 +89,6 @@ tests:
# (optional) list skipped test IDs here, eg '[B101, B406]':
skips:
- B104
- B303
- B604
### (optional) plugin settings - some test plugins require configuration data

Loading…
Cancel
Save