Search in sources :

Example 6 with SecureStoreData

use of io.cdap.cdap.api.security.store.SecureStoreData in project cdap by cdapio.

the class FileSecureStoreService method deserialize.

private SecureStoreData deserialize(byte[] data) throws IOException {
    DataInputStream dis = new DataInputStream(new ByteArrayInputStream(data));
    String name = dis.readUTF();
    boolean descriptionExists = dis.readBoolean();
    String description = descriptionExists ? dis.readUTF() : null;
    long lastModified = dis.readLong();
    Map<String, String> properties = new HashMap<>();
    int len = dis.readInt();
    for (int i = 0; i < len; i++) {
        properties.put(dis.readUTF(), dis.readUTF());
    }
    SecureStoreMetadata meta = new SecureStoreMetadata(name, description, lastModified, properties);
    byte[] secret = new byte[dis.readInt()];
    dis.readFully(secret);
    return new SecureStoreData(meta, secret);
}
Also used : SecureStoreData(io.cdap.cdap.api.security.store.SecureStoreData) ByteArrayInputStream(java.io.ByteArrayInputStream) HashMap(java.util.HashMap) SecureStoreMetadata(io.cdap.cdap.api.security.store.SecureStoreMetadata) DataInputStream(java.io.DataInputStream)

Example 7 with SecureStoreData

use of io.cdap.cdap.api.security.store.SecureStoreData in project cdap by cdapio.

the class SecureStoreHandler method getMetadata.

@Path("/{key-name}/metadata")
@GET
public void getMetadata(HttpRequest httpRequest, HttpResponder httpResponder, @PathParam("namespace-id") String namespace, @PathParam("key-name") String name) throws Exception {
    SecureStoreData secureStoreData = secureStore.get(namespace, name);
    httpResponder.sendJson(HttpResponseStatus.OK, GSON.toJson(secureStoreData.getMetadata()));
}
Also used : SecureStoreData(io.cdap.cdap.api.security.store.SecureStoreData) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 8 with SecureStoreData

use of io.cdap.cdap.api.security.store.SecureStoreData in project cdap by cdapio.

the class RemoteSecureStore method get.

@Override
public SecureStoreData get(String namespace, String name) throws Exception {
    // 1. Get metadata of the secure key
    HttpRequest request = remoteClient.requestBuilder(HttpMethod.GET, createPath(namespace, name) + "/metadata").build();
    HttpResponse response = remoteClient.execute(request);
    handleResponse(response, namespace, name, String.format("Error occurred while getting metadata for key %s:%s", namespace, name));
    SecureStoreMetadata metadata = GSON.fromJson(response.getResponseBodyAsString(), SecureStoreMetadata.class);
    // 2. Get sensitive data for the secure key
    request = remoteClient.requestBuilder(HttpMethod.GET, createPath(namespace, name)).build();
    response = remoteClient.execute(request);
    handleResponse(response, namespace, name, String.format("Error occurred while getting key %s:%s", namespace, name));
    // response is not a json object
    byte[] data = response.getResponseBody();
    return new SecureStoreData(metadata, data);
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) SecureStoreData(io.cdap.cdap.api.security.store.SecureStoreData) SecureStoreMetadata(io.cdap.cdap.api.security.store.SecureStoreMetadata) HttpResponse(io.cdap.common.http.HttpResponse)

Example 9 with SecureStoreData

use of io.cdap.cdap.api.security.store.SecureStoreData in project cdap by cdapio.

the class SecretManagerSecureStoreService method get.

@Override
public SecureStoreData get(String namespace, String name) throws Exception {
    validate(namespace);
    try {
        Secret secret = secretManager.get(namespace, name);
        SecretMetadata metadata = secret.getMetadata();
        return new SecureStoreData(new SecureStoreMetadata(metadata.getName(), metadata.getDescription(), metadata.getCreationTimeMs(), metadata.getProperties()), secret.getData());
    } catch (SecretNotFoundException e) {
        throw new SecureKeyNotFoundException(new SecureKeyId(namespace, name), e);
    }
}
Also used : Secret(io.cdap.cdap.securestore.spi.secret.Secret) SecureStoreData(io.cdap.cdap.api.security.store.SecureStoreData) SecureKeyId(io.cdap.cdap.proto.id.SecureKeyId) SecureStoreMetadata(io.cdap.cdap.api.security.store.SecureStoreMetadata) SecretMetadata(io.cdap.cdap.securestore.spi.secret.SecretMetadata) SecureKeyNotFoundException(io.cdap.cdap.common.SecureKeyNotFoundException) SecretNotFoundException(io.cdap.cdap.securestore.spi.SecretNotFoundException)

Example 10 with SecureStoreData

use of io.cdap.cdap.api.security.store.SecureStoreData in project cdap by cdapio.

the class RemoteSecureStoreTest method testRemoteSecureStore.

@Test
public void testRemoteSecureStore() throws Exception {
    SecureStoreMetadata secureStoreMetadata = new SecureStoreMetadata("key", "description", 1, ImmutableMap.of("prop1", "value1"));
    SecureStoreData secureStoreData = new SecureStoreData(secureStoreMetadata, "value".getBytes(StandardCharsets.UTF_8));
    // test put and get
    remoteSecureStore.put(NAMESPACE1, "key", "value", "description", ImmutableMap.of("prop1", "value1"));
    SecureStoreData actual = remoteSecureStore.get(NAMESPACE1, "key");
    Assert.assertEquals(secureStoreMetadata.getName(), actual.getMetadata().getName());
    Assert.assertArrayEquals(secureStoreData.get(), actual.get());
    Assert.assertEquals(secureStoreMetadata.getDescription(), actual.getMetadata().getDescription());
    Assert.assertEquals(secureStoreMetadata.getProperties().size(), actual.getMetadata().getProperties().size());
    // test list
    List<SecureStoreMetadata> secureData = remoteSecureStore.list(NAMESPACE1);
    Assert.assertEquals(1, secureData.size());
    SecureStoreMetadata metadata = secureData.get(0);
    Assert.assertEquals("key", metadata.getName());
    Assert.assertEquals("description", metadata.getDescription());
    // test delete
    remoteSecureStore.delete(NAMESPACE1, "key");
    Assert.assertEquals(0, remoteSecureStore.list(NAMESPACE1).size());
}
Also used : SecureStoreData(io.cdap.cdap.api.security.store.SecureStoreData) SecureStoreMetadata(io.cdap.cdap.api.security.store.SecureStoreMetadata) Test(org.junit.Test)

Aggregations

SecureStoreData (io.cdap.cdap.api.security.store.SecureStoreData)24 SecureStoreMetadata (io.cdap.cdap.api.security.store.SecureStoreMetadata)18 Test (org.junit.Test)10 SecureKeyId (io.cdap.cdap.proto.id.SecureKeyId)4 IOException (java.io.IOException)4 HashMap (java.util.HashMap)4 NamespaceNotFoundException (io.cdap.cdap.common.NamespaceNotFoundException)2 NotFoundException (io.cdap.cdap.common.NotFoundException)2 SecureKeyNotFoundException (io.cdap.cdap.common.SecureKeyNotFoundException)2 SecretNotFoundException (io.cdap.cdap.securestore.spi.SecretNotFoundException)2 Secret (io.cdap.cdap.securestore.spi.secret.Secret)2 SecretMetadata (io.cdap.cdap.securestore.spi.secret.SecretMetadata)2 HttpRequest (io.cdap.common.http.HttpRequest)2 HttpResponse (io.cdap.common.http.HttpResponse)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 DataInputStream (java.io.DataInputStream)2 KeyStoreException (java.security.KeyStoreException)2 SecretKeySpec (javax.crypto.spec.SecretKeySpec)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2