Search in sources :

Example 16 with SecureStoreMetadata

use of io.cdap.cdap.api.security.store.SecureStoreMetadata in project cdap by caskdata.

the class SecureStoreTest method testList.

@Test
public void testList() throws Exception {
    // Test empty list
    HttpResponse response = list();
    Assert.assertEquals(200, response.getResponseCode());
    List<SecureStoreMetadata> keys = GSON.fromJson(response.getResponseBodyAsString(), LIST_TYPE);
    Assert.assertTrue(keys.isEmpty());
    // One element
    SecureKeyCreateRequest secureKeyCreateRequest = new SecureKeyCreateRequest(DESCRIPTION, DATA, PROPERTIES);
    response = create(KEY, secureKeyCreateRequest);
    Assert.assertEquals(200, response.getResponseCode());
    response = list();
    Assert.assertEquals(200, response.getResponseCode());
    keys = GSON.fromJson(response.getResponseBodyAsString(), LIST_TYPE);
    Assert.assertEquals(1, keys.size());
    Assert.assertEquals(DESCRIPTION, keys.get(0).getDescription());
    // Two elements
    secureKeyCreateRequest = new SecureKeyCreateRequest(DESCRIPTION2, DATA2, PROPERTIES2);
    response = create(KEY2, secureKeyCreateRequest);
    Assert.assertEquals(200, response.getResponseCode());
    response = list();
    Assert.assertEquals(200, response.getResponseCode());
    keys = GSON.fromJson(response.getResponseBodyAsString(), LIST_TYPE);
    Assert.assertEquals(2, keys.size());
    keys.sort(Comparator.comparing(SecureStoreMetadata::getName));
    Assert.assertEquals(DESCRIPTION, keys.get(0).getDescription());
    Assert.assertEquals(DESCRIPTION2, keys.get(1).getDescription());
    // After deleting an element
    response = delete(KEY);
    Assert.assertEquals(200, response.getResponseCode());
    response = list();
    Assert.assertEquals(200, response.getResponseCode());
    keys = GSON.fromJson(response.getResponseBodyAsString(), LIST_TYPE);
    Assert.assertEquals(1, keys.size());
    Assert.assertEquals(DESCRIPTION2, keys.get(0).getDescription());
}
Also used : SecureKeyCreateRequest(io.cdap.cdap.proto.security.SecureKeyCreateRequest) SecureStoreMetadata(io.cdap.cdap.api.security.store.SecureStoreMetadata) HttpResponse(io.cdap.common.http.HttpResponse) Test(org.junit.Test)

Example 17 with SecureStoreMetadata

use of io.cdap.cdap.api.security.store.SecureStoreMetadata in project cdap by caskdata.

the class SecureStoreClientTest method testSecureKeys.

@Test
public void testSecureKeys() throws Exception {
    // no secure keys to begin with
    List<SecureStoreMetadata> secureKeys = client.listKeys(NamespaceId.DEFAULT);
    Assert.assertTrue(secureKeys.isEmpty());
    // create a key
    String key = "securekey";
    String desc = "SomeDesc";
    String data = "secureData";
    Map<String, String> properties = ImmutableMap.of("k1", "v1");
    long creationTime = System.currentTimeMillis();
    SecureKeyId secureKeyId = new SecureKeyId(NamespaceId.DEFAULT.getNamespace(), key);
    client.createKey(secureKeyId, new SecureKeyCreateRequest(desc, data, properties));
    Assert.assertEquals(data, client.getData(secureKeyId));
    Assert.assertEquals(1, client.listKeys(NamespaceId.DEFAULT).size());
    SecureStoreMetadata metadata = client.getKeyMetadata(secureKeyId);
    Assert.assertEquals(desc, metadata.getDescription());
    Assert.assertTrue(metadata.getLastModifiedTime() >= creationTime);
    Assert.assertEquals(properties, metadata.getProperties());
    client.createKey(secureKeyId, new SecureKeyCreateRequest(desc, "updatedSecureData", properties));
    Assert.assertEquals("updatedSecureData", client.getData(secureKeyId));
    Assert.assertEquals(1, client.listKeys(NamespaceId.DEFAULT).size());
    metadata = client.getKeyMetadata(secureKeyId);
    Assert.assertEquals(desc, metadata.getDescription());
    Assert.assertTrue(metadata.getLastModifiedTime() >= creationTime);
    Assert.assertEquals(properties, metadata.getProperties());
    // delete the key
    client.deleteKey(secureKeyId);
    Assert.assertTrue(client.listKeys(NamespaceId.DEFAULT).isEmpty());
}
Also used : SecureKeyCreateRequest(io.cdap.cdap.proto.security.SecureKeyCreateRequest) SecureKeyId(io.cdap.cdap.proto.id.SecureKeyId) SecureStoreMetadata(io.cdap.cdap.api.security.store.SecureStoreMetadata) Test(org.junit.Test)

Example 18 with SecureStoreMetadata

use of io.cdap.cdap.api.security.store.SecureStoreMetadata in project cdap by caskdata.

the class KMSSecureStoreService method get.

/**
 * 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 get(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 = new SecureStoreMetadata(name, metadata.getDescription(), metadata.getCreated().getTime(), metadata.getAttributes());
    KeyProvider.KeyVersion keyVersion = provider.getCurrentKey(keyName);
    return new SecureStoreData(meta, keyVersion.getMaterial());
}
Also used : KeyProvider(org.apache.hadoop.crypto.key.KeyProvider) SecureStoreData(io.cdap.cdap.api.security.store.SecureStoreData) SecureKeyId(io.cdap.cdap.proto.id.SecureKeyId) SecureStoreMetadata(io.cdap.cdap.api.security.store.SecureStoreMetadata) NamespaceNotFoundException(io.cdap.cdap.common.NamespaceNotFoundException) NotFoundException(io.cdap.cdap.common.NotFoundException)

Example 19 with SecureStoreMetadata

use of io.cdap.cdap.api.security.store.SecureStoreMetadata in project cdap by caskdata.

the class SecureStoreClient method listKeys.

/**
 * List all the secure keys in the namespace
 * @param namespaceId {@link NamespaceId} namespace id
 * @return list of key names and descriptions
 * @throws IOException if a network error occurred
 * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
 * @throws NamespaceNotFoundException if the given namespace is not found
 */
public List<SecureStoreMetadata> listKeys(NamespaceId namespaceId) throws IOException, UnauthenticatedException, NamespaceNotFoundException, UnauthorizedException {
    URL url = config.resolveNamespacedURLV3(namespaceId, SECURE_KEYS);
    HttpResponse response = restClient.execute(HttpMethod.GET, url, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND);
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new NamespaceNotFoundException(namespaceId);
    }
    return ObjectResponse.fromJsonBody(response, new TypeToken<List<SecureStoreMetadata>>() {
    }).getResponseObject();
}
Also used : SecureStoreMetadata(io.cdap.cdap.api.security.store.SecureStoreMetadata) TypeToken(com.google.common.reflect.TypeToken) HttpResponse(io.cdap.common.http.HttpResponse) URL(java.net.URL) NamespaceNotFoundException(io.cdap.cdap.common.NamespaceNotFoundException)

Example 20 with SecureStoreMetadata

use of io.cdap.cdap.api.security.store.SecureStoreMetadata in project cdap by caskdata.

the class SecretManagerSecureStoreServiceTest method testSecureStoreService.

@Test
public void testSecureStoreService() throws Exception {
    String key1 = "key1";
    String key2 = "key2";
    String value1 = "value1";
    String value2 = "value2";
    String description1 = "description1";
    String description2 = "description2";
    Map<String, String> properties = new HashMap<>();
    properties.put("prop1", "value1");
    // put key value to secure store
    secureStoreService.put(NAMESPACE1, key1, value1, description1, properties);
    secureStoreService.put(NAMESPACE1, key2, value2, description2, properties);
    // get key value from secure store
    SecureStoreData ns1Key1 = secureStoreService.get(NAMESPACE1, key1);
    SecureStoreData ns1Key2 = secureStoreService.get(NAMESPACE1, key2);
    Assert.assertEquals(key1, ns1Key1.getMetadata().getName());
    Assert.assertArrayEquals(value1.getBytes(StandardCharsets.UTF_8), ns1Key1.get());
    Assert.assertEquals(description1, ns1Key1.getMetadata().getDescription());
    Assert.assertEquals(properties.size(), ns1Key1.getMetadata().getProperties().size());
    Assert.assertEquals(key2, ns1Key2.getMetadata().getName());
    Assert.assertArrayEquals(value2.getBytes(StandardCharsets.UTF_8), ns1Key2.get());
    Assert.assertEquals(description2, ns1Key2.getMetadata().getDescription());
    Assert.assertEquals(properties.size(), ns1Key2.getMetadata().getProperties().size());
    // list key value from secure store
    int i = 1;
    List<SecureStoreMetadata> metadatas = secureStoreService.list(NAMESPACE1);
    metadatas.sort(Comparator.comparing(SecureStoreMetadata::getName));
    for (SecureStoreMetadata metadata : metadatas) {
        Assert.assertEquals("key" + i, metadata.getName());
        Assert.assertEquals("description" + i, metadata.getDescription());
        i++;
    }
    // delete key value from secure store
    secureStoreService.delete(NAMESPACE1, key1);
    secureStoreService.delete(NAMESPACE1, key2);
    Assert.assertEquals(0, secureStoreService.list(NAMESPACE1).size());
}
Also used : SecureStoreData(io.cdap.cdap.api.security.store.SecureStoreData) HashMap(java.util.HashMap) SecureStoreMetadata(io.cdap.cdap.api.security.store.SecureStoreMetadata) Test(org.junit.Test)

Aggregations

SecureStoreMetadata (io.cdap.cdap.api.security.store.SecureStoreMetadata)20 SecureStoreData (io.cdap.cdap.api.security.store.SecureStoreData)9 Test (org.junit.Test)8 HashMap (java.util.HashMap)5 SecureKeyId (io.cdap.cdap.proto.id.SecureKeyId)4 IOException (java.io.IOException)4 HttpResponse (io.cdap.common.http.HttpResponse)3 ArrayList (java.util.ArrayList)3 NamespaceNotFoundException (io.cdap.cdap.common.NamespaceNotFoundException)2 NotFoundException (io.cdap.cdap.common.NotFoundException)2 SecureKeyCreateRequest (io.cdap.cdap.proto.security.SecureKeyCreateRequest)2 SecretMetadata (io.cdap.cdap.securestore.spi.secret.SecretMetadata)2 KeyStoreException (java.security.KeyStoreException)2 Map (java.util.Map)2 KeyProvider (org.apache.hadoop.crypto.key.KeyProvider)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Predicate (com.google.common.base.Predicate)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 TypeToken (com.google.common.reflect.TypeToken)1 AbstractModule (com.google.inject.AbstractModule)1