#1 Retrieval of JSON objects
This commit is contained in:
parent
847cce7bfb
commit
aacbed836a
@ -2,10 +2,7 @@ package de.stklcode.jvault.connector;
|
|||||||
|
|
||||||
import de.stklcode.jvault.connector.exception.VaultConnectorException;
|
import de.stklcode.jvault.connector.exception.VaultConnectorException;
|
||||||
import de.stklcode.jvault.connector.model.AuthBackend;
|
import de.stklcode.jvault.connector.model.AuthBackend;
|
||||||
import de.stklcode.jvault.connector.model.response.SealResponse;
|
import de.stklcode.jvault.connector.model.response.*;
|
||||||
import de.stklcode.jvault.connector.model.response.SecretResponse;
|
|
||||||
import de.stklcode.jvault.connector.model.response.TokenResponse;
|
|
||||||
import de.stklcode.jvault.connector.model.response.AuthResponse;
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
@ -1,8 +1,12 @@
|
|||||||
package de.stklcode.jvault.connector.model.response;
|
package de.stklcode.jvault.connector.model.response;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||||
|
import com.fasterxml.jackson.core.JsonParseException;
|
||||||
|
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import de.stklcode.jvault.connector.exception.InvalidResponseException;
|
import de.stklcode.jvault.connector.exception.InvalidResponseException;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -27,4 +31,20 @@ public class SecretResponse extends VaultDataResponse {
|
|||||||
public String getValue() {
|
public String getValue() {
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get response parsed as JSON
|
||||||
|
* @param type Class to parse response
|
||||||
|
* @param <T> Class to parse response
|
||||||
|
* @return Parsed object
|
||||||
|
* @throws InvalidResponseException on parsing error
|
||||||
|
* @since 0.3
|
||||||
|
*/
|
||||||
|
public <T> T getValue(Class<T> type) throws InvalidResponseException {
|
||||||
|
try {
|
||||||
|
return new ObjectMapper().readValue(getValue(), type);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new InvalidResponseException("Unable to parse response payload: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,15 +1,14 @@
|
|||||||
package de.stklcode.jvault.connector;
|
package de.stklcode.jvault.connector;
|
||||||
|
|
||||||
|
import de.stklcode.jvault.connector.exception.InvalidResponseException;
|
||||||
|
import de.stklcode.jvault.connector.model.response.*;
|
||||||
|
import de.stklcode.jvault.connector.test.Credentials;
|
||||||
import de.stklcode.jvault.connector.test.VaultConfiguration;
|
import de.stklcode.jvault.connector.test.VaultConfiguration;
|
||||||
import de.stklcode.jvault.connector.exception.InvalidRequestException;
|
import de.stklcode.jvault.connector.exception.InvalidRequestException;
|
||||||
import de.stklcode.jvault.connector.exception.PermissionDeniedException;
|
import de.stklcode.jvault.connector.exception.PermissionDeniedException;
|
||||||
import de.stklcode.jvault.connector.exception.VaultConnectorException;
|
import de.stklcode.jvault.connector.exception.VaultConnectorException;
|
||||||
import de.stklcode.jvault.connector.factory.VaultConnectorFactory;
|
import de.stklcode.jvault.connector.factory.VaultConnectorFactory;
|
||||||
import de.stklcode.jvault.connector.model.AuthBackend;
|
import de.stklcode.jvault.connector.model.AuthBackend;
|
||||||
import de.stklcode.jvault.connector.model.response.AuthResponse;
|
|
||||||
import de.stklcode.jvault.connector.model.response.SealResponse;
|
|
||||||
import de.stklcode.jvault.connector.model.response.SecretResponse;
|
|
||||||
import de.stklcode.jvault.connector.model.response.TokenResponse;
|
|
||||||
import org.junit.*;
|
import org.junit.*;
|
||||||
import org.junit.rules.TemporaryFolder;
|
import org.junit.rules.TemporaryFolder;
|
||||||
|
|
||||||
@ -20,9 +19,7 @@ import java.io.IOException;
|
|||||||
import java.net.ServerSocket;
|
import java.net.ServerSocket;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static org.hamcrest.CoreMatchers.hasItem;
|
import static org.hamcrest.CoreMatchers.*;
|
||||||
import static org.hamcrest.CoreMatchers.hasItems;
|
|
||||||
import static org.hamcrest.CoreMatchers.instanceOf;
|
|
||||||
import static org.hamcrest.Matchers.greaterThan;
|
import static org.hamcrest.Matchers.greaterThan;
|
||||||
import static org.hamcrest.core.Is.is;
|
import static org.hamcrest.core.Is.is;
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
@ -45,6 +42,7 @@ public class HTTPVaultConnectorTest {
|
|||||||
private static String USER_ID = "5ADF8218-D7FB-4089-9E38-287465DBF37E";
|
private static String USER_ID = "5ADF8218-D7FB-4089-9E38-287465DBF37E";
|
||||||
private static String SECRET_PATH = "userstore";
|
private static String SECRET_PATH = "userstore";
|
||||||
private static String SECRET_KEY = "foo";
|
private static String SECRET_KEY = "foo";
|
||||||
|
private static String SECRET_KEY_JSON = "json";
|
||||||
private static String SECRET_VALUE = "bar";
|
private static String SECRET_VALUE = "bar";
|
||||||
|
|
||||||
private Process vaultProcess;
|
private Process vaultProcess;
|
||||||
@ -112,9 +110,9 @@ public class HTTPVaultConnectorTest {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void authTokenTest() {
|
public void authTokenTest() {
|
||||||
TokenResponse res = null;
|
TokenResponse res;
|
||||||
try {
|
try {
|
||||||
res = connector.authToken("52135869df23a5e64c5d33a9785af5edb456b8a4a235d1fe135e6fba1c35edf6");
|
connector.authToken("52135869df23a5e64c5d33a9785af5edb456b8a4a235d1fe135e6fba1c35edf6");
|
||||||
fail("Logged in with invalid token");
|
fail("Logged in with invalid token");
|
||||||
} catch (VaultConnectorException ignored) {
|
} catch (VaultConnectorException ignored) {
|
||||||
}
|
}
|
||||||
@ -211,6 +209,21 @@ public class HTTPVaultConnectorTest {
|
|||||||
} catch (VaultConnectorException e) {
|
} catch (VaultConnectorException e) {
|
||||||
fail("Valid secret path could not be read: " + e.getMessage());
|
fail("Valid secret path could not be read: " + e.getMessage());
|
||||||
}
|
}
|
||||||
|
/* Try to read accessible path with JSON value */
|
||||||
|
try {
|
||||||
|
res = connector.readSecret(SECRET_PATH + "/" + SECRET_KEY_JSON);
|
||||||
|
assertThat("Known secret returned null value.", res.getValue(), notNullValue());
|
||||||
|
} catch (VaultConnectorException e) {
|
||||||
|
fail("Valid secret path could not be read: " + e.getMessage());
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
Credentials parsedRes = res.getValue(Credentials.class);
|
||||||
|
assertThat("JSON response was null", parsedRes, notNullValue());
|
||||||
|
assertThat("JSON response incorrect", parsedRes.getUsername(), is("user"));
|
||||||
|
assertThat("JSON response incorrect", parsedRes.getPassword(), is("password"));
|
||||||
|
} catch (InvalidResponseException e) {
|
||||||
|
fail("JSON response could not be parsed: " + e.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -277,7 +290,7 @@ public class HTTPVaultConnectorTest {
|
|||||||
/**
|
/**
|
||||||
* Initialize Vault with resource datastore and generated configuration.
|
* Initialize Vault with resource datastore and generated configuration.
|
||||||
* @return Vault Configuration
|
* @return Vault Configuration
|
||||||
* @throws IllegalStateException
|
* @throws IllegalStateException on error
|
||||||
*/
|
*/
|
||||||
private VaultConfiguration initializeVault() throws IllegalStateException {
|
private VaultConfiguration initializeVault() throws IllegalStateException {
|
||||||
String dataResource = getClass().getResource("/data_dir").getPath();
|
String dataResource = getClass().getResource("/data_dir").getPath();
|
||||||
@ -291,10 +304,10 @@ public class HTTPVaultConnectorTest {
|
|||||||
|
|
||||||
/* Write configuration file */
|
/* Write configuration file */
|
||||||
BufferedWriter bw = null;
|
BufferedWriter bw = null;
|
||||||
File configFIle = null;
|
File configFile = null;
|
||||||
try {
|
try {
|
||||||
configFIle = tmpDir.newFile("vault.conf");
|
configFile = tmpDir.newFile("vault.conf");
|
||||||
bw = new BufferedWriter(new FileWriter(configFIle));
|
bw = new BufferedWriter(new FileWriter(configFile));
|
||||||
bw.write(config.toString());
|
bw.write(config.toString());
|
||||||
}
|
}
|
||||||
catch (IOException e) {
|
catch (IOException e) {
|
||||||
@ -313,7 +326,7 @@ public class HTTPVaultConnectorTest {
|
|||||||
|
|
||||||
/* Start vault process */
|
/* Start vault process */
|
||||||
try {
|
try {
|
||||||
vaultProcess = Runtime.getRuntime().exec("vault server -config " + configFIle.toString());
|
vaultProcess = Runtime.getRuntime().exec("vault server -config " + configFile.toString());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
throw new IllegalStateException("Unable to start vault. Make sure vault binary is in your executable path.");
|
throw new IllegalStateException("Unable to start vault. Make sure vault binary is in your executable path.");
|
||||||
|
@ -0,0 +1,26 @@
|
|||||||
|
package de.stklcode.jvault.connector.test;
|
||||||
|
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Simple credentials class for JSON testing.
|
||||||
|
*
|
||||||
|
* @author Stefan Kalscheuer
|
||||||
|
* @since 0.1
|
||||||
|
*/
|
||||||
|
public class Credentials {
|
||||||
|
@JsonProperty("username")
|
||||||
|
private String username;
|
||||||
|
|
||||||
|
@JsonProperty("password")
|
||||||
|
private String password;
|
||||||
|
|
||||||
|
public String getUsername() {
|
||||||
|
return username;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPassword() {
|
||||||
|
return password;
|
||||||
|
}
|
||||||
|
}
|
@ -1 +1 @@
|
|||||||
{"Key":"core/audit","Value":"AAAAAQJ+0lfxYOKIXzquksd3Il4zfW4ja6BdScu7mCAijGDph63S5yWH92olwI2SQA=="}
|
{"Key":"core/audit","Value":"AAAAAQI4CkDWnI49wp9iDaEMhEgTyBBtXOuIcrn7m4qifUOwQ6reTf3BKc5IQXuhxN5h51KVeinMklz3Ld0Qgw=="}
|
||||||
|
@ -1 +1 @@
|
|||||||
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3CI54AtA=="}
|
@ -0,0 +1 @@
|
|||||||
|
{"Key":"sys/policy/response-wrapping","Value":"AAAAAQLDl3zy1uKv9o2NhIyl43YAtoxGChOUc4aMa7beod+3e8FkdOsZt9BIirHsqjJ+VoxQyz+HroBaNfKPsyos3WLWvz5IUZ1UHr/jLG2SjrJfCKvco85RsFytkzp3T+Z5JB2vVfm22PpBIbjq2+XpHLKIqARqTWYl7Wnql572JZOvPY0w"}
|
Loading…
x
Reference in New Issue
Block a user