Search in sources :

Example 1 with SecureKeyCreateRequest

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

Example 2 with SecureKeyCreateRequest

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

Example 3 with SecureKeyCreateRequest

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

Example 4 with SecureKeyCreateRequest

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());
}
Also used : SecureKeyCreateRequest(co.cask.cdap.proto.security.SecureKeyCreateRequest) SecureKeyId(co.cask.cdap.proto.id.SecureKeyId) SecureStoreMetadata(co.cask.cdap.api.security.store.SecureStoreMetadata) Test(org.junit.Test)

Example 5 with SecureKeyCreateRequest

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));
    }
}
Also used : SecureKeyCreateRequest(co.cask.cdap.proto.security.SecureKeyCreateRequest) HashMap(java.util.HashMap) HttpResponse(org.apache.http.HttpResponse) Test(org.junit.Test)

Aggregations

SecureKeyCreateRequest (co.cask.cdap.proto.security.SecureKeyCreateRequest)6 Test (org.junit.Test)5 SecureKeyId (co.cask.cdap.proto.id.SecureKeyId)3 HttpResponse (org.apache.http.HttpResponse)3 SecureStoreMetadata (co.cask.cdap.api.security.store.SecureStoreMetadata)1 BadRequestException (co.cask.cdap.common.BadRequestException)1 NamespaceNotFoundException (co.cask.cdap.common.NamespaceNotFoundException)1 SecureKeyAlreadyExistsException (co.cask.cdap.common.SecureKeyAlreadyExistsException)1 SecureKeyNotFoundException (co.cask.cdap.common.SecureKeyNotFoundException)1 AuditPolicy (co.cask.cdap.common.security.AuditPolicy)1 NamespaceId (co.cask.cdap.proto.id.NamespaceId)1 HashMap (java.util.HashMap)1 PUT (javax.ws.rs.PUT)1 Path (javax.ws.rs.Path)1