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);
}
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);
}
}
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());
}
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);
}
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();
}
Aggregations