use of io.cdap.cdap.api.artifact.ArtifactSummary in project cdap by cdapio.
the class AppLifecycleHttpHandlerTest method testDeployVersionedAndNonVersionedApp.
@Test
public void testDeployVersionedAndNonVersionedApp() throws Exception {
Id.Artifact artifactId = Id.Artifact.from(Id.Namespace.DEFAULT, "configapp", "1.0.0");
addAppArtifact(artifactId, ConfigTestApp.class);
ApplicationId appId = new ApplicationId(Id.Namespace.DEFAULT.getId(), "cfgAppWithVersion", "1.0.0");
ConfigTestApp.ConfigClass config = new ConfigTestApp.ConfigClass("abc", "def");
AppRequest<ConfigTestApp.ConfigClass> request = new AppRequest<>(new ArtifactSummary(artifactId.getName(), artifactId.getVersion().getVersion()), config);
Assert.assertEquals(200, deploy(appId, request).getResponseCode());
// Cannot update the app created by versioned API with versionId not ending with "-SNAPSHOT"
Assert.assertEquals(409, deploy(appId, request).getResponseCode());
Assert.assertEquals(404, getAppResponse(Id.Namespace.DEFAULT.getId(), appId.getApplication(), "non_existing_version").getResponseCode());
Assert.assertEquals(404, getAppResponse(Id.Namespace.DEFAULT.getId(), appId.getApplication()).getResponseCode());
// Deploy app with default versionId by non-versioned API
Id.Application appIdDefault = Id.Application.from(Id.Namespace.DEFAULT, appId.getApplication());
ConfigTestApp.ConfigClass configDefault = new ConfigTestApp.ConfigClass("uvw", "xyz");
AppRequest<ConfigTestApp.ConfigClass> requestDefault = new AppRequest<>(new ArtifactSummary(artifactId.getName(), artifactId.getVersion().getVersion()), configDefault);
Assert.assertEquals(200, deploy(appIdDefault, requestDefault).getResponseCode());
// Deploy app with versionId "version_2" by versioned API
ApplicationId appIdV2 = new ApplicationId(appId.getNamespace(), appId.getApplication(), "2.0.0");
ConfigTestApp.ConfigClass configV2 = new ConfigTestApp.ConfigClass("ghi", "jkl");
AppRequest<ConfigTestApp.ConfigClass> requestV2 = new AppRequest<>(new ArtifactSummary(artifactId.getName(), artifactId.getVersion().getVersion()), configV2);
Assert.assertEquals(200, deploy(appIdV2, requestV2).getResponseCode());
Set<String> versions = ImmutableSet.of("-SNAPSHOT", "2.0.0", "1.0.0");
Assert.assertEquals(versions, getAppVersions(appId.getNamespace(), appId.getApplication()));
List<JsonObject> appList = getAppList(appId.getNamespace());
Set<String> receivedVersions = new HashSet<>();
for (JsonObject appRecord : appList) {
receivedVersions.add(appRecord.getAsJsonPrimitive("version").getAsString());
}
Assert.assertEquals(versions, receivedVersions);
JsonObject appDetails = getAppDetails(appId.getNamespace(), appId.getApplication(), appId.getVersion());
Assert.assertEquals(GSON.toJson(config), appDetails.get("configuration").getAsString());
Assert.assertEquals(appId.getVersion(), appDetails.get("appVersion").getAsString());
// Get app info for the app with default versionId by versioned API
JsonObject appDetailsDefault = getAppDetails(appId.getNamespace(), appId.getApplication(), ApplicationId.DEFAULT_VERSION);
Assert.assertEquals(GSON.toJson(configDefault), appDetailsDefault.get("configuration").getAsString());
Assert.assertEquals(ApplicationId.DEFAULT_VERSION, appDetailsDefault.get("appVersion").getAsString());
// Get app info for the app with versionId "version_2" by versioned API
JsonObject appDetailsV2 = getAppDetails(appId.getNamespace(), appId.getApplication(), appIdV2.getVersion());
Assert.assertEquals(GSON.toJson(configV2), appDetailsV2.get("configuration").getAsString());
// Update app with default versionId by versioned API
ConfigTestApp.ConfigClass configDefault2 = new ConfigTestApp.ConfigClass("mno", "pqr");
AppRequest<ConfigTestApp.ConfigClass> requestDefault2 = new AppRequest<>(new ArtifactSummary(artifactId.getName(), artifactId.getVersion().getVersion()), configDefault2);
Assert.assertEquals(200, deploy(appIdDefault.toEntityId(), requestDefault2).getResponseCode());
JsonObject appDetailsDefault2 = getAppDetails(appIdDefault.getNamespaceId(), appIdDefault.getId());
Assert.assertEquals(GSON.toJson(configDefault2), appDetailsDefault2.get("configuration").getAsString());
// Get updated app info for the app with default versionId by versioned API
JsonObject appDetailsDefault2WithVersion = getAppDetails(appIdDefault.getNamespaceId(), appIdDefault.getId(), ApplicationId.DEFAULT_VERSION);
Assert.assertEquals(GSON.toJson(configDefault2), appDetailsDefault2WithVersion.get("configuration").getAsString());
Assert.assertEquals(ApplicationId.DEFAULT_VERSION, appDetailsDefault.get("appVersion").getAsString());
deleteApp(appId, 200);
deleteApp(appIdDefault, 200);
deleteApp(appIdV2, 200);
}
use of io.cdap.cdap.api.artifact.ArtifactSummary in project cdap by cdapio.
the class AppLifecycleHttpHandlerTest method testDeployUsingArtifact.
@Test
public void testDeployUsingArtifact() throws Exception {
Id.Artifact artifactId = Id.Artifact.from(Id.Namespace.DEFAULT, "configapp", "1.0.0");
addAppArtifact(artifactId, ConfigTestApp.class);
Id.Application appId = Id.Application.from(Id.Namespace.DEFAULT, "cfgApp");
ConfigTestApp.ConfigClass config = new ConfigTestApp.ConfigClass("abc", "def");
AppRequest<ConfigTestApp.ConfigClass> request = new AppRequest<>(new ArtifactSummary(artifactId.getName(), artifactId.getVersion().getVersion()), config);
Assert.assertEquals(200, deploy(appId, request).getResponseCode());
JsonObject appDetails = getAppDetails(Id.Namespace.DEFAULT.getId(), appId.getId());
Assert.assertEquals(GSON.toJson(config), appDetails.get("configuration").getAsString());
Assert.assertEquals(200, doDelete(getVersionedAPIPath("apps/" + appId.getId(), appId.getNamespaceId())).getResponseCode());
}
use of io.cdap.cdap.api.artifact.ArtifactSummary in project cdap by cdapio.
the class PreviewServiceMainTest method deleteAllArtifact.
/**
* List all artifacts in the default namespaces and delete all of them.
*/
private void deleteAllArtifact() throws IOException {
HttpRequestConfig requestConfig = getHttpRequestConfig();
URL url = getRouterBaseURI().resolve(String.format("/v3/namespaces/default/artifacts")).toURL();
HttpResponse response = HttpRequests.execute(HttpRequest.get(url).build(), requestConfig);
Assert.assertEquals(response.getResponseBodyAsString(), HttpURLConnection.HTTP_OK, response.getResponseCode());
List<ArtifactSummary> summaryList = GSON.fromJson(response.getResponseBodyAsString(), ARTIFACT_SUMMARY_LIST);
for (ArtifactSummary summary : summaryList) {
url = getRouterBaseURI().resolve(String.format("/v3/namespaces/default/artifacts/%s/versions/%s", summary.getName(), summary.getVersion())).toURL();
response = HttpRequests.execute(HttpRequest.delete(url).build(), requestConfig);
Assert.assertEquals(response.getResponseBodyAsString(), HttpURLConnection.HTTP_OK, response.getResponseCode());
}
}
use of io.cdap.cdap.api.artifact.ArtifactSummary in project cdap by cdapio.
the class Spark2Test method deploy.
private ApplicationManager deploy(NamespaceId namespaceId, Class<? extends Application> appClass) throws Exception {
ArtifactId artifactId = new ArtifactId(namespaceId.getNamespace(), appClass.getSimpleName(), "1.0-SNAPSHOT");
addArtifact(artifactId, ARTIFACTS.get(appClass));
AppRequest<?> appRequest = new AppRequest<>(new ArtifactSummary(artifactId.getArtifact(), artifactId.getVersion()), null);
return deployApplication(namespaceId.app(appClass.getSimpleName()), appRequest);
}
use of io.cdap.cdap.api.artifact.ArtifactSummary in project cdap by cdapio.
the class AuthorizationTest method testApps.
@Test
@Category(SlowTests.class)
public void testApps() throws Exception {
try {
deployApplication(NamespaceId.DEFAULT, DummyApp.class);
Assert.fail("App deployment should fail because alice does not have ADMIN privilege on the application");
} catch (UnauthorizedException e) {
// Expected
}
createAuthNamespace();
AccessController accessController = getAccessController();
ApplicationId dummyAppId = AUTH_NAMESPACE.app(DummyApp.class.getSimpleName());
Map<EntityId, Set<? extends Permission>> neededPrivileges = ImmutableMap.<EntityId, Set<? extends Permission>>builder().put(dummyAppId, EnumSet.of(StandardPermission.CREATE, StandardPermission.GET, StandardPermission.DELETE)).put(AUTH_NAMESPACE.artifact(DummyApp.class.getSimpleName(), "1.0-SNAPSHOT"), EnumSet.of(StandardPermission.CREATE)).put(AUTH_NAMESPACE.dataset("whom"), EnumSet.of(StandardPermission.GET, StandardPermission.CREATE)).put(AUTH_NAMESPACE.dataset("customDataset"), EnumSet.of(StandardPermission.GET, StandardPermission.CREATE)).put(AUTH_NAMESPACE.datasetType(KeyValueTable.class.getName()), EnumSet.of(StandardPermission.UPDATE)).build();
setUpPrivilegeAndRegisterForDeletion(ALICE, neededPrivileges);
// alice will not be able to deploy the app since she does not have privilege on the implicit dataset module
try {
deployApplication(AUTH_NAMESPACE, DummyApp.class);
Assert.fail();
} catch (UnauthorizedException e) {
// expected
}
// grant alice the required implicit type and module
grantAndAssertSuccess(AUTH_NAMESPACE.datasetType(DummyApp.CustomDummyDataset.class.getName()), ALICE, EnumSet.of(StandardPermission.GET, StandardPermission.CREATE));
cleanUpEntities.add(AUTH_NAMESPACE.datasetType(DummyApp.CustomDummyDataset.class.getName()));
grantAndAssertSuccess(AUTH_NAMESPACE.datasetModule(DummyApp.CustomDummyDataset.class.getName()), ALICE, EnumSet.of(StandardPermission.CREATE, StandardPermission.GET));
cleanUpEntities.add(AUTH_NAMESPACE.datasetModule(DummyApp.CustomDummyDataset.class.getName()));
// this time it should be successful
ApplicationManager appManager = deployApplication(AUTH_NAMESPACE, DummyApp.class);
// Bob should not have any privileges on Alice's app
Assert.assertTrue("Bob should not have any privileges on alice's app", accessController.listGrants(BOB).isEmpty());
// update should succeed because alice has admin privileges on the app
appManager.update(new AppRequest(new ArtifactSummary(DummyApp.class.getSimpleName(), "1.0-SNAPSHOT")));
// Update should fail for Bob
SecurityRequestContext.setUserId(BOB.getName());
try {
appManager.update(new AppRequest(new ArtifactSummary(DummyApp.class.getSimpleName(), "1.0-SNAPSHOT")));
Assert.fail("App update should have failed because Bob does not have admin privileges on the app.");
} catch (UnauthorizedException expected) {
// expected
}
// grant READ and WRITE to Bob
grantAndAssertSuccess(AUTH_NAMESPACE, BOB, ImmutableSet.of(StandardPermission.GET));
grantAndAssertSuccess(dummyAppId, BOB, ImmutableSet.of(StandardPermission.GET, StandardPermission.UPDATE));
// delete should fail
try {
appManager.delete();
} catch (UnauthorizedException expected) {
// expected
}
// grant DELETE to Bob. Now delete should succeed
grantAndAssertSuccess(dummyAppId, BOB, ImmutableSet.of(StandardPermission.DELETE));
// deletion should succeed since BOB has privileges on the app
appManager.delete();
// Should still have the privilege for the app since we no longer revoke privileges after deletion of an entity
Assert.assertTrue(!getAccessController().isVisible(Collections.singleton(dummyAppId), BOB).isEmpty());
// bob should still have privileges granted to him
Assert.assertEquals(4, accessController.listGrants(BOB).size());
// switch back to Alice
SecurityRequestContext.setUserId(ALICE.getName());
// Deploy a couple of apps in the namespace
// Deploy dummy app should be successful since we already pre-grant the required privileges
deployApplication(AUTH_NAMESPACE, DummyApp.class);
final ApplicationId appId = AUTH_NAMESPACE.app(AllProgramsApp.NAME);
Map<EntityId, Set<? extends Permission>> anotherAppNeededPrivilege = ImmutableMap.<EntityId, Set<? extends Permission>>builder().put(appId, EnumSet.of(StandardPermission.GET, StandardPermission.CREATE, StandardPermission.DELETE)).put(AUTH_NAMESPACE.artifact(AllProgramsApp.class.getSimpleName(), "1.0-SNAPSHOT"), EnumSet.of(StandardPermission.GET, StandardPermission.CREATE)).put(AUTH_NAMESPACE.dataset(AllProgramsApp.DATASET_NAME), EnumSet.of(StandardPermission.GET, StandardPermission.CREATE)).put(AUTH_NAMESPACE.dataset(AllProgramsApp.DATASET_NAME2), EnumSet.of(StandardPermission.GET, StandardPermission.CREATE)).put(AUTH_NAMESPACE.dataset(AllProgramsApp.DATASET_NAME3), EnumSet.of(StandardPermission.GET, StandardPermission.CREATE)).put(AUTH_NAMESPACE.dataset(AllProgramsApp.DS_WITH_SCHEMA_NAME), EnumSet.of(StandardPermission.GET, StandardPermission.CREATE)).put(AUTH_NAMESPACE.datasetType(ObjectMappedTable.class.getName()), EnumSet.of(StandardPermission.GET, StandardPermission.CREATE)).build();
setUpPrivilegeAndRegisterForDeletion(ALICE, anotherAppNeededPrivilege);
Map<EntityId, Set<? extends Permission>> bobDatasetPrivileges = ImmutableMap.<EntityId, Set<? extends Permission>>builder().put(AUTH_NAMESPACE.dataset(AllProgramsApp.DATASET_NAME), EnumSet.of(StandardPermission.UPDATE)).put(AUTH_NAMESPACE.dataset(AllProgramsApp.DATASET_NAME2), EnumSet.of(StandardPermission.UPDATE)).build();
Map<EntityId, Set<? extends Permission>> bobProgramPrivileges = ImmutableMap.<EntityId, Set<? extends Permission>>builder().put(appId, EnumSet.of(StandardPermission.GET)).put(appId.program(ProgramType.SERVICE, AllProgramsApp.NoOpService.NAME), EnumSet.of(ApplicationPermission.EXECUTE)).put(appId.program(ProgramType.WORKER, AllProgramsApp.NoOpWorker.NAME), EnumSet.of(ApplicationPermission.EXECUTE)).build();
setUpPrivilegeAndRegisterForDeletion(BOB, bobDatasetPrivileges);
setUpPrivilegeAndRegisterForDeletion(BOB, bobProgramPrivileges);
deployApplication(AUTH_NAMESPACE, AllProgramsApp.class);
// Switch to BOB since he does not have any privilege
SecurityRequestContext.setUserId(BOB.getName());
// deleting all apps should fail because bob does not have admin privileges on the apps and the namespace
try {
deleteAllApplications(AUTH_NAMESPACE);
Assert.fail("Deleting all applications in the namespace should have failed because bob does not have ADMIN " + "privilege on the workflow app.");
} catch (UnauthorizedException expected) {
// expected
}
// Switch to ALICE, deletion should be successful since ALICE has ADMIN privileges
SecurityRequestContext.setUserId(ALICE.getName());
deleteAllApplications(AUTH_NAMESPACE);
}
Aggregations