use of co.cask.cdap.proto.security.SecureKeyCreateRequest in project cdap by caskdata.
the class SecureStoreTest method testCreate.
@Test
public void testCreate() throws Exception {
SecureKeyCreateRequest secureKeyCreateRequest = new SecureKeyCreateRequest(DESCRIPTION, DATA, PROPERTIES);
HttpResponse response = doPut("/v3/namespaces/default/securekeys/" + KEY, GSON.toJson(secureKeyCreateRequest));
Assert.assertEquals(200, response.getStatusLine().getStatusCode());
response = delete(KEY);
Assert.assertEquals(200, response.getStatusLine().getStatusCode());
}
use of co.cask.cdap.proto.security.SecureKeyCreateRequest in project cdap by caskdata.
the class SecureStoreHandler method create.
@Path("/{key-name}")
@PUT
@AuditPolicy(AuditDetail.REQUEST_BODY)
public void create(HttpRequest httpRequest, HttpResponder httpResponder, @PathParam("namespace-id") String namespace, @PathParam("key-name") String name) throws Exception {
SecureKeyId secureKeyId = new SecureKeyId(namespace, name);
SecureKeyCreateRequest secureKeyCreateRequest = parseBody(httpRequest, SecureKeyCreateRequest.class);
if (secureKeyCreateRequest == null) {
SecureKeyCreateRequest dummy = new SecureKeyCreateRequest("<description>", "<data>", ImmutableMap.of("key", "value"));
throw new BadRequestException("Unable to parse the request. The request body should be of the following format." + " \n" + GSON.toJson(dummy));
}
secureStoreManager.putSecureData(namespace, name, secureKeyCreateRequest.getData(), secureKeyCreateRequest.getDescription(), secureKeyCreateRequest.getProperties());
httpResponder.sendStatus(HttpResponseStatus.OK);
}
use of co.cask.cdap.proto.security.SecureKeyCreateRequest in project cdap by caskdata.
the class SecureStoreClientTest method testErrorScenarios.
@Test
public void testErrorScenarios() throws Exception {
try {
client.listKeys(new NamespaceId("notfound"));
Assert.fail("Should have thrown exception since namespace doesn't exist");
} catch (NamespaceNotFoundException e) {
// expected
}
try {
client.deleteKey(new SecureKeyId(NamespaceId.DEFAULT.getNamespace(), "badkey"));
Assert.fail("Should have thrown exception since the key doesn't exist");
} catch (SecureKeyNotFoundException e) {
// expected
}
try {
client.getData(new SecureKeyId(NamespaceId.DEFAULT.getNamespace(), "badkey"));
Assert.fail("Should have thrown exception since the key doesn't exist");
} catch (SecureKeyNotFoundException e) {
// expected
}
try {
client.getKeyMetadata(new SecureKeyId(NamespaceId.DEFAULT.getNamespace(), "badkey"));
Assert.fail("Should have thrown exception since the key doesn't exist");
} catch (SecureKeyNotFoundException e) {
// expected
}
try {
client.getKeyMetadata(new SecureKeyId("notfound", "somekey"));
Assert.fail("Should have thrown exception since the namespace doesn't exist");
} catch (SecureKeyNotFoundException e) {
// expected
}
SecureKeyId id = new SecureKeyId(NamespaceId.DEFAULT.getNamespace(), "key1");
SecureKeyCreateRequest request = new SecureKeyCreateRequest("", "a", ImmutableMap.<String, String>of());
client.createKey(id, request);
try {
client.createKey(id, request);
Assert.fail("Should have thrown exception since the key already exists");
} catch (SecureKeyAlreadyExistsException e) {
// expected
}
client.deleteKey(id);
}
use of co.cask.cdap.proto.security.SecureKeyCreateRequest in project cdap by caskdata.
the class SecureStoreClientTest method testSecureKeys.
@Test
public void testSecureKeys() throws Exception {
// no secure keys to begin with
Map<String, String> 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());
// delete the key
client.deleteKey(secureKeyId);
Assert.assertTrue(client.listKeys(NamespaceId.DEFAULT).isEmpty());
}
use of co.cask.cdap.proto.security.SecureKeyCreateRequest in project cdap by caskdata.
the class SecureStoreTest method testList.
@Test
public void testList() throws Exception {
// Test empty list
HttpResponse response = doGet("/v3/namespaces/default/securekeys/");
Assert.assertEquals(200, response.getStatusLine().getStatusCode());
Assert.assertEquals("{}", readResponse(response));
// One element
SecureKeyCreateRequest secureKeyCreateRequest = new SecureKeyCreateRequest(DESCRIPTION, DATA, PROPERTIES);
response = doPut("/v3/namespaces/default/securekeys/" + KEY, GSON.toJson(secureKeyCreateRequest));
Assert.assertEquals(200, response.getStatusLine().getStatusCode());
response = doGet("/v3/namespaces/default/securekeys/");
String result = readResponse(response);
Map<String, String> expected = new HashMap<>();
expected.put(KEY, DESCRIPTION);
Map<String, String> returned = GSON.fromJson(result, MAP_TYPE);
for (String entry : returned.keySet()) {
Assert.assertTrue(expected.containsKey(entry));
}
// Two elements
secureKeyCreateRequest = new SecureKeyCreateRequest(DESCRIPTION2, DATA2, PROPERTIES2);
response = doPut("/v3/namespaces/default/securekeys/" + KEY2, GSON.toJson(secureKeyCreateRequest));
Assert.assertEquals(200, response.getStatusLine().getStatusCode());
response = doGet("/v3/namespaces/default/securekeys/");
String result2 = readResponse(response);
returned = GSON.fromJson(result2, MAP_TYPE);
expected.put(KEY2, DESCRIPTION2);
for (String entry : returned.keySet()) {
Assert.assertTrue(expected.containsKey(entry));
}
// After deleting an element
delete(KEY);
response = doGet("/v3/namespaces/default/securekeys/");
String result3 = readResponse(response);
returned = GSON.fromJson(result3, MAP_TYPE);
expected.remove(KEY);
for (String entry : returned.keySet()) {
Assert.assertTrue(expected.containsKey(entry));
}
}
Aggregations