Search in sources :

Example 1 with SecureKeyAlreadyExistsException

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

the class SecureStoreClient method createKey.

/**
 * Creates a secure key
 *
 * @param secureKeyId {@link SecureKeyId} secure key name
 * @param keyCreateRequest {@link SecureKeyCreateRequest}
 * @throws IOException if a network error occurred
 * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
 * @throws SecureKeyAlreadyExistsException if the secure key already exists
 * @throws NamespaceNotFoundException if namespace is not found
 */
public void createKey(SecureKeyId secureKeyId, SecureKeyCreateRequest keyCreateRequest) throws IOException, UnauthenticatedException, AlreadyExistsException, NamespaceNotFoundException, UnauthorizedException {
    URL url = config.resolveNamespacedURLV3(secureKeyId.getParent(), getSecureKeyPath(secureKeyId));
    HttpResponse response = restClient.execute(HttpMethod.PUT, url, GSON.toJson(keyCreateRequest), null, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND, HttpURLConnection.HTTP_CONFLICT);
    if (response.getResponseCode() == HttpURLConnection.HTTP_CONFLICT) {
        throw new SecureKeyAlreadyExistsException(secureKeyId);
    }
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new NamespaceNotFoundException(secureKeyId.getParent());
    }
}
Also used : SecureKeyAlreadyExistsException(co.cask.cdap.common.SecureKeyAlreadyExistsException) HttpResponse(co.cask.common.http.HttpResponse) URL(java.net.URL) NamespaceNotFoundException(co.cask.cdap.common.NamespaceNotFoundException)

Example 2 with SecureKeyAlreadyExistsException

use of co.cask.cdap.common.SecureKeyAlreadyExistsException 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)

Aggregations

NamespaceNotFoundException (co.cask.cdap.common.NamespaceNotFoundException)2 SecureKeyAlreadyExistsException (co.cask.cdap.common.SecureKeyAlreadyExistsException)2 SecureKeyNotFoundException (co.cask.cdap.common.SecureKeyNotFoundException)1 NamespaceId (co.cask.cdap.proto.id.NamespaceId)1 SecureKeyId (co.cask.cdap.proto.id.SecureKeyId)1 SecureKeyCreateRequest (co.cask.cdap.proto.security.SecureKeyCreateRequest)1 HttpResponse (co.cask.common.http.HttpResponse)1 URL (java.net.URL)1 Test (org.junit.Test)1