use of org.eclipse.hono.util.Adapter in project hono by eclipse.
the class HttpTestBase method testUploadMessagesUsingClientCertificate.
/**
* Verifies that a number of messages uploaded to Hono's HTTP adapter using client certificate based authentication
* can be successfully consumed via the AMQP Messaging Network.
*
* @param ctx The test context.
* @throws InterruptedException if the test fails.
*/
@Test
public void testUploadMessagesUsingClientCertificate(final VertxTestContext ctx) throws InterruptedException {
final VertxTestContext setup = new VertxTestContext();
final MultiMap requestHeaders = MultiMap.caseInsensitiveMultiMap().add(HttpHeaders.CONTENT_TYPE, "text/plain").add(HttpHeaders.ORIGIN, ORIGIN_URI);
helper.getCertificate(deviceCert.certificatePath()).compose(cert -> {
final var tenant = Tenants.createTenantForTrustAnchor(cert);
return helper.registry.addDeviceForTenant(tenantId, tenant, deviceId, cert);
}).onComplete(setup.succeedingThenComplete());
assertThat(setup.awaitCompletion(5, TimeUnit.SECONDS)).isTrue();
if (setup.failed()) {
ctx.failNow(setup.causeOfFailure());
return;
}
testUploadMessages(ctx, tenantId, count -> {
return httpClientWithClientCert.create(getEndpointUri(), Buffer.buffer("hello " + count), requestHeaders, ResponsePredicate.status(HttpURLConnection.HTTP_ACCEPTED)).compose(this::verifyAccessControlExposedHeaders);
});
}
use of org.eclipse.hono.util.Adapter in project hono by eclipse.
the class HttpTestBase method testUploadMessageFailsForDisabledDevice.
/**
* Verifies that the HTTP adapter rejects messages from a disabled device with a 404.
*
* @param ctx The test context
*/
@Test
@Timeout(timeUnit = TimeUnit.SECONDS, value = 20)
public void testUploadMessageFailsForDisabledDevice(final VertxTestContext ctx) {
// GIVEN a disabled device
final Tenant tenant = new Tenant();
final Device device = new Device().setEnabled(Boolean.FALSE);
helper.registry.addDeviceForTenant(tenantId, tenant, deviceId, device, PWD).compose(ok -> {
// WHEN the device tries to upload a message
final MultiMap requestHeaders = MultiMap.caseInsensitiveMultiMap().add(HttpHeaders.CONTENT_TYPE, "text/plain").add(HttpHeaders.AUTHORIZATION, authorization);
return httpClient.create(getEndpointUri(), Buffer.buffer("hello"), requestHeaders, ResponsePredicate.status(HttpURLConnection.HTTP_NOT_FOUND));
}).onComplete(ctx.succeedingThenComplete());
}
use of org.eclipse.hono.util.Adapter in project hono by eclipse.
the class TenantApiTests method testGetTenant.
/**
* Verifies that an existing tenant can be retrieved.
*
* @param ctx The vert.x test context.
*/
@Timeout(value = 5, timeUnit = TimeUnit.SECONDS)
@Test
public void testGetTenant(final VertxTestContext ctx) {
final JsonObject defaults = new JsonObject().put("ttl", 30);
final Map<String, Object> httpAdapterExtensions = Map.of("deployment", Map.of("maxInstances", 4));
final ResourceLimits resourceLimits = new ResourceLimits().setMaxConnections(100000).setMaxTtl(30L).setDataVolume(new DataVolume(Instant.parse("2019-07-27T14:30:00Z"), new ResourceLimitsPeriod(PeriodMode.days).setNoOfDays(30), 2147483648L));
final String tenantId = getHelper().getRandomTenantId();
final Tenant tenant = new Tenant();
tenant.setEnabled(true);
tenant.setResourceLimits(resourceLimits);
tenant.setDefaults(defaults.getMap());
tenant.putExtension("customer", "ACME Inc.");
tenant.addAdapterConfig(new Adapter(Constants.PROTOCOL_ADAPTER_TYPE_MQTT).setEnabled(true).setDeviceAuthenticationRequired(false)).addAdapterConfig(new Adapter(Constants.PROTOCOL_ADAPTER_TYPE_HTTP).setEnabled(true).setDeviceAuthenticationRequired(true).setExtensions(httpAdapterExtensions));
// expected tenant object
final TenantObject expectedTenantObject = TenantObject.from(tenantId, true).setDefaults(defaults).setResourceLimits(resourceLimits).addAdapter(new org.eclipse.hono.util.Adapter(Constants.PROTOCOL_ADAPTER_TYPE_MQTT).setEnabled(Boolean.TRUE).setDeviceAuthenticationRequired(Boolean.FALSE)).addAdapter(new org.eclipse.hono.util.Adapter(Constants.PROTOCOL_ADAPTER_TYPE_HTTP).setEnabled(Boolean.TRUE).setDeviceAuthenticationRequired(Boolean.TRUE).setExtensions(httpAdapterExtensions));
getHelper().registry.addTenant(tenantId, tenant).compose(ok -> getAdminClient().get(tenantId, NoopSpan.INSTANCE.context())).onComplete(ctx.succeeding(tenantObject -> {
ctx.verify(() -> {
assertThat(tenantObject.getDefaults()).isEqualTo(expectedTenantObject.getDefaults());
Assertions.assertThat(tenantObject.getAdapters()).usingRecursiveFieldByFieldElementComparator().containsAll(expectedTenantObject.getAdapters());
assertThat(tenantObject.getResourceLimits().getMaxConnections()).isEqualTo(expectedTenantObject.getResourceLimits().getMaxConnections());
assertThat(tenantObject.getResourceLimits().getMaxTtl()).isEqualTo(expectedTenantObject.getResourceLimits().getMaxTtl());
assertThat(tenantObject.getResourceLimits().getDataVolume().getMaxBytes()).isEqualTo(expectedTenantObject.getResourceLimits().getDataVolume().getMaxBytes());
assertThat(tenantObject.getResourceLimits().getDataVolume().getEffectiveSince()).isEqualTo(expectedTenantObject.getResourceLimits().getDataVolume().getEffectiveSince());
assertThat(tenantObject.getResourceLimits().getDataVolume().getPeriod().getMode()).isEqualTo(expectedTenantObject.getResourceLimits().getDataVolume().getPeriod().getMode());
assertThat(tenantObject.getResourceLimits().getDataVolume().getPeriod().getNoOfDays()).isEqualTo(expectedTenantObject.getResourceLimits().getDataVolume().getPeriod().getNoOfDays());
final JsonObject extensions = tenantObject.getProperty("ext", JsonObject.class);
assertThat(extensions.getString("customer")).isEqualTo("ACME Inc.");
// implicitly added by DeviceRegistryHttpClient
assertThat(extensions.getString(TenantConstants.FIELD_EXT_MESSAGING_TYPE)).isEqualTo(IntegrationTestSupport.getConfiguredMessagingType().name());
});
ctx.completeNow();
}));
}
use of org.eclipse.hono.util.Adapter in project hono by eclipse.
the class MqttConnectionIT method testConnectFailsForDisabledDevice.
/**
* Verifies that the adapter rejects connection attempts from devices for which credentials exist but the device is
* disabled.
*
* @param ctx The test context
*/
@Test
public void testConnectFailsForDisabledDevice(final VertxTestContext ctx) {
final Tenant tenant = new Tenant();
helper.registry.addTenant(tenantId, tenant).compose(ok -> {
final var device = new Device();
device.setEnabled(false);
return helper.registry.registerDevice(tenantId, deviceId, device);
}).compose(ok -> {
final PasswordCredential secret = IntegrationTestSupport.createPasswordCredential(deviceId, password);
return helper.registry.addCredentials(tenantId, deviceId, Collections.singleton(secret));
}).compose(ok -> connectToAdapter(IntegrationTestSupport.getUsername(deviceId, tenantId), password)).onComplete(ctx.failing(t -> {
// THEN the connection is refused with a NOT_AUTHORIZED code
ctx.verify(() -> {
assertThat(t).isInstanceOf(MqttConnectionException.class);
assertThat(((MqttConnectionException) t).code()).isEqualTo(MqttConnectReturnCode.CONNECTION_REFUSED_NOT_AUTHORIZED);
});
ctx.completeNow();
}));
}
use of org.eclipse.hono.util.Adapter in project hono by eclipse.
the class MqttConnectionIT method testConnectFailsForDisabledCredentials.
/**
* Verifies that the adapter rejects connection attempts from devices for which credentials exist but are disabled.
*
* @param ctx The test context
*/
@Test
public void testConnectFailsForDisabledCredentials(final VertxTestContext ctx) {
helper.registry.addTenant(tenantId).compose(ok -> {
return helper.registry.registerDevice(tenantId, deviceId);
}).compose(ok -> {
final PasswordCredential secret = IntegrationTestSupport.createPasswordCredential(deviceId, password);
secret.setEnabled(false);
return helper.registry.addCredentials(tenantId, deviceId, Collections.singleton(secret));
}).compose(ok -> connectToAdapter(IntegrationTestSupport.getUsername(deviceId, tenantId), password)).onComplete(ctx.failing(t -> {
// THEN the connection is refused with a NOT_AUTHORIZED code
ctx.verify(() -> {
assertThat(t).isInstanceOf(MqttConnectionException.class);
assertThat(((MqttConnectionException) t).code()).isEqualTo(MqttConnectReturnCode.CONNECTION_REFUSED_BAD_USER_NAME_OR_PASSWORD);
});
ctx.completeNow();
}));
}
Aggregations