use of org.eclipse.hono.service.management.credentials.X509CertificateSecret in project hono by eclipse.
the class CredentialsManagementIT method testAddCredentialsSucceeds.
/**
* Verifies that a newly added device has an empty set of credentials and that the
* service successfully adds arbitrary types of credentials.
*
* @param context The vert.x test context.
*/
@Test
public void testAddCredentialsSucceeds(final VertxTestContext context) {
final PasswordCredential pwdCredential = IntegrationTestSupport.createPasswordCredential(authId, "thePassword");
pwdCredential.getExtensions().put("client-id", "MQTT-client-2384236854");
final PskCredential pskCredential = IntegrationTestSupport.createPskCredentials("psk-id", "psk-key");
final var x509Credential = X509CertificateCredential.fromSubjectDn("emailAddress=foo@bar.com, CN=foo, O=bar", List.of(new X509CertificateSecret()));
x509Credential.setComment("non-standard attribute type");
final List<CommonCredential> credentials = List.of(pwdCredential, pskCredential, x509Credential);
registry.getCredentials(tenantId, deviceId).compose(httpResponse -> {
context.verify(() -> {
assertResourceVersionHasChanged(resourceVersion, httpResponse.headers());
assertThat(httpResponse.bodyAsJsonArray()).isEmpty();
});
return registry.addCredentials(tenantId, deviceId, credentials);
}).compose(httpResponse -> {
context.verify(() -> assertResourceVersionHasChanged(resourceVersion, httpResponse.headers()));
return registry.getCredentials(tenantId, deviceId);
}).onComplete(context.succeeding(httpResponse -> {
context.verify(() -> {
final CommonCredential[] credsOnRecord = httpResponse.bodyAsJson(CommonCredential[].class);
assertThat(credsOnRecord).hasLength(3);
Arrays.stream(credsOnRecord).forEach(creds -> {
assertThat(creds.getExtensions().get("device-id")).isNull();
if (creds instanceof PasswordCredential) {
assertThat(creds.getExtensions().get("client-id")).isEqualTo("MQTT-client-2384236854");
} else if (creds instanceof X509CertificateCredential) {
assertThat(creds.getComment()).isEqualTo("non-standard attribute type");
}
creds.getSecrets().forEach(secret -> {
assertThat(secret.isEnabled()).isTrue();
assertThat(secret.getId()).isNotNull();
});
});
});
context.completeNow();
}));
}
use of org.eclipse.hono.service.management.credentials.X509CertificateSecret in project hono by eclipse.
the class CredentialsManagementIT method testGetAllCredentialsForDeviceSucceeds.
/**
* Verifies that the service returns all credentials registered for a given device regardless of
* authentication identifier and type.
* <p>
* The returned JsonArray must contain exactly the same credentials as originally added.
*
* @param context The vert.x test context.
*/
@Test
public void testGetAllCredentialsForDeviceSucceeds(final VertxTestContext context) {
final List<CommonCredential> credentialsListToAdd = new ArrayList<>();
credentialsListToAdd.add(pskCredentials);
credentialsListToAdd.add(hashedPasswordCredential);
credentialsListToAdd.add(X509CertificateCredential.fromSubjectDn("CN=Acme", List.of(new X509CertificateSecret())));
for (int i = 0; i < 3; i++) {
final GenericSecret secret = new GenericSecret();
secret.setAdditionalProperties(Map.of("field-" + i, "setec astronomy"));
final GenericCredential credential = new GenericCredential("type-" + i, getRandomAuthId(PREFIX_AUTH_ID), List.of(secret));
credentialsListToAdd.add(credential);
}
registry.addCredentials(tenantId, deviceId, credentialsListToAdd).compose(ar -> registry.getCredentials(tenantId, deviceId)).onComplete(context.succeeding(httpResponse -> {
context.verify(() -> assertResponseBodyContainsAllCredentials(httpResponse.bodyAsJsonArray(), credentialsListToAdd));
context.completeNow();
}));
}
use of org.eclipse.hono.service.management.credentials.X509CertificateSecret in project hono by eclipse.
the class MqttConnectionIT method testConnectX509FailsForUnknownSubjectDN.
/**
* Verifies that the adapter rejects connection attempts from devices using a client certificate with an unknown
* subject DN.
*
* @param ctx The test context
*/
@Test
public void testConnectX509FailsForUnknownSubjectDN(final VertxTestContext ctx) {
// GIVEN a registered device
helper.getCertificate(deviceCert.certificatePath()).compose(cert -> {
final var tenant = Tenants.createTenantForTrustAnchor(cert);
return helper.registry.addTenant(tenantId, tenant);
}).compose(ok -> helper.registry.registerDevice(tenantId, deviceId)).compose(ok -> {
final String authId = new X500Principal("CN=4711").getName(X500Principal.RFC2253);
final var credential = X509CertificateCredential.fromSubjectDn(authId, List.of(new X509CertificateSecret()));
return helper.registry.addCredentials(tenantId, deviceId, Collections.singleton(credential));
}).compose(ok -> connectToAdapter(deviceCert)).onComplete(ctx.failing(t -> {
// THEN the connection is refused
ctx.verify(() -> {
assertThat(t).isInstanceOf(MqttConnectionException.class);
assertThat(((MqttConnectionException) t).code()).isEqualTo(MqttConnectReturnCode.CONNECTION_REFUSED_BAD_USER_NAME_OR_PASSWORD);
});
ctx.completeNow();
}));
}
Aggregations