Search in sources :

Example 1 with NamespaceConfig

use of io.cdap.cdap.proto.NamespaceConfig in project cdap by caskdata.

the class DistributedStorageProviderNamespaceAdmin method delete.

@SuppressWarnings("ConstantConditions")
@Override
public void delete(NamespaceId namespaceId) throws IOException, ExploreException, SQLException {
    // delete namespace directory from filesystem
    super.delete(namespaceId);
    if (NamespaceId.DEFAULT.equals(namespaceId)) {
        return;
    }
    // delete HBase namespace
    NamespaceConfig namespaceConfig;
    try {
        namespaceConfig = namespaceQueryAdmin.get(namespaceId).getConfig();
    } catch (Exception ex) {
        throw new IOException("Could not fetch custom HBase mapping.", ex);
    }
    if (!Strings.isNullOrEmpty(namespaceConfig.getHbaseNamespace())) {
        // custom namespace mapping is set for HBase, hence don't do anything during delete since the lifecycle of the
        // namespace will be managed by the user
        LOG.debug("Custom HBase mapping {} was found while deleting {}. Hence skipping deletion of HBase namespace", namespaceConfig.getHbaseNamespace(), namespaceId);
        return;
    }
    // delete HBase namespace
    String namespace = tableUtil.getHBaseNamespace(namespaceId);
    try (HBaseDDLExecutor executor = hBaseDDLExecutorFactory.get()) {
        executor.deleteNamespaceIfExists(namespace);
    }
}
Also used : NamespaceConfig(io.cdap.cdap.proto.NamespaceConfig) HBaseDDLExecutor(io.cdap.cdap.spi.hbase.HBaseDDLExecutor) IOException(java.io.IOException) IOException(java.io.IOException) ExploreException(io.cdap.cdap.explore.service.ExploreException) SQLException(java.sql.SQLException)

Example 2 with NamespaceConfig

use of io.cdap.cdap.proto.NamespaceConfig in project cdap by caskdata.

the class SchedulerQueueResolver method getQueue.

/**
 * Get queue at namespace level if it is empty returns the default queue.
 *
 * @param namespaceId NamespaceId
 * @return schedule queue at namespace level or default queue.
 */
@Nullable
public String getQueue(NamespaceId namespaceId) throws IOException, NamespaceNotFoundException {
    if (namespaceId.equals(NamespaceId.SYSTEM)) {
        return systemQueue;
    }
    NamespaceMeta meta;
    try {
        meta = namespaceQueryAdmin.get(namespaceId);
    } catch (NamespaceNotFoundException e) {
        throw e;
    } catch (Exception e) {
        throw new IOException(e);
    }
    if (meta != null) {
        NamespaceConfig config = meta.getConfig();
        String namespaceQueue = config.getSchedulerQueueName();
        return Strings.isNullOrEmpty(namespaceQueue) ? getDefaultQueue() : namespaceQueue;
    } else {
        return getDefaultQueue();
    }
}
Also used : NamespaceConfig(io.cdap.cdap.proto.NamespaceConfig) NamespaceMeta(io.cdap.cdap.proto.NamespaceMeta) IOException(java.io.IOException) NamespaceNotFoundException(io.cdap.cdap.common.NamespaceNotFoundException) NamespaceNotFoundException(io.cdap.cdap.common.NamespaceNotFoundException) IOException(java.io.IOException) Nullable(javax.annotation.Nullable)

Example 3 with NamespaceConfig

use of io.cdap.cdap.proto.NamespaceConfig in project cdap by caskdata.

the class DefaultOwnerAdmin method getImpersonationInfo.

@Nullable
@Override
public ImpersonationInfo getImpersonationInfo(NamespacedEntityId entityId) throws AccessException {
    try {
        entityId = getEffectiveEntity(entityId);
        if (!entityId.getEntityType().equals(EntityType.NAMESPACE)) {
            KerberosPrincipalId effectiveOwner = ownerStore.getOwner(entityId);
            if (effectiveOwner != null) {
                return new ImpersonationInfo(effectiveOwner.getPrincipal(), SecurityUtil.getKeytabURIforPrincipal(effectiveOwner.getPrincipal(), cConf));
            }
        }
        // (CDAP-8176) Since no owner was found for the entity return namespace principal if present.
        NamespaceConfig nsConfig = getNamespaceConfig(entityId.getNamespaceId());
        return nsConfig.getPrincipal() == null ? null : new ImpersonationInfo(nsConfig.getPrincipal(), nsConfig.getKeytabURI());
    } catch (IOException e) {
        throw AuthEnforceUtil.propagateAccessException(e);
    }
}
Also used : NamespaceConfig(io.cdap.cdap.proto.NamespaceConfig) IOException(java.io.IOException) KerberosPrincipalId(io.cdap.cdap.proto.id.KerberosPrincipalId) Nullable(javax.annotation.Nullable)

Example 4 with NamespaceConfig

use of io.cdap.cdap.proto.NamespaceConfig in project cdap by caskdata.

the class NamespaceHttpHandlerTest method testProperties.

@Test
public void testProperties() throws Exception {
    // create a namespace with principal
    String nsPrincipal = "nsCreator/somehost.net@somekdc.net";
    String nsKeytabURI = "some/path";
    NamespaceMeta impNsMeta = new NamespaceMeta.Builder().setName(NAME).setPrincipal(nsPrincipal).setKeytabURI(nsKeytabURI).build();
    HttpResponse response = createNamespace(GSON.toJson(impNsMeta), impNsMeta.getName());
    assertResponseCode(200, response);
    // verify
    response = getNamespace(NAME);
    JsonObject namespace = readGetResponse(response);
    Assert.assertNotNull(namespace);
    Assert.assertEquals(NAME, namespace.get(NAME_FIELD).getAsString());
    Assert.assertEquals(EMPTY, namespace.get(DESCRIPTION_FIELD).getAsString());
    // Update scheduler queue name.
    String nonexistentName = NAME + "nonexistent";
    NamespaceMeta meta = new NamespaceMeta.Builder(impNsMeta).setName(nonexistentName).setSchedulerQueueName("prod").build();
    setProperties(NAME, meta);
    // assert that the name in the metadata is ignored (the name from the url should be used, instead
    HttpResponse nonexistentGet = getNamespace(nonexistentName);
    Assert.assertEquals(404, nonexistentGet.getResponseCode());
    response = getNamespace(NAME);
    namespace = readGetResponse(response);
    Assert.assertNotNull(namespace);
    NamespaceConfig config = GSON.fromJson(namespace.get(CONFIG_FIELD).getAsJsonObject(), NamespaceConfig.class);
    Assert.assertEquals("prod", config.getSchedulerQueueName());
    Assert.assertEquals(NAME, namespace.get(NAME_FIELD).getAsString());
    Assert.assertEquals(EMPTY, namespace.get(DESCRIPTION_FIELD).getAsString());
    // Update description
    meta = new NamespaceMeta.Builder(impNsMeta).setName(NAME).setDescription("new fancy description").build();
    setProperties(NAME, meta);
    response = getNamespace(NAME);
    namespace = readGetResponse(response);
    Assert.assertNotNull(namespace);
    // verify that the description has changed
    Assert.assertEquals("new fancy description", namespace.get(DESCRIPTION_FIELD).getAsString());
    Assert.assertEquals(NAME, namespace.get(NAME_FIELD).getAsString());
    // verify other properties set earlier has not changed.
    config = GSON.fromJson(namespace.get(CONFIG_FIELD).getAsJsonObject(), NamespaceConfig.class);
    Assert.assertEquals("prod", config.getSchedulerQueueName());
    // verify updating keytab URI with version initialized
    setProperties(NAME, new NamespaceMeta.Builder(impNsMeta).setKeytabURI("new/url").build());
    response = getNamespace(NAME);
    namespace = readGetResponse(response);
    Assert.assertNotNull(namespace);
    config = GSON.fromJson(namespace.get(CONFIG_FIELD).getAsJsonObject(), NamespaceConfig.class);
    // verify that the uri has changed
    Assert.assertEquals("new/url", config.getKeytabURI());
    // cleanup
    response = deleteNamespace(NAME);
    Assert.assertEquals(200, response.getResponseCode());
}
Also used : NamespaceConfig(io.cdap.cdap.proto.NamespaceConfig) NamespaceMeta(io.cdap.cdap.proto.NamespaceMeta) HttpResponse(io.cdap.common.http.HttpResponse) JsonObject(com.google.gson.JsonObject) AppForUnrecoverableResetTest(io.cdap.cdap.AppForUnrecoverableResetTest) Test(org.junit.Test)

Example 5 with NamespaceConfig

use of io.cdap.cdap.proto.NamespaceConfig in project cdap by caskdata.

the class DefaultNamespaceAdmin method updateProperties.

@Override
public synchronized void updateProperties(NamespaceId namespaceId, NamespaceMeta namespaceMeta) throws Exception {
    if (!exists(namespaceId)) {
        throw new NamespaceNotFoundException(namespaceId);
    }
    accessEnforcer.enforce(namespaceId, authenticationContext.getPrincipal(), StandardPermission.UPDATE);
    NamespaceMeta existingMeta = nsStore.get(namespaceId);
    // Already ensured that namespace exists, so namespace meta should not be null
    Preconditions.checkNotNull(existingMeta);
    NamespaceMeta.Builder builder = new NamespaceMeta.Builder(existingMeta);
    if (namespaceMeta.getDescription() != null) {
        builder.setDescription(namespaceMeta.getDescription());
    }
    NamespaceConfig config = namespaceMeta.getConfig();
    if (config != null && !Strings.isNullOrEmpty(config.getSchedulerQueueName())) {
        builder.setSchedulerQueueName(config.getSchedulerQueueName());
    }
    if (config != null && config.getKeytabURI() != null) {
        String keytabURI = config.getKeytabURI();
        if (keytabURI.isEmpty()) {
            throw new BadRequestException("Cannot update keytab URI with an empty URI.");
        }
        String existingKeytabURI = existingMeta.getConfig().getKeytabURIWithoutVersion();
        if (existingKeytabURI == null) {
            throw new BadRequestException("Cannot update keytab URI since there is no existing principal or keytab URI.");
        }
        if (keytabURI.equals(existingKeytabURI)) {
            // The given keytab URI is the same as the existing one, but the content of the keytab file might be changed.
            // Increment the keytab URI version so that the cache will reload content in the updated keytab file.
            builder.incrementKeytabURIVersion();
        } else {
            builder.setKeytabURIWithoutVersion(keytabURI);
            // clear keytab URI version
            builder.setKeytabURIVersion(0);
        }
    }
    if (config != null) {
        builder.setExploreAsPrincipal(config.isExploreAsPrincipal());
    }
    Set<String> difference = existingMeta.getConfig().getDifference(config);
    if (!difference.isEmpty()) {
        throw new BadRequestException(String.format("Mappings %s for namespace %s cannot be updated once the namespace " + "is created.", difference, namespaceId));
    }
    NamespaceMeta updatedMeta = builder.build();
    nsStore.update(updatedMeta);
    // refresh the cache with new meta
    namespaceMetaCache.refresh(namespaceId);
    LOG.info("Namespace {} updated with meta {}", namespaceId, updatedMeta);
}
Also used : NamespaceConfig(io.cdap.cdap.proto.NamespaceConfig) NamespaceMeta(io.cdap.cdap.proto.NamespaceMeta) CacheBuilder(com.google.common.cache.CacheBuilder) BadRequestException(io.cdap.cdap.common.BadRequestException) NamespaceNotFoundException(io.cdap.cdap.common.NamespaceNotFoundException)

Aggregations

NamespaceConfig (io.cdap.cdap.proto.NamespaceConfig)12 NamespaceMeta (io.cdap.cdap.proto.NamespaceMeta)8 IOException (java.io.IOException)6 NamespaceNotFoundException (io.cdap.cdap.common.NamespaceNotFoundException)4 Nullable (javax.annotation.Nullable)4 Test (org.junit.Test)4 CacheBuilder (com.google.common.cache.CacheBuilder)2 JsonObject (com.google.gson.JsonObject)2 AppForUnrecoverableResetTest (io.cdap.cdap.AppForUnrecoverableResetTest)2 BadRequestException (io.cdap.cdap.common.BadRequestException)2 ExploreException (io.cdap.cdap.explore.service.ExploreException)2 KerberosPrincipalId (io.cdap.cdap.proto.id.KerberosPrincipalId)2 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)2 HBaseDDLExecutor (io.cdap.cdap.spi.hbase.HBaseDDLExecutor)2 HttpResponse (io.cdap.common.http.HttpResponse)2 SQLException (java.sql.SQLException)2 Location (org.apache.twill.filesystem.Location)2