Search in sources :

Example 6 with SecureStoreMetadata

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

the class FileSecureStoreTest method testGet.

@Test
public void testGet() throws Exception {
    populateStore();
    SecureStoreMetadata metadata = SecureStoreMetadata.of(KEY1, DESCRIPTION1, PROPERTIES_1);
    SecureStoreData secureStoreData = new SecureStoreData(metadata, VALUE1.getBytes(Charsets.UTF_8));
    Assert.assertArrayEquals(secureStoreData.get(), secureStore.getSecureData(NAMESPACE1, KEY1).get());
    Assert.assertEquals(metadata.getDescription(), secureStore.getSecureData(NAMESPACE1, KEY1).getMetadata().getDescription());
    Assert.assertEquals(metadata.getName(), secureStore.getSecureData(NAMESPACE1, KEY1).getMetadata().getName());
}
Also used : SecureStoreData(co.cask.cdap.api.security.store.SecureStoreData) SecureStoreMetadata(co.cask.cdap.api.security.store.SecureStoreMetadata) Test(org.junit.Test)

Example 7 with SecureStoreMetadata

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

the class KMSSecureStore method listSecureData.

/**
   * List of all the entries in the secure store. No filtering or authentication is done here.
   * This method makes two calls to the KMS provider, one to get the list of keys and then another call to
   * get the metadata for all the keys in the requested namespace.
   * @return A list of {@link SecureStoreMetadata} objects representing the data stored in the store.
   * @param namespace The namespace this key belongs to.
   * @throws NamespaceNotFoundException If the specified namespace does not exist.
   * @throws ConcurrentModificationException If a key was deleted between the time we got the list of keys and when
   * we got their metadata.
   * @throws IOException If there was a problem getting the list 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 Map<String, String> listSecureData(String namespace) throws Exception {
    checkNamespaceExists(namespace);
    String prefix = namespace + NAME_SEPARATOR;
    List<String> keysInNamespace = new ArrayList<>();
    KeyProvider.Metadata[] metadatas;
    try {
        for (String key : provider.getKeys()) {
            if (key.startsWith(prefix)) {
                keysInNamespace.add(key);
            }
        }
        metadatas = provider.getKeysMetadata(keysInNamespace.toArray(new String[keysInNamespace.size()]));
    } catch (IOException e) {
        throw new IOException("Failed to get the list of elements from the secure store.", e);
    }
    // If a key was deleted between the time we get the list of keys and their metadatas then throw an exception
    if (metadatas.length != keysInNamespace.size()) {
        throw new ConcurrentModificationException("A key was deleted while listing was in progress. Please try again.");
    }
    Map<String, String> secureStoreMetadatas = new HashMap<>(metadatas.length);
    for (int i = 0; i < metadatas.length; i++) {
        KeyProvider.Metadata metadata = metadatas[i];
        secureStoreMetadatas.put(keysInNamespace.get(i).substring(prefix.length()), metadata.getDescription());
    }
    return secureStoreMetadatas;
}
Also used : KeyProvider(org.apache.hadoop.crypto.key.KeyProvider) ConcurrentModificationException(java.util.ConcurrentModificationException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SecureStoreMetadata(co.cask.cdap.api.security.store.SecureStoreMetadata) IOException(java.io.IOException)

Aggregations

SecureStoreMetadata (co.cask.cdap.api.security.store.SecureStoreMetadata)7 SecureStoreData (co.cask.cdap.api.security.store.SecureStoreData)4 Test (org.junit.Test)4 SecureKeyId (co.cask.cdap.proto.id.SecureKeyId)3 IOException (java.io.IOException)3 KeyProvider (org.apache.hadoop.crypto.key.KeyProvider)2 AlreadyExistsException (co.cask.cdap.common.AlreadyExistsException)1 NamespaceNotFoundException (co.cask.cdap.common.NamespaceNotFoundException)1 NotFoundException (co.cask.cdap.common.NotFoundException)1 SecureKeyCreateRequest (co.cask.cdap.proto.security.SecureKeyCreateRequest)1 KeyStoreException (java.security.KeyStoreException)1 ArrayList (java.util.ArrayList)1 ConcurrentModificationException (java.util.ConcurrentModificationException)1 HashMap (java.util.HashMap)1