Search in sources :

Example 21 with IOModule

use of io.cdap.cdap.common.guice.IOModule in project cdap by cdapio.

the class FileBasedTokenManagerTest method testFileBasedKey.

/**
 * Test that two token managers can share a key that is written to a file.
 */
@Test
public void testFileBasedKey() throws Exception {
    // Create two token managers that points to the same path
    CConfiguration cConf = CConfiguration.create();
    cConf.set(Constants.CFG_LOCAL_DATA_DIR, TEMP_FOLDER.newFolder().getAbsolutePath());
    TokenManager tokenManager = Guice.createInjector(new IOModule(), new ConfigModule(cConf), new FileBasedCoreSecurityModule(), new InMemoryDiscoveryModule()).getInstance(TokenManager.class);
    tokenManager.startAndWait();
    TokenManager tokenManager2 = Guice.createInjector(new IOModule(), new ConfigModule(cConf), new FileBasedCoreSecurityModule(), new InMemoryDiscoveryModule()).getInstance(TokenManager.class);
    tokenManager2.startAndWait();
    Assert.assertNotSame("ERROR: Both token managers refer to the same object.", tokenManager, tokenManager2);
    String user = "testuser";
    long now = System.currentTimeMillis();
    List<String> groups = Lists.newArrayList("users", "admins");
    UserIdentity identifier = new UserIdentity(user, UserIdentity.IdentifierType.EXTERNAL, groups, now, now + TOKEN_DURATION);
    AccessToken token = tokenManager.signIdentifier(identifier);
    // Since both tokenManagers have the same key, they must both be able to validate the secret.
    tokenManager.validateSecret(token);
    tokenManager2.validateSecret(token);
}
Also used : IOModule(io.cdap.cdap.common.guice.IOModule) InMemoryDiscoveryModule(io.cdap.cdap.common.guice.InMemoryDiscoveryModule) ConfigModule(io.cdap.cdap.common.guice.ConfigModule) CConfiguration(io.cdap.cdap.common.conf.CConfiguration) FileBasedCoreSecurityModule(io.cdap.cdap.security.guice.FileBasedCoreSecurityModule) Test(org.junit.Test)

Example 22 with IOModule

use of io.cdap.cdap.common.guice.IOModule in project cdap by cdapio.

the class FileBasedTokenManagerTest method testKeyUpdate.

@Test
public void testKeyUpdate() throws Exception {
    File keyDir = TEMP_FOLDER.newFolder();
    File keyFile = new File(keyDir, "key");
    CConfiguration cConf = CConfiguration.create();
    cConf.set(Constants.Security.CFG_FILE_BASED_KEYFILE_PATH, keyFile.getAbsolutePath());
    Injector injector = Guice.createInjector(new IOModule(), new ConfigModule(cConf), new FileBasedCoreSecurityModule(), new InMemoryDiscoveryModule());
    Codec<KeyIdentifier> codec = injector.getInstance(Key.get(new TypeLiteral<Codec<KeyIdentifier>>() {
    }));
    FileBasedKeyManager keyManager = injector.getInstance(FileBasedKeyManager.class);
    KeyIdentifier keyIdentifier = generateAndSaveKey(keyFile.toPath(), keyManager, codec, 0);
    // Set the last modified time to 10 seconds ago to workaround the MacOS FS timestamp granularity (1 second)
    // so that test can run faster.
    // noinspection ResultOfMethodCallIgnored
    keyFile.setLastModified(System.currentTimeMillis() - TimeUnit.SECONDS.toMillis(10));
    try {
        keyManager.startAndWait();
        // Upon the key manager starts, the current key should be the same as the one from the key file.
        Assert.assertEquals(keyIdentifier, keyManager.currentKey);
        // Now update the key by doing an atomic move
        Path tempFile = TEMP_FOLDER.newFile().toPath();
        keyIdentifier = generateAndSaveKey(tempFile, keyManager, codec, 1);
        Files.move(tempFile, keyFile.toPath(), StandardCopyOption.ATOMIC_MOVE);
        // Wait for the key change in the key manager
        Tasks.waitFor(keyIdentifier, () -> keyManager.currentKey, 20, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
    } finally {
        keyManager.stopAndWait();
    }
}
Also used : IOModule(io.cdap.cdap.common.guice.IOModule) Path(java.nio.file.Path) InMemoryDiscoveryModule(io.cdap.cdap.common.guice.InMemoryDiscoveryModule) TypeLiteral(com.google.inject.TypeLiteral) Injector(com.google.inject.Injector) ConfigModule(io.cdap.cdap.common.guice.ConfigModule) File(java.io.File) CConfiguration(io.cdap.cdap.common.conf.CConfiguration) FileBasedCoreSecurityModule(io.cdap.cdap.security.guice.FileBasedCoreSecurityModule) Test(org.junit.Test)

Example 23 with IOModule

use of io.cdap.cdap.common.guice.IOModule in project cdap by cdapio.

the class FileBasedTokenManagerTest method getTokenManagerAndCodec.

@Override
protected ImmutablePair<TokenManager, Codec<AccessToken>> getTokenManagerAndCodec() throws IOException {
    CConfiguration cConf = CConfiguration.create();
    cConf.set(Constants.CFG_LOCAL_DATA_DIR, TEMP_FOLDER.newFolder().getAbsolutePath());
    Injector injector = Guice.createInjector(new IOModule(), new ConfigModule(cConf), new FileBasedCoreSecurityModule(), new InMemoryDiscoveryModule());
    TokenManager tokenManager = injector.getInstance(TokenManager.class);
    tokenManager.startAndWait();
    Codec<AccessToken> tokenCodec = injector.getInstance(AccessTokenCodec.class);
    return new ImmutablePair<>(tokenManager, tokenCodec);
}
Also used : IOModule(io.cdap.cdap.common.guice.IOModule) InMemoryDiscoveryModule(io.cdap.cdap.common.guice.InMemoryDiscoveryModule) ImmutablePair(io.cdap.cdap.common.utils.ImmutablePair) Injector(com.google.inject.Injector) ConfigModule(io.cdap.cdap.common.guice.ConfigModule) CConfiguration(io.cdap.cdap.common.conf.CConfiguration) FileBasedCoreSecurityModule(io.cdap.cdap.security.guice.FileBasedCoreSecurityModule)

Example 24 with IOModule

use of io.cdap.cdap.common.guice.IOModule in project cdap by cdapio.

the class TestInMemoryTokenManager method getTokenManagerAndCodec.

@Override
protected ImmutablePair<TokenManager, Codec<AccessToken>> getTokenManagerAndCodec() {
    Injector injector = Guice.createInjector(new IOModule(), new CoreSecurityRuntimeModule().getStandaloneModules(), new ConfigModule(), new InMemoryDiscoveryModule());
    TokenManager tokenManager = injector.getInstance(TokenManager.class);
    tokenManager.startAndWait();
    Codec<AccessToken> tokenCodec = injector.getInstance(AccessTokenCodec.class);
    return new ImmutablePair<>(tokenManager, tokenCodec);
}
Also used : IOModule(io.cdap.cdap.common.guice.IOModule) InMemoryDiscoveryModule(io.cdap.cdap.common.guice.InMemoryDiscoveryModule) ImmutablePair(io.cdap.cdap.common.utils.ImmutablePair) CoreSecurityRuntimeModule(io.cdap.cdap.security.guice.CoreSecurityRuntimeModule) Injector(com.google.inject.Injector) ConfigModule(io.cdap.cdap.common.guice.ConfigModule)

Example 25 with IOModule

use of io.cdap.cdap.common.guice.IOModule in project cdap by cdapio.

the class StandaloneMain method createPersistentModules.

private static List<Module> createPersistentModules(CConfiguration cConf, Configuration hConf) {
    cConf.setInt(Constants.Master.MAX_INSTANCES, 1);
    cConf.setIfUnset(Constants.CFG_DATA_LEVELDB_DIR, Constants.DEFAULT_DATA_LEVELDB_DIR);
    cConf.set(Constants.CFG_DATA_INMEMORY_PERSISTENCE, Constants.InMemoryPersistenceType.LEVELDB.name());
    // configure all services except for router and auth to bind to 127.0.0.1
    String localhost = InetAddress.getLoopbackAddress().getHostAddress();
    cConf.set(Constants.Service.MASTER_SERVICES_BIND_ADDRESS, localhost);
    cConf.set(Constants.MessagingSystem.HTTP_SERVER_BIND_ADDRESS, localhost);
    cConf.set(Constants.Transaction.Container.ADDRESS, localhost);
    cConf.set(Constants.Dataset.Executor.ADDRESS, localhost);
    cConf.set(Constants.Metrics.ADDRESS, localhost);
    cConf.set(Constants.MetricsProcessor.BIND_ADDRESS, localhost);
    cConf.set(Constants.LogSaver.ADDRESS, localhost);
    cConf.set(Constants.LogQuery.ADDRESS, localhost);
    cConf.set(Constants.Explore.SERVER_ADDRESS, localhost);
    cConf.set(Constants.Metadata.SERVICE_BIND_ADDRESS, localhost);
    cConf.set(Constants.Preview.ADDRESS, localhost);
    cConf.set(Constants.SupportBundle.SERVICE_BIND_ADDRESS, localhost);
    return ImmutableList.of(new ConfigModule(cConf, hConf), RemoteAuthenticatorModules.getDefaultModule(), new IOModule(), new ZKClientModule(), new KafkaClientModule(), new MetricsHandlerModule(), new LogQueryRuntimeModule().getStandaloneModules(), new InMemoryDiscoveryModule(), new LocalLocationModule(), new ProgramRunnerRuntimeModule().getStandaloneModules(), new DataFabricModules(StandaloneMain.class.getName()).getStandaloneModules(), new DataSetsModules().getStandaloneModules(), new DataSetServiceModules().getStandaloneModules(), new MetricsClientRuntimeModule().getStandaloneModules(), new LocalLogAppenderModule(), new LogReaderRuntimeModules().getStandaloneModules(), new RouterModules().getStandaloneModules(), new CoreSecurityRuntimeModule().getStandaloneModules(), new ExternalAuthenticationModule(), new SecureStoreServerModule(), new ExploreRuntimeModule().getStandaloneModules(), new ExploreClientModule(), new MetadataServiceModule(), new MetadataReaderWriterModules().getStandaloneModules(), new AuditModule(), new AuthenticationContextModules().getMasterModule(), new AuthorizationModule(), new AuthorizationEnforcementModule().getStandaloneModules(), new PreviewConfigModule(cConf, new Configuration(), SConfiguration.create()), new PreviewManagerModule(false), new PreviewRunnerManagerModule().getStandaloneModules(), new MessagingServerRuntimeModule().getStandaloneModules(), new AppFabricServiceRuntimeModule(cConf).getStandaloneModules(), new MonitorHandlerModule(false), new RuntimeServerModule(), new OperationalStatsModule(), new MetricsWriterModule(), new SupportBundleServiceModule(), new AbstractModule() {

        @Override
        protected void configure() {
            // Needed by MonitorHandlerModuler
            bind(TwillRunner.class).to(NoopTwillRunnerService.class);
            bind(HealthCheckService.class).in(Scopes.SINGLETON);
        }
    });
}
Also used : IOModule(io.cdap.cdap.common.guice.IOModule) InMemoryDiscoveryModule(io.cdap.cdap.common.guice.InMemoryDiscoveryModule) DataSetServiceModules(io.cdap.cdap.data.runtime.DataSetServiceModules) MetricsWriterModule(io.cdap.cdap.metrics.process.loader.MetricsWriterModule) PreviewManagerModule(io.cdap.cdap.app.preview.PreviewManagerModule) Configuration(org.apache.hadoop.conf.Configuration) SConfiguration(io.cdap.cdap.common.conf.SConfiguration) CConfiguration(io.cdap.cdap.common.conf.CConfiguration) ConfigModule(io.cdap.cdap.common.guice.ConfigModule) PreviewConfigModule(io.cdap.cdap.app.preview.PreviewConfigModule) MessagingServerRuntimeModule(io.cdap.cdap.messaging.guice.MessagingServerRuntimeModule) MetricsClientRuntimeModule(io.cdap.cdap.metrics.guice.MetricsClientRuntimeModule) MonitorHandlerModule(io.cdap.cdap.app.guice.MonitorHandlerModule) ZKClientModule(io.cdap.cdap.common.guice.ZKClientModule) PreviewConfigModule(io.cdap.cdap.app.preview.PreviewConfigModule) LocalLocationModule(io.cdap.cdap.common.guice.LocalLocationModule) CoreSecurityRuntimeModule(io.cdap.cdap.security.guice.CoreSecurityRuntimeModule) KafkaClientModule(io.cdap.cdap.common.guice.KafkaClientModule) SupportBundleServiceModule(io.cdap.cdap.support.app.guice.SupportBundleServiceModule) PreviewRunnerManagerModule(io.cdap.cdap.app.preview.PreviewRunnerManagerModule) AuthorizationModule(io.cdap.cdap.app.guice.AuthorizationModule) OperationalStatsModule(io.cdap.cdap.operations.guice.OperationalStatsModule) ExternalAuthenticationModule(io.cdap.cdap.security.guice.ExternalAuthenticationModule) LogReaderRuntimeModules(io.cdap.cdap.logging.guice.LogReaderRuntimeModules) AuthenticationContextModules(io.cdap.cdap.security.auth.context.AuthenticationContextModules) DataSetsModules(io.cdap.cdap.data.runtime.DataSetsModules) MetadataServiceModule(io.cdap.cdap.metadata.MetadataServiceModule) ExploreRuntimeModule(io.cdap.cdap.explore.guice.ExploreRuntimeModule) RouterModules(io.cdap.cdap.gateway.router.RouterModules) LogQueryRuntimeModule(io.cdap.cdap.logging.guice.LogQueryRuntimeModule) AbstractModule(com.google.inject.AbstractModule) MetadataReaderWriterModules(io.cdap.cdap.metadata.MetadataReaderWriterModules) NoopTwillRunnerService(io.cdap.cdap.common.twill.NoopTwillRunnerService) MetricsHandlerModule(io.cdap.cdap.metrics.guice.MetricsHandlerModule) ProgramRunnerRuntimeModule(io.cdap.cdap.app.guice.ProgramRunnerRuntimeModule) RuntimeServerModule(io.cdap.cdap.app.guice.RuntimeServerModule) LocalLogAppenderModule(io.cdap.cdap.logging.guice.LocalLogAppenderModule) ExploreClientModule(io.cdap.cdap.explore.guice.ExploreClientModule) AuditModule(io.cdap.cdap.data2.audit.AuditModule) DataFabricModules(io.cdap.cdap.data.runtime.DataFabricModules) SecureStoreServerModule(io.cdap.cdap.security.guice.SecureStoreServerModule) AuthorizationEnforcementModule(io.cdap.cdap.security.authorization.AuthorizationEnforcementModule) AppFabricServiceRuntimeModule(io.cdap.cdap.app.guice.AppFabricServiceRuntimeModule)

Aggregations

IOModule (io.cdap.cdap.common.guice.IOModule)64 ConfigModule (io.cdap.cdap.common.guice.ConfigModule)62 AbstractModule (com.google.inject.AbstractModule)40 AuthenticationContextModules (io.cdap.cdap.security.auth.context.AuthenticationContextModules)40 InMemoryDiscoveryModule (io.cdap.cdap.common.guice.InMemoryDiscoveryModule)34 CConfiguration (io.cdap.cdap.common.conf.CConfiguration)30 ZKClientModule (io.cdap.cdap.common.guice.ZKClientModule)28 DataSetsModules (io.cdap.cdap.data.runtime.DataSetsModules)28 CoreSecurityRuntimeModule (io.cdap.cdap.security.guice.CoreSecurityRuntimeModule)28 MetricsClientRuntimeModule (io.cdap.cdap.metrics.guice.MetricsClientRuntimeModule)26 AuthorizationEnforcementModule (io.cdap.cdap.security.authorization.AuthorizationEnforcementModule)26 Injector (com.google.inject.Injector)24 Module (com.google.inject.Module)24 DataFabricModules (io.cdap.cdap.data.runtime.DataFabricModules)22 ExploreClientModule (io.cdap.cdap.explore.guice.ExploreClientModule)22 ArrayList (java.util.ArrayList)22 KafkaClientModule (io.cdap.cdap.common.guice.KafkaClientModule)20 DataSetServiceModules (io.cdap.cdap.data.runtime.DataSetServiceModules)20 ZKDiscoveryModule (io.cdap.cdap.common.guice.ZKDiscoveryModule)18 NoOpMetadataServiceClient (io.cdap.cdap.data2.metadata.writer.NoOpMetadataServiceClient)16