use of io.cdap.cdap.api.security.store.SecureStoreData in project cdap by cdapio.
the class FileSecureStoreServiceTest method testDelete.
@Test(expected = NotFoundException.class)
public void testDelete() throws Exception {
populateStore();
SecureStoreMetadata metadata = new SecureStoreMetadata(KEY1, DESCRIPTION1, System.currentTimeMillis(), PROPERTIES_1);
SecureStoreData secureStoreData = new SecureStoreData(metadata, VALUE1.getBytes(Charsets.UTF_8));
Assert.assertArrayEquals(secureStoreData.get(), secureStore.get(NAMESPACE1, KEY1).get());
secureStoreManager.delete(NAMESPACE1, KEY1);
try {
secureStore.get(NAMESPACE1, KEY1);
} catch (IOException ioe) {
Assert.assertTrue(ioe.getMessage().contains("not found in the secure store"));
throw ioe;
}
}
use of io.cdap.cdap.api.security.store.SecureStoreData in project cdap by cdapio.
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());
}
use of io.cdap.cdap.api.security.store.SecureStoreData in project cdap by caskdata.
the class FileSecureStoreService method deserialize.
private SecureStoreData deserialize(byte[] data) throws IOException {
DataInputStream dis = new DataInputStream(new ByteArrayInputStream(data));
String name = dis.readUTF();
boolean descriptionExists = dis.readBoolean();
String description = descriptionExists ? dis.readUTF() : null;
long lastModified = dis.readLong();
Map<String, String> properties = new HashMap<>();
int len = dis.readInt();
for (int i = 0; i < len; i++) {
properties.put(dis.readUTF(), dis.readUTF());
}
SecureStoreMetadata meta = new SecureStoreMetadata(name, description, lastModified, properties);
byte[] secret = new byte[dis.readInt()];
dis.readFully(secret);
return new SecureStoreData(meta, secret);
}
use of io.cdap.cdap.api.security.store.SecureStoreData in project cdap by caskdata.
the class SecureStoreHandler method getMetadata.
@Path("/{key-name}/metadata")
@GET
public void getMetadata(HttpRequest httpRequest, HttpResponder httpResponder, @PathParam("namespace-id") String namespace, @PathParam("key-name") String name) throws Exception {
SecureStoreData secureStoreData = secureStore.get(namespace, name);
httpResponder.sendJson(HttpResponseStatus.OK, GSON.toJson(secureStoreData.getMetadata()));
}
use of io.cdap.cdap.api.security.store.SecureStoreData in project cdap by caskdata.
the class FileSecureStoreService method put.
/**
* Stores an element in the secure store. If the element already exists, it will get overwritten.
* @param namespace The namespace this key belongs to.
* @param name Name of the element to store.
* @param data The data that needs to be securely stored.
* @param description User provided description of the entry.
* @param properties Metadata associated with the data.
* @throws NamespaceNotFoundException If the specified namespace does not exist.
* @throws IOException If there was a problem storing the key to the in memory keystore
* or if there was problem persisting the keystore.
*/
@Override
public void put(String namespace, String name, String data, @Nullable String description, Map<String, String> properties) throws Exception {
checkNamespaceExists(namespace);
String keyName = getKeyName(namespace, name);
SecureStoreMetadata meta = new SecureStoreMetadata(name, description, System.currentTimeMillis(), properties);
SecureStoreData secureStoreData = new SecureStoreData(meta, data.getBytes(Charsets.UTF_8));
writeLock.lock();
try {
keyStore.setKeyEntry(keyName, new SecretKeySpec(serialize(secureStoreData), "none"), password, null);
// Attempt to persist the store.
flush();
LOG.debug(String.format("Successfully stored %s in namespace %s", name, namespace));
} catch (KeyStoreException e) {
// We failed to store the key in the key store. Throw an IOException.
throw new IOException("Failed to store the key. ", e);
} finally {
writeLock.unlock();
}
}
Aggregations