Search in sources :

Example 1 with TenantObject

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

the class HttpTestBase method testUploadMessageFailsForDisabledTenant.

/**
 * Verifies that the HTTP adapter rejects messages from a device
 * that belongs to a tenant for which the HTTP adapter has been disabled.
 *
 * @param ctx The test context
 */
@Test
public void testUploadMessageFailsForDisabledTenant(final TestContext ctx) {
    // GIVEN a tenant for which the HTTP adapter is disabled
    final String tenantId = helper.getRandomTenantId();
    final String deviceId = helper.getRandomDeviceId(tenantId);
    final JsonObject adapterDetailsHttp = new JsonObject().put(TenantConstants.FIELD_ADAPTERS_TYPE, Constants.PROTOCOL_ADAPTER_TYPE_HTTP).put(TenantConstants.FIELD_ENABLED, Boolean.FALSE);
    final TenantObject tenant = TenantObject.from(tenantId, true);
    tenant.addAdapterConfiguration(adapterDetailsHttp);
    final Async setup = ctx.async();
    helper.registry.addTenant(JsonObject.mapFrom(tenant)).compose(ok -> helper.registry.registerDevice(tenantId, deviceId)).setHandler(ctx.asyncAssertSuccess(ok -> setup.complete()));
    setup.await();
    // WHEN a device that belongs to the tenant uploads a message
    send(tenantId, deviceId, Buffer.buffer("hello")).setHandler(ctx.asyncAssertFailure(t -> {
        // THEN the message gets rejected by the HTTP adapter
        ctx.assertEquals(HttpURLConnection.HTTP_FORBIDDEN, ((ServiceInvocationException) t).getErrorCode());
    }));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) BeforeClass(org.junit.BeforeClass) TenantConstants(org.eclipse.hono.util.TenantConstants) LoggerFactory(org.slf4j.LoggerFactory) MessageConsumer(org.eclipse.hono.client.MessageConsumer) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) Constants(org.eclipse.hono.util.Constants) IntegrationTestSupport(org.eclipse.hono.tests.IntegrationTestSupport) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) After(org.junit.After) Timeout(org.junit.rules.Timeout) Message(org.apache.qpid.proton.message.Message) JsonObject(io.vertx.core.json.JsonObject) AfterClass(org.junit.AfterClass) Logger(org.slf4j.Logger) Vertx(io.vertx.core.Vertx) Test(org.junit.Test) MessageHelper(org.eclipse.hono.util.MessageHelper) Future(io.vertx.core.Future) TenantObject(org.eclipse.hono.util.TenantObject) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) CountDownLatch(java.util.concurrent.CountDownLatch) Rule(org.junit.Rule) Buffer(io.vertx.core.buffer.Buffer) CrudHttpClient(org.eclipse.hono.tests.CrudHttpClient) TenantObject(org.eclipse.hono.util.TenantObject) Async(io.vertx.ext.unit.Async) JsonObject(io.vertx.core.json.JsonObject) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) Test(org.junit.Test)

Example 2 with TenantObject

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

the class TelemetryMqttIT method testConnectFailsForDisabledTenant.

/**
 * Verifies that the adapter rejects connection attempts from devices
 * that belong to a tenant for which the adapter has been disabled.
 *
 * @param ctx The test context
 */
@Test
public void testConnectFailsForDisabledTenant(final TestContext ctx) {
    // GIVEN a tenant for which the HTTP adapter is disabled
    final String tenantId = helper.getRandomTenantId();
    final String deviceId = helper.getRandomDeviceId(tenantId);
    final String password = "secret";
    final JsonObject adapterDetailsHttp = new JsonObject().put(TenantConstants.FIELD_ADAPTERS_TYPE, Constants.PROTOCOL_ADAPTER_TYPE_MQTT).put(TenantConstants.FIELD_ENABLED, Boolean.FALSE);
    final TenantObject tenant = TenantObject.from(tenantId, true);
    tenant.addAdapterConfiguration(adapterDetailsHttp);
    helper.registry.addDeviceForTenant(tenant, deviceId, password).compose(ok -> {
        final Future<MqttConnAckMessage> result = Future.future();
        final MqttClientOptions options = new MqttClientOptions().setUsername(IntegrationTestSupport.getUsername(deviceId, tenantId)).setPassword(password);
        mqttClient = MqttClient.create(VERTX, options);
        // WHEN a device that belongs to the tenant tries to connect to the adapter
        mqttClient.connect(IntegrationTestSupport.MQTT_PORT, IntegrationTestSupport.MQTT_HOST, result.completer());
        return result;
    }).setHandler(ctx.asyncAssertFailure(t -> {
    // THEN the connection attempt gets rejected
    }));
}
Also used : TestContext(io.vertx.ext.unit.TestContext) MqttConnAckMessage(io.vertx.mqtt.messages.MqttConnAckMessage) MqttQoS(io.netty.handler.codec.mqtt.MqttQoS) TenantConstants(org.eclipse.hono.util.TenantConstants) RunWith(org.junit.runner.RunWith) MessageConsumer(org.eclipse.hono.client.MessageConsumer) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Constants(org.eclipse.hono.util.Constants) Future(io.vertx.core.Future) TenantObject(org.eclipse.hono.util.TenantObject) Consumer(java.util.function.Consumer) MqttClientOptions(io.vertx.mqtt.MqttClientOptions) TelemetryConstants(org.eclipse.hono.util.TelemetryConstants) IntegrationTestSupport(org.eclipse.hono.tests.IntegrationTestSupport) Buffer(io.vertx.core.buffer.Buffer) MqttClient(io.vertx.mqtt.MqttClient) Message(org.apache.qpid.proton.message.Message) JsonObject(io.vertx.core.json.JsonObject) AsyncResult(io.vertx.core.AsyncResult) Handler(io.vertx.core.Handler) TenantObject(org.eclipse.hono.util.TenantObject) MqttClientOptions(io.vertx.mqtt.MqttClientOptions) JsonObject(io.vertx.core.json.JsonObject) Future(io.vertx.core.Future) Test(org.junit.Test)

Example 3 with TenantObject

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

the class TenantHttpIT method buildTenantPayload.

/**
 * Creates a tenant object for a tenantId.
 * <p>
 * The tenant object created contains configurations for the http and the mqtt adapter.
 *
 * @param tenantId The tenant identifier.
 * @return The tenant object.
 */
private static JsonObject buildTenantPayload(final String tenantId) {
    final JsonArray adapterConfigs = new JsonArray();
    adapterConfigs.add(TenantObject.newAdapterConfig(Constants.PROTOCOL_ADAPTER_TYPE_HTTP, true).put(TenantConstants.FIELD_ADAPTERS_DEVICE_AUTHENTICATION_REQUIRED, true));
    adapterConfigs.add(TenantObject.newAdapterConfig(Constants.PROTOCOL_ADAPTER_TYPE_MQTT, true).put(TenantConstants.FIELD_ADAPTERS_DEVICE_AUTHENTICATION_REQUIRED, true));
    adapterConfigs.add(TenantObject.newAdapterConfig("custom", false).put("options", new JsonObject().put("maxInstances", 4)));
    final TenantObject tenantPayload = TenantObject.from(tenantId, true).setProperty("plan", "unlimited").setAdapterConfigurations(adapterConfigs);
    return JsonObject.mapFrom(tenantPayload);
}
Also used : JsonArray(io.vertx.core.json.JsonArray) TenantObject(org.eclipse.hono.util.TenantObject) JsonObject(io.vertx.core.json.JsonObject)

Example 4 with TenantObject

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

the class FileBasedTenantService method update.

/**
 * Updates a tenant.
 *
 * @param tenantId The identifier of the tenant.
 * @param tenantSpec The information to update the tenant with.
 * @return The outcome of the operation indicating success or failure.
 * @throws NullPointerException if any of the parameters are {@code null}.
 */
public TenantResult<JsonObject> update(final String tenantId, final JsonObject tenantSpec) {
    Objects.requireNonNull(tenantId);
    Objects.requireNonNull(tenantSpec);
    if (getConfig().isModificationEnabled()) {
        if (tenants.containsKey(tenantId)) {
            try {
                final TenantObject tenant = tenantSpec.mapTo(TenantObject.class);
                tenant.setTenantId(tenantId);
                tenants.put(tenantId, tenant);
                dirty = true;
                return TenantResult.from(HttpURLConnection.HTTP_NO_CONTENT);
            } catch (IllegalArgumentException e) {
                return TenantResult.from(HttpURLConnection.HTTP_BAD_REQUEST);
            }
        } else {
            return TenantResult.from(HttpURLConnection.HTTP_NOT_FOUND);
        }
    } else {
        return TenantResult.from(HttpURLConnection.HTTP_FORBIDDEN);
    }
}
Also used : TenantObject(org.eclipse.hono.util.TenantObject)

Example 5 with TenantObject

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

the class FileBasedTenantService method addTenant.

private void addTenant(final JsonObject tenant) {
    try {
        final TenantObject tenantObject = tenant.mapTo(TenantObject.class);
        log.debug("loading tenant [{}]", tenantObject.getTenantId());
        tenants.put(tenantObject.getTenantId(), tenantObject);
    } catch (IllegalArgumentException e) {
        log.warn("cannot deserialize tenant", e);
    }
}
Also used : TenantObject(org.eclipse.hono.util.TenantObject)

Aggregations

TenantObject (org.eclipse.hono.util.TenantObject)13 JsonObject (io.vertx.core.json.JsonObject)9 Message (org.apache.qpid.proton.message.Message)8 Test (org.junit.Test)8 Buffer (io.vertx.core.buffer.Buffer)7 Future (io.vertx.core.Future)6 TestContext (io.vertx.ext.unit.TestContext)6 Vertx (io.vertx.core.Vertx)5 Async (io.vertx.ext.unit.Async)5 HttpURLConnection (java.net.HttpURLConnection)5 TenantConstants (org.eclipse.hono.util.TenantConstants)5 Rule (org.junit.Rule)5 Timeout (org.junit.rules.Timeout)5 Handler (io.vertx.core.Handler)4 TimeUnit (java.util.concurrent.TimeUnit)4 Consumer (java.util.function.Consumer)4 MessageConsumer (org.eclipse.hono.client.MessageConsumer)4 IntegrationTestSupport (org.eclipse.hono.tests.IntegrationTestSupport)4 AfterClass (org.junit.AfterClass)4 Logger (org.slf4j.Logger)4