Merge pull request #235 from bsinno/feature_mgmt_api_target_token_address

Target securityToken and address fully manageable through MGMT API.
This commit is contained in:
Kai Zimmermann
2016-07-05 09:13:28 +02:00
committed by GitHub
10 changed files with 124 additions and 14 deletions

View File

@@ -21,6 +21,17 @@ public class MgmtTargetRequestBody {
@JsonProperty @JsonProperty
private String address; private String address;
@JsonProperty
private String securityToken;
public String getSecurityToken() {
return securityToken;
}
public void setSecurityToken(final String securityToken) {
this.securityToken = securityToken;
}
/** /**
* @return the name * @return the name
*/ */

View File

@@ -182,10 +182,11 @@ public final class MgmtTargetMapper {
} }
static Target fromRequest(final EntityFactory entityFactory, final MgmtTargetRequestBody targetRest) { static Target fromRequest(final EntityFactory entityFactory, final MgmtTargetRequestBody targetRest) {
final Target target = entityFactory.generateTarget(targetRest.getControllerId()); final Target target = entityFactory.generateTarget(targetRest.getControllerId(), targetRest.getSecurityToken());
target.setDescription(targetRest.getDescription()); target.setDescription(targetRest.getDescription());
target.setName(targetRest.getName()); target.setName(targetRest.getName());
target.getTargetInfo().setAddress(targetRest.getAddress()); target.getTargetInfo().setAddress(targetRest.getAddress());
return target; return target;
} }

View File

@@ -125,6 +125,13 @@ public class MgmtTargetResource implements MgmtTargetRestApi {
if (targetRest.getName() != null) { if (targetRest.getName() != null) {
existingTarget.setName(targetRest.getName()); existingTarget.setName(targetRest.getName());
} }
if (targetRest.getAddress() != null) {
existingTarget.getTargetInfo().setAddress(targetRest.getAddress());
}
if (targetRest.getSecurityToken() != null) {
existingTarget.setSecurityToken(targetRest.getSecurityToken());
}
final Target updateTarget = this.targetManagement.updateTarget(existingTarget); final Target updateTarget = this.targetManagement.updateTarget(existingTarget);
return new ResponseEntity<>(MgmtTargetMapper.toResponse(updateTarget), HttpStatus.OK); return new ResponseEntity<>(MgmtTargetMapper.toResponse(updateTarget), HttpStatus.OK);

View File

@@ -355,6 +355,54 @@ public class MgmtTargetResourceTest extends AbstractRestIntegrationTest {
assertThat(findTargetByControllerID.getName()).isEqualTo(knownNameNotModiy); assertThat(findTargetByControllerID.getName()).isEqualTo(knownNameNotModiy);
} }
@Test
@Description("Ensures that target update request is reflected by repository.")
public void updateTargetSecurityToken() throws Exception {
final String knownControllerId = "123";
final String knownNewToken = "6567576565";
final String knownNameNotModiy = "nameNotModiy";
final String body = new JSONObject().put("securityToken", knownNewToken).toString();
// prepare
final Target t = entityFactory.generateTarget(knownControllerId);
t.setName(knownNameNotModiy);
targetManagement.createTarget(t);
mvc.perform(put(MgmtRestConstants.TARGET_V1_REQUEST_MAPPING + "/" + knownControllerId).content(body)
.contentType(MediaType.APPLICATION_JSON)).andDo(MockMvcResultPrinter.print()).andExpect(status().isOk())
.andExpect(jsonPath("$.controllerId", equalTo(knownControllerId)))
.andExpect(jsonPath("$.securityToken", equalTo(knownNewToken)))
.andExpect(jsonPath("$.name", equalTo(knownNameNotModiy)));
final Target findTargetByControllerID = targetManagement.findTargetByControllerID(knownControllerId);
assertThat(findTargetByControllerID.getSecurityToken()).isEqualTo(knownNewToken);
assertThat(findTargetByControllerID.getName()).isEqualTo(knownNameNotModiy);
}
@Test
@Description("Ensures that target update request is reflected by repository.")
public void updateTargetAddress() throws Exception {
final String knownControllerId = "123";
final String knownNewAddress = "amqp://test123/foobar";
final String knownNameNotModiy = "nameNotModiy";
final String body = new JSONObject().put("address", knownNewAddress).toString();
// prepare
final Target t = entityFactory.generateTarget(knownControllerId);
t.setName(knownNameNotModiy);
targetManagement.createTarget(t);
mvc.perform(put(MgmtRestConstants.TARGET_V1_REQUEST_MAPPING + "/" + knownControllerId).content(body)
.contentType(MediaType.APPLICATION_JSON)).andDo(MockMvcResultPrinter.print()).andExpect(status().isOk())
.andExpect(jsonPath("$.controllerId", equalTo(knownControllerId)))
.andExpect(jsonPath("$.address", equalTo(knownNewAddress)))
.andExpect(jsonPath("$.name", equalTo(knownNameNotModiy)));
final Target findTargetByControllerID = targetManagement.findTargetByControllerID(knownControllerId);
assertThat(findTargetByControllerID.getTargetInfo().getAddress().toString()).isEqualTo(knownNewAddress);
assertThat(findTargetByControllerID.getName()).isEqualTo(knownNameNotModiy);
}
@Test @Test
@Description("Ensures that target query returns list of targets in defined format.") @Description("Ensures that target query returns list of targets in defined format.")
public void getTargetWithoutAddtionalRequestParameters() throws Exception { public void getTargetWithoutAddtionalRequestParameters() throws Exception {
@@ -679,7 +727,7 @@ public class MgmtTargetResourceTest extends AbstractRestIntegrationTest {
@Test @Test
public void createTargetsListReturnsSuccessful() throws Exception { public void createTargetsListReturnsSuccessful() throws Exception {
final Target test1 = entityFactory.generateTarget("id1"); final Target test1 = entityFactory.generateTarget("id1", "token");
test1.setDescription("testid1"); test1.setDescription("testid1");
test1.setName("testname1"); test1.setName("testname1");
test1.getTargetInfo().setAddress("amqp://test123/foobar"); test1.getTargetInfo().setAddress("amqp://test123/foobar");
@@ -696,7 +744,7 @@ public class MgmtTargetResourceTest extends AbstractRestIntegrationTest {
targets.add(test3); targets.add(test3);
final MvcResult mvcResult = mvc final MvcResult mvcResult = mvc
.perform(post("/rest/v1/targets/").content(JsonBuilder.targets(targets)) .perform(post("/rest/v1/targets/").content(JsonBuilder.targets(targets, true))
.contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON)) .contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON))
.andDo(MockMvcResultPrinter.print()).andExpect(status().isCreated()) .andDo(MockMvcResultPrinter.print()).andExpect(status().isCreated())
.andExpect(content().contentType(MediaType.APPLICATION_JSON)) .andExpect(content().contentType(MediaType.APPLICATION_JSON))
@@ -705,6 +753,7 @@ public class MgmtTargetResourceTest extends AbstractRestIntegrationTest {
.andExpect(jsonPath("[0].description", equalTo("testid1"))) .andExpect(jsonPath("[0].description", equalTo("testid1")))
.andExpect(jsonPath("[0].createdAt", not(equalTo(0)))) .andExpect(jsonPath("[0].createdAt", not(equalTo(0))))
.andExpect(jsonPath("[0].createdBy", equalTo("bumlux"))) .andExpect(jsonPath("[0].createdBy", equalTo("bumlux")))
.andExpect(jsonPath("[0].securityToken", equalTo("token")))
.andExpect(jsonPath("[0].address", equalTo("amqp://test123/foobar"))) .andExpect(jsonPath("[0].address", equalTo("amqp://test123/foobar")))
.andExpect(jsonPath("[1].name", equalTo("testname2"))) .andExpect(jsonPath("[1].name", equalTo("testname2")))
.andExpect(jsonPath("[1].createdBy", equalTo("bumlux"))) .andExpect(jsonPath("[1].createdBy", equalTo("bumlux")))
@@ -731,6 +780,9 @@ public class MgmtTargetResourceTest extends AbstractRestIntegrationTest {
assertThat(targetManagement.findTargetByControllerID("id1")).isNotNull(); assertThat(targetManagement.findTargetByControllerID("id1")).isNotNull();
assertThat(targetManagement.findTargetByControllerID("id1").getName()).isEqualTo("testname1"); assertThat(targetManagement.findTargetByControllerID("id1").getName()).isEqualTo("testname1");
assertThat(targetManagement.findTargetByControllerID("id1").getDescription()).isEqualTo("testid1"); assertThat(targetManagement.findTargetByControllerID("id1").getDescription()).isEqualTo("testid1");
assertThat(targetManagement.findTargetByControllerID("id1").getSecurityToken()).isEqualTo("token");
assertThat(targetManagement.findTargetByControllerID("id1").getTargetInfo().getAddress().toString())
.isEqualTo("amqp://test123/foobar");
assertThat(targetManagement.findTargetByControllerID("id2")).isNotNull(); assertThat(targetManagement.findTargetByControllerID("id2")).isNotNull();
assertThat(targetManagement.findTargetByControllerID("id2").getName()).isEqualTo("testname2"); assertThat(targetManagement.findTargetByControllerID("id2").getName()).isEqualTo("testname2");
assertThat(targetManagement.findTargetByControllerID("id2").getDescription()).isEqualTo("testid2"); assertThat(targetManagement.findTargetByControllerID("id2").getDescription()).isEqualTo("testid2");

View File

@@ -287,6 +287,7 @@ public interface EntityFactory {
/** /**
* Generates an empty {@link Target} without persisting it. * Generates an empty {@link Target} without persisting it.
* {@link Target#getSecurityToken()} is generated.
* *
* @param controllerID * @param controllerID
* of the {@link Target} * of the {@link Target}
@@ -295,6 +296,19 @@ public interface EntityFactory {
*/ */
Target generateTarget(@NotEmpty String controllerID); Target generateTarget(@NotEmpty String controllerID);
/**
* Generates an empty {@link Target} without persisting it.
*
* @param controllerID
* of the {@link Target}
* @param securityToken
* of the {@link Target} for authentication if enabled on tenant.
* Generates one if empty or <code>null</code>.
*
* @return {@link Target} object
*/
Target generateTarget(@NotEmpty String controllerID, @NotEmpty String securityToken);
/** /**
* Generates an empty {@link TargetFilterQuery} without persisting it. * Generates an empty {@link TargetFilterQuery} without persisting it.
* *

View File

@@ -58,4 +58,10 @@ public interface Target extends NamedEntity {
*/ */
String getSecurityToken(); String getSecurityToken();
/**
* @param token
* new securityToken
*/
void setSecurityToken(String token);
} }

View File

@@ -10,6 +10,7 @@ package org.eclipse.hawkbit.repository.jpa;
import java.util.Collection; import java.util.Collection;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.hawkbit.repository.EntityFactory; import org.eclipse.hawkbit.repository.EntityFactory;
import org.eclipse.hawkbit.repository.jpa.model.JpaAction; import org.eclipse.hawkbit.repository.jpa.model.JpaAction;
import org.eclipse.hawkbit.repository.jpa.model.JpaActionStatus; import org.eclipse.hawkbit.repository.jpa.model.JpaActionStatus;
@@ -87,6 +88,14 @@ public class JpaEntityFactory implements EntityFactory {
return new JpaTarget(controllerId); return new JpaTarget(controllerId);
} }
@Override
public Target generateTarget(final String controllerId, final String securityToken) {
if (StringUtils.isEmpty(securityToken)) {
return new JpaTarget(controllerId);
}
return new JpaTarget(controllerId, securityToken);
}
@Override @Override
public TargetTag generateTargetTag() { public TargetTag generateTargetTag() {
return new JpaTargetTag(); return new JpaTargetTag();

View File

@@ -115,9 +115,21 @@ public class JpaTarget extends AbstractJpaNamedEntity implements Persistable<Lon
* controller ID of the {@link Target} * controller ID of the {@link Target}
*/ */
public JpaTarget(final String controllerId) { public JpaTarget(final String controllerId) {
this(controllerId, SecurityTokenGeneratorHolder.getInstance().generateToken());
}
/**
* Constructor.
*
* @param controllerId
* controller ID of the {@link Target}
* @param securityToken
* for target authentication if enabled
*/
public JpaTarget(final String controllerId, final String securityToken) {
this.controllerId = controllerId; this.controllerId = controllerId;
setName(controllerId); setName(controllerId);
securityToken = SecurityTokenGeneratorHolder.getInstance().generateToken(); this.securityToken = securityToken;
targetInfo = new JpaTargetInfo(this); targetInfo = new JpaTargetInfo(this);
} }

View File

@@ -38,13 +38,13 @@ import org.eclipse.hawkbit.repository.jpa.model.JpaTarget;
import org.eclipse.hawkbit.repository.jpa.model.JpaTargetInfo; import org.eclipse.hawkbit.repository.jpa.model.JpaTargetInfo;
import org.eclipse.hawkbit.repository.jpa.model.JpaTargetTag; import org.eclipse.hawkbit.repository.jpa.model.JpaTargetTag;
import org.eclipse.hawkbit.repository.model.Action.Status; import org.eclipse.hawkbit.repository.model.Action.Status;
import org.eclipse.hawkbit.repository.test.util.WithSpringAuthorityRule;
import org.eclipse.hawkbit.repository.test.util.WithUser;
import org.eclipse.hawkbit.repository.model.DistributionSet; import org.eclipse.hawkbit.repository.model.DistributionSet;
import org.eclipse.hawkbit.repository.model.Tag; import org.eclipse.hawkbit.repository.model.Tag;
import org.eclipse.hawkbit.repository.model.Target; import org.eclipse.hawkbit.repository.model.Target;
import org.eclipse.hawkbit.repository.model.TargetIdName; import org.eclipse.hawkbit.repository.model.TargetIdName;
import org.eclipse.hawkbit.repository.model.TargetTag; import org.eclipse.hawkbit.repository.model.TargetTag;
import org.eclipse.hawkbit.repository.test.util.WithSpringAuthorityRule;
import org.eclipse.hawkbit.repository.test.util.WithUser;
import org.junit.Test; import org.junit.Test;
import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.PageRequest;
@@ -61,7 +61,7 @@ public class TargetManagementTest extends AbstractJpaIntegrationTest {
@Test @Test
@Description("Ensures that retrieving the target security is only permitted with the necessary permissions.") @Description("Ensures that retrieving the target security is only permitted with the necessary permissions.")
public void getTargetSecurityTokenOnlyWithCorrectPermission() throws Exception { public void getTargetSecurityTokenOnlyWithCorrectPermission() throws Exception {
final Target createdTarget = targetManagement.createTarget(new JpaTarget("targetWithSecurityToken")); final Target createdTarget = targetManagement.createTarget(new JpaTarget("targetWithSecurityToken", "token"));
// retrieve security token only with READ_TARGET_SEC_TOKEN permission // retrieve security token only with READ_TARGET_SEC_TOKEN permission
final String securityTokenWithReadPermission = securityRule.runAs(WithSpringAuthorityRule final String securityTokenWithReadPermission = securityRule.runAs(WithSpringAuthorityRule
@@ -80,7 +80,7 @@ public class TargetManagementTest extends AbstractJpaIntegrationTest {
return createdTarget.getSecurityToken(); return createdTarget.getSecurityToken();
}); });
assertThat(createdTarget.getSecurityToken()).isNotNull(); assertThat(createdTarget.getSecurityToken()).isEqualTo("token");
assertThat(securityTokenWithReadPermission).isNotNull(); assertThat(securityTokenWithReadPermission).isNotNull();
assertThat(securityTokenAsSystemCode).isNotNull(); assertThat(securityTokenAsSystemCode).isNotNull();

View File

@@ -367,11 +367,7 @@ public abstract class JsonBuilder {
} }
/** public static String targets(final List<Target> targets, final boolean withToken) {
* @param targets
* @return
*/
public static String targets(final List<Target> targets) {
final StringBuilder builder = new StringBuilder(); final StringBuilder builder = new StringBuilder();
builder.append("["); builder.append("[");
@@ -381,10 +377,12 @@ public abstract class JsonBuilder {
final String address = target.getTargetInfo().getAddress() != null final String address = target.getTargetInfo().getAddress() != null
? target.getTargetInfo().getAddress().toString() : null; ? target.getTargetInfo().getAddress().toString() : null;
final String token = withToken ? target.getSecurityToken() : null;
builder.append(new JSONObject().put("controllerId", target.getControllerId()) builder.append(new JSONObject().put("controllerId", target.getControllerId())
.put("description", target.getDescription()).put("name", target.getName()).put("createdAt", "0") .put("description", target.getDescription()).put("name", target.getName()).put("createdAt", "0")
.put("updatedAt", "0").put("createdBy", "fghdfkjghdfkjh").put("updatedBy", "fghdfkjghdfkjh") .put("updatedAt", "0").put("createdBy", "fghdfkjghdfkjh").put("updatedBy", "fghdfkjghdfkjh")
.put("address", address).toString()); .put("address", address).put("securityToken", token).toString());
} catch (final Exception e) { } catch (final Exception e) {
e.printStackTrace(); e.printStackTrace();
} }