Repository API query signatures Entity free (#403)

* Migrated target management queries to IDs inetsead of full entities

Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com>

* Added missing comment.

Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com>

* refactored target,DS,cont,deploy,rg mangement.

Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com>

* Adde versioning documentation.

Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com>

* Rollout, Dist and Software mgmt refactored

Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com>

* Readded line that was remove by incident. 

Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com>

* Fixed broken tests.

Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com>

* Query management refactored

Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com>

* Fix bug of auto assign DS delete

Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com>

* Switch to collection

Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com>

* Fixed compile error

Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com>

* Small glitches

Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com>

* Fixed test after merge

Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com>
This commit is contained in:
Kai Zimmermann
2017-01-11 14:32:55 +01:00
committed by GitHub
parent 824ef2982f
commit 889d1492fb
97 changed files with 1126 additions and 1289 deletions

View File

@@ -116,46 +116,43 @@ public class AmqpAuthenticationMessageHandler extends BaseAmqpService {
*
* @param secruityToken
* the security token which holds the target ID to check on
* @param artifact
* @param artifactId
* the artifact to verify if the given target is allowed to
* download it
*/
private void checkIfArtifactIsAssignedToTarget(final TenantSecurityToken secruityToken,
final org.eclipse.hawkbit.repository.model.Artifact artifact) {
private void checkIfArtifactIsAssignedToTarget(final TenantSecurityToken secruityToken, final Long artifactId) {
if (secruityToken.getControllerId() != null) {
checkByControllerId(artifact, secruityToken.getControllerId());
checkByControllerId(artifactId, secruityToken.getControllerId());
} else if (secruityToken.getTargetId() != null) {
checkByTargetId(artifact, secruityToken.getTargetId());
checkByTargetId(artifactId, secruityToken.getTargetId());
} else {
LOG.info("anonymous download no authentication check for artifact {}", artifact);
LOG.info("anonymous download no authentication check for artifact {}", artifactId);
return;
}
}
private void checkByTargetId(final org.eclipse.hawkbit.repository.model.Artifact localArtifact,
final Long targetId) {
private void checkByTargetId(final Long artifactId, final Long targetId) {
LOG.debug("no anonymous download request, doing authentication check for target {} and artifact {}", targetId,
localArtifact);
if (!controllerManagement.hasTargetArtifactAssigned(targetId, localArtifact)) {
artifactId);
if (!controllerManagement.hasTargetArtifactAssigned(targetId, artifactId)) {
LOG.info("target {} tried to download artifact {} which is not assigned to the target", targetId,
localArtifact);
artifactId);
throw new EntityNotFoundException();
}
LOG.info("download security check for target {} and artifact {} granted", targetId, localArtifact);
LOG.info("download security check for target {} and artifact {} granted", targetId, artifactId);
}
private void checkByControllerId(final org.eclipse.hawkbit.repository.model.Artifact localArtifact,
final String controllerId) {
private void checkByControllerId(final Long artifactId, final String controllerId) {
LOG.debug("no anonymous download request, doing authentication check for target {} and artifact {}",
controllerId, localArtifact);
if (!controllerManagement.hasTargetArtifactAssigned(controllerId, localArtifact)) {
controllerId, artifactId);
if (!controllerManagement.hasTargetArtifactAssigned(controllerId, artifactId)) {
LOG.info("target {} tried to download artifact {} which is not assigned to the target", controllerId,
localArtifact);
artifactId);
throw new EntityNotFoundException();
}
LOG.info("download security check for target {} and artifact {} granted", controllerId, localArtifact);
LOG.info("download security check for target {} and artifact {} granted", controllerId, artifactId);
}
private org.eclipse.hawkbit.repository.model.Artifact findArtifactByFileResource(final FileResource fileResource) {
@@ -201,9 +198,9 @@ public class AmqpAuthenticationMessageHandler extends BaseAmqpService {
throw new EntityNotFoundException();
}
checkIfArtifactIsAssignedToTarget(secruityToken, localArtifact);
checkIfArtifactIsAssignedToTarget(secruityToken, localArtifact.getId());
final Artifact artifact = convertDbArtifact(artifactManagement.loadArtifactBinary(localArtifact));
final Artifact artifact = convertDbArtifact(artifactManagement.loadArtifactBinary(localArtifact.getId()));
if (artifact == null) {
throw new EntityNotFoundException();
}

View File

@@ -180,7 +180,8 @@ public class AmqpMessageHandlerService extends BaseAmqpService {
}
private void lookIfUpdateAvailable(final Target target) {
final Optional<Action> actionOptional = controllerManagement.findOldestActiveActionByTarget(target);
final Optional<Action> actionOptional = controllerManagement
.findOldestActiveActionByTarget(target.getControllerId());
if (!actionOptional.isPresent()) {
return;
}
@@ -286,7 +287,7 @@ public class AmqpMessageHandlerService extends BaseAmqpService {
}
private void updateLastPollTime(final Target target) {
controllerManagement.updateTargetStatus(target.getTargetInfo(), null, System.currentTimeMillis(), null);
controllerManagement.updateLastTargetQuery(target.getControllerId(), null);
}
private static String convertCorrelationId(final Message message) {

View File

@@ -35,7 +35,6 @@ import org.eclipse.hawkbit.repository.jpa.JpaEntityFactory;
import org.eclipse.hawkbit.repository.jpa.model.JpaArtifact;
import org.eclipse.hawkbit.repository.jpa.model.JpaSoftwareModule;
import org.eclipse.hawkbit.repository.jpa.model.JpaSoftwareModuleType;
import org.eclipse.hawkbit.repository.model.Artifact;
import org.eclipse.hawkbit.repository.model.Target;
import org.eclipse.hawkbit.repository.model.TenantConfigurationValue;
import org.eclipse.hawkbit.repository.model.TenantMetaData;
@@ -153,8 +152,9 @@ public class AmqpControllerAuthenticationTest {
authenticationManager.postConstruct();
final Artifact testArtifact = new JpaArtifact("afilename", "afilename", new JpaSoftwareModule(
final JpaArtifact testArtifact = new JpaArtifact("afilename", "afilename", new JpaSoftwareModule(
new JpaSoftwareModuleType("a key", "a name", null, 1), "a name", null, null, null));
testArtifact.setId(1L);
when(artifactManagementMock.findArtifact(ARTIFACT_ID)).thenReturn(testArtifact);
when(artifactManagementMock.findFirstArtifactBySHA1(SHA1)).thenReturn(testArtifact);
@@ -162,7 +162,7 @@ public class AmqpControllerAuthenticationTest {
final DbArtifact artifact = new DbArtifact();
artifact.setSize(ARTIFACT_SIZE);
artifact.setHashes(new DbArtifactHash("sha1 test", "md5 test"));
when(artifactManagementMock.loadArtifactBinary(testArtifact)).thenReturn(artifact);
when(artifactManagementMock.loadArtifactBinary(1L)).thenReturn(artifact);
amqpMessageHandlerService = new AmqpMessageHandlerService(rabbitTemplate,
mock(AmqpMessageDispatcherService.class), controllerManagementMock, new JpaEntityFactory());
@@ -173,8 +173,8 @@ public class AmqpControllerAuthenticationTest {
when(hostnameResolverMock.resolveHostname()).thenReturn(new URL("http://localhost"));
when(controllerManagementMock.hasTargetArtifactAssigned(TARGET_ID, testArtifact)).thenReturn(true);
when(controllerManagementMock.hasTargetArtifactAssigned(CONTROLLER_ID, testArtifact)).thenReturn(true);
when(controllerManagementMock.hasTargetArtifactAssigned(TARGET_ID, 1L)).thenReturn(true);
when(controllerManagementMock.hasTargetArtifactAssigned(CONTROLLER_ID, 1L)).thenReturn(true);
}
@Test

View File

@@ -10,6 +10,7 @@ package org.eclipse.hawkbit.amqp;
import static org.fest.assertions.api.Assertions.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.anyLong;
import static org.mockito.Matchers.anyObject;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
@@ -49,9 +50,7 @@ import org.eclipse.hawkbit.repository.model.Action.Status;
import org.eclipse.hawkbit.repository.model.Artifact;
import org.eclipse.hawkbit.repository.model.DistributionSet;
import org.eclipse.hawkbit.repository.model.Target;
import org.eclipse.hawkbit.repository.model.Target;
import org.eclipse.hawkbit.repository.model.TargetInfo;
import org.eclipse.hawkbit.repository.model.TargetUpdateStatus;
import org.eclipse.hawkbit.security.SecurityTokenGenerator;
import org.junit.Before;
import org.junit.Test;
@@ -149,10 +148,12 @@ public class AmqpMessageHandlerServiceTest {
messageProperties.setHeader(MessageHeaderKey.THING_ID, "1");
final Message message = messageConverter.toMessage(new byte[0], messageProperties);
final Target targetMock = mock(Target.class);
final ArgumentCaptor<String> targetIdCaptor = ArgumentCaptor.forClass(String.class);
final ArgumentCaptor<URI> uriCaptor = ArgumentCaptor.forClass(URI.class);
when(controllerManagementMock.findOrRegisterTargetIfItDoesNotexist(targetIdCaptor.capture(),
uriCaptor.capture())).thenReturn(null);
uriCaptor.capture())).thenReturn(targetMock);
when(controllerManagementMock.findOldestActiveActionByTarget(Matchers.any())).thenReturn(Optional.empty());
amqpMessageHandlerService.onMessage(message, MessageType.THING_CREATED.name(), TENANT, "vHost");
@@ -326,11 +327,14 @@ public class AmqpMessageHandlerServiceTest {
// mock
final Artifact localArtifactMock = mock(Artifact.class);
final Long mockedArtifactId = 1L;
when(localArtifactMock.getId()).thenReturn(mockedArtifactId);
final DbArtifact dbArtifactMock = mock(DbArtifact.class);
when(artifactManagementMock.findFirstArtifactBySHA1(anyString())).thenReturn(localArtifactMock);
when(controllerManagementMock.hasTargetArtifactAssigned(securityToken.getControllerId(), localArtifactMock))
when(controllerManagementMock.hasTargetArtifactAssigned(securityToken.getControllerId(), mockedArtifactId))
.thenReturn(true);
when(artifactManagementMock.loadArtifactBinary(localArtifactMock)).thenReturn(dbArtifactMock);
when(artifactManagementMock.loadArtifactBinary(anyLong())).thenReturn(dbArtifactMock);
when(dbArtifactMock.getArtifactId()).thenReturn("artifactId");
when(dbArtifactMock.getSize()).thenReturn(1L);
when(dbArtifactMock.getHashes()).thenReturn(new DbArtifactHash("sha1", "md5"));
@@ -377,8 +381,7 @@ public class AmqpMessageHandlerServiceTest {
amqpMessageHandlerService.onMessage(message, MessageType.EVENT.name(), TENANT, "vHost");
// verify
verify(controllerManagementMock).updateTargetStatus(Matchers.any(TargetInfo.class),
Matchers.isNull(TargetUpdateStatus.class), Matchers.isNotNull(Long.class), Matchers.isNull(URI.class));
verify(controllerManagementMock).updateLastTargetQuery(Matchers.any(String.class), Matchers.isNull(URI.class));
final ArgumentCaptor<String> tenantCaptor = ArgumentCaptor.forClass(String.class);
final ArgumentCaptor<Target> targetCaptor = ArgumentCaptor.forClass(Target.class);