| 1 |
# --- BEGIN COPYRIGHT BLOCK --- |
| 2 |
# Copyright (C) 2020 Red Hat, Inc. |
| 3 |
# All rights reserved. |
| 4 |
# |
| 5 |
# License: GPL (version 3 or any later version). |
| 6 |
# See LICENSE for details. |
| 7 |
# --- END COPYRIGHT BLOCK --- |
| 8 |
# |
| 9 |
import time |
| 10 |
import pytest |
| 11 |
import logging |
| 12 |
import os |
| 13 |
|
| 14 |
from lib389 import DEFAULT_SUFFIX |
| 15 |
from lib389.cli_idm.client_config import sssd_conf, ldap_conf, display |
| 16 |
from lib389.plugins import MemberOfPlugin |
| 17 |
from test389.topologies import topology_st |
| 18 |
from lib389.cli_base import FakeArgs |
| 19 |
from lib389.idm.group import Groups |
| 20 |
from lib389.idm.user import nsUserAccounts |
| 21 |
from lib389.utils import ds_is_older |
| 22 |
|
| 23 |
pytestmark = pytest.mark.tier0 |
| 24 |
|
| 25 |
LOG_FILE = '/tmp/dsidm.log' |
| 26 |
logging.getLogger(__name__).setLevel(logging.DEBUG) |
| 27 |
log = logging.getLogger(__name__) |
| 28 |
|
| 29 |
|
| 30 |
@pytest.fixture(scope="function") |
| 31 |
def set_log_file(request): |
| 32 |
fh = logging.FileHandler(LOG_FILE) |
| 33 |
fh.setLevel(logging.DEBUG) |
| 34 |
log.addHandler(fh) |
| 35 |
|
| 36 |
def fin(): |
| 37 |
log.info('Delete log file') |
| 38 |
os.remove(LOG_FILE) |
| 39 |
|
| 40 |
request.addfinalizer(fin) |
| 41 |
|
| 42 |
|
| 43 |
def check_value_in_log_and_reset(content_list, content_list2=None, check_value=None): |
| 44 |
with open(LOG_FILE, 'r+') as f: |
| 45 |
file_content = f.read() |
| 46 |
if content_list2 is not None: |
| 47 |
log.info('Check if content is present in output') |
| 48 |
for item in content_list + content_list2: |
| 49 |
assert item.lower() in file_content.lower() |
| 50 |
else: |
| 51 |
log.info('Check if content is present in output') |
| 52 |
for item in content_list: |
| 53 |
assert item.lower() in file_content.lower() |
| 54 |
|
| 55 |
if check_value is not None: |
| 56 |
log.info('Check if value is present in output') |
| 57 |
assert check_value in file_content |
| 58 |
|
| 59 |
log.info('Reset log file for next test') |
| 60 |
f.truncate(0) |
| 61 |
|
| 62 |
|
| 63 |
@pytest.mark.skipif(ds_is_older("1.4.2"), reason="Not implemented") |
| 64 |
def test_dsidm_config_sssd(topology_st, set_log_file): |
| 65 |
""" Test dsidm creation of sssd.conf content |
| 66 |
|
| 67 |
:id: 77812ba6-b133-40f4-91a7-13309618f24d |
| 68 |
:setup: Standalone instance |
| 69 |
:steps: |
| 70 |
1. Run dsidm client_config sssd.conf |
| 71 |
2. Enable MemberOfPlugin |
| 72 |
3. Run dsidm client_config sssd.conf with allowed group |
| 73 |
:expectedresults: |
| 74 |
1. Success |
| 75 |
2. Success |
| 76 |
3. Success |
| 77 |
""" |
| 78 |
|
| 79 |
standalone = topology_st.standalone |
| 80 |
|
| 81 |
sssd_content_list = ['Generated by 389 Directory Server - dsidm', |
| 82 |
'id_provider = ldap', |
| 83 |
'auth_provider = ldap', |
| 84 |
'access_provider = ldap', |
| 85 |
'chpass_provider = ldap', |
| 86 |
'ldap_search_base = ' + DEFAULT_SUFFIX, |
| 87 |
'ldap_uri = ' + standalone.ldapuri, |
| 88 |
'ldap_user_member_of = memberof', |
| 89 |
'ignore_group_members = False', |
| 90 |
'[sssd]', |
| 91 |
'services = nss, pam, ssh, sudo', |
| 92 |
'config_file_version = 2', |
| 93 |
'domains = ldap', |
| 94 |
'[nss]', |
| 95 |
'homedir_substring = /home'] |
| 96 |
|
| 97 |
schema = 'ldap_schema = rfc2307' |
| 98 |
args = FakeArgs() |
| 99 |
args.allowed_group = None |
| 100 |
|
| 101 |
log.info('Create sssd.conf content') |
| 102 |
sssd_conf(standalone, DEFAULT_SUFFIX, log, args) |
| 103 |
|
| 104 |
log.info('Check if config creation was successful') |
| 105 |
check_value_in_log_and_reset(sssd_content_list, check_value=schema) |
| 106 |
|
| 107 |
log.info('Now we test allowed_group argument') |
| 108 |
log.info('Enable MemberOf plugin') |
| 109 |
plugin = MemberOfPlugin(standalone) |
| 110 |
plugin.enable() |
| 111 |
standalone.restart() |
| 112 |
|
| 113 |
log.info('Create test group') |
| 114 |
groups = Groups(standalone, DEFAULT_SUFFIX) |
| 115 |
test_group = groups.create(properties={"cn": "new_group", |
| 116 |
"description": "testgroup"}) |
| 117 |
|
| 118 |
log.info('Create sssd.conf content with allowed group') |
| 119 |
filter_msg = ['ldap_access_filter = (memberOf={})'.format(test_group.dn), 'ldap_schema = rfc2307bis'] |
| 120 |
args.allowed_group = test_group.rdn |
| 121 |
sssd_conf(standalone, DEFAULT_SUFFIX, log, args) |
| 122 |
|
| 123 |
log.info('Check if config creation was successful') |
| 124 |
check_value_in_log_and_reset(sssd_content_list, filter_msg) |
| 125 |
|
| 126 |
|
| 127 |
@pytest.mark.skipif(ds_is_older("1.4.2"), reason="Not implemented") |
| 128 |
def test_dsidm_config_ldap(topology_st, set_log_file): |
| 129 |
""" Test dsidm creation of ldap.conf content |
| 130 |
|
| 131 |
:id: 29ffcc91-9104-4c90-bcdf-0f6a4082322c |
| 132 |
:setup: Standalone instance |
| 133 |
:steps: |
| 134 |
1. Create instance |
| 135 |
2. Run dsidm client_config ldap.conf |
| 136 |
:expectedresults: |
| 137 |
1. Success |
| 138 |
2. Success |
| 139 |
""" |
| 140 |
|
| 141 |
standalone = topology_st.standalone |
| 142 |
args = FakeArgs() |
| 143 |
|
| 144 |
ldap_content_list = ['OpenLDAP client configuration', |
| 145 |
'Generated by 389 Directory Server - dsidm', |
| 146 |
'BASE ' + DEFAULT_SUFFIX, |
| 147 |
'URI ' + standalone.ldapuri, |
| 148 |
'DEREF never', |
| 149 |
'TLS_CACERTDIR /etc/openldap/certs'] |
| 150 |
|
| 151 |
log.info('Create ldap.conf content') |
| 152 |
ldap_conf(standalone, DEFAULT_SUFFIX, log, args) |
| 153 |
|
| 154 |
log.info('Check if config creation was successful') |
| 155 |
check_value_in_log_and_reset(ldap_content_list) |
| 156 |
|
| 157 |
|
| 158 |
@pytest.mark.skipif(ds_is_older("1.4.2"), reason="Not implemented") |
| 159 |
def test_dsidm_config_display(topology_st, set_log_file): |
| 160 |
""" Test dsidm display option |
| 161 |
|
| 162 |
:id: 6e888ae2-8835-44d5-846b-e971d76aa461 |
| 163 |
:setup: Standalone instance |
| 164 |
:steps: |
| 165 |
1. Run dsidm client_config display |
| 166 |
2. Enable MemberOfPlugin |
| 167 |
3. Run dsidm client_config display with MemberOfPlugin |
| 168 |
:expectedresults: |
| 169 |
1. Success |
| 170 |
2. Success |
| 171 |
3. Success |
| 172 |
""" |
| 173 |
|
| 174 |
standalone = topology_st.standalone |
| 175 |
users = nsUserAccounts(standalone, DEFAULT_SUFFIX) |
| 176 |
groups = Groups(standalone, DEFAULT_SUFFIX) |
| 177 |
|
| 178 |
display_content_list = ['ldap_uri = ' + standalone.ldapuri, |
| 179 |
'ldap_uri = ldaps:///dc%3Dexample%2Cdc%3Dcom', |
| 180 |
'group_basedn = ' + groups._basedn, |
| 181 |
'basedn = ' + DEFAULT_SUFFIX, |
| 182 |
'user_basedn = ' + users._basedn, |
| 183 |
'user_filter = (&(objectclass=nsPerson)(objectclass=nsAccount)(objectclass=nsOrgPerson)' |
| 184 |
'(objectclass=posixAccount))', |
| 185 |
'unique id = nsUniqueId', |
| 186 |
'group member attribute = member', |
| 187 |
'user rdn = uid', |
| 188 |
'user identifier = uid', |
| 189 |
'group_filter = (&(objectclass=groupOfNames))', |
| 190 |
'group rdn = cn'] |
| 191 |
|
| 192 |
schema_type = 'rfc2307' |
| 193 |
args = FakeArgs() |
| 194 |
|
| 195 |
log.info('Test dsidm display option') |
| 196 |
display(standalone, DEFAULT_SUFFIX, log, args) |
| 197 |
|
| 198 |
log.info('Check if display option was successful') |
| 199 |
check_value_in_log_and_reset(display_content_list, check_value=schema_type) |
| 200 |
|
| 201 |
log.info('Enable MemberOf plugin') |
| 202 |
plugin = MemberOfPlugin(standalone) |
| 203 |
plugin.enable() |
| 204 |
standalone.restart() |
| 205 |
|
| 206 |
log.info('Test dsidm display option with MemberOf plugin') |
| 207 |
display(standalone, DEFAULT_SUFFIX, log, args) |
| 208 |
|
| 209 |
log.info('Check if display option was successful with MemberOf plugin enabled') |
| 210 |
schema_type = 'rfc2307bis' |
| 211 |
check_value_in_log_and_reset(display_content_list, check_value=schema_type) |
| 212 |
|
| 213 |
|
| 214 |
if __name__ == '__main__': |
| 215 |
# Run isolated |
| 216 |
# -s for DEBUG mode |
| 217 |
CURRENT_FILE = os.path.realpath(__file__) |
| 218 |
pytest.main("-s %s" % CURRENT_FILE) |