diff options
-rw-r--r-- | src/config/SSSDConfig.py | 142 | ||||
-rwxr-xr-x | src/config/SSSDConfigTest.py | 123 | ||||
-rw-r--r-- | src/config/testconfigs/sssd-valid.conf | 3 |
3 files changed, 268 insertions, 0 deletions
diff --git a/src/config/SSSDConfig.py b/src/config/SSSDConfig.py index 9fbe6742..a545800b 100644 --- a/src/config/SSSDConfig.py +++ b/src/config/SSSDConfig.py @@ -1350,6 +1350,64 @@ class SSSDConfig(SSSDChangeConf): of.close() os.umask(old_umask) + def list_active_services(self): + """ + Return a list of all active services. + + === Returns === + The list of active services. + + === Errors === + NotInitializedError: + This SSSDConfig object has not had import_config() or new_config() + run on it yet. + """ + if not self.initialized: + raise NotInitializedError + + if (self.has_option('sssd', 'services')): + active_services = striplist(self.get('sssd', 'services').split(',')) + service_dict = dict.fromkeys(active_services) + if service_dict.has_key(''): + del service_dict[''] + + # Remove any entries in this list that don't + # correspond to an active service, for integrity + configured_services = self.list_services() + for srv in service_dict.keys(): + if srv not in configured_services: + del service_dict[srv] + + active_services = service_dict.keys() + else: + active_services = [] + + return active_services + + def list_inactive_services(self): + """ + Return a list of all disabled services. + + === Returns === + The list of inactive services. + + === Errors === + NotInitializedError: + This SSSDConfig object has not had import_config() or new_config() + run on it yet. + """ + if not self.initialized: + raise NotInitializedError + + if (self.has_option('sssd', 'services')): + active_services = striplist(self.get('sssd', 'services').split(',')) + else: + active_services = [] + + services = [x for x in self.list_services() + if x not in active_services] + return services + def list_services(self): """ Retrieve a list of known services. @@ -1433,6 +1491,90 @@ class SSSDConfig(SSSDChangeConf): self.save_service(service) return service + def activate_service(self, name): + """ + Activate a service + + name: + The name of the service to activate + + === Returns === + No return value + + === Errors === + NotInitializedError: + This SSSDConfig object has not had import_config() or new_config() + run on it yet. + NoServiceError: + There is no such service with the specified name in the SSSDConfig. + """ + + if not self.initialized: + raise NotInitializedError + + if name not in self.list_services(): + raise NoServiceError + + item = self.get_option_index('sssd', 'services')[1] + if not item: + self.set('sssd','services', name) + return + + # Turn the items into a set of dictionary keys + # This guarantees uniqueness and makes it easy + # to add a new value + service_dict = dict.fromkeys(striplist(item['value'].split(','))) + if service_dict.has_key(''): + del service_dict[''] + + # Add a new key for the service being activated + service_dict[name] = None + + # Write out the joined keys + self.set('sssd','services', ", ".join(service_dict.keys())) + + def deactivate_service(self, name): + """ + Deactivate a service + + name: + The name of the service to deactivate + + === Returns === + No return value + + === Errors === + NotInitializedError: + This SSSDConfig object has not had import_config() or new_config() + run on it yet. + NoServiceError: + There is no such service with the specified name in the SSSDConfig. + """ + + if not self.initialized: + raise NotInitializedError + + if name not in self.list_services(): + raise NoServiceError + item = self.get_option_index('sssd', 'services')[1] + if not item: + self.set('sssd','services', '') + return + + # Turn the items into a set of dictionary keys + # This guarantees uniqueness and makes it easy + # to remove the one unwanted value. + service_dict = dict.fromkeys(striplist(item['value'].split(','))) + if service_dict.has_key(''): + del service_dict[''] + + # Remove the unwanted service from the lest + if service_dict.has_key(name): + del service_dict[name] + + # Write out the joined keys + self.set('sssd','services', ", ".join(service_dict.keys())) + def delete_service(self, name): """ Remove a service from the SSSDConfig object. This function will also diff --git a/src/config/SSSDConfigTest.py b/src/config/SSSDConfigTest.py index 5bad40ed..16dbedfc 100755 --- a/src/config/SSSDConfigTest.py +++ b/src/config/SSSDConfigTest.py @@ -1080,6 +1080,7 @@ class SSSDConfigTestSSSDConfig(unittest.TestCase): 'sssd', 'nss', 'pam', + 'sudo', 'domain/PROXY', 'domain/IPA', 'domain/LOCAL', @@ -1184,6 +1185,54 @@ class SSSDConfigTestSSSDConfig(unittest.TestCase): #TODO Write tests to compare output files pass + def testListActiveServices(self): + sssdconfig = SSSDConfig.SSSDConfig(srcdir + "/etc/sssd.api.conf", + srcdir + "/etc/sssd.api.d") + + # Negative Test - Not Initialized + self.assertRaises(SSSDConfig.NotInitializedError, sssdconfig.list_active_services) + + # Positive Test + sssdconfig.import_config(srcdir + '/testconfigs/sssd-valid.conf') + + control_list = [ + 'nss', + 'pam'] + active_services = sssdconfig.list_active_services() + + for service in control_list: + self.assertTrue(service in active_services, + "Service [%s] missing" % + service) + for service in active_services: + self.assertTrue(service in control_list, + "Service [%s] unexpectedly found" % + service) + + def testListInactiveServices(self): + sssdconfig = SSSDConfig.SSSDConfig(srcdir + "/etc/sssd.api.conf", + srcdir + "/etc/sssd.api.d") + + # Negative Test - Not Initialized + self.assertRaises(SSSDConfig.NotInitializedError, sssdconfig.list_inactive_services) + + # Positive Test + sssdconfig.import_config(srcdir + '/testconfigs/sssd-valid.conf') + + control_list = [ + 'sssd', + 'sudo'] + inactive_services = sssdconfig.list_inactive_services() + + for service in control_list: + self.assertTrue(service in inactive_services, + "Service [%s] missing" % + service) + for service in inactive_services: + self.assertTrue(service in control_list, + "Service [%s] unexpectedly found" % + service) + def testListServices(self): sssdconfig = SSSDConfig.SSSDConfig(srcdir + "/etc/sssd.api.conf", srcdir + "/etc/sssd.api.d") @@ -1281,6 +1330,80 @@ class SSSDConfigTestSSSDConfig(unittest.TestCase): # Negative Test - Type Error self.assertRaises(TypeError, sssdconfig.save_service, self) + def testActivateService(self): + sssdconfig = SSSDConfig.SSSDConfig(srcdir + "/etc/sssd.api.conf", + srcdir + "/etc/sssd.api.d") + + service_name = 'sudo' + + # Negative test - Not initialized + self.assertRaises(SSSDConfig.NotInitializedError, + sssdconfig.activate_service, service_name) + + sssdconfig.import_config(srcdir + "/testconfigs/sssd-valid.conf") + + # Positive test - Activate an inactive service + self.assertTrue(service_name in sssdconfig.list_services()) + self.assertFalse(service_name in sssdconfig.list_active_services()) + self.assertTrue(service_name in sssdconfig.list_inactive_services()) + + sssdconfig.activate_service(service_name) + self.assertTrue(service_name in sssdconfig.list_services()) + self.assertTrue(service_name in sssdconfig.list_active_services()) + self.assertFalse(service_name in sssdconfig.list_inactive_services()) + + # Positive test - Activate an active service + # This should succeed + sssdconfig.activate_service(service_name) + self.assertTrue(service_name in sssdconfig.list_services()) + self.assertTrue(service_name in sssdconfig.list_active_services()) + self.assertFalse(service_name in sssdconfig.list_inactive_services()) + + # Negative test - Invalid service name + self.assertRaises(SSSDConfig.NoServiceError, + sssdconfig.activate_service, 'nosuchservice') + + # Negative test - Invalid service name type + self.assertRaises(SSSDConfig.NoServiceError, + sssdconfig.activate_service, self) + + def testDeactivateService(self): + sssdconfig = SSSDConfig.SSSDConfig(srcdir + "/etc/sssd.api.conf", + srcdir + "/etc/sssd.api.d") + + service_name = 'pam' + + # Negative test - Not initialized + self.assertRaises(SSSDConfig.NotInitializedError, + sssdconfig.activate_service, service_name) + + sssdconfig.import_config(srcdir + "/testconfigs/sssd-valid.conf") + + # Positive test -Deactivate an active service + self.assertTrue(service_name in sssdconfig.list_services()) + self.assertTrue(service_name in sssdconfig.list_active_services()) + self.assertFalse(service_name in sssdconfig.list_inactive_services()) + + sssdconfig.deactivate_service(service_name) + self.assertTrue(service_name in sssdconfig.list_services()) + self.assertFalse(service_name in sssdconfig.list_active_services()) + self.assertTrue(service_name in sssdconfig.list_inactive_services()) + + # Positive test - Deactivate an inactive service + # This should succeed + sssdconfig.deactivate_service(service_name) + self.assertTrue(service_name in sssdconfig.list_services()) + self.assertFalse(service_name in sssdconfig.list_active_services()) + self.assertTrue(service_name in sssdconfig.list_inactive_services()) + + # Negative test - Invalid service name + self.assertRaises(SSSDConfig.NoServiceError, + sssdconfig.activate_service, 'nosuchservice') + + # Negative test - Invalid service name type + self.assertRaises(SSSDConfig.NoServiceError, + sssdconfig.activate_service, self) + def testListActiveDomains(self): sssdconfig = SSSDConfig.SSSDConfig(srcdir + "/etc/sssd.api.conf", srcdir + "/etc/sssd.api.d") diff --git a/src/config/testconfigs/sssd-valid.conf b/src/config/testconfigs/sssd-valid.conf index a2824d3f..571ce77f 100644 --- a/src/config/testconfigs/sssd-valid.conf +++ b/src/config/testconfigs/sssd-valid.conf @@ -55,3 +55,6 @@ nosuchoption = True [pam] debug_level = 0 nosuchoption = True + +[sudo] +debug_level = 0 |