Skip to content

Commit

Permalink
Tests: Fix PEP8 on updated AD suites.
Browse files Browse the repository at this point in the history
Reviewed-by: Madhuri Upadhye <[email protected]>
  • Loading branch information
jakub-vavra-cz committed May 7, 2024
1 parent 7bf051e commit 2285475
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 57 deletions.
8 changes: 4 additions & 4 deletions src/tests/multihost/ad/test_ad_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,9 @@ def test_0001_provider_config_cross_interference(
client.clear_sssd_cache()

# Evaluate test results
assert usr_cmd_1.returncode == 0,\
assert usr_cmd_1.returncode == 0, \
f"getent passwd {aduser} failed (AD without additional LDAP)."
assert usr_cmd_2.returncode != 0,\
assert usr_cmd_2.returncode != 0, \
f"getent passwd {aduser} passed (AD with LDAP with an " \
f"obfuscated password)."
assert "[sdap_cli_auth_step] (0x1000): Invalid authtoken type" \
Expand Down Expand Up @@ -273,8 +273,8 @@ def test_0003_gssapi_ssh(self, multihost, adjoin, create_aduser_group):
ssh.prompt(timeout=5)
ssh.sendline('klist -A')
ssh.prompt(timeout=5)
ssh.sendline('ssh -v -o StrictHostKeyChecking=no -o GSSAPIAuthentication=yes '
'-o PasswordAuthentication=no '
ssh.sendline(f'ssh -v -o StrictHostKeyChecking=no -o GSSAPIAuthentication=yes '
f'-o PasswordAuthentication=no '
f'-o PubkeyAuthentication=no -K -l {ad_user} '
f'{multihost.client[0].sys_hostname} id')
ssh.prompt(timeout=30)
Expand Down
2 changes: 1 addition & 1 deletion src/tests/multihost/ad/test_adparameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ def test_0009_bz1565761(self, multihost, adjoin):
user = "Administrator"
client.clear_sssd_cache()
multihost.client[0].run_command(
f"id {user}@{domain}", raiseonerr=False
f"id {user}@{domain}", raiseonerr=False
)
grep = 'grep -ire "Domain not found" /var/log/sssd/'
cmd = multihost.client[0].run_command(grep, raiseonerr=False)
Expand Down
86 changes: 42 additions & 44 deletions src/tests/multihost/ad/test_adparameters_ported.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ def change_client_hostname(session_multihost, request):
f'hostnamectl set-hostname {new_hostname}', raiseonerr=False
)


def restore():
""" Restore hostname """
# Temporary way of changing hostname
Expand All @@ -81,7 +80,7 @@ def restore():
)
# Permanent way of changing hostname
session_multihost.client[0].run_command(
f'hostnamectl set-hostname {old_hostname}', raiseonerr=False
f'hostnamectl set-hostname {old_hostname}', raiseonerr=False
)
request.addfinalizer(restore)

Expand Down Expand Up @@ -180,11 +179,13 @@ def set_ssh_key_ldap(session_multihost, user, pubkey, operation="replace"):
tfile.flush()
session_multihost.client[0].transport.put_file(
tfile.name, f'/tmp/mod.{myid}.ldif')
ldap_cmd = f'ldapmodify -H ldap://{session_multihost.ad[0].hostname}' \
f' -v -x -D "cn=Administrator,cn=Users,' \
f'{session_multihost.ad[0].domain_basedn_entry}" -w ' \
f'"{session_multihost.ad[0].ssh_password}" ' \
f'-f /tmp/mod.{myid}.ldif'
ldap_cmd = (
f"ldapmodify -H ldap://{session_multihost.ad[0].hostname}"
f' -v -x -D "cn=Administrator,cn=Users,'
f'{session_multihost.ad[0].domain_basedn_entry}" -w '
f'"{session_multihost.ad[0].ssh_password}" '
f"-f /tmp/mod.{myid}.ldif"
)
cmd = session_multihost.client[0].run_command(ldap_cmd, raiseonerr=False)
return cmd.returncode == 0

Expand Down Expand Up @@ -300,7 +301,7 @@ def test_0002_ad_parameters_junk_domain(
ad_realm = multihost.ad[0].domainname.upper()
# Join AD manually to set the user-principal properly
joincmd = f"realm join --user=Administrator --user-principal=host/" \
f"{hostname}@{ad_realm} {multihost.ad[0].domainname.lower()}"
f"{hostname}@{ad_realm} {multihost.ad[0].domainname.lower()}"
multihost.client[0].run_command(
joincmd, stdin_text=multihost.ad[0].ssh_password,
raiseonerr=False)
Expand Down Expand Up @@ -424,10 +425,10 @@ def test_0003_ad_parameters_junk_domain_invalid_keytab(
shortname = hostname_cmd.stdout_text.rstrip().upper()

ktutil_cmd = f'{{ echo "addent -password -p host/{shortname}@' \
f'INVALIDDOMAIN.COM -k 2 -e rc4-hmac"; sleep 1; echo ' \
f'"Secret123"; sleep 1; echo "rkt /etc/krb5.keytab"; ' \
f'sleep 1; echo "wkt /tmp/first_invalid.keytab"; ' \
f'sleep 1; echo "quit"; }} | ktutil'
f'INVALIDDOMAIN.COM -k 2 -e rc4-hmac"; sleep 1; echo ' \
f'"Secret123"; sleep 1; echo "rkt /etc/krb5.keytab"; ' \
f'sleep 1; echo "wkt /tmp/first_invalid.keytab"; ' \
f'sleep 1; echo "quit"; }} | ktutil'

multihost.client[0].run_command(ktutil_cmd, raiseonerr=False)
# Get keytab info for debugging purposes
Expand Down Expand Up @@ -557,7 +558,6 @@ def test_0004_ad_parameters_valid_domain_shorthost(
assert su_result, "The su command failed!"
assert log_message in log_str or log_message in log_str_child


@staticmethod
@pytest.mark.tier2
def test_0005_ad_parameters_blank_domain(
Expand Down Expand Up @@ -919,7 +919,6 @@ def test_0010_ad_parameters_ldap_sasl_short(
client.restore_sssd_conf()
client.clear_sssd_cache()


# EVALUATION
assert f"Option ldap_sasl_authid has value " \
f"host/{hostname}" in logs
Expand Down Expand Up @@ -1264,7 +1263,7 @@ def test_0015_ad_parameters_ad_hostname_machine(
f"/var/log/sssd/sssd_{multihost.ad[0].domainname.lower()}.log"). \
decode('utf-8')
log_str_child = multihost.client[0].get_file_contents(
"/var/log/sssd/ldap_child.log").decode('utf-8')
"/var/log/sssd/ldap_child.log").decode('utf-8')
logs = log_str + log_str_child
if "kautest.com" in logs:
break
Expand All @@ -1279,7 +1278,6 @@ def test_0015_ad_parameters_ad_hostname_machine(
assert "Setting ad_hostname to [host1.kautest.com]" in logs
assert f"Will look for host1.kautest.com@{ad_realm}" in logs


@staticmethod
@pytest.mark.tier1_2
@pytest.mark.c_ares
Expand Down Expand Up @@ -1605,11 +1603,11 @@ def test_0019_ad_parameters_ldap_id_mapping_false(

# Get uid and gid for the aduser and adgroup
get_uid_cmd = f"powershell.exe -inputformat none -noprofile 'Get-" \
f"ADUser -Identity {aduser} -Properties uidNumber'"
f"ADUser -Identity {aduser} -Properties uidNumber'"
cmd = multihost.ad[0].run_command(get_uid_cmd, raiseonerr=False)
uid = re.findall("uidNumber.*:[^0-9]+([0-9]+)", cmd.stdout_text)[0]
get_gid_cmd = f"powershell.exe -inputformat none -noprofile 'Get-" \
f"ADGroup -Identity {adgroup} -Properties gidNumber'"
f"ADGroup -Identity {adgroup} -Properties gidNumber'"
cmd = multihost.ad[0].run_command(get_gid_cmd, raiseonerr=False)
gid = re.findall("gidNumber.*:[^0-9]+([0-9]+)", cmd.stdout_text)[0]

Expand Down Expand Up @@ -1970,7 +1968,7 @@ def test_0024_ad_parameters_getgrgid_nested(
https://bugzilla.redhat.com/show_bug.cgi?id=887961
:customerscenario: False
"""
adjoin(membersw='adcli')
adjoin(membersw='a dcli')
client = sssdTools(multihost.client[0], multihost.ad[0])
# Create AD user without posix attributes
(userplain, _) = create_plain_aduser_group
Expand Down Expand Up @@ -2263,7 +2261,7 @@ def test_0028_ad_parameters_nested_in_nonposix_group(
'id_provider': 'ldap',
'ldap_schema': 'ad',
'ldap_default_bind_dn': f'CN=administrator,CN=Users'
f',{multihost.ad[0].domain_basedn_entry}',
f',{multihost.ad[0].domain_basedn_entry}',
'use_fully_qualified_names': 'false',
'ldap_id_use_start_tls': 'True',
'ldap_tls_cacert': '/etc/openldap/certs/ad_cert.pem',
Expand Down Expand Up @@ -2337,7 +2335,7 @@ def test_0029_ad_parameters_tokengroups_with_ldap(
'ldap_uri': f'ldaps://{multihost.ad[0].sys_hostname}',
'ldap_default_authtok': multihost.ad[0].ssh_password,
'ldap_default_bind_dn': f'CN=administrator,CN=Users'
f',{multihost.ad[0].domain_basedn_entry}',
f',{multihost.ad[0].domain_basedn_entry}',
'debug_level': '9',
'ldap_referrals': 'false',
'use_fully_qualified_names': 'True',
Expand Down Expand Up @@ -2394,7 +2392,7 @@ def test_0030_ad_parameters_tokengroups_searchbase(
(aduser, _) = create_aduser_group

# Create a subtree
subtree = f'subtree-{random.randint(999,9999)}'
subtree = f'subtree-{random.randint(999, 9999)}'
with tempfile.NamedTemporaryFile(mode='w') as tfile:
tfile.write(f"dn: OU={subtree},"
f"{multihost.ad[0].domain_basedn_entry}\n")
Expand All @@ -2407,9 +2405,9 @@ def test_0030_ad_parameters_tokengroups_searchbase(
tfile.flush()
multihost.client[0].transport.put_file(tfile.name, '/tmp/mod.ldif')
ldap_cmd = f'ldapadd -a -v -x -H ldap://{multihost.ad[0].hostname}' \
f' -D "cn=Administrator,cn=Users,' \
f'{multihost.ad[0].domain_basedn_entry}" -w ' \
f'"{multihost.ad[0].ssh_password}" -f /tmp/mod.ldif'
f' -D "cn=Administrator,cn=Users,' \
f'{multihost.ad[0].domain_basedn_entry}" -w ' \
f'"{multihost.ad[0].ssh_password}" -f /tmp/mod.ldif'

multihost.client[0].run_command(ldap_cmd)

Expand All @@ -2423,7 +2421,7 @@ def test_0030_ad_parameters_tokengroups_searchbase(
'ldap_uri': f'ldaps://{multihost.ad[0].sys_hostname}',
'ldap_default_authtok': multihost.ad[0].ssh_password,
'ldap_default_bind_dn': f'CN=administrator,CN=Users'
f',{multihost.ad[0].domain_basedn_entry}',
f',{multihost.ad[0].domain_basedn_entry}',
'ldap_referrals': 'false',
'debug_level': '9',
'use_fully_qualified_names': 'True',
Expand All @@ -2450,10 +2448,10 @@ def test_0030_ad_parameters_tokengroups_searchbase(
# Teardown
# Remove subtree from ldap
ldap_cmd = f'ldapdelete -v -x -H ldap://{multihost.ad[0].hostname}' \
f' -D "cn=Administrator,cn=Users,' \
f'{multihost.ad[0].domain_basedn_entry}" -w ' \
f'"{multihost.ad[0].ssh_password}" "OU={subtree},' \
f'{multihost.ad[0].domain_basedn_entry}"'
f' -D "cn=Administrator,cn=Users,' \
f'{multihost.ad[0].domain_basedn_entry}" -w ' \
f'"{multihost.ad[0].ssh_password}" "OU={subtree},' \
f'{multihost.ad[0].domain_basedn_entry}"'

multihost.client[0].run_command(ldap_cmd, raiseonerr=False)

Expand Down Expand Up @@ -2579,9 +2577,9 @@ def test_0032_ad_parameters_group_name_attribute(multihost, adjoin):
tfile.flush()
multihost.client[0].transport.put_file(tfile.name, '/tmp/mod.ldif')
ldap_cmd = f'ldapmodify -v -x -H ldap://{multihost.ad[0].hostname}' \
f' -D "cn=Administrator,cn=Users,' \
f'{multihost.ad[0].domain_basedn_entry}" -w ' \
f'"{multihost.ad[0].ssh_password}" -f /tmp/mod.ldif'
f' -D "cn=Administrator,cn=Users,' \
f'{multihost.ad[0].domain_basedn_entry}" -w ' \
f'"{multihost.ad[0].ssh_password}" -f /tmp/mod.ldif'
multihost.client[0].run_command(ldap_cmd)

# Search for the AD group
Expand Down Expand Up @@ -2661,9 +2659,9 @@ def test_0033_ad_parameters_group_cleanup_sanitize(multihost, adjoin):
tfile.flush()
multihost.client[0].transport.put_file(tfile.name, '/tmp/mod.ldif')
ldap_cmd = f'ldapmodify -v -x -H ldap://{multihost.ad[0].hostname}' \
f' -D "cn=Administrator,cn=Users,' \
f'{multihost.ad[0].domain_basedn_entry}" -w ' \
f'"{multihost.ad[0].ssh_password}" -f /tmp/mod.ldif'
f' -D "cn=Administrator,cn=Users,' \
f'{multihost.ad[0].domain_basedn_entry}" -w ' \
f'"{multihost.ad[0].ssh_password}" -f /tmp/mod.ldif'
multihost.client[0].run_command(ldap_cmd)

# Search for the AD user and group
Expand Down Expand Up @@ -2961,8 +2959,8 @@ def test_0036_ad_parameters_renewal_leaks_descriptors(multihost, adjoin):
print(f'Descriptors: initial:{initial}, stable:{stable},'
f' final: {final}.')
assert stable >= final, f"File descriptors are increasing!\n" \
f"Descriptors: initial:{initial}," \
f" stable:{stable}, final: {final}."
f"Descriptors: initial:{initial}," \
f" stable:{stable}, final: {final}."

@staticmethod
@pytest.mark.tier1_2
Expand Down Expand Up @@ -3060,10 +3058,10 @@ def test_0038_ad_parameters_authentication_failure_invalid_keytab(

# With ktutil add invalid principle in the keytab file.
ktutil_cmd = f'{{ echo "addent -password -p Test1337@{ad_domain} -k' \
f' 3 -e aes128-cts-hmac-sha1-96"; sleep 1; echo "Secret' \
f'123"; echo "rkt /etc/krb5.keytab"; sleep 1; echo "wkt' \
f' /tmp/first_invalid.keytab"; sleep 1; echo "quit"; }}' \
f' | ktutil'
f' 3 -e aes128-cts-hmac-sha1-96"; sleep 1; echo "Secret' \
f'123"; echo "rkt /etc/krb5.keytab"; sleep 1; echo "wkt' \
f' /tmp/first_invalid.keytab"; sleep 1; echo "quit"; }}' \
f' | ktutil'

multihost.client[0].run_command(ktutil_cmd, raiseonerr=False)

Expand Down Expand Up @@ -3227,7 +3225,7 @@ def test_0040_ad_parameters_newline_ssh_key(
'ad_enable_gc': 'False',
'ldap_user_ssh_public_key': 'msDS-cloudExtensionAttribute1',
'ldap_user_search_base': f'CN=Users,'
f'{multihost.ad[0].domain_basedn_entry}',
f'{multihost.ad[0].domain_basedn_entry}',
'ldap_id_mapping': 'False',
'debug_level': '9',
'cache_credentials': 'True',
Expand Down Expand Up @@ -3863,5 +3861,5 @@ def test_0047_ad_parameters_filter_group(
# Evaluate test results
assert getent_groupinfo, f"Could not find group {adgroup}!"
assert id_cmd.returncode == 0, f"User {aduser} was not found!"
assert getent_groupinfo['gid'] not in id_cmd.stdout_text,\
assert getent_groupinfo['gid'] not in id_cmd.stdout_text, \
f"{adgroup} gid was not filtered!"
14 changes: 6 additions & 8 deletions src/tests/multihost/ad/test_adschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,23 @@ def fixture_prepare_users(session_multihost, request):

# Add gecos to user 1
usr = f"powershell.exe -inputformat none -noprofile 'Set-ADUser " \
f"-Identity \"{ad_user_1}\" -Add @{{" \
f"gecos = \"{ad_user_1}\";}}'"
f"-Identity \"{ad_user_1}\" -Add @{{" \
f"gecos = \"{ad_user_1}\";}}'"
session_multihost.ad[0].run_command(usr, raiseonerr=False)

# Set user primary group
upg = f"powershell.exe -inputformat none -noprofile " \
f"'Set-ADUserPrimaryGroup {ad_user_1} \'{ad_group_1}\''"
f"'Set-ADUserPrimaryGroup {ad_user_1} \'{ad_group_1}\''"
res = session_multihost.ad[0].run_command(upg, raiseonerr=False)
# Windows 2012R2 does not know Set-ADUserPrimaryGroup
# This is a crude re-implementation
if "'Set-ADUserPrimaryGroup' is not recognized" in res.stderr_text:
info_cmd = f"powershell.exe -inputformat none -noprofile '" \
f"write-host $(Get-ADGroup -Identity {ad_group_1}).SID'"
f"write-host $(Get-ADGroup -Identity {ad_group_1}).SID'"
cmd = session_multihost.ad[0].run_command(info_cmd, raiseonerr=False)
group_id = cmd.stdout_text.strip().split('-')[-1]
pgp_cmd = f"powershell.exe -inputformat none -noprofile Set-ADUser " \
f"-Identity {ad_user_1} -Replace @{{'primaryGroupID' = " \
f"'{group_id}'}}"
f"-Identity {ad_user_1} -Replace @{{'primaryGroupID' = '{group_id}'}}"
session_multihost.ad[0].run_command(pgp_cmd, raiseonerr=False)

# Setup posix user 2
Expand All @@ -64,8 +63,7 @@ def fixture_prepare_users(session_multihost, request):

# Add gecos to user 2
usr = f"powershell.exe -inputformat none -noprofile 'Set-ADUser " \
f"-Identity \"{ad_user_2}\" -Add @{{" \
f"gecos = \"{ad_user_2}\";}}'"
f"-Identity \"{ad_user_2}\" -Add @{{gecos = \"{ad_user_2}\";}}'"
session_multihost.ad[0].run_command(usr, raiseonerr=False)

def remove_ad_user_groups():
Expand Down

0 comments on commit 2285475

Please sign in to comment.