/** * Get the profile id for the provided entity id from its own preferences from preference dataset. * * @param entityId entity id to lookup the profile id * @return the profile id configured for this entity id, if any */ private Optional<ProfileId> getProfileId(EntityId entityId) { NamespaceId namespaceId = entityId.getEntityType().equals(EntityType.INSTANCE) ? NamespaceId.SYSTEM : ((NamespacedEntityId) entityId).getNamespaceId(); String profileName = preferencesDataset.getPreferences(entityId).get(SystemArguments.PROFILE_NAME); return profileName == null ? Optional.empty() : Optional.of(ProfileId.fromScopedName(namespaceId, profileName)); }
private boolean isEnforcingOnSamePrincipalId(EntityId entityId, Principal principal) { return entityId.getEntityType().equals(EntityType.KERBEROSPRINCIPAL) && principal.getName().equals(entityId.getEntityName()); } }
@Override protected boolean shouldRunInSeparateTx(MetadataMessage message) { EntityType entityType = message.getEntityId().getEntityType(); return entityType.equals(EntityType.INSTANCE) || entityType.equals(EntityType.NAMESPACE); }
@Override protected boolean shouldRunInSeparateTx(MetadataMessage message) { EntityType entityType = message.getEntityId().getEntityType(); return entityType.equals(EntityType.INSTANCE) || entityType.equals(EntityType.NAMESPACE); }
/** * Get the profile id for the provided entity id from the resolved preferences from preference dataset, * if no profile is inside, it will return the default profile * * @param entityId entity id to lookup the profile id * @return the profile id which will be used by this entity id, default profile if not find */ // TODO: CDAP-13579 consider preference key starts with [scope].[name].system.profile.name private ProfileId getResolvedProfileId(EntityId entityId) { NamespaceId namespaceId = entityId.getEntityType().equals(EntityType.INSTANCE) ? NamespaceId.SYSTEM : ((NamespacedEntityId) entityId).getNamespaceId(); return SystemArguments.getProfileIdFromArgs( namespaceId, preferencesDataset.getResolvedPreferences(entityId)).orElse(ProfileId.NATIVE); }
/** * Get the resolved preferences for the entity id, the preferences are resolved from instance -> namespace -> app * -> program level * * @param entityId the entity id to get the preferences from * @return the map which contains the preferences */ public Map<String, String> getResolvedPreferences(EntityId entityId) { // if it is instance level get the properties and return if (entityId.getEntityType().equals(EntityType.INSTANCE)) { return getPreferences(entityId); } Map<String, String> properties = new HashMap<>(); // if the entity id has a parent id, get the preference from its parent if (entityId instanceof ParentedId) { properties.putAll(getResolvedPreferences(((ParentedId) entityId).getParent())); } else if (entityId.getEntityType() == EntityType.NAMESPACE) { // otherwise it is a namespace id, which we want to look at the instance level properties.putAll(getResolvedPreferences(new InstanceId(""))); } // put the current level property properties.putAll(getPreferences(entityId)); return properties; }
/** * Get the resolved preferences for the entity id, the preferences are resolved from instance -> namespace -> app * -> program level * * @param entityId the entity id to get the preferences from * @return the map which contains the preferences */ public Map<String, String> getResolvedPreferences(EntityId entityId) { // if it is instance level get the properties and return if (entityId.getEntityType().equals(EntityType.INSTANCE)) { return getPreferences(entityId); } Map<String, String> properties = new HashMap<>(); // if the entity id has a parent id, get the preference from its parent if (entityId instanceof ParentedId) { properties.putAll(getResolvedPreferences(((ParentedId) entityId).getParent())); } else if (entityId.getEntityType() == EntityType.NAMESPACE) { // otherwise it is a namespace id, which we want to look at the instance level properties.putAll(getResolvedPreferences(new InstanceId(""))); } // put the current level property properties.putAll(getPreferences(entityId)); return properties; }
private String getUserFriendlyEntityStr(EntityId entityId) { switch (entityId.getEntityType()) { case INSTANCE: return "the system instance"; case NAMESPACE: return String.format("namespace '%s'", entityId.getEntityName()); case APPLICATION: ApplicationId applicationId = (ApplicationId) entityId; return String.format("application '%s' in namespace '%s'", applicationId.getApplication(), applicationId.getNamespace()); case PROGRAM: ProgramId programId = (ProgramId) entityId; return String.format("%s '%s' in namespace '%s'", programId.getType().name().toLowerCase(), programId.getProgram(), programId.getNamespace()); } return entityId.toString(); }
private String getUserFriendlyEntityStr(EntityId entityId) { switch (entityId.getEntityType()) { case INSTANCE: return "the system instance"; case NAMESPACE: return String.format("namespace '%s'", entityId.getEntityName()); case APPLICATION: ApplicationId applicationId = (ApplicationId) entityId; return String.format("application '%s' in namespace '%s'", applicationId.getApplication(), applicationId.getNamespace()); case PROGRAM: ProgramId programId = (ProgramId) entityId; return String.format("%s '%s' in namespace '%s'", programId.getType().name().toLowerCase(), programId.getProgram(), programId.getNamespace()); } return entityId.toString(); }
/** * Get the profile id for the provided entity id from the resolved preferences from preference dataset, * if no profile is inside, it will return the default profile * * @param entityId entity id to lookup the profile id * @return the profile id which will be used by this entity id, default profile if not find */ // TODO: CDAP-13579 consider preference key starts with [scope].[name].system.profile.name private ProfileId getResolvedProfileId(EntityId entityId) { NamespaceId namespaceId = entityId.getEntityType().equals(EntityType.INSTANCE) ? NamespaceId.SYSTEM : ((NamespacedEntityId) entityId).getNamespaceId(); String profileName = preferencesDataset.getResolvedPreference(entityId, SystemArguments.PROFILE_NAME); return profileName == null ? ProfileId.NATIVE : ProfileId.fromScopedName(namespaceId, profileName); }
/** * Remove the profile metadata according to the message, currently only meant for application and schedule. */ private void removeProfileMetadata(MetadataMessage message) { EntityId entity = message.getEntityId(); Map<MetadataEntity, Set<String>> toRemove = new HashMap<>(); // We only care about application and schedules. if (entity.getEntityType().equals(EntityType.APPLICATION)) { ApplicationId appId = (ApplicationId) message.getEntityId(); ApplicationSpecification appSpec = message.getPayload(GSON, ApplicationSpecification.class); for (ProgramId programId : getAllProfileAllowedPrograms(appSpec, appId)) { toRemove.put(programId.toMetadataEntity(), PROFILE_METADATA_KEY_SET); } for (ScheduleId scheduleId : getSchedulesInApp(appId, appSpec.getProgramSchedules())) { toRemove.put(scheduleId.toMetadataEntity(), PROFILE_METADATA_KEY_SET); } } else if (entity.getEntityType().equals(EntityType.SCHEDULE)) { toRemove.put(entity.toMetadataEntity(), PROFILE_METADATA_KEY_SET); } if (!toRemove.isEmpty()) { metadataStore.removeProperties(MetadataScope.SYSTEM, toRemove); } }
private void deleteConfig(EntityId entityId) { Transactionals.execute(transactional, context -> { PreferencesDataset dataset = PreferencesDataset.get(context, datasetFramework); Map<String, String> oldProp = dataset.getPreferences(entityId); NamespaceId namespaceId = entityId.getEntityType().equals(EntityType.INSTANCE) ? NamespaceId.SYSTEM : ((NamespacedEntityId) entityId).getNamespaceId(); Optional<ProfileId> oldProfile = SystemArguments.getProfileIdFromArgs(namespaceId, oldProp); dataset.deleteProperties(entityId); // if there is profile properties, publish the message to update metadata and remove the assignment if (oldProfile.isPresent()) { ProfileDataset.get(context, datasetFramework).removeProfileAssignment(oldProfile.get(), entityId); adminEventPublisher.publishProfileUnAssignment(entityId); } }); }
private void deleteConfig(EntityId entityId) { Transactionals.execute(transactional, context -> { PreferencesDataset dataset = PreferencesDataset.get(context, datasetFramework); Map<String, String> oldProp = dataset.getPreferences(entityId); NamespaceId namespaceId = entityId.getEntityType().equals(EntityType.INSTANCE) ? NamespaceId.SYSTEM : ((NamespacedEntityId) entityId).getNamespaceId(); Optional<ProfileId> oldProfile = SystemArguments.getProfileIdFromArgs(namespaceId, oldProp); dataset.deleteProperties(entityId); // if there is profile properties, publish the message to update metadata and remove the assignment if (oldProfile.isPresent()) { ProfileDataset.get(context, datasetFramework).removeProfileAssignment(oldProfile.get(), entityId); adminEventPublisher.publishProfileUnAssignment(entityId); } }); }
/** * Remove the profile metadata according to the message, currently only meant for application and schedule. */ private void removeProfileMetadata(MetadataMessage message) { EntityId entity = message.getEntityId(); // We only care about application and schedules. if (entity.getEntityType().equals(EntityType.APPLICATION)) { ApplicationId appId = (ApplicationId) message.getEntityId(); ApplicationSpecification appSpec = message.getPayload(GSON, ApplicationSpecification.class); for (ProgramId programId : getProgramsWithType(appId, ProgramType.WORKFLOW, appSpec.getWorkflows())) { metadataDataset.removeProperties(programId.toMetadataEntity(), Collections.singleton(PROFILE_METADATA_KEY)); } for (ScheduleId scheduleId : getSchedulesInApp(appId, appSpec.getProgramSchedules())) { metadataDataset.removeProperties(scheduleId.toMetadataEntity(), Collections.singleton(PROFILE_METADATA_KEY)); } } if (entity.getEntityType().equals(EntityType.SCHEDULE)) { metadataDataset.removeProperties(message.getEntityId().toMetadataEntity(), Collections.singleton(PROFILE_METADATA_KEY)); } }
@Override public int hashCode() { if (entityId.getEntityType().equals(EntityType.ARTIFACT)) { ArtifactId artifactId = (ArtifactId) entityId; return Objects.hash(artifactId.getEntityType(), artifactId.getNamespace(), artifactId.getArtifact()); } if (entityId.getEntityType().equals(EntityType.APPLICATION)) { ApplicationId applicationId = (ApplicationId) entityId; return Objects.hash(applicationId.getEntityType(), applicationId.getNamespace(), applicationId.getApplication()); } if (entityId.getEntityType().equals(EntityType.PROGRAM)) { ProgramId programId = (ProgramId) entityId; return Objects.hash(programId.getEntityType(), programId.getNamespace(), programId.getApplication(), programId.getType(), programId.getProgram()); } return Objects.hash(entityId); } }
/** * Delete the preferences for the entity id * * @param entityId the entity id to delete the preferences */ public void deleteProperties(EntityId entityId) { switch (entityId.getEntityType()) { case INSTANCE: deleteConfig(EMPTY_NAMESPACE, getMultipartKey(INSTANCE_PROPERTIES)); break; case NAMESPACE: NamespaceId namespaceId = (NamespaceId) entityId; deleteConfig(namespaceId.getNamespace(), getMultipartKey(namespaceId.getNamespace())); break; case APPLICATION: ApplicationId appId = (ApplicationId) entityId; deleteConfig(appId.getNamespace(), getMultipartKey(appId.getNamespace(), appId.getApplication())); break; case PROGRAM: ProgramId programId = (ProgramId) entityId; deleteConfig(programId.getNamespace(), getMultipartKey(programId.getNamespace(), programId.getApplication(), programId.getType().getCategoryName(), programId.getProgram())); break; default: throw new UnsupportedOperationException( String.format("Preferences cannot be used on this entity type: %s", entityId.getEntityType())); } }
/** * Get the preferences for the entity id * * @param entityId the entity id to get the preferences from * @return the map which contains the preferences */ public Map<String, String> getPreferences(EntityId entityId) { switch (entityId.getEntityType()) { case INSTANCE: return getConfigProperties(EMPTY_NAMESPACE, getMultipartKey(INSTANCE_PROPERTIES)); case NAMESPACE: NamespaceId namespaceId = (NamespaceId) entityId; return getConfigProperties(namespaceId.getNamespace(), getMultipartKey(namespaceId.getNamespace())); case APPLICATION: ApplicationId appId = (ApplicationId) entityId; return getConfigProperties(appId.getNamespace(), getMultipartKey(appId.getNamespace(), appId.getApplication())); case PROGRAM: ProgramId programId = (ProgramId) entityId; return getConfigProperties(programId.getNamespace(), getMultipartKey(programId.getNamespace(), programId.getApplication(), programId.getType().getCategoryName(), programId.getProgram())); default: throw new UnsupportedOperationException( String.format("Preferences cannot be used on this entity type: %s", entityId.getEntityType())); } }
/** * Set the preferences for the entity id * * @param entityId the entity id to set the preferences from * @param propMap the map which contains the preferences */ public void setPreferences(EntityId entityId, Map<String, String> propMap) { switch (entityId.getEntityType()) { case INSTANCE: setConfig(EMPTY_NAMESPACE, getMultipartKey(INSTANCE_PROPERTIES), propMap); break; case NAMESPACE: NamespaceId namespaceId = (NamespaceId) entityId; setConfig(namespaceId.getNamespace(), getMultipartKey(namespaceId.getNamespace()), propMap); break; case APPLICATION: ApplicationId appId = (ApplicationId) entityId; setConfig(appId.getNamespace(), getMultipartKey(appId.getNamespace(), appId.getApplication()), propMap); break; case PROGRAM: ProgramId programId = (ProgramId) entityId; setConfig(programId.getNamespace(), getMultipartKey(programId.getNamespace(), programId.getApplication(), programId.getType().getCategoryName(), programId.getProgram()), propMap); break; default: throw new UnsupportedOperationException( String.format("Preferences cannot be used on this entity type: %s", entityId.getEntityType())); } }
/** * Get the preferences for the entity id * * @param entityId the entity id to get the preferences from * @return the map which contains the preferences */ public Map<String, String> getPreferences(EntityId entityId) { switch (entityId.getEntityType()) { case INSTANCE: return getConfigProperties(EMPTY_NAMESPACE, getMultipartKey(INSTANCE_PROPERTIES)); case NAMESPACE: NamespaceId namespaceId = (NamespaceId) entityId; return getConfigProperties(namespaceId.getNamespace(), getMultipartKey(namespaceId.getNamespace())); case APPLICATION: ApplicationId appId = (ApplicationId) entityId; return getConfigProperties(appId.getNamespace(), getMultipartKey(appId.getNamespace(), appId.getApplication())); case PROGRAM: ProgramId programId = (ProgramId) entityId; return getConfigProperties(programId.getNamespace(), getMultipartKey(programId.getNamespace(), programId.getApplication(), programId.getType().getCategoryName(), programId.getProgram())); default: throw new UnsupportedOperationException( String.format("Preferences cannot be used on this entity type: %s", entityId.getEntityType())); } }
/** * Delete the preferences for the entity id * * @param entityId the entity id to delete the preferences */ public void deleteProperties(EntityId entityId) { switch (entityId.getEntityType()) { case INSTANCE: deleteConfig(EMPTY_NAMESPACE, getMultipartKey(INSTANCE_PROPERTIES)); break; case NAMESPACE: NamespaceId namespaceId = (NamespaceId) entityId; deleteConfig(namespaceId.getNamespace(), getMultipartKey(namespaceId.getNamespace())); break; case APPLICATION: ApplicationId appId = (ApplicationId) entityId; deleteConfig(appId.getNamespace(), getMultipartKey(appId.getNamespace(), appId.getApplication())); break; case PROGRAM: ProgramId programId = (ProgramId) entityId; deleteConfig(programId.getNamespace(), getMultipartKey(programId.getNamespace(), programId.getApplication(), programId.getType().getCategoryName(), programId.getProgram())); break; default: throw new UnsupportedOperationException( String.format("Preferences cannot be used on this entity type: %s", entityId.getEntityType())); } }