Skip to content

Commit

Permalink
make Ditto default namespace configurable;
Browse files Browse the repository at this point in the history
use Ditto default namespace if no namespace is specified during creation;

Signed-off-by: Stefan Maute <stefan.maute@bosch.io>
  • Loading branch information
Stefan Maute committed Apr 22, 2022
1 parent 2967d5e commit d6d8ef0
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import javax.annotation.concurrent.Immutable;

import org.eclipse.ditto.base.service.config.ServiceSpecificConfig;
import org.eclipse.ditto.internal.utils.config.KnownConfigValue;
import org.eclipse.ditto.internal.utils.health.config.WithHealthCheckConfig;

/**
Expand All @@ -23,6 +24,14 @@
@Immutable
public interface ConciergeConfig extends ServiceSpecificConfig, WithHealthCheckConfig {


/**
* Returns the default namespace which is used when no namespace is provided.
*
* @return the default namespace.
*/
String getDefaultNamespace();

/**
* Returns the config of Concierge's enforcement behaviour.
*
Expand All @@ -44,4 +53,32 @@ public interface ConciergeConfig extends ServiceSpecificConfig, WithHealthCheckC
*/
ThingsAggregatorConfig getThingsAggregatorConfig();

enum ConciergeConfigValue implements KnownConfigValue {

/**
* The default namespace to use for creating things without specified namespace.
*
* @since 2.5.0
*/
DEFAULT_NAMESPACE("default-namespace", "org.eclipse.ditto");

private final String path;
private final Object defaultValue;

ConciergeConfigValue(final String thePath, final Object theDefaultValue) {
path = thePath;
defaultValue = theDefaultValue;
}

@Override
public Object getDefaultValue() {
return defaultValue;
}

@Override
public String getConfigPath() {
return path;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import java.util.Objects;

import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;

import org.eclipse.ditto.base.service.config.DittoServiceConfig;
Expand All @@ -41,13 +40,15 @@ public final class DittoConciergeConfig implements ConciergeConfig, WithConfigPa
private final DefaultEnforcementConfig enforcementConfig;
private final DefaultCachesConfig cachesConfig;
private final DefaultThingsAggregatorConfig thingsAggregatorConfig;
private final String defaultNamespace;

private DittoConciergeConfig(final ScopedConfig dittoScopedConfig) {
serviceSpecificConfig = DittoServiceConfig.of(dittoScopedConfig, CONFIG_PATH);
healthCheckConfig = DefaultHealthCheckConfig.of(dittoScopedConfig);
enforcementConfig = DefaultEnforcementConfig.of(serviceSpecificConfig);
cachesConfig = DefaultCachesConfig.of(serviceSpecificConfig);
thingsAggregatorConfig = DefaultThingsAggregatorConfig.of(serviceSpecificConfig);
defaultNamespace = dittoScopedConfig.getString(ConciergeConfigValue.DEFAULT_NAMESPACE.getConfigPath());
}

/**
Expand All @@ -62,6 +63,11 @@ public static DittoConciergeConfig of(final ScopedConfig dittoScopedConfig) {
return new DittoConciergeConfig(dittoScopedConfig);
}

@Override
public String getDefaultNamespace() {
return defaultNamespace;
}

@Override
public EnforcementConfig getEnforcementConfig() {
return enforcementConfig;
Expand Down Expand Up @@ -113,7 +119,7 @@ public String getConfigPath() {
}

@Override
public boolean equals(@Nullable final Object o) {
public boolean equals(final Object o) {
if (this == o) {
return true;
}
Expand All @@ -122,16 +128,15 @@ public boolean equals(@Nullable final Object o) {
}
final DittoConciergeConfig that = (DittoConciergeConfig) o;
return serviceSpecificConfig.equals(that.serviceSpecificConfig) &&
healthCheckConfig.equals(that.healthCheckConfig) &&
enforcementConfig.equals(that.enforcementConfig) &&
cachesConfig.equals(that.cachesConfig) &&
thingsAggregatorConfig.equals(that.thingsAggregatorConfig);
healthCheckConfig.equals(that.healthCheckConfig) && enforcementConfig.equals(that.enforcementConfig) &&
cachesConfig.equals(that.cachesConfig) && thingsAggregatorConfig.equals(that.thingsAggregatorConfig) &&
defaultNamespace.equals(that.defaultNamespace);
}

@Override
public int hashCode() {
return Objects.hash(serviceSpecificConfig, healthCheckConfig, enforcementConfig, cachesConfig,
thingsAggregatorConfig);
thingsAggregatorConfig, defaultNamespace);
}

@Override
Expand All @@ -142,6 +147,8 @@ public String toString() {
", enforcementConfig=" + enforcementConfig +
", cachesConfig=" + cachesConfig +
", thingsAggregatorConfig=" + thingsAggregatorConfig +
", defaultNamespace='" + defaultNamespace +
"]";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,6 @@
*/
public final class DefaultEnforcerActorFactory implements EnforcerActorFactory<ConciergeConfig> {

/**
* Default namespace for {@code CreateThing} commands without any namespace.
*/
private static final String DEFAULT_NAMESPACE = "org.eclipse.ditto";

private static final String ENFORCER_CACHE_METRIC_NAME_PREFIX = "ditto_authorization_enforcer_cache_";
private static final String ID_CACHE_METRIC_NAME_PREFIX = "ditto_authorization_id_cache_";

Expand Down Expand Up @@ -114,7 +109,8 @@ public ActorRef startEnforcerActor(final ActorContext context,

// pre-enforcer
final BlockedNamespaces blockedNamespaces = BlockedNamespaces.of(actorSystem);
final PreEnforcer preEnforcer = newPreEnforcer(blockedNamespaces, PlaceholderSubstitution.newInstance());
final PreEnforcer preEnforcer = newPreEnforcer(blockedNamespaces, PlaceholderSubstitution.newInstance(),
conciergeConfig.getDefaultNamespace());

final DistributedAcks distributedAcks = DistributedAcks.lookup(actorSystem);
final LiveSignalPub liveSignalPub = LiveSignalPub.of(context, distributedAcks);
Expand Down Expand Up @@ -187,25 +183,25 @@ public static <T extends DittoHeadersSettable<?>> T setOriginatorHeader(final T
}

private static PreEnforcer newPreEnforcer(final BlockedNamespaces blockedNamespaces,
final PlaceholderSubstitution placeholderSubstitution) {
final PlaceholderSubstitution placeholderSubstitution, final String defaultNamespace) {

return dittoHeadersSettable ->
BlockNamespaceBehavior.of(blockedNamespaces)
.block(dittoHeadersSettable)
.thenApply(CommandWithOptionalEntityValidator.getInstance())
.thenApply(DefaultEnforcerActorFactory::prependDefaultNamespaceToCreateThing)
.thenApply(signal -> prependDefaultNamespaceToCreateThing(signal, defaultNamespace))
.thenApply(DefaultEnforcerActorFactory::setOriginatorHeader)
.thenCompose(placeholderSubstitution);
}

private static DittoHeadersSettable<?> prependDefaultNamespaceToCreateThing(final DittoHeadersSettable<?> signal) {
if (signal instanceof CreateThing) {
final CreateThing createThing = (CreateThing) signal;
private static DittoHeadersSettable<?> prependDefaultNamespaceToCreateThing(final DittoHeadersSettable<?> signal,
final String defaultNamespace) {
if (signal instanceof CreateThing createThing) {
final Thing thing = createThing.getThing();
final Optional<String> namespace = thing.getNamespace();
if (namespace.isEmpty()) {
if (namespace.isEmpty() || namespace.get().equals("")) {
final Thing thingInDefaultNamespace = thing.toBuilder()
.setId(ThingId.of(DEFAULT_NAMESPACE, createThing.getEntityId().toString()))
.setId(ThingId.of(defaultNamespace, createThing.getEntityId().toString().substring(1)))
.build();
final JsonObject initialPolicy = createThing.getInitialPolicy().orElse(null);
return CreateThing.of(thingInDefaultNamespace, initialPolicy, createThing.getDittoHeaders());
Expand Down
4 changes: 4 additions & 0 deletions concierge/service/src/main/resources/concierge.conf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ ditto {
mapping-strategy.implementation = "org.eclipse.ditto.concierge.service.ConciergeMappingStrategies"
mapping-strategy.implementation = ${?MAPPING_STRATEGY_IMPLEMENTATION}

default-namespace = "org.eclipse.ditto"
default-namespace = ${?DITTO_DEFAULT_NAMESPACE}

concierge {

enforcement {
# configuration for retrieval of policies/things during enforcement via sharding
ask-with-retry {
Expand Down

0 comments on commit d6d8ef0

Please sign in to comment.