Skip to content

Commit

Permalink
KAA-876: Device management
Browse files Browse the repository at this point in the history
* KAA-876: Trustful credentials service implementation

* KAA-876: Fix the Admin UI
  • Loading branch information
bkhablenko authored and ashvayka committed Apr 6, 2016
1 parent 57ddd99 commit c7283de
Show file tree
Hide file tree
Showing 23 changed files with 264 additions and 30 deletions.
Expand Up @@ -19,6 +19,7 @@
import org.kaaproject.kaa.client.channel.KaaChannelManager;
import org.kaaproject.kaa.client.channel.ServerType;
import org.kaaproject.kaa.client.channel.TransportConnectionInfo;
import org.kaaproject.kaa.client.channel.failover.FailoverDecision.FailoverAction;
import org.kaaproject.kaa.client.channel.failover.strategies.DefaultFailoverStrategy;
import org.kaaproject.kaa.client.channel.failover.strategies.FailoverStrategy;
import org.kaaproject.kaa.client.context.ExecutorContext;
Expand Down
Expand Up @@ -107,6 +107,7 @@ public class DaoConstants {
public static final String APPLICATION_NAME = NAME;
public static final String APPLICATION_SEQUENCE_NUMBER = SEQUENCE_NUMBER;
public static final String APPLICATION_TENANT_ID = TENANT_ID;
public static final String APPLICATION_CREDENTIALS_SERVICE_NAME = "credentials_service";

/**
* User constants.
Expand Down
Expand Up @@ -16,24 +16,26 @@

package org.kaaproject.kaa.server.common.dao.model.sql;

import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;
import org.kaaproject.kaa.common.dto.ApplicationDto;
import static org.kaaproject.kaa.server.common.dao.DaoConstants.APPLICATION_APPLICATION_TOKEN;
import static org.kaaproject.kaa.server.common.dao.DaoConstants.APPLICATION_CREDENTIALS_SERVICE_NAME;
import static org.kaaproject.kaa.server.common.dao.DaoConstants.APPLICATION_NAME;
import static org.kaaproject.kaa.server.common.dao.DaoConstants.APPLICATION_SEQUENCE_NUMBER;
import static org.kaaproject.kaa.server.common.dao.DaoConstants.APPLICATION_TABLE_NAME;
import static org.kaaproject.kaa.server.common.dao.DaoConstants.APPLICATION_TENANT_ID;
import static org.kaaproject.kaa.server.common.dao.model.sql.ModelUtils.getLongId;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import java.io.Serializable;

import static org.kaaproject.kaa.server.common.dao.DaoConstants.APPLICATION_APPLICATION_TOKEN;
import static org.kaaproject.kaa.server.common.dao.DaoConstants.APPLICATION_NAME;
import static org.kaaproject.kaa.server.common.dao.DaoConstants.APPLICATION_SEQUENCE_NUMBER;
import static org.kaaproject.kaa.server.common.dao.DaoConstants.APPLICATION_TABLE_NAME;
import static org.kaaproject.kaa.server.common.dao.DaoConstants.APPLICATION_TENANT_ID;
import static org.kaaproject.kaa.server.common.dao.model.sql.ModelUtils.getLongId;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;
import org.kaaproject.kaa.common.dto.ApplicationDto;

@Entity
@Table(name = APPLICATION_TABLE_NAME, uniqueConstraints = {
Expand All @@ -56,6 +58,9 @@ public class Application extends GenericModel<ApplicationDto> implements Seriali
@OnDelete(action = OnDeleteAction.CASCADE)
private Tenant tenant;

@Column(name = APPLICATION_CREDENTIALS_SERVICE_NAME)
private String credentialsServiceName;

public Application() {
}

Expand All @@ -73,6 +78,7 @@ public Application(ApplicationDto dto) {
if (tenantId != null) {
this.tenant = new Tenant(tenantId);
}
this.credentialsServiceName = dto.getCredentialsServiceName();
}
}

Expand Down Expand Up @@ -108,14 +114,22 @@ public void setTenant(Tenant tenant) {
this.tenant = tenant;
}

public String getCredentialsServiceName() {
return credentialsServiceName;
}

public void setCredentialsServiceName(String credentialsServiceName) {
this.credentialsServiceName = credentialsServiceName;
}

public int incrementSequenceNumber() {
return ++sequenceNumber;
}

@Override
public String toString() {
return "Application [id=" + id + ", applicationToken=" + applicationToken + ", name=" + name + ", sequenceNumber=" + sequenceNumber
+ ", tenant=" + tenant + "]";
+ ", tenant=" + tenant + ", credentialsServiceName=" + credentialsServiceName + "]";
}

@Override
Expand Down Expand Up @@ -196,6 +210,7 @@ public ApplicationDto toDto() {
if (tenant != null) {
dto.setTenantId(tenant.getStringId());
}
dto.setCredentialsServiceName(credentialsServiceName);
return dto;
}
}
Expand Up @@ -40,9 +40,9 @@
*/
@Service
@Transactional
public class CredentialsServiceImpl implements CredentialsService {
public class InternalCredentialsService implements CredentialsService {

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

private CredentialsDao<Credentials> credentialsDao;

Expand Down
@@ -0,0 +1,68 @@
/**
* 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.common.dao.service;

import java.util.Optional;

import org.kaaproject.kaa.common.dto.credentials.CredentialsDto;
import org.kaaproject.kaa.common.dto.credentials.CredentialsStatus;
import org.kaaproject.kaa.server.common.dao.CredentialsService;
import org.kaaproject.kaa.server.common.dao.exception.CredentialsServiceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
* A dummy credentials service to be used in case of no credentials validation
* is needed.
*
* @author Bohdan Khablenko
*
* @since v0.9.0
*/
@Service
public class TrustfulCredentialsService implements CredentialsService {

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

@Override
public CredentialsDto provideCredentials(String applicationId, CredentialsDto credentials) throws CredentialsServiceException {
LOG.debug("Returning credentials provided [{}]", credentials);
return credentials;
}

@Override
public Optional<CredentialsDto> lookupCredentials(String applicationId, String credentialsId) throws CredentialsServiceException {
CredentialsDto credentials = new CredentialsDto(credentialsId, null, CredentialsStatus.AVAILABLE);
LOG.debug("Returning dummy credentials [{}]", credentials);
return Optional.of(credentials);
}

@Override
public void markCredentialsInUse(String applicationId, String credentialsId) throws CredentialsServiceException {
this.updateStatus(applicationId, credentialsId, CredentialsStatus.IN_USE);
}

@Override
public void markCredentialsRevoked(String applicationId, String credentialsId) throws CredentialsServiceException {
this.updateStatus(applicationId, credentialsId, CredentialsStatus.REVOKED);
}

private void updateStatus(String applicationId, String credentialsId, CredentialsStatus status) {
LOG.debug("Consider credentials [{}] for application [{}] to be [{}]", credentialsId, applicationId, status);
}
}
6 changes: 5 additions & 1 deletion server/common/dao/src/main/resources/common-dao-context.xml
Expand Up @@ -90,8 +90,12 @@
<property name="endpointRegistrationDao" ref="endpointRegistrationDao"/>
</bean>

<bean id="credentialsService" class="org.kaaproject.kaa.server.common.dao.service.CredentialsServiceImpl">
<!-- CREDENTIALS SERVICES -->

<bean id="internalCredentialsService" class="org.kaaproject.kaa.server.common.dao.service.InternalCredentialsService">
<property name="credentialsDao" ref="credentialsDao"/>
</bean>

<bean id="trustfulCredentialsService" class="org.kaaproject.kaa.server.common.dao.service.TrustfulCredentialsService"/>

</beans>
Expand Up @@ -27,6 +27,7 @@ public class ApplicationDto implements HasId, Serializable {
private String name;
private int sequenceNumber;
private String tenantId;
private String credentialsServiceName;

@Override
public String getId() {
Expand Down Expand Up @@ -70,6 +71,14 @@ public void setTenantId(String tenantId) {
this.tenantId = tenantId;
}

public String getCredentialsServiceName() {
return this.credentialsServiceName;
}

public void setCredentialsServiceName(String credentialsServiceName) {
this.credentialsServiceName = credentialsServiceName;
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down Expand Up @@ -109,6 +118,6 @@ public int hashCode() {
@Override
public String toString() {
return "ApplicationDto{" + "id='" + id + '\'' + ", applicationToken='" + applicationToken + '\'' + ", name='" + name + '\''
+ ", sequenceNumber=" + sequenceNumber + ", tenantId='" + tenantId + '\'' + '}';
+ ", sequenceNumber=" + sequenceNumber + ", tenantId='" + tenantId + ", credentialsServiceName='" + credentialsServiceName + '\'' + '}';
}
}
Expand Up @@ -37,13 +37,19 @@ public class CredentialsDto implements HasId, Serializable {
private String id;

private byte[] credentialsBody;
private CredentialsStatus status = CredentialsStatus.AVAILABLE;
private CredentialsStatus status;

public CredentialsDto() {
this(null, CredentialsStatus.AVAILABLE);
}

public CredentialsDto(byte[] credentialsBody, CredentialsStatus status) {
this.credentialsBody = Arrays.copyOf(credentialsBody, credentialsBody.length);
this(null, credentialsBody, status);
}

public CredentialsDto(String credentialsId, byte[] credentialsBody, CredentialsStatus status) {
this.id = credentialsId;
this.credentialsBody = credentialsBody != null ? Arrays.copyOf(credentialsBody, credentialsBody.length) : null;
this.status = status;
}

Expand Down
Expand Up @@ -38,6 +38,7 @@
@ContextConfiguration(locations = "/mongo-dao-test-context.xml")
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
public class CredentialsMongoDaoTest extends AbstractMongoTest {

private static final byte[] CREDENTIALS_BODY = "credentials_body".getBytes();
private static final String APPLICATION_ID = "application_id";

Expand Down
Expand Up @@ -84,7 +84,7 @@
<property name="endpointRegistrationDao" ref="endpointRegistrationDao"/>
</bean>

<bean id="credentialsService" class="org.kaaproject.kaa.server.common.dao.service.CredentialsServiceImpl">
<bean id="internalCredentialsService" class="org.kaaproject.kaa.server.common.dao.service.InternalCredentialsService">
<property name="credentialsDao" ref="credentialsDao"/>
</bean>

Expand Down
Expand Up @@ -16,17 +16,21 @@

package org.kaaproject.kaa.server.admin.client.mvp.activity;

import java.util.List;

import org.kaaproject.kaa.common.dto.ApplicationDto;
import org.kaaproject.kaa.server.admin.client.KaaAdmin;
import org.kaaproject.kaa.server.admin.client.mvp.ClientFactory;
import org.kaaproject.kaa.server.admin.client.mvp.place.ApplicationPlace;
import org.kaaproject.kaa.server.admin.client.mvp.place.SdkProfilesPlace;
import org.kaaproject.kaa.server.admin.client.mvp.view.ApplicationView;
import org.kaaproject.kaa.server.admin.client.util.Utils;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.ValueListBox;

public class ApplicationActivity
extends
Expand Down Expand Up @@ -77,11 +81,32 @@ protected void onEntityRetrieved() {
}
detailsView.getApplicationName().setValue(entity.getName());

ValueListBox<String> serviceNames = this.detailsView.getCredentialsServiceName();
if (serviceNames != null) {
KaaAdmin.getDataSource().getCredentialsServiceNames(new AsyncCallback<List<String>>() {

@Override
public void onFailure(Throwable caught) {
Utils.handleException(caught, ApplicationActivity.this.detailsView);
}

@Override
public void onSuccess(List<String> result) {
ApplicationActivity.this.detailsView.getCredentialsServiceName().setAcceptableValues(result);
}
});
String serviceName = this.entity.getCredentialsServiceName();
if (!"".equals(serviceName) && serviceName != null) {
serviceNames.setValue(serviceName);
serviceNames.setEnabled(false);
}
}
}

@Override
protected void onSave() {
entity.setName(detailsView.getApplicationName().getValue());
entity.setCredentialsServiceName(detailsView.getCredentialsServiceName().getValue());
}

@Override
Expand Down
Expand Up @@ -1524,4 +1524,12 @@ protected void onResult(List<SdkProfileDto> result) {
}
});
}

public void getCredentialsServiceNames(final AsyncCallback<List<String>> callback) {
rpcService.getCredentialsServiceNames(new DataCallback<List<String>>(callback) {
@Override
protected void onResult(List<String> result) {
}
});
}
}
Expand Up @@ -18,6 +18,7 @@

import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.ValueListBox;

public interface ApplicationView extends BaseDetailsView {

Expand All @@ -27,6 +28,8 @@ public interface ApplicationView extends BaseDetailsView {

HasValue<String> getApplicationToken();

ValueListBox<String> getCredentialsServiceName();

HasClickHandlers getGenerateSdkButton();

}

0 comments on commit c7283de

Please sign in to comment.