Skip to content

Commit

Permalink
Translating URLs for Ipv6
Browse files Browse the repository at this point in the history
  • Loading branch information
jyejare committed May 6, 2024
1 parent 4dd7ff5 commit 7072ef7
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
1 change: 0 additions & 1 deletion conf/migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"""

from packaging.version import Version

from robottelo.logging import logger


Expand Down
3 changes: 3 additions & 0 deletions robottelo/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from robottelo.config.validators import VALIDATORS
from robottelo.logging import logger, robottelo_root_dir
from robottelo.utils.url import ipv6_hostname_translation

if not os.getenv('ROBOTTELO_DIR'):
# dynaconf robottelo file uses ROBOTELLO_DIR for screenshots
Expand Down Expand Up @@ -44,6 +45,8 @@ def get_settings():
f'Dynaconf validation failed, continuing for the sake of unit tests\n{err}'
)

ipv6_hostname_translation(settings)

return settings


Expand Down
1 change: 1 addition & 0 deletions robottelo/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import base64
import re

from robottelo.logging import logger
from cryptography.hazmat.backends import default_backend as crypto_default_backend
from cryptography.hazmat.primitives import serialization as crypto_serialization
from cryptography.hazmat.primitives.asymmetric import rsa
Expand Down
43 changes: 43 additions & 0 deletions robottelo/utils/url.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from urllib.parse import urlparse
from robottelo.logging import logger


def is_url(url):
Expand All @@ -7,3 +8,45 @@ def is_url(url):
return all([result.scheme, result.netloc])
except (ValueError, AttributeError):
return False


def is_ipv4_url(text):
if isinstance(text, str) and 'ipv4' in text:
# Did not find the better way to filter only URLs so skipping it simple
# and open for reviewers suggestions
if 'redhat.com' in text:
return True
return False


def ipv6_translator(settings_list, setting_major, settings):
dotted_settings = '.'.join(setting_major)
for _key, _val in settings_list.items():
if is_ipv4_url(_val):
settings.set(f'{dotted_settings}.{_key}', str(_val).replace('ipv4', 'ipv6'))
logger.debug(f'Setting translated to IPv6, Path: {dotted_settings}.{_key}')
elif type(_val) is list:
updated = False
new_list = _val
for i in range(len(new_list)):
if is_ipv4_url(new_list[i]):
new_list[i] = new_list[i].replace('ipv4', 'ipv6')
updated = True
if updated:
settings.set(f'{dotted_settings}.{_key}', new_list)
logger.debug(f'Setting translated to IPv6, Path: {dotted_settings}.{_key}')
elif type(_val) is dict:
new_setting_major = setting_major + [_key]
ipv6_translator(settings_list=_val, setting_major=new_setting_major, settings=settings)


def ipv6_hostname_translation(settings):
"""Migrates any ipv4 containing hostname in conf to ipv6 hostname"""
settings_path = []
if settings.server.is_ipv6:
all_settings = settings.loaded_by_loaders.items()
for loader_name, loader_settings in tuple(all_settings):
if loader_name.loader == 'yaml':
ipv6_translator(loader_settings, settings_path, settings)
else:
logger.debug('Ipv6 Hostname dynaconf migration hook is skipped for ipv4 testing')

0 comments on commit 7072ef7

Please sign in to comment.