Search in sources :

Example 6 with EntityImpersonator

use of co.cask.cdap.security.impersonation.EntityImpersonator in project cdap by caskdata.

the class ConfiguratorTest method testAppWithConfig.

@Test
public void testAppWithConfig() throws Exception {
    LocationFactory locationFactory = new LocalLocationFactory(TMP_FOLDER.newFolder());
    Location appJar = AppJarHelper.createDeploymentJar(locationFactory, ConfigTestApp.class);
    Id.Artifact artifactId = Id.Artifact.from(Id.Namespace.DEFAULT, ConfigTestApp.class.getSimpleName(), "1.0.0");
    CConfiguration cConf = CConfiguration.create();
    ArtifactRepository artifactRepo = new ArtifactRepository(conf, null, null, authorizer, new DummyProgramRunnerFactory(), new DefaultImpersonator(cConf, null), authEnforcer, authenticationContext);
    ConfigTestApp.ConfigClass config = new ConfigTestApp.ConfigClass("myStream", "myTable");
    // Create a configurator that is testable. Provide it an application.
    try (CloseableClassLoader artifactClassLoader = artifactRepo.createArtifactClassLoader(appJar, new EntityImpersonator(artifactId.getNamespace().toEntityId(), new DefaultImpersonator(cConf, null)))) {
        Configurator configuratorWithConfig = new InMemoryConfigurator(conf, Id.Namespace.DEFAULT, artifactId, ConfigTestApp.class.getName(), artifactRepo, artifactClassLoader, null, null, new Gson().toJson(config));
        ListenableFuture<ConfigResponse> result = configuratorWithConfig.config();
        ConfigResponse response = result.get(10, TimeUnit.SECONDS);
        Assert.assertNotNull(response);
        ApplicationSpecificationAdapter adapter = ApplicationSpecificationAdapter.create(new ReflectionSchemaGenerator());
        ApplicationSpecification specification = adapter.fromJson(response.get());
        Assert.assertNotNull(specification);
        Assert.assertTrue(specification.getStreams().size() == 1);
        Assert.assertTrue(specification.getStreams().containsKey("myStream"));
        Assert.assertTrue(specification.getDatasets().size() == 1);
        Assert.assertTrue(specification.getDatasets().containsKey("myTable"));
        Configurator configuratorWithoutConfig = new InMemoryConfigurator(conf, Id.Namespace.DEFAULT, artifactId, ConfigTestApp.class.getName(), artifactRepo, artifactClassLoader, null, null, null);
        result = configuratorWithoutConfig.config();
        response = result.get(10, TimeUnit.SECONDS);
        Assert.assertNotNull(response);
        specification = adapter.fromJson(response.get());
        Assert.assertNotNull(specification);
        Assert.assertTrue(specification.getStreams().size() == 1);
        Assert.assertTrue(specification.getStreams().containsKey(ConfigTestApp.DEFAULT_STREAM));
        Assert.assertTrue(specification.getDatasets().size() == 1);
        Assert.assertTrue(specification.getDatasets().containsKey(ConfigTestApp.DEFAULT_TABLE));
    }
}
Also used : ApplicationSpecification(co.cask.cdap.api.app.ApplicationSpecification) DummyProgramRunnerFactory(co.cask.cdap.app.runtime.DummyProgramRunnerFactory) Configurator(co.cask.cdap.app.deploy.Configurator) EntityImpersonator(co.cask.cdap.security.impersonation.EntityImpersonator) Gson(com.google.gson.Gson) ArtifactRepository(co.cask.cdap.internal.app.runtime.artifact.ArtifactRepository) ConfigResponse(co.cask.cdap.app.deploy.ConfigResponse) CloseableClassLoader(co.cask.cdap.api.artifact.CloseableClassLoader) ReflectionSchemaGenerator(co.cask.cdap.internal.io.ReflectionSchemaGenerator) CConfiguration(co.cask.cdap.common.conf.CConfiguration) DefaultImpersonator(co.cask.cdap.security.impersonation.DefaultImpersonator) LocalLocationFactory(org.apache.twill.filesystem.LocalLocationFactory) LocationFactory(org.apache.twill.filesystem.LocationFactory) ApplicationSpecificationAdapter(co.cask.cdap.internal.app.ApplicationSpecificationAdapter) Id(co.cask.cdap.proto.Id) LocalLocationFactory(org.apache.twill.filesystem.LocalLocationFactory) ConfigTestApp(co.cask.cdap.ConfigTestApp) Location(org.apache.twill.filesystem.Location) Test(org.junit.Test)

Example 7 with EntityImpersonator

use of co.cask.cdap.security.impersonation.EntityImpersonator in project cdap by caskdata.

the class ArtifactInspectorTest method testInvalidConfigApp.

@Test(expected = InvalidArtifactException.class)
public void testInvalidConfigApp() throws Exception {
    Manifest manifest = new Manifest();
    File appFile = createJar(InvalidConfigApp.class, new File(TMP_FOLDER.newFolder(), "InvalidConfigApp-1.0.0.jar"), manifest);
    Id.Artifact artifactId = Id.Artifact.from(Id.Namespace.DEFAULT, "InvalidConfigApp", "1.0.0");
    Location artifactLocation = Locations.toLocation(appFile);
    try (CloseableClassLoader artifactClassLoader = classLoaderFactory.createClassLoader(artifactLocation, new EntityImpersonator(artifactId.toEntityId(), new DefaultImpersonator(CConfiguration.create(), null)))) {
        artifactInspector.inspectArtifact(artifactId, appFile, artifactClassLoader);
    }
}
Also used : EntityImpersonator(co.cask.cdap.security.impersonation.EntityImpersonator) Id(co.cask.cdap.proto.Id) CloseableClassLoader(co.cask.cdap.api.artifact.CloseableClassLoader) Manifest(java.util.jar.Manifest) File(java.io.File) DefaultImpersonator(co.cask.cdap.security.impersonation.DefaultImpersonator) Location(org.apache.twill.filesystem.Location) Test(org.junit.Test)

Aggregations

EntityImpersonator (co.cask.cdap.security.impersonation.EntityImpersonator)7 CloseableClassLoader (co.cask.cdap.api.artifact.CloseableClassLoader)5 Location (org.apache.twill.filesystem.Location)5 Id (co.cask.cdap.proto.Id)4 DefaultImpersonator (co.cask.cdap.security.impersonation.DefaultImpersonator)4 Test (org.junit.Test)4 ApplicationSpecification (co.cask.cdap.api.app.ApplicationSpecification)3 ConfigResponse (co.cask.cdap.app.deploy.ConfigResponse)3 ReflectionSchemaGenerator (co.cask.cdap.internal.io.ReflectionSchemaGenerator)3 ApplicationClass (co.cask.cdap.api.artifact.ApplicationClass)2 ArtifactClasses (co.cask.cdap.api.artifact.ArtifactClasses)2 ArtifactRange (co.cask.cdap.api.artifact.ArtifactRange)2 PluginClass (co.cask.cdap.api.plugin.PluginClass)2 Configurator (co.cask.cdap.app.deploy.Configurator)2 DummyProgramRunnerFactory (co.cask.cdap.app.runtime.DummyProgramRunnerFactory)2 CConfiguration (co.cask.cdap.common.conf.CConfiguration)2 ApplicationSpecificationAdapter (co.cask.cdap.internal.app.ApplicationSpecificationAdapter)2 ArtifactRepository (co.cask.cdap.internal.app.runtime.artifact.ArtifactRepository)2 File (java.io.File)2 Manifest (java.util.jar.Manifest)2