use of io.cdap.cdap.api.security.store.SecureStoreMetadata in project cdap by caskdata.
the class RemoteSecureStore method get.
@Override
public SecureStoreData get(String namespace, String name) throws Exception {
// 1. Get metadata of the secure key
HttpRequest request = remoteClient.requestBuilder(HttpMethod.GET, createPath(namespace, name) + "/metadata").build();
HttpResponse response = remoteClient.execute(request);
handleResponse(response, namespace, name, String.format("Error occurred while getting metadata for key %s:%s", namespace, name));
SecureStoreMetadata metadata = GSON.fromJson(response.getResponseBodyAsString(), SecureStoreMetadata.class);
// 2. Get sensitive data for the secure key
request = remoteClient.requestBuilder(HttpMethod.GET, createPath(namespace, name)).build();
response = remoteClient.execute(request);
handleResponse(response, namespace, name, String.format("Error occurred while getting key %s:%s", namespace, name));
// response is not a json object
byte[] data = response.getResponseBody();
return new SecureStoreData(metadata, data);
}
use of io.cdap.cdap.api.security.store.SecureStoreMetadata 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.SecureStoreMetadata 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();
}
}
use of io.cdap.cdap.api.security.store.SecureStoreMetadata in project cdap by caskdata.
the class FileSecureStoreServiceTest method verifyList.
private void verifyList(List<SecureStoreMetadata> metadatas, ImmutableMap<String, String> map) {
Assert.assertEquals(metadatas.size(), map.size());
UnmodifiableIterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
for (SecureStoreMetadata metadata : metadatas) {
Map.Entry<String, String> expected = iterator.next();
Assert.assertEquals(expected.getKey(), metadata.getName());
Assert.assertEquals(expected.getValue(), metadata.getDescription());
}
}
use of io.cdap.cdap.api.security.store.SecureStoreMetadata in project cdap by caskdata.
the class RemoteSecureStoreTest method testRemoteSecureStore.
@Test
public void testRemoteSecureStore() throws Exception {
SecureStoreMetadata secureStoreMetadata = new SecureStoreMetadata("key", "description", 1, ImmutableMap.of("prop1", "value1"));
SecureStoreData secureStoreData = new SecureStoreData(secureStoreMetadata, "value".getBytes(StandardCharsets.UTF_8));
// test put and get
remoteSecureStore.put(NAMESPACE1, "key", "value", "description", ImmutableMap.of("prop1", "value1"));
SecureStoreData actual = remoteSecureStore.get(NAMESPACE1, "key");
Assert.assertEquals(secureStoreMetadata.getName(), actual.getMetadata().getName());
Assert.assertArrayEquals(secureStoreData.get(), actual.get());
Assert.assertEquals(secureStoreMetadata.getDescription(), actual.getMetadata().getDescription());
Assert.assertEquals(secureStoreMetadata.getProperties().size(), actual.getMetadata().getProperties().size());
// test list
List<SecureStoreMetadata> secureData = remoteSecureStore.list(NAMESPACE1);
Assert.assertEquals(1, secureData.size());
SecureStoreMetadata metadata = secureData.get(0);
Assert.assertEquals("key", metadata.getName());
Assert.assertEquals("description", metadata.getDescription());
// test delete
remoteSecureStore.delete(NAMESPACE1, "key");
Assert.assertEquals(0, remoteSecureStore.list(NAMESPACE1).size());
}
Aggregations