use of io.cdap.cdap.proto.id.SecureKeyId 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.id.SecureKeyId in project cdap by caskdata.
the class SecureStoreHandler method get.
@Path("/{key-name}")
@GET
public void get(HttpRequest httpRequest, HttpResponder httpResponder, @PathParam("namespace-id") String namespace, @PathParam("key-name") String name) throws Exception {
SecureKeyId secureKeyId = new SecureKeyId(namespace, name);
httpResponder.sendByteArray(HttpResponseStatus.OK, secureStore.get(namespace, name).get(), new DefaultHttpHeaders().set(HttpHeaderNames.CONTENT_TYPE, "text/plain;charset=utf-8"));
}
use of io.cdap.cdap.proto.id.SecureKeyId in project cdap by caskdata.
the class DefaultSecureStoreService method put.
/**
* Puts the user provided data in the secure store, if the user has admin access to the key.
*
* @throws UnauthorizedException If the user does not have write permissions on the namespace.
* @throws NamespaceNotFoundException If the specified namespace does not exist.
* @throws IOException If there was a problem storing the key to underlying provider.
*/
@Override
public final synchronized void put(String namespace, String name, String value, @Nullable String description, Map<String, String> properties) throws Exception {
Principal principal = authenticationContext.getPrincipal();
NamespaceId namespaceId = new NamespaceId(namespace);
SecureKeyId secureKeyId = namespaceId.secureKey(name);
accessEnforcer.enforce(secureKeyId, principal, StandardPermission.UPDATE);
secureStoreService.put(namespace, name, value, description, properties);
}
use of io.cdap.cdap.proto.id.SecureKeyId in project cdap by caskdata.
the class DefaultSecureStoreService method delete.
/**
* Deletes the key if the user has ADMIN privileges to the key.
*
* @throws UnauthorizedException If the user does not have admin privileges required to delete the secure key.
* @throws NamespaceNotFoundException If the specified namespace does not exist.
* @throws NotFoundException If the key to be deleted is not found.
* @throws IOException If there was a problem deleting it from the underlying provider.
*/
@Override
public final void delete(String namespace, String name) throws Exception {
Principal principal = authenticationContext.getPrincipal();
SecureKeyId secureKeyId = new SecureKeyId(namespace, name);
accessEnforcer.enforce(secureKeyId, principal, StandardPermission.DELETE);
secureStoreService.delete(namespace, name);
}
use of io.cdap.cdap.proto.id.SecureKeyId 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