Search in sources :

Example 6 with CredentialsObject

use of org.eclipse.hono.util.CredentialsObject in project hono by eclipse.

the class FileBasedCredentialsServiceTest method testLoadCredentialsCanReadOutputOfSaveToFile.

/**
 * Verifies that the file written by the registry when persisting the registry's contents can
 * be loaded in again.
 *
 * @param ctx The vert.x test context.
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testLoadCredentialsCanReadOutputOfSaveToFile(final TestContext ctx) {
    // GIVEN a service configured to persist credentials to file
    // that contains some credentials
    props.setFilename(FILE_NAME);
    props.setSaveToFile(true);
    when(fileSystem.existsBlocking(FILE_NAME)).thenReturn(Boolean.TRUE);
    final Async add = ctx.async(2);
    final CredentialsObject hashedPassword = CredentialsObject.fromHashedPassword("4700", "bumlux", "secret", "sha-512", null, null, null);
    final CredentialsObject psk = CredentialsObject.fromPresharedKey("4711", "sensor1", "sharedkey".getBytes(StandardCharsets.UTF_8), null, null);
    svc.add(Constants.DEFAULT_TENANT, JsonObject.mapFrom(psk), ctx.asyncAssertSuccess(s -> {
        ctx.assertEquals(HttpURLConnection.HTTP_CREATED, s.getStatus());
        add.countDown();
    }));
    svc.add("OTHER_TENANT", JsonObject.mapFrom(hashedPassword), ctx.asyncAssertSuccess(s -> {
        ctx.assertEquals(HttpURLConnection.HTTP_CREATED, s.getStatus());
        add.countDown();
    }));
    add.await(2000);
    // WHEN saving the registry content to the file and clearing the registry
    final Async write = ctx.async();
    doAnswer(invocation -> {
        Handler handler = invocation.getArgument(2);
        handler.handle(Future.succeededFuture());
        write.complete();
        return null;
    }).when(fileSystem).writeFile(eq(FILE_NAME), any(Buffer.class), any(Handler.class));
    svc.saveToFile();
    write.await(2000);
    ArgumentCaptor<Buffer> buffer = ArgumentCaptor.forClass(Buffer.class);
    verify(fileSystem).writeFile(eq(FILE_NAME), buffer.capture(), any(Handler.class));
    svc.clear();
    assertNotRegistered(svc, Constants.DEFAULT_PATH_SEPARATOR, "sensor1", CredentialsConstants.SECRETS_TYPE_PRESHARED_KEY, ctx);
    // THEN the credentials can be loaded back in from the file
    final Async read = ctx.async();
    doAnswer(invocation -> {
        Handler handler = invocation.getArgument(1);
        handler.handle(Future.succeededFuture(buffer.getValue()));
        read.complete();
        return null;
    }).when(fileSystem).readFile(eq(FILE_NAME), any(Handler.class));
    svc.loadCredentials();
    read.await(2000);
    assertRegistered(svc, Constants.DEFAULT_TENANT, "sensor1", CredentialsConstants.SECRETS_TYPE_PRESHARED_KEY, ctx);
    assertRegistered(svc, "OTHER_TENANT", "bumlux", CredentialsConstants.SECRETS_TYPE_HASHED_PASSWORD, ctx);
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) HttpURLConnection(java.net.HttpURLConnection) TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) RunWith(org.junit.runner.RunWith) Constants(org.eclipse.hono.util.Constants) Context(io.vertx.core.Context) Assert.assertThat(org.junit.Assert.assertThat) ArgumentCaptor(org.mockito.ArgumentCaptor) EventBus(io.vertx.core.eventbus.EventBus) Matchers.eq(org.mockito.Matchers.eq) JsonObject(io.vertx.core.json.JsonObject) Before(org.junit.Before) Vertx(io.vertx.core.Vertx) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Future(io.vertx.core.Future) StandardCharsets(java.nio.charset.StandardCharsets) CredentialsConstants(org.eclipse.hono.util.CredentialsConstants) Matchers.any(org.mockito.Matchers.any) Mockito(org.mockito.Mockito) JsonArray(io.vertx.core.json.JsonArray) Buffer(io.vertx.core.buffer.Buffer) FileSystem(io.vertx.core.file.FileSystem) CredentialsService(org.eclipse.hono.service.credentials.CredentialsService) Handler(io.vertx.core.Handler) CredentialsObject(org.eclipse.hono.util.CredentialsObject) Buffer(io.vertx.core.buffer.Buffer) Async(io.vertx.ext.unit.Async) CredentialsObject(org.eclipse.hono.util.CredentialsObject) Handler(io.vertx.core.Handler) Test(org.junit.Test)

Example 7 with CredentialsObject

use of org.eclipse.hono.util.CredentialsObject in project hono by eclipse.

the class BaseCredentialsService method processAddRequest.

private Future<EventBusMessage> processAddRequest(final EventBusMessage request) {
    final String tenantId = request.getTenant();
    final CredentialsObject payload = Optional.ofNullable(request.getJsonPayload()).map(json -> json.mapTo(CredentialsObject.class)).orElse(null);
    if (tenantId == null || payload == null) {
        return Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST));
    } else if (payload.isValid()) {
        final Future<CredentialsResult<JsonObject>> result = Future.future();
        add(tenantId, JsonObject.mapFrom(payload), result.completer());
        return result.map(res -> {
            return request.getResponse(res.getStatus()).setDeviceId(payload.getDeviceId()).setCacheDirective(res.getCacheDirective());
        });
    } else {
        return Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST));
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) CredentialsResult(org.eclipse.hono.util.CredentialsResult) TenantConstants(org.eclipse.hono.util.TenantConstants) ClientErrorException(org.eclipse.hono.client.ClientErrorException) EventBusMessage(org.eclipse.hono.util.EventBusMessage) Future(io.vertx.core.Future) CredentialsConstants(org.eclipse.hono.util.CredentialsConstants) Objects(java.util.Objects) EventBusService(org.eclipse.hono.service.EventBusService) Optional(java.util.Optional) JsonObject(io.vertx.core.json.JsonObject) AsyncResult(io.vertx.core.AsyncResult) Handler(io.vertx.core.Handler) CredentialsObject(org.eclipse.hono.util.CredentialsObject) CredentialsObject(org.eclipse.hono.util.CredentialsObject) ClientErrorException(org.eclipse.hono.client.ClientErrorException) Future(io.vertx.core.Future) JsonObject(io.vertx.core.json.JsonObject)

Example 8 with CredentialsObject

use of org.eclipse.hono.util.CredentialsObject in project hono by eclipse.

the class AbstractDeviceCredentialsTest method testValidateFailsIfCredentialsAreDisabled.

/**
 * Verifies that credentials validation fails if the credentials on record are disabled.
 */
@Test
public void testValidateFailsIfCredentialsAreDisabled() {
    final AbstractDeviceCredentials creds = getDeviceCredentials("type", "identity", true);
    final CredentialsObject credentialsOnRecord = getCredentialsObject("type", "identity", "device", false).addSecret(CredentialsObject.emptySecret(Instant.now().minusSeconds(120), null));
    assertFalse(creds.validate(credentialsOnRecord));
}
Also used : CredentialsObject(org.eclipse.hono.util.CredentialsObject) Test(org.junit.Test)

Example 9 with CredentialsObject

use of org.eclipse.hono.util.CredentialsObject in project hono by eclipse.

the class BaseCredentialsServiceTest method testGetFailsForMissingAuthId.

/**
 * Verifies that the base service fails a request for getting credentials
 * with a 400 error code if the authentication identifier is missing.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testGetFailsForMissingAuthId(final TestContext ctx) {
    // GIVEN a request for getting credentials that does not specify an auth ID
    final CredentialsObject malformedPayload = new CredentialsObject().setType("my-type").addSecret(CredentialsObject.emptySecret(null, null));
    final EventBusMessage request = createRequestForPayload(CredentialsConstants.CredentialsAction.get, JsonObject.mapFrom(malformedPayload));
    // WHEN processing the request
    service.processRequest(request).setHandler(ctx.asyncAssertFailure(t -> {
        // THEN the response contains a 400 error code
        ctx.assertEquals(HttpURLConnection.HTTP_BAD_REQUEST, ((ServiceInvocationException) t).getErrorCode());
    }));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) TestContext(io.vertx.ext.unit.TestContext) BeforeClass(org.junit.BeforeClass) CredentialsResult(org.eclipse.hono.util.CredentialsResult) RunWith(org.junit.runner.RunWith) Test(org.junit.Test) EventBusMessage(org.eclipse.hono.util.EventBusMessage) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) ServiceConfigProperties(org.eclipse.hono.config.ServiceConfigProperties) Future(io.vertx.core.Future) CredentialsConstants(org.eclipse.hono.util.CredentialsConstants) Timeout(org.junit.rules.Timeout) JsonObject(io.vertx.core.json.JsonObject) AsyncResult(io.vertx.core.AsyncResult) Handler(io.vertx.core.Handler) CredentialsObject(org.eclipse.hono.util.CredentialsObject) EventBusMessage(org.eclipse.hono.util.EventBusMessage) CredentialsObject(org.eclipse.hono.util.CredentialsObject) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) Test(org.junit.Test)

Aggregations

CredentialsObject (org.eclipse.hono.util.CredentialsObject)9 Test (org.junit.Test)7 Future (io.vertx.core.Future)5 Handler (io.vertx.core.Handler)5 JsonObject (io.vertx.core.json.JsonObject)5 HttpURLConnection (java.net.HttpURLConnection)5 CredentialsConstants (org.eclipse.hono.util.CredentialsConstants)5 AsyncResult (io.vertx.core.AsyncResult)4 CredentialsResult (org.eclipse.hono.util.CredentialsResult)4 EventBusMessage (org.eclipse.hono.util.EventBusMessage)4 TestContext (io.vertx.ext.unit.TestContext)3 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)3 RunWith (org.junit.runner.RunWith)3 Objects (java.util.Objects)2 Optional (java.util.Optional)2 ClientErrorException (org.eclipse.hono.client.ClientErrorException)2 ServiceInvocationException (org.eclipse.hono.client.ServiceInvocationException)2 ServiceConfigProperties (org.eclipse.hono.config.ServiceConfigProperties)2 EventBusService (org.eclipse.hono.service.EventBusService)2 TenantConstants (org.eclipse.hono.util.TenantConstants)2