Search in sources :

Example 16 with ScheduleId

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

the class ProfileMetadataMessageProcessor method collectScheduleProfileMetadata.

private void collectScheduleProfileMetadata(ProgramSchedule schedule, ProfileId programProfile, List<MetadataMutation> updates) {
    ScheduleId scheduleId = schedule.getScheduleId();
    // if we are able to get profile from preferences or schedule properties, use it
    // otherwise default profile will be used
    Optional<ProfileId> scheduleProfileId = SystemArguments.getProfileIdFromArgs(scheduleId.getNamespaceId(), schedule.getProperties());
    programProfile = scheduleProfileId.orElse(programProfile);
    addProfileMetadataUpdate(scheduleId, programProfile, updates);
}
Also used : ProfileId(io.cdap.cdap.proto.id.ProfileId) ScheduleId(io.cdap.cdap.proto.id.ScheduleId)

Example 17 with ScheduleId

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

the class ProfileMetadataMessageProcessor method removeProfileMetadata.

/**
 * Remove the profile metadata according to the message, currently only meant for application and schedule.
 */
private void removeProfileMetadata(MetadataMessage message) throws IOException {
    EntityId entity = message.getEntityId();
    List<MetadataMutation> deletes = new ArrayList<>();
    // We only care about application and schedules.
    if (entity.getEntityType().equals(EntityType.APPLICATION)) {
        LOG.trace("Removing profile metadata for {}", entity);
        ApplicationId appId = (ApplicationId) message.getEntityId();
        ApplicationSpecification appSpec = message.getPayload(GSON, ApplicationSpecification.class);
        for (ProgramId programId : getAllProfileAllowedPrograms(appSpec, appId)) {
            addProfileMetadataDelete(programId, deletes);
        }
        for (ScheduleId scheduleId : getSchedulesInApp(appId, appSpec.getProgramSchedules())) {
            addProfileMetadataDelete(scheduleId, deletes);
        }
        addPluginMetadataDelete(appId, appSpec, deletes);
    } else if (entity.getEntityType().equals(EntityType.SCHEDULE)) {
        addProfileMetadataDelete((NamespacedEntityId) entity, deletes);
    }
    if (!deletes.isEmpty()) {
        metadataStorage.batch(deletes, MutationOptions.DEFAULT);
    }
}
Also used : NamespacedEntityId(io.cdap.cdap.proto.id.NamespacedEntityId) EntityId(io.cdap.cdap.proto.id.EntityId) ApplicationSpecification(io.cdap.cdap.api.app.ApplicationSpecification) MetadataMutation(io.cdap.cdap.spi.metadata.MetadataMutation) NamespacedEntityId(io.cdap.cdap.proto.id.NamespacedEntityId) ArrayList(java.util.ArrayList) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) ProgramId(io.cdap.cdap.proto.id.ProgramId) ScheduleId(io.cdap.cdap.proto.id.ScheduleId)

Example 18 with ScheduleId

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

the class AuthorizationTest method testScheduleAuth.

@Test
public void testScheduleAuth() throws Exception {
    createAuthNamespace();
    ApplicationId appId = AUTH_NAMESPACE.app(AppWithSchedule.class.getSimpleName());
    Map<EntityId, Set<? extends Permission>> neededPrivileges = ImmutableMap.<EntityId, Set<? extends Permission>>builder().put(appId, EnumSet.of(StandardPermission.CREATE, StandardPermission.GET)).put(AUTH_NAMESPACE.artifact(AppWithSchedule.class.getSimpleName(), "1.0-SNAPSHOT"), EnumSet.of(StandardPermission.CREATE)).put(AUTH_NAMESPACE.dataset(AppWithSchedule.INPUT_NAME), EnumSet.of(StandardPermission.CREATE, StandardPermission.GET)).put(AUTH_NAMESPACE.dataset(AppWithSchedule.OUTPUT_NAME), EnumSet.of(StandardPermission.CREATE, StandardPermission.GET)).put(AUTH_NAMESPACE.datasetType(ObjectStore.class.getName()), EnumSet.of(StandardPermission.UPDATE)).build();
    setUpPrivilegeAndRegisterForDeletion(ALICE, neededPrivileges);
    ApplicationManager appManager = deployApplication(AUTH_NAMESPACE, AppWithSchedule.class);
    String workflowName = AppWithSchedule.SampleWorkflow.class.getSimpleName();
    ProgramId workflowID = new ProgramId(AUTH_NAMESPACE.getNamespace(), AppWithSchedule.class.getSimpleName(), ProgramType.WORKFLOW, workflowName);
    cleanUpEntities.add(workflowID);
    final WorkflowManager workflowManager = appManager.getWorkflowManager(workflowName);
    ScheduleManager scheduleManager = workflowManager.getSchedule(AppWithSchedule.EVERY_HOUR_SCHEDULE);
    // switch to BOB
    SecurityRequestContext.setUserId(BOB.getName());
    // try to resume schedule as BOB. It should fail since BOB does not have execute privileges on the programs
    try {
        scheduleManager.resume();
        Assert.fail("Resuming schedule should have failed since BOB does not have EXECUTE on the program");
    } catch (UnauthorizedException e) {
    // Expected
    }
    // bob should also not be able see the status of the schedule
    try {
        scheduleManager.status(HttpURLConnection.HTTP_FORBIDDEN);
        Assert.fail("Getting schedule status should have failed since BOB does not have any privilege on the program");
    } catch (UnauthorizedException e) {
    // Expected
    }
    // give BOB READ permission in the workflow
    grantAndAssertSuccess(workflowID, BOB, EnumSet.of(StandardPermission.GET));
    // switch to BOB
    SecurityRequestContext.setUserId(BOB.getName());
    // try to resume schedule as BOB. It should fail since BOB has READ but not EXECUTE on the workflow
    try {
        scheduleManager.resume();
        Assert.fail("Resuming schedule should have failed since BOB does not have EXECUTE on the program");
    } catch (UnauthorizedException e) {
    // Expected
    }
    // but BOB should be able to get schedule status now
    Assert.assertEquals(ProgramScheduleStatus.SUSPENDED.name(), scheduleManager.status(HttpURLConnection.HTTP_OK));
    // give BOB EXECUTE permission in the workflow
    grantAndAssertSuccess(workflowID, BOB, EnumSet.of(ApplicationPermission.EXECUTE));
    // switch to BOB
    SecurityRequestContext.setUserId(BOB.getName());
    // try to resume the schedule. This should pass and workflow should run
    scheduleManager.resume();
    Assert.assertEquals(ProgramScheduleStatus.SCHEDULED.name(), scheduleManager.status(HttpURLConnection.HTTP_OK));
    // suspend the schedule so that it does not start running again
    scheduleManager.suspend();
    Assert.assertEquals(ProgramScheduleStatus.SUSPENDED.name(), scheduleManager.status(HttpURLConnection.HTTP_OK));
    ScheduleId scheduleId = new ScheduleId(appId.getNamespace(), appId.getApplication(), appId.getVersion(), "testSchedule");
    ScheduleDetail scheduleDetail = new ScheduleDetail(AUTH_NAMESPACE.getNamespace(), AppWithSchedule.class.getSimpleName(), "1.0-SNAPSHOT", "testSchedule", "Something 2", new ScheduleProgramInfo(SchedulableProgramType.WORKFLOW, workflowName), Collections.<String, String>emptyMap(), new TimeTrigger("*/1 * * * *"), Collections.<Constraint>emptyList(), TimeUnit.HOURS.toMillis(6), null, null);
    try {
        addSchedule(scheduleId, scheduleDetail);
        Assert.fail("Adding schedule should fail since BOB does not have AMDIN on the app");
    } catch (UnauthorizedException e) {
    // expected
    }
    // grant BOB EXECUTE on the app
    grantAndAssertSuccess(appId, BOB, EnumSet.of(ApplicationPermission.EXECUTE));
    // add schedule should succeed
    addSchedule(scheduleId, scheduleDetail);
    Assert.assertEquals(ProgramScheduleStatus.SUSPENDED.name(), workflowManager.getSchedule(scheduleId.getSchedule()).status(HttpURLConnection.HTTP_OK));
    // update schedule should succeed
    updateSchedule(scheduleId, scheduleDetail);
    Assert.assertEquals(ProgramScheduleStatus.SUSPENDED.name(), workflowManager.getSchedule(scheduleId.getSchedule()).status(HttpURLConnection.HTTP_OK));
    // revoke EXECUTE from BOB
    getAccessController().revoke(Authorizable.fromEntityId(appId), BOB, EnumSet.of(ApplicationPermission.EXECUTE));
    try {
        // delete schedule should fail since we revoke the ADMIN privilege from BOB
        deleteSchedule(scheduleId);
        Assert.fail("Deleting schedule should fail since BOB does not have AMDIN on the app");
    } catch (UnauthorizedException e) {
    // expected
    }
    try {
        updateSchedule(scheduleId, scheduleDetail);
        Assert.fail("Updating schedule should fail since BOB does not have AMDIN on the app");
    } catch (UnauthorizedException e) {
    // expected
    }
    // grant BOB EXECUTE on the app again
    grantAndAssertSuccess(appId, BOB, EnumSet.of(ApplicationPermission.EXECUTE));
    deleteSchedule(scheduleId);
    workflowManager.getSchedule(scheduleId.getSchedule()).status(HttpURLConnection.HTTP_NOT_FOUND);
    // switch to Alice
    SecurityRequestContext.setUserId(ALICE.getName());
}
Also used : ScheduleManager(io.cdap.cdap.test.ScheduleManager) ObjectStore(io.cdap.cdap.api.dataset.lib.ObjectStore) ApplicationManager(io.cdap.cdap.test.ApplicationManager) PartitionedFileSet(io.cdap.cdap.api.dataset.lib.PartitionedFileSet) EnumSet(java.util.EnumSet) Set(java.util.Set) ImmutableSet(com.google.common.collect.ImmutableSet) HashSet(java.util.HashSet) TimeTrigger(io.cdap.cdap.internal.app.runtime.schedule.trigger.TimeTrigger) WorkflowManager(io.cdap.cdap.test.WorkflowManager) AppWithSchedule(io.cdap.cdap.test.app.AppWithSchedule) ProgramId(io.cdap.cdap.proto.id.ProgramId) ScheduleId(io.cdap.cdap.proto.id.ScheduleId) EntityId(io.cdap.cdap.proto.id.EntityId) GrantedPermission(io.cdap.cdap.proto.security.GrantedPermission) ApplicationPermission(io.cdap.cdap.proto.security.ApplicationPermission) AccessPermission(io.cdap.cdap.proto.security.AccessPermission) Permission(io.cdap.cdap.proto.security.Permission) StandardPermission(io.cdap.cdap.proto.security.StandardPermission) UnauthorizedException(io.cdap.cdap.security.spi.authorization.UnauthorizedException) ScheduleDetail(io.cdap.cdap.proto.ScheduleDetail) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) ScheduleProgramInfo(io.cdap.cdap.api.workflow.ScheduleProgramInfo) Test(org.junit.Test)

Example 19 with ScheduleId

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

the class SparkTest method testSparkProgramStatusSchedule.

@Test
public void testSparkProgramStatusSchedule() throws Exception {
    ApplicationManager appManager = deploy(TestSparkApp.class);
    ScheduleId scheduleId = new ScheduleId(NamespaceId.DEFAULT.getNamespace(), TestSparkApp.class.getSimpleName(), "schedule");
    appManager.enableSchedule(scheduleId);
    WorkflowManager workflowManager = appManager.getWorkflowManager(TestSparkApp.TriggeredWorkflow.class.getSimpleName());
    int numRuns = workflowManager.getHistory(ProgramRunStatus.COMPLETED).size();
    // Start the upstream program
    SparkManager sparkManager = appManager.getSparkManager(TestSparkApp.ScalaClassicSpark.class.getSimpleName());
    sparkManager.start();
    // Wait for the downstream to complete
    workflowManager.waitForRun(ProgramRunStatus.COMPLETED, 5, TimeUnit.MINUTES);
    // Run again with the kryo serializer
    sparkManager.start(Collections.singletonMap("spark.serializer", "org.apache.spark.serializer.KryoSerializer"));
    // Wait for the downstream to complete again
    workflowManager.waitForRuns(ProgramRunStatus.COMPLETED, numRuns + 2, 5, TimeUnit.MINUTES);
}
Also used : ApplicationManager(io.cdap.cdap.test.ApplicationManager) SparkManager(io.cdap.cdap.test.SparkManager) WorkflowManager(io.cdap.cdap.test.WorkflowManager) TestSparkApp(io.cdap.cdap.spark.app.TestSparkApp) ScheduleId(io.cdap.cdap.proto.id.ScheduleId) Test(org.junit.Test)

Example 20 with ScheduleId

use of io.cdap.cdap.proto.id.ScheduleId in project cdap by cdapio.

the class OwnerTable method createRowKey.

// Create row key from Namespaced Entity ID
private String createRowKey(NamespacedEntityId namespacedEntityId) {
    StringBuilder builder = new StringBuilder();
    switch(namespacedEntityId.getEntityType()) {
        case NAMESPACE:
            NamespaceId id = (NamespaceId) namespacedEntityId;
            builder.append(NAMESPACE_ROW_KEY_PREFIX);
            builder.append(ROW_KEY_SEPARATOR);
            builder.append(id.getNamespace());
            break;
        case PROGRAM:
            ProgramId program = (ProgramId) namespacedEntityId;
            builder.append(NAMESPACE_ROW_KEY_PREFIX);
            builder.append(ROW_KEY_SEPARATOR);
            builder.append(program.getNamespace());
            builder.append(ROW_KEY_SEPARATOR);
            builder.append(APP_ID_ROW_KEY_PREFIX);
            builder.append(ROW_KEY_SEPARATOR);
            builder.append(program.getApplication());
            builder.append(ROW_KEY_SEPARATOR);
            builder.append(PROGRAM_TYPE_ROW_KEY_PREFIX);
            builder.append(ROW_KEY_SEPARATOR);
            builder.append(program.getType().name());
            builder.append(ROW_KEY_SEPARATOR);
            builder.append(PROGRAM_ID_ROW_KEY_PREFIX);
            builder.append(ROW_KEY_SEPARATOR);
            builder.append(program.getProgram());
            break;
        case APPLICATION:
            ApplicationId application = (ApplicationId) namespacedEntityId;
            builder.append(NAMESPACE_ROW_KEY_PREFIX);
            builder.append(ROW_KEY_SEPARATOR);
            builder.append(application.getNamespace());
            builder.append(ROW_KEY_SEPARATOR);
            builder.append(APP_ID_ROW_KEY_PREFIX);
            builder.append(ROW_KEY_SEPARATOR);
            builder.append(application.getApplication());
            break;
        case DATASET:
            DatasetId datasetInstance = (DatasetId) namespacedEntityId;
            builder.append(NAMESPACE_ROW_KEY_PREFIX);
            builder.append(ROW_KEY_SEPARATOR);
            builder.append(datasetInstance.getNamespace());
            builder.append(ROW_KEY_SEPARATOR);
            builder.append(DATASET_ID_ROW_KEY_PREFIX);
            builder.append(ROW_KEY_SEPARATOR);
            builder.append(datasetInstance.getDataset());
            break;
        case ARTIFACT:
            ArtifactId artifactId = (ArtifactId) namespacedEntityId;
            builder.append(NAMESPACE_ROW_KEY_PREFIX);
            builder.append(ROW_KEY_SEPARATOR);
            builder.append(artifactId.getNamespace());
            builder.append(ROW_KEY_SEPARATOR);
            builder.append(ARTIFACT_ID_ROW_KEY_PREFIX);
            builder.append(ROW_KEY_SEPARATOR);
            builder.append(artifactId.getArtifact());
            builder.append(ROW_KEY_SEPARATOR);
            builder.append(ARTIFACT_VERSION_ROW_KEY_PREFIX);
            builder.append(ROW_KEY_SEPARATOR);
            builder.append(artifactId.getVersion());
            break;
        case SCHEDULE:
            ScheduleId scheduleId = (ScheduleId) namespacedEntityId;
            builder.append(NAMESPACE_ROW_KEY_PREFIX);
            builder.append(ROW_KEY_SEPARATOR);
            builder.append(scheduleId.getNamespace());
            builder.append(ROW_KEY_SEPARATOR);
            builder.append(APP_ID_ROW_KEY_PREFIX);
            builder.append(ROW_KEY_SEPARATOR);
            builder.append(scheduleId.getApplication());
            builder.append(ROW_KEY_SEPARATOR);
            builder.append(ARTIFACT_VERSION_ROW_KEY_PREFIX);
            builder.append(ROW_KEY_SEPARATOR);
            builder.append(scheduleId.getVersion());
            builder.append(ROW_KEY_SEPARATOR);
            builder.append(SCHEDULE_NAME_ROW_KEY_PREFIX);
            builder.append(ROW_KEY_SEPARATOR);
            builder.append(scheduleId.getSchedule());
            break;
        default:
            throw new IllegalArgumentException(String.format("Error converting id for entity, %s. " + "Unexpected entity type %s", namespacedEntityId.toString(), namespacedEntityId.getEntityType().toString()));
    }
    return builder.toString();
}
Also used : ArtifactId(io.cdap.cdap.proto.id.ArtifactId) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) ProgramId(io.cdap.cdap.proto.id.ProgramId) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) ScheduleId(io.cdap.cdap.proto.id.ScheduleId) DatasetId(io.cdap.cdap.proto.id.DatasetId)

Aggregations

ScheduleId (io.cdap.cdap.proto.id.ScheduleId)66 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)36 ProgramId (io.cdap.cdap.proto.id.ProgramId)20 ScheduleDetail (io.cdap.cdap.proto.ScheduleDetail)18 Test (org.junit.Test)16 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)14 CommandInputError (io.cdap.cdap.cli.exception.CommandInputError)12 NotFoundException (io.cdap.cdap.common.NotFoundException)12 ScheduleProgramInfo (io.cdap.cdap.api.workflow.ScheduleProgramInfo)10 ProgramSchedule (io.cdap.cdap.internal.app.runtime.schedule.ProgramSchedule)10 ProfileId (io.cdap.cdap.proto.id.ProfileId)10 ApplicationSpecification (io.cdap.cdap.api.app.ApplicationSpecification)8 ProgramType (io.cdap.cdap.proto.ProgramType)8 StructuredRow (io.cdap.cdap.spi.data.StructuredRow)8 TimeTrigger (io.cdap.cdap.internal.app.runtime.schedule.trigger.TimeTrigger)6 Constraint (io.cdap.cdap.internal.schedule.constraint.Constraint)6 ProtoTrigger (io.cdap.cdap.proto.ProtoTrigger)6 AppRequest (io.cdap.cdap.proto.artifact.AppRequest)6 ApplicationManager (io.cdap.cdap.test.ApplicationManager)5 ArtifactSummary (io.cdap.cdap.api.artifact.ArtifactSummary)4