Search in sources :

Example 1 with SecureKeyCreateRequest

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);
}
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 2 with SecureKeyCreateRequest

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));
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) SecureKeyCreateRequest(io.cdap.cdap.proto.security.SecureKeyCreateRequest) HttpResponse(io.cdap.common.http.HttpResponse)

Example 3 with SecureKeyCreateRequest

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());
}
Also used : SecureKeyCreateRequest(io.cdap.cdap.proto.security.SecureKeyCreateRequest) HttpResponse(io.cdap.common.http.HttpResponse) Test(org.junit.Test)

Example 4 with SecureKeyCreateRequest

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());
}
Also used : SecureKeyCreateRequest(io.cdap.cdap.proto.security.SecureKeyCreateRequest) SecureStoreMetadata(io.cdap.cdap.api.security.store.SecureStoreMetadata) HttpResponse(io.cdap.common.http.HttpResponse) Test(org.junit.Test)

Example 5 with SecureKeyCreateRequest

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

SecureKeyCreateRequest (io.cdap.cdap.proto.security.SecureKeyCreateRequest)6 Test (org.junit.Test)4 SecureKeyId (io.cdap.cdap.proto.id.SecureKeyId)3 HttpResponse (io.cdap.common.http.HttpResponse)3 SecureStoreMetadata (io.cdap.cdap.api.security.store.SecureStoreMetadata)2 BadRequestException (io.cdap.cdap.common.BadRequestException)1 NamespaceNotFoundException (io.cdap.cdap.common.NamespaceNotFoundException)1 SecureKeyNotFoundException (io.cdap.cdap.common.SecureKeyNotFoundException)1 AuditPolicy (io.cdap.cdap.common.security.AuditPolicy)1 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)1 HttpRequest (io.cdap.common.http.HttpRequest)1 IOException (java.io.IOException)1 PUT (javax.ws.rs.PUT)1 Path (javax.ws.rs.Path)1