Search in sources :

Example 1 with InstanceId

use of io.cdap.cdap.proto.id.InstanceId in project cdap by caskdata.

the class EntityExistenceTest method testExists.

@Test
@SuppressWarnings("unchecked")
public void testExists() throws NotFoundException, UnauthorizedException {
    existenceVerifier.ensureExists(new InstanceId(EXISTS));
    existenceVerifier.ensureExists(NAMESPACE);
    existenceVerifier.ensureExists(ARTIFACT);
    ApplicationId app = NAMESPACE.app(AllProgramsApp.NAME);
    existenceVerifier.ensureExists(app);
    existenceVerifier.ensureExists(app.mr(AllProgramsApp.NoOpMR.NAME));
    existenceVerifier.ensureExists(NAMESPACE.dataset(AllProgramsApp.DATASET_NAME));
}
Also used : InstanceId(io.cdap.cdap.proto.id.InstanceId) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) Test(org.junit.Test)

Example 2 with InstanceId

use of io.cdap.cdap.proto.id.InstanceId in project cdap by caskdata.

the class ProfileServiceTest method testAddDeleteAssignments.

@Test
public void testAddDeleteAssignments() throws Exception {
    ProfileId myProfile = NamespaceId.DEFAULT.profile("MyProfile");
    Profile profile1 = new Profile("MyProfile", Profile.NATIVE.getLabel(), Profile.NATIVE.getDescription(), Profile.NATIVE.getScope(), Profile.NATIVE.getProvisioner());
    profileService.saveProfile(myProfile, profile1);
    // add a profile assignment and verify
    Set<EntityId> expected = new HashSet<>();
    expected.add(NamespaceId.DEFAULT);
    profileService.addProfileAssignment(myProfile, NamespaceId.DEFAULT);
    Assert.assertEquals(expected, profileService.getProfileAssignments(myProfile));
    // add more and verify
    InstanceId instanceId = new InstanceId("");
    ApplicationId myApp = NamespaceId.DEFAULT.app("myApp");
    ProgramId myProgram = myApp.workflow("myProgram");
    expected.add(instanceId);
    expected.add(myApp);
    expected.add(myProgram);
    profileService.addProfileAssignment(myProfile, instanceId);
    profileService.addProfileAssignment(myProfile, myApp);
    profileService.addProfileAssignment(myProfile, myProgram);
    Assert.assertEquals(expected, profileService.getProfileAssignments(myProfile));
    // add same entity id should not affect
    profileService.addProfileAssignment(myProfile, myApp);
    Assert.assertEquals(expected, profileService.getProfileAssignments(myProfile));
    // delete one and verify
    expected.remove(myApp);
    profileService.removeProfileAssignment(myProfile, myApp);
    Assert.assertEquals(expected, profileService.getProfileAssignments(myProfile));
    // delete all
    for (EntityId entityId : expected) {
        profileService.removeProfileAssignment(myProfile, entityId);
    }
    expected.clear();
    Assert.assertEquals(expected, profileService.getProfileAssignments(myProfile));
    // delete again should not affect
    profileService.removeProfileAssignment(myProfile, myApp);
    Assert.assertEquals(expected, profileService.getProfileAssignments(myProfile));
    profileService.disableProfile(myProfile);
    profileService.deleteProfile(myProfile);
}
Also used : ProfileId(io.cdap.cdap.proto.id.ProfileId) EntityId(io.cdap.cdap.proto.id.EntityId) InstanceId(io.cdap.cdap.proto.id.InstanceId) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) ProgramId(io.cdap.cdap.proto.id.ProgramId) Profile(io.cdap.cdap.proto.profile.Profile) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with InstanceId

use of io.cdap.cdap.proto.id.InstanceId in project cdap by caskdata.

the class ApplicationLifecycleService method createAppDetailsArchive.

/**
 * Creates a ZIP archive that contains the {@link ApplicationDetail} for all applications. The archive created will
 * contain a directory entry for each of the namespace. Inside each namespace directory, it contains the
 * application detail json, the application name as the file name, with {@code ".json"} as the file extension.
 * <p/>
 * This method requires instance admin permission.
 *
 * @param zipOut the {@link ZipOutputStream} for writing out the application details
 */
public void createAppDetailsArchive(ZipOutputStream zipOut) throws Exception {
    accessEnforcer.enforce(new InstanceId(cConf.get(Constants.INSTANCE_NAME)), authenticationContext.getPrincipal(), StandardPermission.GET);
    Set<NamespaceId> namespaces = new HashSet<>();
    JsonWriter jsonWriter = new JsonWriter(new OutputStreamWriter(zipOut, StandardCharsets.UTF_8));
    store.scanApplications(batchSize, (appId, appSpec) -> {
        // Skip the SYSTEM namespace apps
        if (NamespaceId.SYSTEM.equals(appId.getParent())) {
            return;
        }
        try {
            ApplicationDetail applicationDetail = enforceApplicationDetailAccess(appId, ApplicationDetail.fromSpec(appSpec, null));
            // Add a directory for the namespace
            if (namespaces.add(appId.getParent())) {
                ZipEntry entry = new ZipEntry(appId.getNamespace() + "/");
                zipOut.putNextEntry(entry);
                zipOut.closeEntry();
            }
            ZipEntry entry = new ZipEntry(appId.getNamespace() + "/" + appId.getApplication() + ".json");
            zipOut.putNextEntry(entry);
            GSON.toJson(applicationDetail, ApplicationDetail.class, jsonWriter);
            jsonWriter.flush();
            zipOut.closeEntry();
        } catch (IOException | AccessException e) {
            throw new RuntimeException("Failed to add zip entry for application " + appId, e);
        }
    });
}
Also used : ApplicationDetail(io.cdap.cdap.proto.ApplicationDetail) AccessException(io.cdap.cdap.api.security.AccessException) InstanceId(io.cdap.cdap.proto.id.InstanceId) ZipEntry(java.util.zip.ZipEntry) OutputStreamWriter(java.io.OutputStreamWriter) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) IOException(java.io.IOException) JsonIOException(com.google.gson.JsonIOException) JsonWriter(com.google.gson.stream.JsonWriter) HashSet(java.util.HashSet)

Example 4 with InstanceId

use of io.cdap.cdap.proto.id.InstanceId in project cdap by caskdata.

the class MonitorHandlerAuthorizationTest method testGetServiceSpecAuthorization.

@Test
public void testGetServiceSpecAuthorization() throws Exception {
    InstanceId instanceId = InstanceId.SELF;
    MonitorHandler handler = createMonitorHandler(Authorizable.fromEntityId(instanceId, EntityType.SYSTEM_SERVICE), Arrays.asList(StandardPermission.LIST));
    HttpRequest request = mock(HttpRequest.class);
    HttpResponder responder = mock(HttpResponder.class);
    AuthenticationTestContext.actAsPrincipal(UNPRIVILEGED_PRINCIPAL);
    try {
        handler.getServiceSpec(request, responder);
    } catch (UnauthorizedException e) {
    // expected
    }
    AuthenticationTestContext.actAsPrincipal(MASTER_PRINCIPAL);
    handler.getServiceSpec(request, responder);
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) HttpResponder(io.cdap.http.HttpResponder) InstanceId(io.cdap.cdap.proto.id.InstanceId) MonitorHandler(io.cdap.cdap.gateway.handlers.MonitorHandler) UnauthorizedException(io.cdap.cdap.security.spi.authorization.UnauthorizedException) Test(org.junit.Test)

Example 5 with InstanceId

use of io.cdap.cdap.proto.id.InstanceId in project cdap by caskdata.

the class PreferencesHttpHandler method deleteInstancePrefs.

@Path("/preferences")
@DELETE
public void deleteInstancePrefs(HttpRequest request, HttpResponder responder) throws Exception {
    InstanceId instanceId = new InstanceId("");
    accessEnforcer.enforce(instanceId, authenticationContext.getPrincipal(), StandardPermission.UPDATE);
    preferencesService.deleteProperties();
    responder.sendStatus(HttpResponseStatus.OK);
}
Also used : InstanceId(io.cdap.cdap.proto.id.InstanceId) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE)

Aggregations

InstanceId (io.cdap.cdap.proto.id.InstanceId)16 Test (org.junit.Test)8 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)5 HashSet (java.util.HashSet)4 ProfileService (io.cdap.cdap.internal.profile.ProfileService)3 EntityId (io.cdap.cdap.proto.id.EntityId)3 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)3 HashMap (java.util.HashMap)3 Service (com.google.common.util.concurrent.Service)2 MetricsCollectionService (io.cdap.cdap.api.metrics.MetricsCollectionService)2 HealthCheckService (io.cdap.cdap.common.service.HealthCheckService)2 NoopTwillRunnerService (io.cdap.cdap.common.twill.NoopTwillRunnerService)2 PreferencesService (io.cdap.cdap.config.PreferencesService)2 DatasetService (io.cdap.cdap.data2.datafabric.dataset.service.DatasetService)2 PreferencesDetail (io.cdap.cdap.proto.PreferencesDetail)2 Path (javax.ws.rs.Path)2 JsonIOException (com.google.gson.JsonIOException)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 JsonWriter (com.google.gson.stream.JsonWriter)1 AbstractModule (com.google.inject.AbstractModule)1