Skip to content

Commit

Permalink
Use sha256 to generate repofile suffix hash
Browse files Browse the repository at this point in the history
Signed-off-by: Chenxiong Qi <cqi@redhat.com>
  • Loading branch information
tkdchen committed Mar 4, 2021
1 parent 596c3b2 commit 5bee237
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 21 deletions.
17 changes: 2 additions & 15 deletions atomic_reactor/utils/yum.py
Expand Up @@ -10,8 +10,7 @@
"""
from atomic_reactor.constants import YUM_REPOS_DIR
from atomic_reactor.util import get_retrying_requests_session
from hashlib import md5
from atomic_reactor.util import get_retrying_requests_session, sha256sum
import os
import os.path
import logging
Expand All @@ -26,18 +25,6 @@
REPO_SUFFIX = ".repo"


def generate_repofile_suffix_hash(repo_url, len_=5):
"""Generate a hash used as suffix added to a repo filename
:param str repo_url: the repo URL.
:param int len_: the length of the hash from the beginning.
:return: the first N characters of the generated hash.
:rtype: str
"""
h = md5(repo_url.encode('utf-8')).hexdigest()
return h[:len_] if len_ > 0 else h


class YumRepo(object):
def __init__(self, repourl, content='', dst_repos_dir=YUM_REPOS_DIR, add_hash=True):
self.add_hash = add_hash
Expand All @@ -62,7 +49,7 @@ def filename(self):
if not basename.endswith(REPO_SUFFIX):
basename += REPO_SUFFIX
if self.add_hash:
suffix = '-' + generate_repofile_suffix_hash(self.repourl)
suffix = '-' + sha256sum(self.repourl, abbrev_len=5)
else:
suffix = ''
final_name = suffix.join(os.path.splitext(basename))
Expand Down
6 changes: 3 additions & 3 deletions tests/plugins/test_add_yum_repo_by_url.py
Expand Up @@ -65,7 +65,7 @@ def test_no_repourls(inject_proxy):
def test_single_repourl(inject_proxy):
tasker, workflow = prepare()
url = 'http://example.com/example%20repo.repo'
filename = 'example repo-4ca91.repo'
filename = 'example repo-a8b44.repo'
runner = PreBuildPluginsRunner(tasker, workflow, [{
'name': AddYumRepoByUrlPlugin.key,
'args': {'repourls': [url], 'inject_proxy': inject_proxy}}])
Expand All @@ -84,9 +84,9 @@ def test_single_repourl(inject_proxy):
@pytest.mark.parametrize('inject_proxy', [None, 'http://proxy.example.com'])
@pytest.mark.parametrize(('repos', 'filenames'), (
(['http://example.com/a/b/c/myrepo.repo', 'http://example.com/repo-2.repo'],
['myrepo-d0856.repo', 'repo-2-ba4b3.repo']),
['myrepo-b9003.repo', 'repo-2-e5f47.repo']),
(['http://example.com/spam/myrepo.repo', 'http://example.com/bacon/myrepo.repo'],
['myrepo-608de.repo', 'myrepo-a1f78.repo']),
['myrepo-91be9.repo', 'myrepo-c8e02.repo']),
))
def test_multiple_repourls(caplog,
base_from_scratch, parent_images, inject_proxy,
Expand Down
6 changes: 3 additions & 3 deletions tests/plugins/test_yum_inject.py
Expand Up @@ -30,8 +30,8 @@
from atomic_reactor.plugins.pre_reactor_config import (
ReactorConfig, ReactorConfigPlugin, WORKSPACE_CONF_KEY
)
from atomic_reactor.util import render_yum_repo, df_parser
from atomic_reactor.utils.yum import YumRepo, generate_repofile_suffix_hash
from atomic_reactor.util import render_yum_repo, df_parser, sha256sum
from atomic_reactor.utils.yum import YumRepo
import os.path
from collections import namedtuple
import requests
Expand Down Expand Up @@ -678,7 +678,7 @@ def test_inject_ca_bundle(repofile_url,

# Ensure Dockerfile is update correctly
expected = expected_final_dockerfile.format(
generate_repofile_suffix_hash(repofile_url)
sha256sum(repofile_url, abbrev_len=5)
)
assert expected == df_parser(str(dockerfile)).content

Expand Down

0 comments on commit 5bee237

Please sign in to comment.