|
1 | 1 | # |
2 | | -# Copyright (C) 2025 FreeIPA Contributors see COPYING for license |
| 2 | +# Copyright (C) 2024 FreeIPA Contributors see COPYING for license |
3 | 3 | # |
4 | 4 |
|
5 | 5 | from util import capture_results, m_api |
6 | 6 | from base import BaseTest |
| 7 | +from unittest.mock import patch |
7 | 8 | from ipahealthcheck.core import config, constants |
8 | 9 | from ipahealthcheck.ipa.plugin import registry |
9 | | -from ipahealthcheck.ipa.config import IPAkrbLastSuccessfulAuth |
| 10 | +from ipahealthcheck.ipa.config import ( |
| 11 | + IPAkrbLastSuccessfulAuth, |
| 12 | + SSSDAllowedUids389Check |
| 13 | +) |
| 14 | + |
| 15 | +from SSSDConfig import NoOptionError |
| 16 | +from SSSDConfig import NoServiceError |
| 17 | + |
| 18 | + |
| 19 | +class SSSDService(): |
| 20 | + def __init__(self, return_option, uids): |
| 21 | + self.uids = uids |
| 22 | + self.return_option = return_option |
| 23 | + |
| 24 | + def get_option(self, option): |
| 25 | + if not self.return_option: |
| 26 | + raise NoOptionError |
| 27 | + return self.uids |
| 28 | + |
| 29 | + |
| 30 | +class SSSDConfig(): |
| 31 | + def __init__(self, return_service=True, return_option=False, uids=None): |
| 32 | + """ |
| 33 | + Knobs to control what data the configuration returns. |
| 34 | + """ |
| 35 | + self.return_service = return_service |
| 36 | + self.return_option = return_option |
| 37 | + self.uids = uids |
| 38 | + |
| 39 | + def import_config(self): |
| 40 | + pass |
| 41 | + |
| 42 | + def get_service(self, service): |
| 43 | + if not self.return_service: |
| 44 | + raise NoServiceError() |
| 45 | + return SSSDService(self.return_option, self.uids) |
10 | 46 |
|
11 | 47 |
|
12 | 48 | class TestkrbLastSuccessfulAuth(BaseTest): |
@@ -52,3 +88,107 @@ def test_last_success_enabled(self): |
52 | 88 | assert result.result == constants.WARNING |
53 | 89 | assert result.source == 'ipahealthcheck.ipa.config' |
54 | 90 | assert result.check == 'IPAkrbLastSuccessfulAuth' |
| 91 | + |
| 92 | + |
| 93 | +class TestSSSDAllowedUids389Check(BaseTest): |
| 94 | + |
| 95 | + @patch('SSSDConfig.SSSDConfig') |
| 96 | + def test_sssd_no_pac_section(self, mock_sssd): |
| 97 | + """There is no pac section in sssd.conf""" |
| 98 | + mock_sssd.return_value = SSSDConfig(return_service=False, |
| 99 | + return_option=False) |
| 100 | + framework = object() |
| 101 | + registry.initialize(framework, config.Config()) |
| 102 | + f = SSSDAllowedUids389Check(registry) |
| 103 | + self.results = capture_results(f) |
| 104 | + |
| 105 | + assert len(self.results) == 0 |
| 106 | + |
| 107 | + @patch('SSSDConfig.SSSDConfig') |
| 108 | + def test_sssd_no_allowed_uids_configured(self, mock_sssd): |
| 109 | + """There is no allowed_uids option in the pac section""" |
| 110 | + mock_sssd.return_value = SSSDConfig(return_service=True, |
| 111 | + return_option=False) |
| 112 | + framework = object() |
| 113 | + registry.initialize(framework, config.Config()) |
| 114 | + f = SSSDAllowedUids389Check(registry) |
| 115 | + self.results = capture_results(f) |
| 116 | + |
| 117 | + assert len(self.results) == 1 |
| 118 | + result = self.results.results[0] |
| 119 | + assert result.result == constants.SUCCESS |
| 120 | + assert result.source == 'ipahealthcheck.ipa.config' |
| 121 | + assert result.check == 'SSSDAllowedUids389Check' |
| 122 | + |
| 123 | + @patch('SSSDConfig.SSSDConfig') |
| 124 | + def test_sssd_ok_allowed_uids_configured(self, mock_sssd): |
| 125 | + """There is now allowed_uids option in the pac section""" |
| 126 | + mock_sssd.return_value = SSSDConfig(return_service=True, |
| 127 | + return_option=True, |
| 128 | + uids='0') |
| 129 | + framework = object() |
| 130 | + registry.initialize(framework, config.Config()) |
| 131 | + f = SSSDAllowedUids389Check(registry) |
| 132 | + self.results = capture_results(f) |
| 133 | + |
| 134 | + assert len(self.results) == 1 |
| 135 | + result = self.results.results[0] |
| 136 | + assert result.result == constants.SUCCESS |
| 137 | + assert result.source == 'ipahealthcheck.ipa.config' |
| 138 | + assert result.check == 'SSSDAllowedUids389Check' |
| 139 | + |
| 140 | + @patch('SSSDConfig.SSSDConfig') |
| 141 | + def test_sssd_ok_multiple_allowed_uids_configured(self, mock_sssd): |
| 142 | + """There is now allowed_uids option in the pac section""" |
| 143 | + mock_sssd.return_value = SSSDConfig(return_service=True, |
| 144 | + return_option=True, |
| 145 | + uids='0, 100000') |
| 146 | + |
| 147 | + # uid 100000 is a value I picked out of the air. It doesn't |
| 148 | + # matter what it is as it isn't prohibited |
| 149 | + framework = object() |
| 150 | + registry.initialize(framework, config.Config()) |
| 151 | + f = SSSDAllowedUids389Check(registry) |
| 152 | + self.results = capture_results(f) |
| 153 | + |
| 154 | + assert len(self.results) == 1 |
| 155 | + result = self.results.results[0] |
| 156 | + assert result.result == constants.SUCCESS |
| 157 | + assert result.source == 'ipahealthcheck.ipa.config' |
| 158 | + assert result.check == 'SSSDAllowedUids389Check' |
| 159 | + |
| 160 | + @patch('SSSDConfig.SSSDConfig') |
| 161 | + def test_sssd_bad_allowed_uids_configured(self, mock_sssd): |
| 162 | + """There is now allowed_uids option in the pac section""" |
| 163 | + mock_sssd.return_value = SSSDConfig(return_service=True, |
| 164 | + return_option=True, |
| 165 | + uids='0, 389') |
| 166 | + framework = object() |
| 167 | + registry.initialize(framework, config.Config()) |
| 168 | + f = SSSDAllowedUids389Check(registry) |
| 169 | + self.results = capture_results(f) |
| 170 | + |
| 171 | + assert len(self.results) == 1 |
| 172 | + result = self.results.results[0] |
| 173 | + assert result.result == constants.ERROR |
| 174 | + assert result.kw.get('invalid') == '389' |
| 175 | + assert result.source == 'ipahealthcheck.ipa.config' |
| 176 | + assert result.check == 'SSSDAllowedUids389Check' |
| 177 | + |
| 178 | + @patch('SSSDConfig.SSSDConfig') |
| 179 | + def test_sssd_bad_alpha_allowed_uids_configured(self, mock_sssd): |
| 180 | + """There is now allowed_uids option in the pac section""" |
| 181 | + mock_sssd.return_value = SSSDConfig(return_service=True, |
| 182 | + return_option=True, |
| 183 | + uids='root, dirsrv') |
| 184 | + framework = object() |
| 185 | + registry.initialize(framework, config.Config()) |
| 186 | + f = SSSDAllowedUids389Check(registry) |
| 187 | + self.results = capture_results(f) |
| 188 | + |
| 189 | + assert len(self.results) == 1 |
| 190 | + result = self.results.results[0] |
| 191 | + assert result.result == constants.ERROR |
| 192 | + assert result.kw.get('invalid') == 'dirsrv' |
| 193 | + assert result.source == 'ipahealthcheck.ipa.config' |
| 194 | + assert result.check == 'SSSDAllowedUids389Check' |
0 commit comments