Search in sources :

Example 1 with NamespaceCannotBeCreatedException

use of io.cdap.cdap.common.NamespaceCannotBeCreatedException 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);
}
Also used : MasterEnvironment(io.cdap.cdap.master.spi.environment.MasterEnvironment) NamespaceCannotBeCreatedException(io.cdap.cdap.common.NamespaceCannotBeCreatedException) BadRequestException(io.cdap.cdap.common.BadRequestException) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) NamespaceAlreadyExistsException(io.cdap.cdap.common.NamespaceAlreadyExistsException) KerberosPrincipalId(io.cdap.cdap.proto.id.KerberosPrincipalId) Principal(io.cdap.cdap.proto.security.Principal) UserGroupInformation(org.apache.hadoop.security.UserGroupInformation)

Example 2 with NamespaceCannotBeCreatedException

use of io.cdap.cdap.common.NamespaceCannotBeCreatedException in project cdap by caskdata.

the class TestSQLQueryTestRun method testSQLQueryWithCustomMapping.

@Test(timeout = 120000L)
public void testSQLQueryWithCustomMapping() throws Exception {
    // trying to create a cdap namespace with custom hive database when the database does not exists in hive should
    // fail.
    String customHiveDatabase = "custom_db";
    try {
        namespaceAdmin.create(new NamespaceMeta.Builder().setName(testSpace).setHiveDatabase(customHiveDatabase).build());
        Assert.fail();
    } catch (NamespaceCannotBeCreatedException e) {
        // expected exception. Make sure that the namespace creation failed because of explore exception
        Assert.assertTrue(e.getCause() instanceof ExploreException);
    }
    // create the custom database in hive
    try (Connection connection = getQueryClient();
        ResultSet results = connection.prepareStatement(String.format("CREATE DATABASE %s", customHiveDatabase)).executeQuery()) {
        // run a query over the dataset
        Assert.assertNotNull(results);
    }
    // check that the custom hive database got created
    checkDatabaseExists(customHiveDatabase);
    // now the namespace create should work
    namespaceAdmin.create(new NamespaceMeta.Builder().setName(testSpace).setHiveDatabase(customHiveDatabase).build());
    // test some sql query on this custom hive database
    testSQLQuery();
    // delete the cdap namespace
    namespaceAdmin.delete(testSpace);
    // deleting the cdap namespace should not have deleted the custom hive database
    checkDatabaseExists(customHiveDatabase);
}
Also used : NamespaceCannotBeCreatedException(io.cdap.cdap.common.NamespaceCannotBeCreatedException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) ExploreException(io.cdap.cdap.explore.service.ExploreException) Test(org.junit.Test)

Aggregations

NamespaceCannotBeCreatedException (io.cdap.cdap.common.NamespaceCannotBeCreatedException)2 BadRequestException (io.cdap.cdap.common.BadRequestException)1 NamespaceAlreadyExistsException (io.cdap.cdap.common.NamespaceAlreadyExistsException)1 ExploreException (io.cdap.cdap.explore.service.ExploreException)1 MasterEnvironment (io.cdap.cdap.master.spi.environment.MasterEnvironment)1 KerberosPrincipalId (io.cdap.cdap.proto.id.KerberosPrincipalId)1 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)1 Principal (io.cdap.cdap.proto.security.Principal)1 Connection (java.sql.Connection)1 ResultSet (java.sql.ResultSet)1 UserGroupInformation (org.apache.hadoop.security.UserGroupInformation)1 Test (org.junit.Test)1