Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions robottelo/content_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,27 @@ def get_repo_files_by_url(url, extension='rpm'):
return sorted([os.path.basename(f) for f in get_repo_files_urls_by_url(url, extension)])


def get_baseurl_by_repofile(repo_url, verify_ssl=True):
"""
Returns the baseurl from a remote yum .repo file.

:param repo_url: URL to the .repo file
:return: baseurl string
:raises requests.HTTPError: if URL not accessible
:raises ValueError: if baseurl not found
"""
response = requests.get(repo_url, verify=verify_ssl, timeout=10)
response.raise_for_status()

for line in response.text.splitlines():
line = line.strip()

if line.startswith('baseurl='):
return line.split('=', 1)[1].strip()

raise ValueError(f'No baseurl found in {repo_url}')
Comment on lines +81 to +90

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Handle inline comments and empty values when extracting the baseurl

This will return anything after baseurl=, including inline comments (e.g. baseurl=http://foo # comment) or an empty value. Consider stripping inline comments first (e.g. split on #), ensuring the value is non-empty and URL-like, and otherwise continuing the scan or raising a clearer error to avoid silently returning malformed values.

Suggested change
response = requests.get(repo_url, verify=verify_ssl, timeout=10)
response.raise_for_status()
for line in response.text.splitlines():
line = line.strip()
if line.startswith('baseurl='):
return line.split('=', 1)[1].strip()
raise ValueError(f'No baseurl found in {repo_url}')
response = requests.get(repo_url, verify=verify_ssl, timeout=10)
response.raise_for_status()
for line in response.text.splitlines():
line = line.strip()
if line.startswith('baseurl='):
# Extract raw value after "baseurl="
raw_value = line.split('=', 1)[1].strip()
if not raw_value:
# Empty baseurl, continue scanning other lines
continue
# Strip inline comments: everything after '#' is considered a comment
value = raw_value.split('#', 1)[0].strip()
if not value:
# Only comment or whitespace after baseurl=, keep scanning
continue
# Basic sanity check to avoid obviously malformed values
# (most yum/dnf baseurls are URLs or file paths)
if "://" not in value and not value.startswith("file:"):
# Not URL-like, keep scanning for a better candidate
continue
return value
raise ValueError(f'No valid baseurl found in repository metadata from {repo_url}')



def get_repomd(repo_url):
"""Fetches content of the repomd file of a repository

Expand Down
7 changes: 5 additions & 2 deletions tests/foreman/api/test_convert2rhel.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

from robottelo.config import settings
from robottelo.constants import DEFAULT_ARCHITECTURE, REPOS
from robottelo.content_info import get_baseurl_by_repofile
from robottelo.utils.issue_handlers import is_open


Expand Down Expand Up @@ -134,7 +135,8 @@ def centos(

centos_host.enable_ipv6_dnf_proxy()
assert centos_host.execute('yum -y update').status == 0
repo_url = settings.repos.convert2rhel.convert_to_rhel_repo.format(major)
repofile_url = settings.repos.convert2rhel.convert_to_rhel_repofile.format(major)
repo_url = get_baseurl_by_repofile(repofile_url)
repo = create_repo(module_target_sat, module_els_sca_manifest_org, repo_url)
cv = update_cv(
module_target_sat, module_promoted_cv, module_lce, enable_rhel_subscriptions + [repo]
Expand Down Expand Up @@ -229,7 +231,8 @@ def oracle(
if oracle_host.execute('needs-restarting -r').status == 1:
oracle_host.power_control(state='reboot')

repo_url = settings.repos.convert2rhel.convert_to_rhel_repo.format(major)
repofile_url = settings.repos.convert2rhel.convert_to_rhel_repofile.format(major)
repo_url = get_baseurl_by_repofile(repofile_url)
repo = create_repo(module_target_sat, module_els_sca_manifest_org, repo_url, ssl_cert)
cv = update_cv(
module_target_sat, module_promoted_cv, module_lce, enable_rhel_subscriptions + [repo]
Expand Down
Loading