Search in sources :

Example 6 with NamespaceNotFoundException

use of co.cask.cdap.common.NamespaceNotFoundException in project cdap by caskdata.

the class AbstractNamespaceClient method deleteDatasets.

@Override
public void deleteDatasets(NamespaceId namespaceId) throws Exception {
    URL url = resolve(String.format("unrecoverable/namespaces/%s/datasets", namespaceId.getNamespace()));
    HttpResponse response = execute(HttpRequest.delete(url).build());
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new NamespaceNotFoundException(namespaceId);
    } else if (HttpURLConnection.HTTP_FORBIDDEN == response.getResponseCode()) {
        String msg = String.format("Datasets in the namespace '%s' cannot be deleted. Reason: '%s'.", namespaceId, response.getResponseBodyAsString());
        throw new NamespaceCannotBeDeletedException(namespaceId, msg);
    } else if (response.getResponseCode() == HttpURLConnection.HTTP_OK) {
        return;
    }
    throw new IOException(String.format("Cannot delete datasets in namespace %s. Reason: %s", namespaceId, response.getResponseBodyAsString()));
}
Also used : HttpResponse(co.cask.common.http.HttpResponse) IOException(java.io.IOException) NamespaceCannotBeDeletedException(co.cask.cdap.common.NamespaceCannotBeDeletedException) URL(java.net.URL) NamespaceNotFoundException(co.cask.cdap.common.NamespaceNotFoundException)

Example 7 with NamespaceNotFoundException

use of co.cask.cdap.common.NamespaceNotFoundException 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(Id.Namespace namespaceId) throws IOException, NamespaceNotFoundException {
    NamespaceMeta meta;
    try {
        meta = namespaceQueryAdmin.get(namespaceId.toEntityId());
    } 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(co.cask.cdap.proto.NamespaceConfig) NamespaceMeta(co.cask.cdap.proto.NamespaceMeta) IOException(java.io.IOException) NamespaceNotFoundException(co.cask.cdap.common.NamespaceNotFoundException) NamespaceNotFoundException(co.cask.cdap.common.NamespaceNotFoundException) IOException(java.io.IOException) Nullable(javax.annotation.Nullable)

Example 8 with NamespaceNotFoundException

use of co.cask.cdap.common.NamespaceNotFoundException in project cdap by caskdata.

the class SecureStoreClientTest method testErrorScenarios.

@Test
public void testErrorScenarios() throws Exception {
    try {
        client.listKeys(new NamespaceId("notfound"));
        Assert.fail("Should have thrown exception since namespace doesn't exist");
    } catch (NamespaceNotFoundException e) {
    // expected
    }
    try {
        client.deleteKey(new SecureKeyId(NamespaceId.DEFAULT.getNamespace(), "badkey"));
        Assert.fail("Should have thrown exception since the key doesn't exist");
    } catch (SecureKeyNotFoundException e) {
    // expected
    }
    try {
        client.getData(new SecureKeyId(NamespaceId.DEFAULT.getNamespace(), "badkey"));
        Assert.fail("Should have thrown exception since the key doesn't exist");
    } catch (SecureKeyNotFoundException e) {
    // expected
    }
    try {
        client.getKeyMetadata(new SecureKeyId(NamespaceId.DEFAULT.getNamespace(), "badkey"));
        Assert.fail("Should have thrown exception since the key doesn't exist");
    } catch (SecureKeyNotFoundException e) {
    // expected
    }
    try {
        client.getKeyMetadata(new SecureKeyId("notfound", "somekey"));
        Assert.fail("Should have thrown exception since the namespace doesn't exist");
    } catch (SecureKeyNotFoundException e) {
    // expected
    }
    SecureKeyId id = new SecureKeyId(NamespaceId.DEFAULT.getNamespace(), "key1");
    SecureKeyCreateRequest request = new SecureKeyCreateRequest("", "a", ImmutableMap.<String, String>of());
    client.createKey(id, request);
    try {
        client.createKey(id, request);
        Assert.fail("Should have thrown exception since the key already exists");
    } catch (SecureKeyAlreadyExistsException e) {
    // expected
    }
    client.deleteKey(id);
}
Also used : SecureKeyCreateRequest(co.cask.cdap.proto.security.SecureKeyCreateRequest) SecureKeyAlreadyExistsException(co.cask.cdap.common.SecureKeyAlreadyExistsException) SecureKeyId(co.cask.cdap.proto.id.SecureKeyId) SecureKeyNotFoundException(co.cask.cdap.common.SecureKeyNotFoundException) NamespaceId(co.cask.cdap.proto.id.NamespaceId) NamespaceNotFoundException(co.cask.cdap.common.NamespaceNotFoundException) Test(org.junit.Test)

Example 9 with NamespaceNotFoundException

use of co.cask.cdap.common.NamespaceNotFoundException in project cdap by caskdata.

the class KMSSecureStore method getSecureData.

/**
   * Returns the data stored in the secure store. Makes two calls to the provider, one to get the metadata and another
   * to get the data.
   * @param namespace The namespace this key belongs to.
   * @param name Name of the key.
   * @return An object representing the securely stored data associated with the name.
   * @throws NamespaceNotFoundException If the specified namespace does not exist.
   * @throws IOException If there was a problem getting the key or the metadata from the underlying key provider.
   */
// Unfortunately KeyProvider does not specify the underlying cause except in the message, so we can not throw a
// more specific exception.
@Override
public SecureStoreData getSecureData(String namespace, String name) throws Exception {
    checkNamespaceExists(namespace);
    String keyName = getKeyName(namespace, name);
    KeyProvider.Metadata metadata = provider.getMetadata(keyName);
    // Provider returns null if the key is not found.
    if (metadata == null) {
        throw new NotFoundException(new SecureKeyId(namespace, name));
    }
    SecureStoreMetadata meta = SecureStoreMetadata.of(name, metadata.getDescription(), metadata.getAttributes());
    KeyProvider.KeyVersion keyVersion = provider.getCurrentKey(keyName);
    return new SecureStoreData(meta, keyVersion.getMaterial());
}
Also used : KeyProvider(org.apache.hadoop.crypto.key.KeyProvider) SecureStoreData(co.cask.cdap.api.security.store.SecureStoreData) SecureKeyId(co.cask.cdap.proto.id.SecureKeyId) SecureStoreMetadata(co.cask.cdap.api.security.store.SecureStoreMetadata) NamespaceNotFoundException(co.cask.cdap.common.NamespaceNotFoundException) NotFoundException(co.cask.cdap.common.NotFoundException)

Example 10 with NamespaceNotFoundException

use of co.cask.cdap.common.NamespaceNotFoundException in project cdap by caskdata.

the class FileSecureStore method deleteSecureData.

/**
   * Deletes the element with the given name. Flushes the keystore after deleting the key from the in memory keystore.
   * If the flush fails, we attempt to insert to key back to the in memory store and notify the user that delete failed.
   * If the insertion in the key store fails after a flush failure then there would be a discrepancy between the
   * in memory store and the file on the disk. This will be remedied the next time a flush happens.
   * If another flush does not happen and the system is restarted, the only time that file is read,
   * then we will have an extra key in the keystore.
   * @param namespace The namespace this key belongs to.
   * @param name Name of the element to be deleted.
   * @throws NamespaceNotFoundException If the specified namespace does not exist.
   * @throws NotFoundException If the key to be deleted is not found.
   * @throws IOException If their was a problem during deleting the key from the in memory store
   * or if there was a problem persisting the keystore after deleting the element.
   */
@Override
public void deleteSecureData(String namespace, String name) throws Exception {
    checkNamespaceExists(namespace);
    String keyName = getKeyName(namespace, name);
    Key key = null;
    writeLock.lock();
    try {
        if (!keyStore.containsAlias(keyName)) {
            throw new NotFoundException(new SecureKeyId(namespace, name));
        }
        key = deleteFromStore(keyName, password);
        flush();
        LOG.debug(String.format("Successfully deleted key %s from namespace %s", name, namespace));
    } catch (UnrecoverableKeyException | NoSuchAlgorithmException | KeyStoreException e) {
        throw new IOException("Failed to delete the key. ", e);
    } catch (IOException ioe) {
        try {
            keyStore.setKeyEntry(keyName, key, password, null);
        } catch (KeyStoreException e) {
            ioe.addSuppressed(e);
        }
        throw ioe;
    } finally {
        writeLock.unlock();
    }
}
Also used : SecureKeyId(co.cask.cdap.proto.id.SecureKeyId) UnrecoverableKeyException(java.security.UnrecoverableKeyException) NamespaceNotFoundException(co.cask.cdap.common.NamespaceNotFoundException) NotFoundException(co.cask.cdap.common.NotFoundException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStoreException(java.security.KeyStoreException) IOException(java.io.IOException) Key(java.security.Key)

Aggregations

NamespaceNotFoundException (co.cask.cdap.common.NamespaceNotFoundException)20 IOException (java.io.IOException)11 NotFoundException (co.cask.cdap.common.NotFoundException)8 NamespaceId (co.cask.cdap.proto.id.NamespaceId)7 NamespaceMeta (co.cask.cdap.proto.NamespaceMeta)5 UnauthorizedException (co.cask.cdap.security.spi.authorization.UnauthorizedException)5 HttpResponse (co.cask.common.http.HttpResponse)5 URL (java.net.URL)5 BadRequestException (co.cask.cdap.common.BadRequestException)4 NamespaceCannotBeDeletedException (co.cask.cdap.common.NamespaceCannotBeDeletedException)3 SecureKeyId (co.cask.cdap.proto.id.SecureKeyId)3 Test (org.junit.Test)3 DatasetManagementException (co.cask.cdap.api.dataset.DatasetManagementException)2 NamespaceAlreadyExistsException (co.cask.cdap.common.NamespaceAlreadyExistsException)2 SecureKeyAlreadyExistsException (co.cask.cdap.common.SecureKeyAlreadyExistsException)2 NamespaceConfig (co.cask.cdap.proto.NamespaceConfig)2 StreamDetail (co.cask.cdap.proto.StreamDetail)2 DatasetId (co.cask.cdap.proto.id.DatasetId)2 TypeToken (com.google.common.reflect.TypeToken)2 JsonSyntaxException (com.google.gson.JsonSyntaxException)2