use of io.cdap.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(FullHttpRequest httpRequest, HttpResponder httpResponder, @PathParam("namespace-id") String namespace, @PathParam("key-name") String name) throws Exception {
SecureKeyId secureKeyId = new SecureKeyId(namespace, name);
SecureKeyCreateRequest secureKeyCreateRequest;
try {
secureKeyCreateRequest = parseBody(httpRequest);
} catch (IOException e) {
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));
}
if (Strings.isNullOrEmpty(secureKeyCreateRequest.getData()) || secureKeyCreateRequest.getData().trim().isEmpty()) {
throw new BadRequestException("The data field must not be null or empty. The data will be stored securely " + "under provided key name.");
}
secureStoreManager.put(namespace, name, secureKeyCreateRequest.getData(), secureKeyCreateRequest.getDescription(), secureKeyCreateRequest.getProperties());
httpResponder.sendStatus(HttpResponseStatus.OK);
}
use of io.cdap.cdap.proto.security.SecureKeyCreateRequest in project cdap by caskdata.
the class RemoteSecureStore method put.
@Override
public void put(String namespace, String name, String data, @Nullable String description, Map<String, String> properties) throws Exception {
SecureKeyCreateRequest createRequest = new SecureKeyCreateRequest(description, data, properties);
HttpRequest request = remoteClient.requestBuilder(HttpMethod.PUT, createPath(namespace, name)).withBody(GSON.toJson(createRequest)).build();
HttpResponse response = remoteClient.execute(request);
handleResponse(response, namespace, name, String.format("Error occurred while putting key %s:%s", namespace, name));
}
use of io.cdap.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 = create(KEY, secureKeyCreateRequest);
Assert.assertEquals(200, response.getResponseCode());
response = get(KEY);
Assert.assertEquals(200, response.getResponseCode());
Assert.assertEquals(DATA, response.getResponseBodyAsString());
response = delete(KEY);
Assert.assertEquals(200, response.getResponseCode());
}
use of io.cdap.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 = 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.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);
client.deleteKey(id);
}
Aggregations