Search in sources :

Example 6 with SecureKeyId

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);
}
Also used : SecureKeyCreateRequest(io.cdap.cdap.proto.security.SecureKeyCreateRequest) SecureKeyId(io.cdap.cdap.proto.id.SecureKeyId) BadRequestException(io.cdap.cdap.common.BadRequestException) IOException(java.io.IOException) Path(javax.ws.rs.Path) AuditPolicy(io.cdap.cdap.common.security.AuditPolicy) PUT(javax.ws.rs.PUT)

Example 7 with SecureKeyId

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"));
}
Also used : SecureKeyId(io.cdap.cdap.proto.id.SecureKeyId) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 8 with SecureKeyId

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);
}
Also used : SecureKeyId(io.cdap.cdap.proto.id.SecureKeyId) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) Principal(io.cdap.cdap.proto.security.Principal)

Example 9 with SecureKeyId

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);
}
Also used : SecureKeyId(io.cdap.cdap.proto.id.SecureKeyId) Principal(io.cdap.cdap.proto.security.Principal)

Example 10 with SecureKeyId

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);
}
Also used : SecureKeyCreateRequest(io.cdap.cdap.proto.security.SecureKeyCreateRequest) SecureKeyId(io.cdap.cdap.proto.id.SecureKeyId) SecureKeyNotFoundException(io.cdap.cdap.common.SecureKeyNotFoundException) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) NamespaceNotFoundException(io.cdap.cdap.common.NamespaceNotFoundException) Test(org.junit.Test)

Aggregations

SecureKeyId (io.cdap.cdap.proto.id.SecureKeyId)13 SecureStoreMetadata (io.cdap.cdap.api.security.store.SecureStoreMetadata)4 NamespaceNotFoundException (io.cdap.cdap.common.NamespaceNotFoundException)4 Test (org.junit.Test)4 NotFoundException (io.cdap.cdap.common.NotFoundException)3 Principal (io.cdap.cdap.proto.security.Principal)3 SecureKeyCreateRequest (io.cdap.cdap.proto.security.SecureKeyCreateRequest)3 IOException (java.io.IOException)3 SecureStoreData (io.cdap.cdap.api.security.store.SecureStoreData)2 SecureKeyNotFoundException (io.cdap.cdap.common.SecureKeyNotFoundException)2 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)2 Key (java.security.Key)2 KeyStoreException (java.security.KeyStoreException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 UnrecoverableKeyException (java.security.UnrecoverableKeyException)2 Path (javax.ws.rs.Path)2 Predicate (com.google.common.base.Predicate)1 BadRequestException (io.cdap.cdap.common.BadRequestException)1 AuditPolicy (io.cdap.cdap.common.security.AuditPolicy)1 GrantedPermission (io.cdap.cdap.proto.security.GrantedPermission)1