Skip to content

Commit

Permalink
KAA-876: Credentials Service Locator improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
ashvayka committed Apr 6, 2016
1 parent c7283de commit c4821a6
Show file tree
Hide file tree
Showing 11 changed files with 163 additions and 35 deletions.
Expand Up @@ -138,6 +138,7 @@
import org.kaaproject.kaa.server.control.service.zk.ControlZkService;
import org.kaaproject.kaa.server.hash.ConsistentHashResolver;
import org.kaaproject.kaa.server.node.service.credentials.CredentialsServiceLocator;
import org.kaaproject.kaa.server.node.service.credentials.CredentialsServiceRegistry;
import org.kaaproject.kaa.server.node.service.thrift.OperationsServiceMsg;
import org.kaaproject.kaa.server.resolve.OperationsServerResolver;
import org.kaaproject.kaa.server.thrift.NeighborTemplate;
Expand All @@ -146,6 +147,7 @@
import org.slf4j.LoggerFactory;
import org.slf4j.helpers.MessageFormatter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

Expand Down Expand Up @@ -248,8 +250,12 @@ public class DefaultControlService implements ControlService {
private CTLService ctlService;

@Autowired
@Qualifier("rootCredentialsServiceLocator")
private CredentialsServiceLocator credentialsServiceLocator;


@Autowired
private CredentialsServiceRegistry credentialsServiceRegistry;

@Autowired
private EndpointRegistrationService endpointRegistrationService;

Expand Down Expand Up @@ -2319,6 +2325,6 @@ public void provideRegistration(

@Override
public List<String> getCredentialsServiceNames() throws ControlServiceException {
return this.credentialsServiceLocator.getCredentialsServiceNames();
return this.credentialsServiceRegistry.getCredentialsServiceNames();
}
}
Expand Up @@ -16,8 +16,6 @@

package org.kaaproject.kaa.server.node.service.credentials;

import java.util.List;

/**
* Allows each application to have a credentials service of its own.
*
Expand All @@ -38,18 +36,4 @@ public interface CredentialsServiceLocator {
* specified application.
*/
CredentialsService getCredentialsService(String applicationId);

/**
* Returns the names of credentials services configured. This method is used
* to set acceptable values of the listbox used to specify a credentials
* service for an application via the Admin UI.
*
* The default implementation loads all credentials services configured as
* Spring beans and returns their names.
*
* @return The names of credentials services configured
*/
default List<String> getCredentialsServiceNames() {
throw new UnsupportedOperationException();
}
}
@@ -0,0 +1,18 @@
package org.kaaproject.kaa.server.node.service.credentials;

import java.util.List;

public interface CredentialsServiceRegistry {

/**
* Returns the names of credentials services configured. This method is used
* to set acceptable values of the listbox used to specify a credentials
* service for an application via the Admin UI.
*
* The default implementation loads all credentials services configured as
* Spring beans and returns their names.
*
* @return The names of credentials services configured
*/
List<String> getCredentialsServiceNames();
}
Expand Up @@ -16,16 +16,18 @@

package org.kaaproject.kaa.server.node.service.credentials;

import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.kaaproject.kaa.server.common.dao.ApplicationService;
import org.kaaproject.kaa.server.common.dao.service.TrustfulCredentialsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

/**
Expand All @@ -37,31 +39,38 @@
*
* @since v0.9.0
*/
@Service
public final class DefaultCredentialsServiceLocator implements CredentialsServiceLocator {
@Service("rootCredentialsServiceLocator")
public final class DefaultCredentialsServiceLocator implements CredentialsServiceLocator, CredentialsServiceRegistry {

private static final Logger LOG = LoggerFactory.getLogger(DefaultCredentialsServiceLocator.class);

@Autowired
private ApplicationContext applicationContext;
public static final String DEFAULT_CREDENTIALS_SERVICE_NAME = "Trustful";

@Autowired
private ApplicationService applicationService;

@Resource
private Map<String, CredentialsServiceLocator> credentialsServiceLocatorMap;

@Override
public CredentialsService getCredentialsService(String applicationId) {
String serviceName = this.applicationService.findAppById(applicationId).getCredentialsServiceName();
if (StringUtils.isBlank(serviceName)) {
serviceName = StringUtils.uncapitalize(TrustfulCredentialsService.class.getSimpleName());
LOG.debug("No credentials service configured for application [{}], using [{}]", applicationId, serviceName);
serviceName = DEFAULT_CREDENTIALS_SERVICE_NAME;
}
CredentialsServiceLocator locator = credentialsServiceLocatorMap.get(serviceName);
if (locator == null) {
throw new IllegalStateException("Can't find credentials service factory for name: " + serviceName);
} else {
return locator.getCredentialsService(applicationId);
}
return new CredentialsServiceAdapter(applicationId, this.applicationContext.getBean(serviceName,
org.kaaproject.kaa.server.common.dao.CredentialsService.class));
}

@Override
public List<String> getCredentialsServiceNames() {
Class<?> type = org.kaaproject.kaa.server.common.dao.CredentialsService.class;
return Arrays.asList(this.applicationContext.getBeanNamesForType(type));
List<String> result = new ArrayList<>(credentialsServiceLocatorMap.keySet());
Collections.sort(result);
return result;
}
}
@@ -0,0 +1,32 @@
/**
* Copyright 2014-2016 CyberVision, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.kaaproject.kaa.server.node.service.credentials;

public class InternalCredentialsServiceLocator implements CredentialsServiceLocator {

private final org.kaaproject.kaa.server.common.dao.CredentialsService service;

public InternalCredentialsServiceLocator(org.kaaproject.kaa.server.common.dao.CredentialsService service) {
super();
this.service = service;
}

@Override
public CredentialsService getCredentialsService(String applicationId) {
return new CredentialsServiceAdapter(applicationId, service);
}

}
Expand Up @@ -30,6 +30,7 @@
import org.kaaproject.kaa.server.operations.service.security.KeyStoreService;
import org.kaaproject.kaa.server.operations.service.user.EndpointUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

Expand Down Expand Up @@ -84,6 +85,7 @@ public class AkkaContext {
private EndpointUserService endpointUserService;

@Autowired
@Qualifier("rootCredentialsServiceLocator")
private CredentialsServiceLocator credentialsServiceLocator;

@Autowired
Expand Down
16 changes: 13 additions & 3 deletions server/node/src/main/resources/kaaNodeContext.xml
Expand Up @@ -63,8 +63,19 @@
<bean id="registrationService" class="org.kaaproject.kaa.server.node.service.registration.DefaultRegistrationService">
<property name="endpointRegistrationService" ref="endpointRegistrationService"/>
</bean>

<bean id="credentialsServiceLocator" class="org.kaaproject.kaa.server.node.service.credentials.DefaultCredentialsServiceLocator"/>

<bean id="trustfulCredentialsServiceLocator" class="org.kaaproject.kaa.server.node.service.credentials.InternalCredentialsServiceLocator">
<constructor-arg ref="trustfulCredentialsService"/>
</bean>

<bean id="internalCredentialsServiceLocator" class="org.kaaproject.kaa.server.node.service.credentials.InternalCredentialsServiceLocator">
<constructor-arg ref="internalCredentialsService"/>
</bean>

<util:map id="credentialsServiceLocatorMap">
<entry key="Trustful" value-ref="trustfulCredentialsServiceLocator"/>
<entry key="Internal" value-ref="internalCredentialsServiceLocator"/>
</util:map>

<!-- Control Service -->

Expand Down Expand Up @@ -131,5 +142,4 @@
<property name="enabled" value="#{properties[metrics_enabled]}"/>
</bean>


</beans>
@@ -0,0 +1,32 @@
package org.kaaproject.kaa.server.node.service.credentials;

import java.util.List;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.annotation.DirtiesContext.ClassMode;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;



@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "/common-test-context.xml")
@DirtiesContext(classMode = ClassMode.AFTER_CLASS)
public class DefaultCredentialsServiceLocatorTest {

@Autowired
private DefaultCredentialsServiceLocator credentialsServiceLocator;

@Test
public void testCredentailsServiceLoad(){
List<String> serviceNames = credentialsServiceLocator.getCredentialsServiceNames();
Assert.assertEquals(2, serviceNames.size());
Assert.assertEquals("Internal", serviceNames.get(0));
Assert.assertEquals("Trustful", serviceNames.get(1));
}

}
12 changes: 12 additions & 0 deletions server/node/src/test/resources/common-test-context.xml
Expand Up @@ -126,5 +126,17 @@
<property name="enabled" value="#{properties[metrics_enabled]}"/>
</bean>

<bean id="trustfulCredentialsServiceLocator" class="org.kaaproject.kaa.server.node.service.credentials.InternalCredentialsServiceLocator">
<constructor-arg ref="trustfulCredentialsService"/>
</bean>

<bean id="internalCredentialsServiceLocator" class="org.kaaproject.kaa.server.node.service.credentials.InternalCredentialsServiceLocator">
<constructor-arg ref="internalCredentialsService"/>
</bean>

<util:map id="credentialsServiceLocatorMap">
<entry key="Trustful" value-ref="trustfulCredentialsServiceLocator"/>
<entry key="Internal" value-ref="internalCredentialsServiceLocator"/>
</util:map>

</beans>
12 changes: 12 additions & 0 deletions server/node/src/test/resources/common-zk-test-context.xml
Expand Up @@ -126,5 +126,17 @@
<property name="enabled" value="#{properties[metrics_enabled]}"/>
</bean>

<bean id="trustfulCredentialsServiceLocator" class="org.kaaproject.kaa.server.node.service.credentials.InternalCredentialsServiceLocator">
<constructor-arg ref="trustfulCredentialsService"/>
</bean>

<bean id="internalCredentialsServiceLocator" class="org.kaaproject.kaa.server.node.service.credentials.InternalCredentialsServiceLocator">
<constructor-arg ref="internalCredentialsService"/>
</bean>

<util:map id="credentialsServiceLocatorMap">
<entry key="Trustful" value-ref="trustfulCredentialsServiceLocator"/>
<entry key="Internal" value-ref="internalCredentialsServiceLocator"/>
</util:map>

</beans>
15 changes: 13 additions & 2 deletions server/node/src/test/resources/operations/common-test-context.xml
Expand Up @@ -33,7 +33,7 @@
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">

<context:component-scan base-package="org.kaaproject.kaa.server.operations" />
<context:component-scan base-package="org.kaaproject.kaa.server.node, org.kaaproject.kaa.server.operations" />

<import resource="classpath:mongo-dao-test-context.xml" />

Expand Down Expand Up @@ -93,5 +93,16 @@
<property name="endpointRegistrationService" ref="endpointRegistrationService"/>
</bean>

<bean id="credentialsServiceLocator" class="org.kaaproject.kaa.server.node.service.credentials.DefaultCredentialsServiceLocator"/>
<bean id="trustfulCredentialsServiceLocator" class="org.kaaproject.kaa.server.node.service.credentials.InternalCredentialsServiceLocator">
<constructor-arg ref="trustfulCredentialsService"/>
</bean>

<bean id="internalCredentialsServiceLocator" class="org.kaaproject.kaa.server.node.service.credentials.InternalCredentialsServiceLocator">
<constructor-arg ref="internalCredentialsService"/>
</bean>

<util:map id="credentialsServiceLocatorMap">
<entry key="Trustful" value-ref="trustfulCredentialsServiceLocator"/>
<entry key="Internal" value-ref="internalCredentialsServiceLocator"/>
</util:map>
</beans>

0 comments on commit c4821a6

Please sign in to comment.