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);
}
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();
}
}
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);
}
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);
}
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);
}
});
}
Aggregations