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