Skip to content

Commit

Permalink
Refactored and added some comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Eugene Sevastianov committed May 29, 2019
1 parent 0af5d8c commit d4237cf
Show file tree
Hide file tree
Showing 14 changed files with 107 additions and 145 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

/**
* Basic authentication storage/cache/resource handler config.
* The config is an option to specify classes of user/role managers, caches and notifiers.
* If a config field is specified then the corresponding class is instantiated
* regardless of what type of Druid component runs it (see {@link BasicSecurityDruidModule}).
* Hence every Druid component might be a user/role manager and notify others by sending notifications.
* Every field must be a valid class name (appropriate for the corresponding goal) or null.
*/
public class BasicAuthClassCompositionConfig
{
@JsonProperty
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@
import io.druid.guice.LazySingleton;
import io.druid.guice.LifecycleModule;
import io.druid.initialization.DruidModule;
import io.druid.metadata.MetadataStorage;
import io.druid.metadata.MetadataStorageProvider;
import io.druid.security.basic.authentication.BasicHTTPAuthenticator;
import io.druid.security.basic.authentication.BasicHTTPEscalator;
import io.druid.security.basic.authentication.db.cache.BasicAuthenticatorCacheManager;
Expand All @@ -60,6 +58,7 @@
import io.druid.security.basic.authorization.endpoint.CoordinatorBasicAuthorizerResourceHandler;
import io.druid.security.basic.authorization.endpoint.DefaultBasicAuthorizerResourceHandler;

import javax.annotation.Nullable;
import java.util.List;

public class BasicSecurityDruidModule implements DruidModule
Expand All @@ -78,9 +77,6 @@ public void configure(Binder binder)

Jerseys.addResource(binder, BasicAuthenticatorResource.class);
Jerseys.addResource(binder, BasicAuthorizerResource.class);

binder.bind(MetadataStorage.class).toProvider(MetadataStorageProvider.class);
LifecycleModule.register(binder, MetadataStorage.class);
}

@Provides
Expand All @@ -90,15 +86,12 @@ public static BasicAuthenticatorMetadataStorageUpdater createAuthenticatorStorag
BasicAuthClassCompositionConfig config
) throws ClassNotFoundException
{
if (config.getAuthenticatorMetadataStorageUpdater() != null) {
return (BasicAuthenticatorMetadataStorageUpdater)
injector.getInstance(Class.forName(config.getAuthenticatorMetadataStorageUpdater()));
}
if (isCoordinator(injector)) {
return injector.getInstance(CoordinatorBasicAuthenticatorMetadataStorageUpdater.class);
} else {
return null;
}
return getInstance(
injector,
config.getAuthenticatorMetadataStorageUpdater(),
CoordinatorBasicAuthenticatorMetadataStorageUpdater.class,
null
);
}

@Provides
Expand All @@ -108,15 +101,12 @@ public static BasicAuthenticatorCacheManager createAuthenticatorCacheManager(
BasicAuthClassCompositionConfig config
) throws ClassNotFoundException
{
if (config.getAuthenticatorCacheManager() != null) {
return (BasicAuthenticatorCacheManager)
injector.getInstance(Class.forName(config.getAuthenticatorCacheManager()));
}
if (isCoordinator(injector)) {
return injector.getInstance(MetadataStoragePollingBasicAuthenticatorCacheManager.class);
} else {
return injector.getInstance(CoordinatorPollingBasicAuthenticatorCacheManager.class);
}
return getInstance(
injector,
config.getAuthenticatorCacheManager(),
MetadataStoragePollingBasicAuthenticatorCacheManager.class,
CoordinatorPollingBasicAuthenticatorCacheManager.class
);
}

@Provides
Expand All @@ -126,15 +116,12 @@ public static BasicAuthenticatorResourceHandler createAuthenticatorResourceHandl
BasicAuthClassCompositionConfig config
) throws ClassNotFoundException
{
if (config.getAuthenticatorResourceHandler() != null) {
return (BasicAuthenticatorResourceHandler)
injector.getInstance(Class.forName(config.getAuthenticatorResourceHandler()));
}
if (isCoordinator(injector)) {
return injector.getInstance(CoordinatorBasicAuthenticatorResourceHandler.class);
} else {
return injector.getInstance(DefaultBasicAuthenticatorResourceHandler.class);
}
return getInstance(
injector,
config.getAuthenticatorResourceHandler(),
CoordinatorBasicAuthenticatorResourceHandler.class,
DefaultBasicAuthenticatorResourceHandler.class
);
}

@Provides
Expand All @@ -144,34 +131,27 @@ public static BasicAuthenticatorCacheNotifier createAuthenticatorCacheNotifier(
BasicAuthClassCompositionConfig config
) throws ClassNotFoundException
{
if (config.getAuthenticatorCacheNotifier() != null) {
return (BasicAuthenticatorCacheNotifier)
injector.getInstance(Class.forName(config.getAuthenticatorCacheNotifier()));
}
if (isCoordinator(injector)) {
return injector.getInstance(CoordinatorBasicAuthenticatorCacheNotifier.class);
} else {
return null;
}
return getInstance(
injector,
config.getAuthenticatorCacheNotifier(),
CoordinatorBasicAuthenticatorCacheNotifier.class,
null
);
}

@Provides
@LazySingleton
public static BasicAuthorizerMetadataStorageUpdater createAuthorizerStorageUpdater(
final Injector injector,
BasicAuthClassCompositionConfig config
)
throws ClassNotFoundException
) throws ClassNotFoundException
{
if (config.getAuthorizerMetadataStorageUpdater() != null) {
return (BasicAuthorizerMetadataStorageUpdater)
injector.getInstance(Class.forName(config.getAuthorizerMetadataStorageUpdater()));
}
if (isCoordinator(injector)) {
return injector.getInstance(CoordinatorBasicAuthorizerMetadataStorageUpdater.class);
} else {
return null;
}
return getInstance(
injector,
config.getAuthorizerMetadataStorageUpdater(),
CoordinatorBasicAuthorizerMetadataStorageUpdater.class,
null
);
}

@Provides
Expand All @@ -181,15 +161,12 @@ public static BasicAuthorizerCacheManager createAuthorizerCacheManager(
BasicAuthClassCompositionConfig config
) throws ClassNotFoundException
{
if (config.getAuthorizerCacheManager() != null) {
return (BasicAuthorizerCacheManager)
injector.getInstance(Class.forName(config.getAuthorizerCacheManager()));
}
if (isCoordinator(injector)) {
return injector.getInstance(MetadataStoragePollingBasicAuthorizerCacheManager.class);
} else {
return injector.getInstance(CoordinatorPollingBasicAuthorizerCacheManager.class);
}
return getInstance(
injector,
config.getAuthorizerCacheManager(),
MetadataStoragePollingBasicAuthorizerCacheManager.class,
CoordinatorPollingBasicAuthorizerCacheManager.class
);
}

@Provides
Expand All @@ -199,15 +176,12 @@ public static BasicAuthorizerResourceHandler createAuthorizerResourceHandler(
BasicAuthClassCompositionConfig config
) throws ClassNotFoundException
{
if (config.getAuthorizerResourceHandler() != null) {
return (BasicAuthorizerResourceHandler)
injector.getInstance(Class.forName(config.getAuthorizerResourceHandler()));
}
if (isCoordinator(injector)) {
return injector.getInstance(CoordinatorBasicAuthorizerResourceHandler.class);
} else {
return injector.getInstance(DefaultBasicAuthorizerResourceHandler.class);
}
return getInstance(
injector,
config.getAuthorizerResourceHandler(),
CoordinatorBasicAuthorizerResourceHandler.class,
DefaultBasicAuthorizerResourceHandler.class
);
}

@Provides
Expand All @@ -217,15 +191,12 @@ public static BasicAuthorizerCacheNotifier createAuthorizerCacheNotifier(
BasicAuthClassCompositionConfig config
) throws ClassNotFoundException
{
if (config.getAuthorizerCacheNotifier() != null) {
return (BasicAuthorizerCacheNotifier)
injector.getInstance(Class.forName(config.getAuthorizerCacheNotifier()));
}
if (isCoordinator(injector)) {
return injector.getInstance(CoordinatorBasicAuthorizerCacheNotifier.class);
} else {
return null;
}
return getInstance(
injector,
config.getAuthorizerCacheNotifier(),
CoordinatorBasicAuthorizerCacheNotifier.class,
null
);
}

@Override
Expand All @@ -240,6 +211,33 @@ public List<? extends Module> getJacksonModules()
);
}

/**
* Returns the instance provided either by a config property or coordinator-run class or default class.
* The order of check corresponds to the order of method params.
*/
@Nullable
public static <T> T getInstance(
Injector injector,
String configClassName,
Class<? extends T> isCoordClass,
Class<? extends T> defaultClass
) throws ClassNotFoundException
{
if (configClassName != null) {
// ClassCastException is thrown in case of a mismatch, configuration fix is required.
@SuppressWarnings("unchecked")
final T instance = (T) injector.getInstance(Class.forName(configClassName));
return instance;
}
if (isCoordClass != null && isCoordinator(injector)) {
return injector.getInstance(isCoordClass);
}
if (defaultClass != null) {
return injector.getInstance(defaultClass);
}
return null;
}

private static boolean isCoordinator(Injector injector)
{
final String serviceName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@

package io.druid.security.basic.authentication.db.cache;

/**
* Noop basic authenticator cache notifier.
* Might be used as a config option to override default authenticator cache notifier.
*/
public class NoopBasicAuthenticatorCacheNotifier implements BasicAuthenticatorCacheNotifier
{
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@

package io.druid.security.basic.authorization.db.cache;

/**
* Noop basic authorizer cache notifier.
* Might be used as a config option to override default authorizer cache notifier.
*/
public class NoopBasicAuthorizerCacheNotifier implements BasicAuthorizerCacheNotifier
{
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import io.druid.security.basic.BasicSecurityDBResourceException;
import io.druid.security.basic.authentication.BasicHTTPAuthenticator;
import io.druid.security.basic.authentication.BasicHTTPEscalator;
import io.druid.security.basic.authentication.db.cache.NoopBasicAuthenticatorCacheNotifier;
import io.druid.security.basic.authentication.db.updater.CoordinatorBasicAuthenticatorMetadataStorageUpdater;
import io.druid.security.basic.authentication.entity.BasicAuthenticatorCredentialUpdate;
import io.druid.security.basic.authentication.entity.BasicAuthenticatorCredentials;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import io.druid.security.basic.BasicAuthUtils;
import io.druid.security.basic.authentication.BasicHTTPAuthenticator;
import io.druid.security.basic.authentication.BasicHTTPEscalator;
import io.druid.security.basic.authentication.db.cache.NoopBasicAuthenticatorCacheNotifier;
import io.druid.security.basic.authentication.db.updater.CoordinatorBasicAuthenticatorMetadataStorageUpdater;
import io.druid.security.basic.authentication.endpoint.BasicAuthenticatorResource;
import io.druid.security.basic.authentication.endpoint.CoordinatorBasicAuthenticatorResourceHandler;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import io.druid.security.basic.BasicAuthCommonCacheConfig;
import io.druid.security.basic.authorization.BasicRoleBasedAuthorizer;
import io.druid.security.basic.authorization.db.cache.MetadataStoragePollingBasicAuthorizerCacheManager;
import io.druid.security.basic.authorization.db.cache.NoopBasicAuthorizerCacheNotifier;
import io.druid.security.basic.authorization.db.updater.CoordinatorBasicAuthorizerMetadataStorageUpdater;
import io.druid.server.security.Access;
import io.druid.server.security.Action;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import io.druid.security.basic.BasicAuthUtils;
import io.druid.security.basic.BasicSecurityDBResourceException;
import io.druid.security.basic.authorization.BasicRoleBasedAuthorizer;
import io.druid.security.basic.authorization.db.cache.NoopBasicAuthorizerCacheNotifier;
import io.druid.security.basic.authorization.db.updater.CoordinatorBasicAuthorizerMetadataStorageUpdater;
import io.druid.security.basic.authorization.entity.BasicAuthorizerPermission;
import io.druid.security.basic.authorization.entity.BasicAuthorizerRole;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import io.druid.security.basic.BasicAuthCommonCacheConfig;
import io.druid.security.basic.BasicAuthUtils;
import io.druid.security.basic.authorization.BasicRoleBasedAuthorizer;
import io.druid.security.basic.authorization.db.cache.NoopBasicAuthorizerCacheNotifier;
import io.druid.security.basic.authorization.db.updater.CoordinatorBasicAuthorizerMetadataStorageUpdater;
import io.druid.security.basic.authorization.endpoint.BasicAuthorizerResource;
import io.druid.security.basic.authorization.endpoint.CoordinatorBasicAuthorizerResourceHandler;
Expand Down

This file was deleted.

4 changes: 2 additions & 2 deletions integration-tests/docker/historical.conf
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ command=java
-Dfile.encoding=UTF-8
-Ddruid.host=%(ENV_HOST_IP)s
-Ddruid.zk.service.host=druid-zookeeper-kafka
-Ddruid.s3.accessKey=AKIAIMKECRUYKDQGR6YQ
-Ddruid.s3.secretKey=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
-Ddruid.s3.accessKey=AKIAJI7DG7CDECGBQ6NA
-Ddruid.s3.secretKey=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
-Ddruid.processing.buffer.sizeBytes=75000000
-Ddruid.processing.numThreads=3
-Ddruid.server.http.numThreads=100
Expand Down
Loading

0 comments on commit d4237cf

Please sign in to comment.