Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixing bulk requests in the transit api #195

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/main/java/com/bettercloud/vault/api/Logical.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.bettercloud.vault.json.Json;
import com.bettercloud.vault.json.JsonArray;
import com.bettercloud.vault.json.JsonObject;
import com.bettercloud.vault.json.JsonValue;
import com.bettercloud.vault.response.LogicalResponse;
import com.bettercloud.vault.rest.Rest;
import com.bettercloud.vault.rest.RestException;
Expand Down Expand Up @@ -247,6 +248,8 @@ private LogicalResponse write(final String path, final Map<String, Object> nameV
requestJson = requestJson.add(pair.getKey(), (Float) pair.getValue());
} else if (value instanceof Double) {
requestJson = requestJson.add(pair.getKey(), (Double) pair.getValue());
} else if (value instanceof JsonValue) {
requestJson = requestJson.add(pair.getKey(), (JsonValue) pair.getValue());
} else {
requestJson = requestJson.add(pair.getKey(), pair.getValue().toString());
}
Expand Down
160 changes: 160 additions & 0 deletions src/test/java/com/bettercloud/vault/vault/api/TransitApiTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package com.bettercloud.vault.vault.api;

import com.bettercloud.vault.Vault;
import com.bettercloud.vault.VaultConfig;
import com.bettercloud.vault.json.JsonArray;
import com.bettercloud.vault.json.JsonObject;
import com.bettercloud.vault.response.LogicalResponse;
import com.bettercloud.vault.vault.VaultTestUtils;
import com.bettercloud.vault.vault.mock.MockVault;
import org.eclipse.jetty.server.Server;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.Collections;
import java.util.Optional;

import static org.junit.Assert.assertEquals;

public class TransitApiTest {

private static final String[] PLAIN_DATA = {"MU45MjIyMTM=", "MU45MjIxMTM=", "MA=="};

private static final String[] CIPHER_DATA = {
"1jFhRYWHiddSKgEFyVRpX8ieX7UU+748NBw",
"1jFhRYWHiddSKgEFyVRpX8ieX7UU+748NBw",
"HKecXE3hnGBoAxrfgoD5U0yAvji7b5X6V1fP"
};

private Server server;
private MockVault vaultServer;

public void start(String response) throws Exception {
vaultServer = new MockVault(200, response);
server = VaultTestUtils.initHttpMockVault(vaultServer);
server.start();
}

@After
public void after() throws Exception {
VaultTestUtils.shutdownMockVault(server);
}

@Test
public void testTransitEncrypt() throws Exception {
final JsonObject expectedRequest = new JsonObject()
.add("plaintext", PLAIN_DATA[0]);
final JsonObject expectedResponse = new JsonObject()
.add("data", new JsonObject()
.add("ciphertext", CIPHER_DATA[0]));

start(expectedResponse.toString());

final VaultConfig vaultConfig = new VaultConfig()
.address("http://127.0.0.1:8999")
.build();
final Vault vault = new Vault(vaultConfig, 1);

LogicalResponse response = vault.logical().write("transit/encrypt/test",
Collections.singletonMap("plaintext", PLAIN_DATA[0]));

assertEquals("http://127.0.0.1:8999/v1/transit/encrypt/test", vaultServer.getRequestUrl());
assertEquals(Optional.of(expectedRequest), vaultServer.getRequestBody());
assertEquals(200, response.getRestResponse().getStatus());
}

@Test
public void testTransitDecrypt() throws Exception {
final JsonObject expectedRequest = new JsonObject()
.add("ciphertext", CIPHER_DATA[0]);
final JsonObject expectedResponse = new JsonObject()
.add("data", new JsonObject()
.add("plaintext", PLAIN_DATA[0]));

start(expectedResponse.toString());

final VaultConfig vaultConfig = new VaultConfig()
.address("http://127.0.0.1:8999")
.build();
final Vault vault = new Vault(vaultConfig, 1);

LogicalResponse response = vault.logical().write("transit/decrypt/test",
Collections.singletonMap("ciphertext", CIPHER_DATA[0]));

assertEquals("http://127.0.0.1:8999/v1/transit/decrypt/test", vaultServer.getRequestUrl());
assertEquals(Optional.of(expectedRequest), vaultServer.getRequestBody());
assertEquals(200, response.getRestResponse().getStatus());
}

@Test
public void testBulkTransitEncrypt() throws Exception {
JsonArray batchRequest = new JsonArray();
for (String text : PLAIN_DATA) {
batchRequest.add(new JsonObject().add("plaintext", text));
}
JsonArray batchResponse = new JsonArray();
for (String text : CIPHER_DATA) {
batchResponse.add(new JsonObject().add("ciphertext", text));
}
final JsonObject expectedRequest = new JsonObject()
.add("batch_input", batchRequest);
final JsonObject expectedResponse = new JsonObject()
.add("data", new JsonObject()
.add("batch_results", batchResponse));

start(expectedResponse.toString());

final VaultConfig vaultConfig = new VaultConfig()
.address("http://127.0.0.1:8999")
.build();
final Vault vault = new Vault(vaultConfig, 1);

JsonArray batch = new JsonArray();
for (String text : PLAIN_DATA) {
batch.add(new JsonObject().add("plaintext", text));
}
LogicalResponse response = vault.logical().write("transit/encrypt/test",
Collections.singletonMap("batch_input", batch));

assertEquals(Optional.of(expectedRequest), vaultServer.getRequestBody());
assertEquals("http://127.0.0.1:8999/v1/transit/encrypt/test", vaultServer.getRequestUrl());
assertEquals(200, response.getRestResponse().getStatus());
}

@Test
public void testBulkTransitDecrypt() throws Exception {
JsonArray batchRequest = new JsonArray();
for (String text : CIPHER_DATA) {
batchRequest.add(new JsonObject().add("ciphertext", text));
}
JsonArray batchResponse = new JsonArray();
for (String text : PLAIN_DATA) {
batchResponse.add(new JsonObject().add("plaintext", text));
}
final JsonObject expectedRequest = new JsonObject()
.add("batch_input", batchRequest);
final JsonObject expectedResponse = new JsonObject()
.add("data", new JsonObject()
.add("batch_results", batchResponse));

start(expectedResponse.toString());

final VaultConfig vaultConfig = new VaultConfig()
.address("http://127.0.0.1:8999")
.build();
final Vault vault = new Vault(vaultConfig, 1);

JsonArray batch = new JsonArray();
for (String text : CIPHER_DATA) {
batch.add(new JsonObject().add("ciphertext", text));
}
LogicalResponse response = vault.logical().write("transit/decrypt/test",
Collections.singletonMap("batch_input", batch));

assertEquals(Optional.of(expectedRequest), vaultServer.getRequestBody());
assertEquals("http://127.0.0.1:8999/v1/transit/decrypt/test", vaultServer.getRequestUrl());
assertEquals(200, response.getRestResponse().getStatus());
}

}