diff options
author | Stephen Gallagher <sgallagh@redhat.com> | 2009-12-08 22:51:35 -0500 |
---|---|---|
committer | Stephen Gallagher <sgallagh@redhat.com> | 2009-12-08 23:35:30 -0500 |
commit | 519f6d060dc52159d1ad1e8141367650c447adcf (patch) | |
tree | b15adc8ce55749767efb16aa2d4b4ec058480100 /server | |
parent | 974b478d04543a1912515f53160c0e5c6e31805c (diff) | |
download | sssd-519f6d060dc52159d1ad1e8141367650c447adcf.tar.gz sssd-519f6d060dc52159d1ad1e8141367650c447adcf.tar.bz2 sssd-519f6d060dc52159d1ad1e8141367650c447adcf.zip |
Fix SSSDConfig API bugs around [de-]activation of domains
Adds two new public functions:
SSSDConfig.activate_domain()
SSSDConfig.deactivate_domain()
These two functions are used during the save_domain() call to
ensure that the active domain list is always kept up to date.
Diffstat (limited to 'server')
-rw-r--r-- | server/config/SSSDConfig.py | 96 | ||||
-rw-r--r-- | server/config/SSSDConfigTest.py | 63 |
2 files changed, 152 insertions, 7 deletions
diff --git a/server/config/SSSDConfig.py b/server/config/SSSDConfig.py index 3b8c128e..3a9ab4b4 100644 --- a/server/config/SSSDConfig.py +++ b/server/config/SSSDConfig.py @@ -1266,6 +1266,89 @@ class SSSDConfig(SSSDChangeConf): self.save_domain(domain) return domain + def activate_domain(self, name): + """ + Activate a configured domain + + name: + The name of the configured domain to activate + + === Returns === + No return value + + === Errors === + NotInitializedError: + This SSSDConfig object has not had import_config() or new_config() + run on it yet. + NoDomainError: + No domain by this name is configured + """ + + if not self.initialized: + raise NotInitializedError + + if name not in self.list_domains(): + raise NoDomainError + + item = self.get_option_index('sssd', 'domains')[1] + if not item: + self.set('sssd','domains', name) + return + + # Turn the items into a set of dictionary keys + # This guarantees uniqueness and makes it easy + # to add a new value + domain_dict = dict.fromkeys(striplist(item['value'].split(','))) + if domain_dict.has_key(''): + del domain_dict[''] + + # Add a new key for the domain being activated + domain_dict[name] = None + + # Write out the joined keys + self.set('sssd','domains', ", ".join(domain_dict.keys())) + + def deactivate_domain(self, name): + """ + Deactivate a configured domain + + name: + The name of the configured domain to deactivate + + === Returns === + No return value + + === Errors === + NotInitializedError: + This SSSDConfig object has not had import_config() or new_config() + run on it yet. + NoDomainError: + No domain by this name is configured + """ + + if not self.initialized: + raise NotInitializedError + + if name not in self.list_domains(): + raise NoDomainError + item = self.get_option_index('sssd', 'domains')[1] + if not item: + self.set('sssd','domains', '') + return + + # Turn the items into a set of dictionary keys + # This guarantees uniqueness and makes it easy + # to remove the one unwanted value. + domain_dict = dict.fromkeys(striplist(item['value'].split(','))) + if domain_dict.has_key(''): + del domain_dict[''] + + # Add a new key for the domain being activated + del domain_dict[name] + + # Write out the joined keys + self.set('sssd','domains', ", ".join(domain_dict.keys())) + def delete_domain(self, name): """ Remove a domain from the SSSDConfig object. This function will also @@ -1282,6 +1365,9 @@ class SSSDConfig(SSSDChangeConf): """ if not self.initialized: raise NotInitializedError + + # Remove the domain from the active domains list if applicable + self.deactivate_domain(name) self.delete_option('section', 'domain/%s' % name) def save_domain(self, domain): @@ -1341,10 +1427,6 @@ class SSSDConfig(SSSDChangeConf): self.add_section(sectionname, addkw, index) if domain.active: - if domain.get_name not in self.list_active_domains(): - # Add it to the list of active domains - item = self.get_option_index('sssd', 'domains')[1] - if item: - item['value'] += ", %s" % domain.get_name() - else: - self.set('sssd', 'domains', domain.get_name()) + self.activate_domain(name) + else: + self.deactivate_domain(name) diff --git a/server/config/SSSDConfigTest.py b/server/config/SSSDConfigTest.py index 1f8c4f8c..973ef07a 100644 --- a/server/config/SSSDConfigTest.py +++ b/server/config/SSSDConfigTest.py @@ -1183,6 +1183,69 @@ class SSSDConfigTestSSSDConfig(unittest.TestCase): self.assertFalse('example.com' in sssdconfig.list_active_domains()) self.assertFalse('example.com' in sssdconfig.list_inactive_domains()) self.assertFalse(sssdconfig.has_section('domain/example.com')) + self.assertEquals(domain.oldname, None) + + # Positive test - Set the domain inactive and save it + activelist = sssdconfig.list_active_domains() + inactivelist = sssdconfig.list_inactive_domains() + + domain.set_active(False) + sssdconfig.save_domain(domain) + + self.assertFalse('example.com2' in sssdconfig.list_active_domains()) + self.assertTrue('example.com2' in sssdconfig.list_inactive_domains()) + + self.assertEquals(len(sssdconfig.list_active_domains()), + len(activelist)-1) + self.assertEquals(len(sssdconfig.list_inactive_domains()), + len(inactivelist)+1) + + # Positive test - Set the domain active and save it + activelist = sssdconfig.list_active_domains() + inactivelist = sssdconfig.list_inactive_domains() + domain.set_active(True) + sssdconfig.save_domain(domain) + + self.assertTrue('example.com2' in sssdconfig.list_active_domains()) + self.assertFalse('example.com2' in sssdconfig.list_inactive_domains()) + + self.assertEquals(len(sssdconfig.list_active_domains()), + len(activelist)+1) + self.assertEquals(len(sssdconfig.list_inactive_domains()), + len(inactivelist)-1) + + # Positive test - Set the domain inactive and save it + activelist = sssdconfig.list_active_domains() + inactivelist = sssdconfig.list_inactive_domains() + + sssdconfig.deactivate_domain(domain.get_name()) + + self.assertFalse('example.com2' in sssdconfig.list_active_domains()) + self.assertTrue('example.com2' in sssdconfig.list_inactive_domains()) + + self.assertEquals(len(sssdconfig.list_active_domains()), + len(activelist)-1) + self.assertEquals(len(sssdconfig.list_inactive_domains()), + len(inactivelist)+1) + + # Positive test - Set the domain active and save it + activelist = sssdconfig.list_active_domains() + inactivelist = sssdconfig.list_inactive_domains() + + sssdconfig.activate_domain(domain.get_name()) + + self.assertTrue('example.com2' in sssdconfig.list_active_domains()) + self.assertFalse('example.com2' in sssdconfig.list_inactive_domains()) + + self.assertEquals(len(sssdconfig.list_active_domains()), + len(activelist)+1) + self.assertEquals(len(sssdconfig.list_inactive_domains()), + len(inactivelist)-1) + + + def testActivateDomain(self): + sssdconfig = SSSDConfig.SSSDConfig("etc/sssd.api.conf", + "etc/sssd.api.d") if __name__ == "__main__": error = 0 |