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()));
}
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();
}
}
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);
}
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());
}
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();
}
}
Aggregations