ResourceFactory: Validate a resource's configuration

Probably only a quickfix, but feels still more proper than the
previous solution, on a second thought..
This commit is contained in:
Johannes Meyer 2016-02-12 14:18:19 +01:00
parent fc30af806b
commit cdb873cbdb
2 changed files with 9 additions and 14 deletions

View File

@ -81,16 +81,12 @@ class ResourceFactory implements ConfigAwareFactory
} }
/** /**
* Create a single resource from the given configuration. * Create and return a resource based on the given configuration
* *
* NOTE: The factory does not test if the given configuration is valid and the resource is accessible, this * @param ConfigObject $config The configuration of the resource to create
* depends entirely on the implementation of the returned resource.
* *
* @param ConfigObject $config The configuration for the created resource. * @return Selectable The resource
* * @throws ConfigurationError In case of an unsupported type or invalid configuration
* @return DbConnection|LdapConnection|LivestatusConnection An object that can be used to access
* the given resource. The returned class depends on the configuration property 'type'.
* @throws ConfigurationError When an unsupported type is given
*/ */
public static function createResource(ConfigObject $config) public static function createResource(ConfigObject $config)
{ {
@ -99,6 +95,10 @@ class ResourceFactory implements ConfigAwareFactory
$resource = new DbConnection($config); $resource = new DbConnection($config);
break; break;
case 'ldap': case 'ldap':
if (empty($config->root_dn)) {
throw new ConfigurationError('LDAP root DN missing');
}
$resource = new LdapConnection($config); $resource = new LdapConnection($config);
break; break;
case 'livestatus': case 'livestatus':
@ -116,6 +116,7 @@ class ResourceFactory implements ConfigAwareFactory
$config->type $config->type
); );
} }
return $resource; return $resource;
} }

View File

@ -15,7 +15,6 @@ use Icinga\Data\Sortable;
use Icinga\Data\Filter\Filter; use Icinga\Data\Filter\Filter;
use Icinga\Data\Filter\FilterChain; use Icinga\Data\Filter\FilterChain;
use Icinga\Data\Filter\FilterExpression; use Icinga\Data\Filter\FilterExpression;
use Icinga\Exception\ConfigurationError;
use Icinga\Exception\ProgrammingError; use Icinga\Exception\ProgrammingError;
use Icinga\Protocol\Ldap\LdapException; use Icinga\Protocol\Ldap\LdapException;
@ -169,12 +168,7 @@ class LdapConnection implements Selectable, Inspectable
$this->hostname = $config->hostname; $this->hostname = $config->hostname;
$this->bindDn = $config->bind_dn; $this->bindDn = $config->bind_dn;
$this->bindPw = $config->bind_pw; $this->bindPw = $config->bind_pw;
if (empty($config->root_dn)) {
throw new ConfigurationError('LDAP root DN missing');
}
$this->rootDn = $config->root_dn; $this->rootDn = $config->root_dn;
$this->port = $config->get('port', 389); $this->port = $config->get('port', 389);
$this->encryption = $config->encryption; $this->encryption = $config->encryption;