Search in sources :

Example 1 with RegistryManagementConstants

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

the class IdentityTemplate method checkValidity.

/**
 * Checks if the template is valid.
 * <p>
 * The following placeholders are supported.
 * <ul>
 * <li>{@value RegistryManagementConstants#PLACEHOLDER_SUBJECT_DN} for <em>Subject Distinguished Name (DN)</em></li>
 * <li>{@value RegistryManagementConstants#PLACEHOLDER_SUBJECT_CN} for <em>Common Name (CN)</em></li>
 * <li>{@value RegistryManagementConstants#PLACEHOLDER_SUBJECT_OU} for <em>Organizational Unit Name (OU)</em></li>
 * <li>{@value RegistryManagementConstants#PLACEHOLDER_SUBJECT_O} for <em>Organization Name (O)</em></li>
 * </ul>
 *
 * @param template The identity template.
 * @throws NullPointerException if template is {@code null}.
 * @throws IllegalArgumentException if the template does not contain any placeholders
 *                                  or contains any unsupported placeholders.
 */
public static void checkValidity(final String template) {
    Objects.requireNonNull(template, "template must not be null");
    final Matcher matcher = PLACEHOLDER_PATTERN.matcher(template);
    final List<String> placeholders = new ArrayList<>();
    while (matcher.find()) {
        placeholders.add(String.format("{{%s}}", matcher.group(1)));
    }
    if (placeholders.isEmpty()) {
        throw new IllegalArgumentException(String.format("template [%s] must contain at least one placeholder", template));
    }
    final List<String> unsupportedPlaceHolders = placeholders.stream().filter(placeholder -> !SUPPORTED_PLACE_HOLDERS.contains(placeholder)).collect(Collectors.toList());
    if (!unsupportedPlaceHolders.isEmpty()) {
        throw new IllegalArgumentException(String.format("template [%s] contains unsupported placeholders %s", template, unsupportedPlaceHolders));
    }
}
Also used : Arrays(java.util.Arrays) LdapName(javax.naming.ldap.LdapName) X500Principal(javax.security.auth.x500.X500Principal) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Objects(java.util.Objects) List(java.util.List) Matcher(java.util.regex.Matcher) Rdn(javax.naming.ldap.Rdn) InvalidNameException(javax.naming.InvalidNameException) Pattern(java.util.regex.Pattern) RegistryManagementConstants(org.eclipse.hono.util.RegistryManagementConstants) Strings(org.eclipse.hono.util.Strings) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList)

Example 2 with RegistryManagementConstants

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

the class AbstractRegistrationService method getSupportedGatewaysForDevice.

/**
 * Gets the list of gateways that may act on behalf of a given device.
 * <p>
 * To compile this list of gateways, this default implementation gets the list of gateways from the value of the
 * {@link RegistryManagementConstants#FIELD_VIA} property in the device's registration information and resolves
 * the members of the groups that are in the {@link RegistryManagementConstants#FIELD_VIA_GROUPS} property of the device.
 * <p>
 * Subclasses may override this method to provide a different means to determine the supported gateways.
 *
 * @param tenantId The tenant id.
 * @param deviceId The device id.
 * @param registrationInfo The device's registration information.
 * @param span The active OpenTracing span for this operation. It is not to be closed in this method! An
 *            implementation should log (error) events on this span and it may set tags and use this span as the
 *            parent for any spans created in this method.
 * @return A future indicating the outcome of the operation.
 *         <p>
 *         The future will succeed with the list of gateways as a JSON array of Strings (never {@code null})
 *         or it will fail with a {@link ServiceInvocationException} indicating the cause of the failure.
 */
protected Future<JsonArray> getSupportedGatewaysForDevice(final String tenantId, final String deviceId, final JsonObject registrationInfo, final Span span) {
    final JsonArray via = convertObjectToJsonArray(registrationInfo.getValue(RegistryManagementConstants.FIELD_VIA));
    final JsonArray viaGroups = convertObjectToJsonArray(registrationInfo.getValue(RegistryManagementConstants.FIELD_VIA_GROUPS));
    final Future<JsonArray> resultFuture;
    if (viaGroups.isEmpty()) {
        resultFuture = Future.succeededFuture(via);
    } else {
        resultFuture = resolveGroupMembers(tenantId, viaGroups, span).compose(groupMembers -> {
            for (final Object gateway : groupMembers) {
                if (!via.contains(gateway)) {
                    via.add(gateway);
                }
            }
            return Future.succeededFuture(via);
        }).recover(thr -> {
            LOG.debug("failed to resolve group members", thr);
            TracingHelper.logError(span, "failed to resolve group members: " + thr.getMessage());
            return Future.failedFuture(thr);
        });
    }
    return resultFuture;
}
Also used : JsonArray(io.vertx.core.json.JsonArray) HttpURLConnection(java.net.HttpURLConnection) CacheDirective(org.eclipse.hono.util.CacheDirective) Lifecycle(org.eclipse.hono.util.Lifecycle) LoggerFactory(org.slf4j.LoggerFactory) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) Constants(org.eclipse.hono.util.Constants) CompositeFuture(io.vertx.core.CompositeFuture) RegistrationResult(org.eclipse.hono.util.RegistrationResult) JsonObject(io.vertx.core.json.JsonObject) TracingHelper(org.eclipse.hono.tracing.TracingHelper) TenantInformationService(org.eclipse.hono.deviceregistry.service.tenant.TenantInformationService) RegistryManagementConstants(org.eclipse.hono.util.RegistryManagementConstants) NoopTenantInformationService(org.eclipse.hono.deviceregistry.service.tenant.NoopTenantInformationService) Device(org.eclipse.hono.service.management.device.Device) Logger(org.slf4j.Logger) Set(java.util.Set) RegistrationConstants(org.eclipse.hono.util.RegistrationConstants) DeviceStatus(org.eclipse.hono.service.management.device.DeviceStatus) Collectors(java.util.stream.Collectors) Future(io.vertx.core.Future) Objects(java.util.Objects) RegistrationService(org.eclipse.hono.service.registration.RegistrationService) JsonArray(io.vertx.core.json.JsonArray) Optional(java.util.Optional) Span(io.opentracing.Span) NoopSpan(io.opentracing.noop.NoopSpan) Collections(java.util.Collections) JsonObject(io.vertx.core.json.JsonObject)

Aggregations

Objects (java.util.Objects)2 Collectors (java.util.stream.Collectors)2 RegistryManagementConstants (org.eclipse.hono.util.RegistryManagementConstants)2 Span (io.opentracing.Span)1 NoopSpan (io.opentracing.noop.NoopSpan)1 CompositeFuture (io.vertx.core.CompositeFuture)1 Future (io.vertx.core.Future)1 JsonArray (io.vertx.core.json.JsonArray)1 JsonObject (io.vertx.core.json.JsonObject)1 HttpURLConnection (java.net.HttpURLConnection)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 List (java.util.List)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 InvalidNameException (javax.naming.InvalidNameException)1 LdapName (javax.naming.ldap.LdapName)1