use of io.cdap.cdap.proto.id.InstanceId in project cdap by caskdata.
the class PreferencesHttpHandler method setInstancePrefs.
@Path("/preferences")
@PUT
@AuditPolicy(AuditDetail.REQUEST_BODY)
public void setInstancePrefs(FullHttpRequest request, HttpResponder responder) throws Exception {
InstanceId instanceId = new InstanceId("");
accessEnforcer.enforce(instanceId, authenticationContext.getPrincipal(), StandardPermission.UPDATE);
try {
Map<String, String> propMap = decodeArguments(request);
preferencesService.setProperties(propMap);
responder.sendStatus(HttpResponseStatus.OK);
} catch (JsonSyntaxException jsonEx) {
responder.sendString(HttpResponseStatus.BAD_REQUEST, "Invalid JSON in body");
}
}
use of io.cdap.cdap.proto.id.InstanceId in project cdap by caskdata.
the class TestBase method finish.
@AfterClass
public static void finish() throws Exception {
if (--nestedStartCount != 0) {
return;
}
if (previewRunnerManager instanceof Service) {
((Service) previewRunnerManager).stopAndWait();
}
previewHttpServer.stopAndWait();
if (cConf.getBoolean(Constants.Security.Authorization.ENABLED)) {
InstanceId instance = new InstanceId(cConf.get(Constants.INSTANCE_NAME));
Principal principal = new Principal(System.getProperty("user.name"), Principal.PrincipalType.USER);
accessControllerInstantiator.get().grant(Authorizable.fromEntityId(instance), principal, ImmutableSet.of(StandardPermission.UPDATE));
accessControllerInstantiator.get().grant(Authorizable.fromEntityId(NamespaceId.DEFAULT), principal, ImmutableSet.of(StandardPermission.UPDATE));
}
namespaceAdmin.delete(NamespaceId.DEFAULT);
accessControllerInstantiator.close();
if (programScheduler instanceof Service) {
((Service) programScheduler).stopAndWait();
}
metricsCollectionService.stopAndWait();
if (scheduler instanceof Service) {
((Service) scheduler).stopAndWait();
}
Closeables.closeQuietly(exploreClient);
if (exploreExecutorService != null) {
exploreExecutorService.stopAndWait();
}
datasetService.stopAndWait();
dsOpService.stopAndWait();
metadataService.stopAndWait();
metadataSubscriberService.stopAndWait();
Closeables.closeQuietly(metadataStorage);
txService.stopAndWait();
if (messagingService instanceof Service) {
((Service) messagingService).stopAndWait();
}
appFabricServer.stopAndWait();
supportBundleInternalService.stopAndWait();
appFabricHealthCheckService.stopAndWait();
}
use of io.cdap.cdap.proto.id.InstanceId in project cdap by caskdata.
the class TestBase method initialize.
@BeforeClass
public static void initialize() throws Exception {
if (nestedStartCount++ > 0) {
return;
}
File localDataDir = TMP_FOLDER.newFolder();
cConf = createCConf(localDataDir);
CConfiguration previewCConf = createPreviewConf(cConf);
LevelDBTableService previewLevelDBTableService = new LevelDBTableService();
previewLevelDBTableService.setConfiguration(previewCConf);
// enable default services
File capabilityFolder = new File(localDataDir.toString(), "capability");
capabilityFolder.mkdir();
cConf.set(Constants.Capability.CONFIG_DIR, capabilityFolder.getAbsolutePath());
cConf.setInt(Constants.Capability.AUTO_INSTALL_THREADS, 5);
org.apache.hadoop.conf.Configuration hConf = new org.apache.hadoop.conf.Configuration();
hConf.addResource("mapred-site-local.xml");
hConf.reloadConfiguration();
hConf.set(Constants.CFG_LOCAL_DATA_DIR, localDataDir.getAbsolutePath());
hConf.set(Constants.AppFabric.OUTPUT_DIR, cConf.get(Constants.AppFabric.OUTPUT_DIR));
hConf.set("hadoop.tmp.dir", new File(localDataDir, cConf.get(Constants.AppFabric.TEMP_DIR)).getAbsolutePath());
// Windows specific requirements
if (OSDetector.isWindows()) {
File tmpDir = TMP_FOLDER.newFolder();
File binDir = new File(tmpDir, "bin");
Assert.assertTrue(binDir.mkdirs());
copyTempFile("hadoop.dll", tmpDir);
copyTempFile("winutils.exe", binDir);
System.setProperty("hadoop.home.dir", tmpDir.getAbsolutePath());
System.load(new File(tmpDir, "hadoop.dll").getAbsolutePath());
}
injector = Guice.createInjector(createDataFabricModule(), new TransactionExecutorModule(), new DataSetsModules().getStandaloneModules(), new DataSetServiceModules().getInMemoryModules(), new ConfigModule(cConf, hConf), RemoteAuthenticatorModules.getNoOpModule(), new IOModule(), new LocalLocationModule(), new InMemoryDiscoveryModule(), new AppFabricServiceRuntimeModule(cConf).getInMemoryModules(), new MonitorHandlerModule(false), new AuthenticationContextModules().getMasterModule(), new AuthorizationModule(), new AuthorizationEnforcementModule().getInMemoryModules(), new ProgramRunnerRuntimeModule().getInMemoryModules(), new SecureStoreServerModule(), new MetadataReaderWriterModules().getInMemoryModules(), new MetadataServiceModule(), new AbstractModule() {
@Override
protected void configure() {
bind(MetricsManager.class).toProvider(MetricsManagerProvider.class);
}
}, new MetricsClientRuntimeModule().getInMemoryModules(), new LocalLogAppenderModule(), new LogReaderRuntimeModules().getInMemoryModules(), new ExploreRuntimeModule().getInMemoryModules(), new ExploreClientModule(), new MessagingServerRuntimeModule().getInMemoryModules(), new PreviewConfigModule(cConf, new Configuration(), SConfiguration.create()), new PreviewManagerModule(false), new PreviewRunnerManagerModule().getInMemoryModules(), new SupportBundleServiceModule(), new MockProvisionerModule(), new AbstractModule() {
@Override
protected void configure() {
install(new FactoryModuleBuilder().implement(ApplicationManager.class, DefaultApplicationManager.class).build(ApplicationManagerFactory.class));
install(new FactoryModuleBuilder().implement(ArtifactManager.class, DefaultArtifactManager.class).build(ArtifactManagerFactory.class));
bind(TemporaryFolder.class).toInstance(TMP_FOLDER);
bind(AuthorizationHandler.class).in(Scopes.SINGLETON);
// Needed by MonitorHandlerModuler
bind(TwillRunner.class).to(NoopTwillRunnerService.class);
bind(MetadataSubscriberService.class).in(Scopes.SINGLETON);
}
});
messagingService = injector.getInstance(MessagingService.class);
if (messagingService instanceof Service) {
((Service) messagingService).startAndWait();
}
txService = injector.getInstance(TransactionManager.class);
txService.startAndWait();
metadataSubscriberService = injector.getInstance(MetadataSubscriberService.class);
metadataStorage = injector.getInstance(MetadataStorage.class);
metadataAdmin = injector.getInstance(MetadataAdmin.class);
metadataStorage.createIndex();
metadataService = injector.getInstance(MetadataService.class);
metadataService.startAndWait();
// Define all StructuredTable before starting any services that need StructuredTable
StoreDefinition.createAllTables(injector.getInstance(StructuredTableAdmin.class));
dsOpService = injector.getInstance(DatasetOpExecutorService.class);
dsOpService.startAndWait();
datasetService = injector.getInstance(DatasetService.class);
datasetService.startAndWait();
metricsCollectionService = injector.getInstance(MetricsCollectionService.class);
metricsCollectionService.startAndWait();
if (cConf.getBoolean(Constants.Explore.EXPLORE_ENABLED)) {
exploreExecutorService = injector.getInstance(ExploreExecutorService.class);
exploreExecutorService.startAndWait();
// wait for explore service to be discoverable
DiscoveryServiceClient discoveryService = injector.getInstance(DiscoveryServiceClient.class);
EndpointStrategy endpointStrategy = new RandomEndpointStrategy(() -> discoveryService.discover(Constants.Service.EXPLORE_HTTP_USER_SERVICE));
Preconditions.checkNotNull(endpointStrategy.pick(5, TimeUnit.SECONDS), "%s service is not up after 5 seconds", Constants.Service.EXPLORE_HTTP_USER_SERVICE);
exploreClient = injector.getInstance(ExploreClient.class);
}
programScheduler = injector.getInstance(Scheduler.class);
if (programScheduler instanceof Service) {
((Service) programScheduler).startAndWait();
}
testManager = injector.getInstance(UnitTestManager.class);
metricsManager = injector.getInstance(MetricsManager.class);
accessControllerInstantiator = injector.getInstance(AccessControllerInstantiator.class);
// This is needed so the logged-in user can successfully create the default namespace
if (cConf.getBoolean(Constants.Security.Authorization.ENABLED)) {
String user = System.getProperty("user.name");
SecurityRequestContext.setUserId(user);
InstanceId instance = new InstanceId(cConf.get(Constants.INSTANCE_NAME));
Principal principal = new Principal(user, Principal.PrincipalType.USER);
accessControllerInstantiator.get().grant(Authorizable.fromEntityId(instance), principal, EnumSet.allOf(StandardPermission.class));
accessControllerInstantiator.get().grant(Authorizable.fromEntityId(NamespaceId.DEFAULT), principal, EnumSet.allOf(StandardPermission.class));
}
namespaceAdmin = injector.getInstance(NamespaceAdmin.class);
if (firstInit) {
// only create the default namespace on first test. if multiple tests are run in the same JVM,
// then any time after the first time, the default namespace already exists. That is because
// the namespaceAdmin.delete(Id.Namespace.DEFAULT) in finish() only clears the default namespace
// but does not remove it entirely
namespaceAdmin.create(NamespaceMeta.DEFAULT);
ProfileService profileService = injector.getInstance(ProfileService.class);
profileService.saveProfile(ProfileId.NATIVE, Profile.NATIVE);
}
secureStore = injector.getInstance(SecureStore.class);
secureStoreManager = injector.getInstance(SecureStoreManager.class);
messagingContext = new MultiThreadMessagingContext(messagingService);
firstInit = false;
previewHttpServer = injector.getInstance(PreviewHttpServer.class);
previewHttpServer.startAndWait();
fieldLineageAdmin = injector.getInstance(FieldLineageAdmin.class);
lineageAdmin = injector.getInstance(LineageAdmin.class);
metadataSubscriberService.startAndWait();
previewRunnerManager = injector.getInstance(PreviewRunnerManager.class);
if (previewRunnerManager instanceof Service) {
((Service) previewRunnerManager).startAndWait();
}
appFabricServer = injector.getInstance(AppFabricServer.class);
appFabricServer.startAndWait();
preferencesService = injector.getInstance(PreferencesService.class);
scheduler = injector.getInstance(Scheduler.class);
if (scheduler instanceof Service) {
((Service) scheduler).startAndWait();
}
if (scheduler instanceof CoreSchedulerService) {
((CoreSchedulerService) scheduler).waitUntilFunctional(10, TimeUnit.SECONDS);
}
supportBundleInternalService = injector.getInstance(SupportBundleInternalService.class);
supportBundleInternalService.startAndWait();
appFabricHealthCheckService = injector.getInstance(HealthCheckService.class);
appFabricHealthCheckService.helper(Constants.AppFabricHealthCheck.APP_FABRIC_HEALTH_CHECK_SERVICE, cConf, Constants.Service.MASTER_SERVICES_BIND_ADDRESS);
appFabricHealthCheckService.startAndWait();
}
use of io.cdap.cdap.proto.id.InstanceId in project cdap by caskdata.
the class PreferencesTable method getResolvedPreferences.
/**
* Get the resolved preferences for the entity id, the preferences are resolved from
* instance -> namespace -> application -> program level
* (i.e. preferences at child level e.g. program take precedence over those at parent level e.g. application)
*
* @param entityId the entity id to get the preferences from
* @return preferences detail
*/
public PreferencesDetail getResolvedPreferences(EntityId entityId) throws IOException {
// if it is instance level get the properties and return
if (entityId.getEntityType().equals(EntityType.INSTANCE)) {
PreferencesDetail preferences = getPreferences(entityId);
return new PreferencesDetail(preferences.getProperties(), preferences.getSeqId(), true);
}
PreferencesDetail parentPreferences = null;
// if the entity id has a parent id, get the preference from its parent
if (entityId instanceof ParentedId) {
parentPreferences = getResolvedPreferences(((ParentedId) entityId).getParent());
} else if (entityId.getEntityType() == EntityType.NAMESPACE) {
parentPreferences = getResolvedPreferences(new InstanceId(""));
}
PreferencesDetail preferences = getPreferences(entityId);
PreferencesDetail resolved = new PreferencesDetail(preferences.getProperties(), preferences.getSeqId(), true);
if (parentPreferences != null) {
resolved = PreferencesDetail.resolve(parentPreferences, resolved);
}
return resolved;
}
use of io.cdap.cdap.proto.id.InstanceId in project cdap by caskdata.
the class AuthEnforceRewriterTest method test.
@Test
public void test() throws Exception {
ByteCodeClassLoader classLoader = new ByteCodeClassLoader(getClass().getClassLoader());
classLoader.addClass(rewrite(DummyAuthEnforce.ValidAuthEnforceAnnotations.class));
classLoader.addClass(rewrite(DummyAuthEnforce.AnotherValidAuthEnforceAnnotations.class));
classLoader.addClass(rewrite(DummyAuthEnforce.ClassImplementingInterfaceWithAuthAnnotation.class));
classLoader.addClass(rewrite(DummyAuthEnforce.ClassWithoutAuthEnforce.class));
classLoader.addClass(rewrite(DummyAuthEnforce.ValidAuthEnforceWithFields.class));
// Need to invoke the method on the object created from the rewritten class in the classloader since trying to
// cast it here to DummyAuthEnforce will fail since the object is created from a class which was loaded from a
// different classloader.
Class<?> cls = classLoader.loadClass(DummyAuthEnforce.ValidAuthEnforceAnnotations.class.getName());
Object rewrittenObject = loadRewritten(classLoader, DummyAuthEnforce.class.getName(), cls.getName());
invokeSetters(cls, rewrittenObject);
// tests a valid AuthEnforce annotation which has single action
testRewrite(getMethod(cls, "testSingleAction", NamespaceId.class), rewrittenObject, ExceptionAccessEnforcer.ExpectedException.class, NamespaceId.DEFAULT);
// tests a valid AuthEnforce annotation which has multiple action
testRewrite(getMethod(cls, "testMultipleAction", NamespaceId.class), rewrittenObject, ExceptionAccessEnforcer.ExpectedException.class, NamespaceId.DEFAULT);
// test that the class rewrite did not affect other non annotated methods
testRewrite(getMethod(cls, "testNoAuthEnforceAnnotation", NamespaceId.class), rewrittenObject, DummyAuthEnforce.EnforceNotCalledException.class, NamespaceId.DEFAULT);
// test that the class rewrite works for method whose signature does not specify throws exception
testRewrite(getMethod(cls, "testMethodWithoutException", NamespaceId.class), rewrittenObject, ExceptionAccessEnforcer.ExpectedException.class, NamespaceId.DEFAULT);
testRewrite(getMethod(cls, "testNameAnnotationPref", NamespaceId.class, String.class), rewrittenObject, NamespaceId.DEFAULT, ExceptionAccessEnforcer.ExpectedException.class, NamespaceId.DEFAULT, "dataset");
testRewrite(getMethod(cls, "testMultipleParts", String.class, String.class), rewrittenObject, new DatasetId("ns", "dataset"), ExceptionAccessEnforcer.ExpectedException.class, "ns", "dataset");
testRewrite(getMethod(cls, "testQueryPathParamAnnotations", String.class, String.class), rewrittenObject, new DatasetId("ns", "dataset"), ExceptionAccessEnforcer.ExpectedException.class, "ns", "dataset");
testRewrite(getMethod(cls, "testMultipleAnnotationsPref", NamespaceId.class), rewrittenObject, ExceptionAccessEnforcer.ExpectedException.class, NamespaceId.DEFAULT);
// test that class rewriting does not happen for classes which does not have AuthEnforce annotation on its method
cls = classLoader.loadClass(DummyAuthEnforce.ClassWithoutAuthEnforce.class.getName());
rewrittenObject = loadRewritten(classLoader, DummyAuthEnforce.class.getName(), cls.getName());
invokeSetters(cls, rewrittenObject);
testRewrite(getMethod(cls, "methodWithoutAuthEnforce", NamespaceId.class), rewrittenObject, DummyAuthEnforce.EnforceNotCalledException.class, NamespaceId.DEFAULT);
// test that class rewriting works for a valid annotated method in another inner class and needs the
// invokeSetters to called independently for this
cls = classLoader.loadClass(DummyAuthEnforce.AnotherValidAuthEnforceAnnotations.class.getName());
rewrittenObject = loadRewritten(classLoader, DummyAuthEnforce.class.getName(), cls.getName());
invokeSetters(cls, rewrittenObject);
testRewrite(getMethod(cls, "testSomeOtherAction", NamespaceId.class), rewrittenObject, ExceptionAccessEnforcer.ExpectedException.class, NamespaceId.DEFAULT);
// test that class rewriting works for a valid annotation with field instances
cls = classLoader.loadClass(DummyAuthEnforce.ValidAuthEnforceWithFields.class.getName());
rewrittenObject = loadRewritten(classLoader, DummyAuthEnforce.class.getName(), cls.getName());
invokeSetters(cls, rewrittenObject);
testRewrite(getMethod(cls, "testNoParameters"), rewrittenObject, ExceptionAccessEnforcer.ExpectedException.class);
testRewrite(getMethod(cls, "testParaNameSameAsField", NamespaceId.class), rewrittenObject, new NamespaceId("ns"), ExceptionAccessEnforcer.ExpectedException.class, NamespaceId.DEFAULT);
testRewrite(getMethod(cls, "testParaPreference", InstanceId.class), rewrittenObject, new InstanceId("i1"), ExceptionAccessEnforcer.ExpectedException.class, new InstanceId("i1"));
testRewrite(getMethod(cls, "testThisClassPreference", NamespaceId.class), rewrittenObject, new NamespaceId("ns"), ExceptionAccessEnforcer.ExpectedException.class, NamespaceId.DEFAULT);
}
Aggregations