use of io.cdap.cdap.proto.id.NamespaceId in project cdap by caskdata.
the class SystemAppTask method run.
@Override
public void run(RunnableTaskContext context) throws Exception {
ArtifactId systemAppArtifactId = context.getArtifactId();
if (systemAppArtifactId == null) {
throw new IllegalArgumentException("Missing artifactId from the system app task request");
}
LOG.debug("Received system app task for artifact {}", systemAppArtifactId);
Injector injector = createInjector(cConf);
ArtifactRepository artifactRepository = injector.getInstance(ArtifactRepository.class);
Impersonator impersonator = injector.getInstance(Impersonator.class);
String systemAppNamespace = context.getNamespace();
Id.Artifact artifactId = Id.Artifact.from(Id.Namespace.from(systemAppNamespace), systemAppArtifactId.getName(), systemAppArtifactId.getVersion());
ArtifactLocalizerClient localizerClient = injector.getInstance(ArtifactLocalizerClient.class);
File artifactLocation = localizerClient.getUnpackedArtifactLocation(Artifacts.toProtoArtifactId(new NamespaceId(systemAppNamespace), systemAppArtifactId));
EntityImpersonator classLoaderImpersonator = new EntityImpersonator(artifactId.toEntityId(), impersonator);
try (CloseableClassLoader artifactClassLoader = artifactRepository.createArtifactClassLoader(new ArtifactDescriptor(artifactId.getNamespace().getId(), artifactId.toArtifactId(), Locations.toLocation(artifactLocation)), classLoaderImpersonator);
SystemAppTaskContext systemAppTaskContext = buildTaskSystemAppContext(injector, systemAppNamespace, systemAppArtifactId, artifactClassLoader)) {
RunnableTaskRequest taskRequest = context.getEmbeddedRequest();
String taskClassName = taskRequest.getClassName();
if (taskClassName == null) {
LOG.debug("No system app task to execute");
return;
}
LOG.debug("Requested to run system app task {}", taskClassName);
Class<?> clazz = artifactClassLoader.loadClass(taskClassName);
if (!(RunnableTask.class.isAssignableFrom(clazz))) {
throw new ClassCastException(String.format("%s is not a RunnableTask", taskClassName));
}
LOG.debug("Launching system app task {}", taskClassName);
RunnableTask runnableTask = (RunnableTask) injector.getInstance(clazz);
RunnableTaskContext runnableTaskContext = new RunnableTaskContext(taskRequest.getParam().getSimpleParam(), null, null, null, systemAppTaskContext) {
@Override
public void writeResult(byte[] data) throws IOException {
context.writeResult(data);
}
@Override
public void setTerminateOnComplete(boolean terminate) {
context.setTerminateOnComplete(terminate);
}
@Override
public boolean isTerminateOnComplete() {
return context.isTerminateOnComplete();
}
};
runnableTask.run(runnableTaskContext);
LOG.debug("System app task completed {}", taskClassName);
}
}
use of io.cdap.cdap.proto.id.NamespaceId in project cdap by caskdata.
the class InMemoryNamespaceAdmin method create.
@Override
public synchronized void create(NamespaceMeta metadata) throws Exception {
NamespaceId id = new NamespaceId(metadata.getName());
NamespaceMeta existing = namespaces.putIfAbsent(id, metadata);
if (existing != null) {
throw new NamespaceAlreadyExistsException(id);
}
}
use of io.cdap.cdap.proto.id.NamespaceId in project cdap by caskdata.
the class NamespacedExploreQueryExecutorHttpHandler method query.
@POST
@Path("data/explore/queries")
@AuditPolicy(AuditDetail.REQUEST_BODY)
public void query(FullHttpRequest request, HttpResponder responder, @PathParam("namespace-id") final String namespaceId) throws Exception {
try {
Map<String, String> args = decodeArguments(request);
final String query = args.get("query");
final Map<String, String> additionalSessionConf = new HashMap<>(args);
additionalSessionConf.remove("query");
LOG.trace("Received query: {}", query);
QueryHandle queryHandle = impersonator.doAs(new NamespaceId(namespaceId), new Callable<QueryHandle>() {
@Override
public QueryHandle call() throws Exception {
return exploreService.execute(new NamespaceId(namespaceId), query, additionalSessionConf);
}
}, ImpersonatedOpType.EXPLORE);
responder.sendJson(HttpResponseStatus.OK, GSON.toJson(queryHandle));
} catch (IllegalArgumentException e) {
LOG.debug("Got exception:", e);
responder.sendString(HttpResponseStatus.BAD_REQUEST, e.getMessage());
} catch (SQLException e) {
LOG.debug("Got exception:", e);
responder.sendString(HttpResponseStatus.BAD_REQUEST, String.format("[SQLState %s] %s", e.getSQLState(), e.getMessage()));
}
}
use of io.cdap.cdap.proto.id.NamespaceId in project cdap by caskdata.
the class NamespacedExploreQueryExecutorHttpHandler method getQueryLiveHandles.
@GET
@Path("data/explore/queries")
public void getQueryLiveHandles(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @QueryParam("offset") @DefaultValue("9223372036854775807") long offset, @QueryParam("cursor") @DefaultValue("next") String cursor, @QueryParam("limit") @DefaultValue("50") int limit) throws ExploreException, SQLException {
boolean isForward = "next".equals(cursor);
// this operation doesn't interact with hive, so doesn't require impersonation
List<QueryInfo> queries = exploreService.getQueries(new NamespaceId(namespaceId));
// return the queries by after filtering (> offset) and limiting number of queries
responder.sendJson(HttpResponseStatus.OK, GSON.toJson(filterQueries(queries, offset, isForward, limit)));
}
use of io.cdap.cdap.proto.id.NamespaceId in project cdap by caskdata.
the class DefaultNamespaceAdmin method create.
/**
* Creates a new namespace
*
* @param metadata the {@link NamespaceMeta} for the new namespace to be created
* @throws NamespaceAlreadyExistsException if the specified namespace already exists
*/
@Override
public synchronized void create(final NamespaceMeta metadata) throws Exception {
// TODO: CDAP-1427 - This should be transactional, but we don't support transactions on files yet
Preconditions.checkArgument(metadata != null, "Namespace metadata should not be null.");
NamespaceId namespace = metadata.getNamespaceId();
if (exists(namespace)) {
throw new NamespaceAlreadyExistsException(namespace);
}
// need to enforce on the principal id if impersonation is involved
String ownerPrincipal = metadata.getConfig().getPrincipal();
Principal requestingUser = authenticationContext.getPrincipal();
if (ownerPrincipal != null) {
accessEnforcer.enforce(new KerberosPrincipalId(ownerPrincipal), requestingUser, AccessPermission.SET_OWNER);
}
accessEnforcer.enforce(namespace, requestingUser, StandardPermission.CREATE);
// If this namespace has custom mapping then validate the given custom mapping
if (hasCustomMapping(metadata)) {
validateCustomMapping(metadata);
}
// check that the user has configured either both or none of the following configuration: principal and keytab URI
boolean hasValidKerberosConf = false;
if (metadata.getConfig() != null) {
String configuredPrincipal = metadata.getConfig().getPrincipal();
String configuredKeytabURI = metadata.getConfig().getKeytabURI();
if ((!Strings.isNullOrEmpty(configuredPrincipal) && Strings.isNullOrEmpty(configuredKeytabURI)) || (Strings.isNullOrEmpty(configuredPrincipal) && !Strings.isNullOrEmpty(configuredKeytabURI))) {
throw new BadRequestException(String.format("Either both or none of the following two configurations must be configured. " + "Configured principal: %s, Configured keytabURI: %s", configuredPrincipal, configuredKeytabURI));
}
hasValidKerberosConf = true;
}
// check that if explore as principal is explicitly set to false then user has kerberos configuration
if (!metadata.getConfig().isExploreAsPrincipal() && !hasValidKerberosConf) {
throw new BadRequestException(String.format("No kerberos principal or keytab-uri was provided while '%s' was set to true.", NamespaceConfig.EXPLORE_AS_PRINCIPAL));
}
// store the meta first in the namespace store because namespacedLocationFactory needs to look up location
// mapping from namespace config
nsStore.create(metadata);
try {
UserGroupInformation ugi;
if (NamespaceId.DEFAULT.equals(namespace)) {
ugi = UserGroupInformation.getCurrentUser();
} else {
ugi = impersonator.getUGI(namespace);
}
ImpersonationUtils.doAs(ugi, (Callable<Void>) () -> {
storageProviderNamespaceAdmin.get().create(metadata);
return null;
});
// if needed, run master environment specific logic
MasterEnvironment masterEnv = MasterEnvironments.getMasterEnvironment();
if (masterEnv != null) {
masterEnv.onNamespaceCreation(namespace.getNamespace(), metadata.getConfig().getConfigs());
}
} catch (Throwable t) {
LOG.error(String.format("Failed to create namespace '%s'", namespace.getNamespace()), t);
// failed to create namespace in underlying storage so delete the namespace meta stored in the store earlier
deleteNamespaceMeta(metadata.getNamespaceId());
throw new NamespaceCannotBeCreatedException(namespace, t);
}
emitNamespaceCountMetric();
LOG.info("Namespace {} created with meta {}", metadata.getNamespaceId(), metadata);
}
Aggregations