Search in sources :

Example 1 with SecureKeyNotFoundException

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

the class SecureStoreClient method getKeyMetadata.

/**
 * Get the metadata associated with the given secure key
 *
 * @param secureKeyId {@link SecureKeyId} secure key name
 * @return {@link SecureStoreMetadata} metadata associated with the key
 * @throws IOException if a network error occurred
 * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
 * @throws SecureKeyNotFoundException if secure key or the namespace is not found
 */
public SecureStoreMetadata getKeyMetadata(SecureKeyId secureKeyId) throws IOException, UnauthenticatedException, SecureKeyNotFoundException, UnauthorizedException {
    URL url = config.resolveNamespacedURLV3(secureKeyId.getParent(), String.format("%s/metadata", getSecureKeyPath(secureKeyId)));
    HttpResponse response = restClient.execute(HttpMethod.GET, url, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND);
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new SecureKeyNotFoundException(secureKeyId);
    }
    return GSON.fromJson(response.getResponseBodyAsString(), SecureStoreMetadata.class);
}
Also used : SecureKeyNotFoundException(io.cdap.cdap.common.SecureKeyNotFoundException) HttpResponse(io.cdap.common.http.HttpResponse) URL(java.net.URL)

Example 2 with SecureKeyNotFoundException

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

the class SecretManagerSecureStoreService method get.

@Override
public SecureStoreData get(String namespace, String name) throws Exception {
    validate(namespace);
    try {
        Secret secret = secretManager.get(namespace, name);
        SecretMetadata metadata = secret.getMetadata();
        return new SecureStoreData(new SecureStoreMetadata(metadata.getName(), metadata.getDescription(), metadata.getCreationTimeMs(), metadata.getProperties()), secret.getData());
    } catch (SecretNotFoundException e) {
        throw new SecureKeyNotFoundException(new SecureKeyId(namespace, name), e);
    }
}
Also used : Secret(io.cdap.cdap.securestore.spi.secret.Secret) SecureStoreData(io.cdap.cdap.api.security.store.SecureStoreData) SecureKeyId(io.cdap.cdap.proto.id.SecureKeyId) SecureStoreMetadata(io.cdap.cdap.api.security.store.SecureStoreMetadata) SecretMetadata(io.cdap.cdap.securestore.spi.secret.SecretMetadata) SecureKeyNotFoundException(io.cdap.cdap.common.SecureKeyNotFoundException) SecretNotFoundException(io.cdap.cdap.securestore.spi.SecretNotFoundException)

Example 3 with SecureKeyNotFoundException

use of io.cdap.cdap.common.SecureKeyNotFoundException 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);
    client.deleteKey(id);
}
Also used : SecureKeyCreateRequest(io.cdap.cdap.proto.security.SecureKeyCreateRequest) SecureKeyId(io.cdap.cdap.proto.id.SecureKeyId) SecureKeyNotFoundException(io.cdap.cdap.common.SecureKeyNotFoundException) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) NamespaceNotFoundException(io.cdap.cdap.common.NamespaceNotFoundException) Test(org.junit.Test)

Example 4 with SecureKeyNotFoundException

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

the class SecureStoreClient method deleteKey.

/**
 * Delete the secure key
 * @param secureKeyId {@link SecureKeyId} secure key name
 * @throws IOException if a network error occurred
 * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
 * @throws SecureKeyNotFoundException if secure key or the namespace is not found
 */
public void deleteKey(SecureKeyId secureKeyId) throws IOException, UnauthenticatedException, SecureKeyNotFoundException, UnauthorizedException {
    URL url = config.resolveNamespacedURLV3(secureKeyId.getParent(), getSecureKeyPath(secureKeyId));
    HttpResponse response = restClient.execute(HttpMethod.DELETE, url, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND);
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new SecureKeyNotFoundException(secureKeyId);
    }
}
Also used : SecureKeyNotFoundException(io.cdap.cdap.common.SecureKeyNotFoundException) HttpResponse(io.cdap.common.http.HttpResponse) URL(java.net.URL)

Example 5 with SecureKeyNotFoundException

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

the class SecureStoreClient method getData.

/**
 * Fetch the data associated with the given secure key
 *
 * @param secureKeyId {@link SecureKeyId} secure key name
 * @return the secure data in a utf-8 encoded byte array
 * @throws IOException if a network error occurred
 * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
 * @throws SecureKeyNotFoundException if secure key or the namespace is not found
 */
public String getData(SecureKeyId secureKeyId) throws IOException, UnauthenticatedException, SecureKeyNotFoundException, UnauthorizedException {
    URL url = config.resolveNamespacedURLV3(secureKeyId.getParent(), getSecureKeyPath(secureKeyId));
    HttpResponse response = restClient.execute(HttpMethod.GET, url, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND);
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new SecureKeyNotFoundException(secureKeyId);
    }
    return response.getResponseBodyAsString();
}
Also used : SecureKeyNotFoundException(io.cdap.cdap.common.SecureKeyNotFoundException) HttpResponse(io.cdap.common.http.HttpResponse) URL(java.net.URL)

Aggregations

SecureKeyNotFoundException (io.cdap.cdap.common.SecureKeyNotFoundException)5 HttpResponse (io.cdap.common.http.HttpResponse)3 URL (java.net.URL)3 SecureKeyId (io.cdap.cdap.proto.id.SecureKeyId)2 SecureStoreData (io.cdap.cdap.api.security.store.SecureStoreData)1 SecureStoreMetadata (io.cdap.cdap.api.security.store.SecureStoreMetadata)1 NamespaceNotFoundException (io.cdap.cdap.common.NamespaceNotFoundException)1 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)1 SecureKeyCreateRequest (io.cdap.cdap.proto.security.SecureKeyCreateRequest)1 SecretNotFoundException (io.cdap.cdap.securestore.spi.SecretNotFoundException)1 Secret (io.cdap.cdap.securestore.spi.secret.Secret)1 SecretMetadata (io.cdap.cdap.securestore.spi.secret.SecretMetadata)1 Test (org.junit.Test)1