Search in sources :

Example 11 with SecureStoreData

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

the class FileSecureStoreServiceTest method testDelete.

@Test(expected = NotFoundException.class)
public void testDelete() throws Exception {
    populateStore();
    SecureStoreMetadata metadata = new SecureStoreMetadata(KEY1, DESCRIPTION1, System.currentTimeMillis(), PROPERTIES_1);
    SecureStoreData secureStoreData = new SecureStoreData(metadata, VALUE1.getBytes(Charsets.UTF_8));
    Assert.assertArrayEquals(secureStoreData.get(), secureStore.get(NAMESPACE1, KEY1).get());
    secureStoreManager.delete(NAMESPACE1, KEY1);
    try {
        secureStore.get(NAMESPACE1, KEY1);
    } catch (IOException ioe) {
        Assert.assertTrue(ioe.getMessage().contains("not found in the secure store"));
        throw ioe;
    }
}
Also used : SecureStoreData(io.cdap.cdap.api.security.store.SecureStoreData) SecureStoreMetadata(io.cdap.cdap.api.security.store.SecureStoreMetadata) IOException(java.io.IOException) Test(org.junit.Test)

Example 12 with SecureStoreData

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

the class KMSSecureStoreService method get.

/**
 * Returns the data stored in the secure store. Makes two calls to the provider, one to get the metadata and another
 * to get the data.
 * @param namespace The namespace this key belongs to.
 * @param name Name of the key.
 * @return An object representing the securely stored data associated with the name.
 * @throws NamespaceNotFoundException If the specified namespace does not exist.
 * @throws IOException If there was a problem getting the key or the metadata from the underlying key provider.
 */
// Unfortunately KeyProvider does not specify the underlying cause except in the message, so we can not throw a
// more specific exception.
@Override
public SecureStoreData get(String namespace, String name) throws Exception {
    checkNamespaceExists(namespace);
    String keyName = getKeyName(namespace, name);
    KeyProvider.Metadata metadata = provider.getMetadata(keyName);
    // Provider returns null if the key is not found.
    if (metadata == null) {
        throw new NotFoundException(new SecureKeyId(namespace, name));
    }
    SecureStoreMetadata meta = new SecureStoreMetadata(name, metadata.getDescription(), metadata.getCreated().getTime(), metadata.getAttributes());
    KeyProvider.KeyVersion keyVersion = provider.getCurrentKey(keyName);
    return new SecureStoreData(meta, keyVersion.getMaterial());
}
Also used : KeyProvider(org.apache.hadoop.crypto.key.KeyProvider) SecureStoreData(io.cdap.cdap.api.security.store.SecureStoreData) SecureKeyId(io.cdap.cdap.proto.id.SecureKeyId) SecureStoreMetadata(io.cdap.cdap.api.security.store.SecureStoreMetadata) NamespaceNotFoundException(io.cdap.cdap.common.NamespaceNotFoundException) NotFoundException(io.cdap.cdap.common.NotFoundException)

Example 13 with SecureStoreData

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

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 14 with SecureStoreData

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

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 15 with SecureStoreData

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

the class FileSecureStoreService method put.

/**
 * Stores an element in the secure store. If the element already exists, it will get overwritten.
 * @param namespace The namespace this key belongs to.
 * @param name Name of the element to store.
 * @param data The data that needs to be securely stored.
 * @param description User provided description of the entry.
 * @param properties Metadata associated with the data.
 * @throws NamespaceNotFoundException If the specified namespace does not exist.
 * @throws IOException If there was a problem storing the key to the in memory keystore
 * or if there was problem persisting the keystore.
 */
@Override
public void put(String namespace, String name, String data, @Nullable String description, Map<String, String> properties) throws Exception {
    checkNamespaceExists(namespace);
    String keyName = getKeyName(namespace, name);
    SecureStoreMetadata meta = new SecureStoreMetadata(name, description, System.currentTimeMillis(), properties);
    SecureStoreData secureStoreData = new SecureStoreData(meta, data.getBytes(Charsets.UTF_8));
    writeLock.lock();
    try {
        keyStore.setKeyEntry(keyName, new SecretKeySpec(serialize(secureStoreData), "none"), password, null);
        // Attempt to persist the store.
        flush();
        LOG.debug(String.format("Successfully stored %s in namespace %s", name, namespace));
    } catch (KeyStoreException e) {
        // We failed to store the key in the key store. Throw an IOException.
        throw new IOException("Failed to store the key. ", e);
    } finally {
        writeLock.unlock();
    }
}
Also used : SecureStoreData(io.cdap.cdap.api.security.store.SecureStoreData) SecureStoreMetadata(io.cdap.cdap.api.security.store.SecureStoreMetadata) SecretKeySpec(javax.crypto.spec.SecretKeySpec) KeyStoreException(java.security.KeyStoreException) IOException(java.io.IOException)

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