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