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());
}));
}
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
}));
}
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);
}
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);
}
}
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);
}
}
Aggregations