BAEL-4539: Fix failing tests for Vault (#13636)
Co-authored-by: Tapan Avasthi <tavasthi@Tapans-MacBook-Air.local>
This commit is contained in:
parent
ef3e667606
commit
fe2f6c8bbb
@ -5,54 +5,54 @@ import java.util.Optional;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.vault.core.VaultKeyValueOperations;
|
||||
import org.springframework.vault.core.VaultKeyValueOperationsSupport;
|
||||
import org.springframework.vault.core.VaultTemplate;
|
||||
import org.springframework.vault.support.VaultResponseSupport;
|
||||
|
||||
/**
|
||||
* Sample service to demonstrate storing and retrieval of secrets.
|
||||
*
|
||||
*
|
||||
* NOTE: We need to configure Vault and provide the Vault uri in the properties file.
|
||||
*
|
||||
*/
|
||||
@Service
|
||||
public class CredentialsService {
|
||||
private final VaultTemplate vaultTemplate;
|
||||
private final VaultKeyValueOperations vaultKeyValueOperations;
|
||||
private final CredentialsRepository credentialsRepository;
|
||||
|
||||
@Autowired
|
||||
private VaultTemplate vaultTemplate;
|
||||
|
||||
@Autowired
|
||||
private CredentialsRepository credentialsRepository;
|
||||
public CredentialsService(VaultTemplate vaultTemplate, CredentialsRepository credentialsRepository) {
|
||||
this.vaultTemplate = vaultTemplate;
|
||||
this.credentialsRepository = credentialsRepository;
|
||||
this.vaultKeyValueOperations = vaultTemplate.opsForKeyValue("credentials/myapp", VaultKeyValueOperationsSupport.KeyValueBackend.KV_2);
|
||||
}
|
||||
|
||||
/**
|
||||
* To Secure Credentials
|
||||
* @param credentials
|
||||
* @return VaultResponse
|
||||
* @throws URISyntaxException
|
||||
*/
|
||||
public void secureCredentials(Credentials credentials) throws URISyntaxException {
|
||||
|
||||
vaultTemplate.write("credentials/myapp", credentials);
|
||||
* To Secure Credentials
|
||||
* @param credentials
|
||||
* @return VaultResponse
|
||||
* @throws URISyntaxException
|
||||
*/
|
||||
public void secureCredentials(Credentials credentials) {
|
||||
vaultKeyValueOperations.put(credentials.getUsername(), credentials);
|
||||
}
|
||||
|
||||
/**
|
||||
* To Retrieve Credentials
|
||||
* @return Credentials
|
||||
* @throws URISyntaxException
|
||||
*/
|
||||
public Credentials accessCredentials() throws URISyntaxException {
|
||||
|
||||
VaultResponseSupport<Credentials> response = vaultTemplate.read("credentials/myapp", Credentials.class);
|
||||
public Credentials accessCredentials(String username) {
|
||||
VaultResponseSupport<Credentials> response = vaultKeyValueOperations.get(username, Credentials.class);
|
||||
return response.getData();
|
||||
}
|
||||
|
||||
public Credentials saveCredentials(Credentials credentials) {
|
||||
|
||||
return credentialsRepository.save(credentials);
|
||||
}
|
||||
|
||||
public Optional<Credentials> findById(String username) {
|
||||
|
||||
return credentialsRepository.findById(username);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.vault.repository.configuration.EnableVaultRepositories;
|
||||
|
||||
/**
|
||||
* This live test requires:
|
||||
@ -17,6 +18,7 @@ import org.springframework.test.context.junit4.SpringRunner;
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = SpringVaultApplication.class)
|
||||
@EnableVaultRepositories
|
||||
public class SpringContextLiveTest {
|
||||
|
||||
@Test
|
||||
|
@ -7,11 +7,12 @@ import java.io.InputStreamReader;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* This is a test class to initialize Vault.
|
||||
*/
|
||||
public class VaultInitializer implements Closeable {
|
||||
|
||||
public static final String API_VERSION = "v1";
|
||||
private static final String UNSEAL_KEY = "Unseal Key:";
|
||||
private static final String ROOT_TOKEN = "Root Token:";
|
||||
|
||||
@ -27,7 +28,7 @@ public class VaultInitializer implements Closeable {
|
||||
return unSealKey;
|
||||
}
|
||||
|
||||
public static final VaultInitializer initializeValut() {
|
||||
public static final VaultInitializer initializeVault() {
|
||||
VaultInitializer vaultProcess = new VaultInitializer();
|
||||
vaultProcess.start();
|
||||
// Secrets is by default enabled.
|
||||
@ -37,8 +38,9 @@ public class VaultInitializer implements Closeable {
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private void enableSecrets() {
|
||||
System.out.println("Enabling Secrets at path credentials/myapp...");
|
||||
ProcessBuilder pb = new ProcessBuilder("vault", "secrets", "enable", "-path=credentials/myapp", "kv");
|
||||
System.out.println("Enabling Secrets at path secret/...");
|
||||
ProcessBuilder pb = new ProcessBuilder("vault", "secrets", "enable", "-path=credentials/myapp/", String.format("kv-%s", API_VERSION)); ;
|
||||
|
||||
Map<String, String> map = pb.environment();
|
||||
map.put("VAULT_ADDR", "http://127.0.0.1:8200");
|
||||
try {
|
||||
@ -106,8 +108,7 @@ public class VaultInitializer implements Closeable {
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
|
||||
System.out.println("stoping vault");
|
||||
System.out.println("stopping vault");
|
||||
vaultProcess.destroy();
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,103 @@
|
||||
package com.baeldung.springvault;
|
||||
|
||||
import static com.baeldung.springvault.VaultInitializer.API_VERSION;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Assume;
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.annotation.DirtiesContext.ClassMode;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
import org.springframework.vault.core.VaultTemplate;
|
||||
import org.springframework.vault.repository.configuration.EnableVaultRepositories;
|
||||
|
||||
/**
|
||||
* These tests are requiring the {@code vault} command to be installed and available in the executing
|
||||
* platform. So, if you intend to run them in your environment, the please install the vault and then
|
||||
* run the ignored tests.
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringBootTest(classes = CredentialsService.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
@ContextConfiguration(classes = VaultTestConfiguration.class, loader = AnnotationConfigContextLoader.class)
|
||||
@DirtiesContext(classMode = ClassMode.AFTER_CLASS)
|
||||
@EnableVaultRepositories
|
||||
public class VaultIntegrationManualTest {
|
||||
@Autowired
|
||||
private CredentialsRepository credentialsRepository;
|
||||
|
||||
@Autowired
|
||||
private VaultTemplate vaultTemplate;
|
||||
|
||||
private CredentialsService credentialsService;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
this.credentialsService = new CredentialsService(vaultTemplate, credentialsRepository);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to secure credentials.
|
||||
*
|
||||
* @throws URISyntaxException
|
||||
*/
|
||||
@Test
|
||||
public void givenCredentials_whenSecureCredentials_thenCredentialsSecuredSuccessfully() throws URISyntaxException {
|
||||
// Given
|
||||
Credentials credentials = new Credentials("username", "password");
|
||||
// When
|
||||
credentialsService.secureCredentials(credentials);
|
||||
// Then
|
||||
Credentials storedCredentials = credentialsService.accessCredentials(credentials.getUsername());
|
||||
Assertions.assertNotNull(storedCredentials);
|
||||
Assertions.assertEquals(credentials.getUsername(), storedCredentials.getUsername());
|
||||
Assertions.assertEquals(credentials.getPassword(), storedCredentials.getPassword());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCredentials_whenSave_thenReturnCredentials() throws InterruptedException {
|
||||
Assume.assumeTrue("v1".equals(API_VERSION));
|
||||
|
||||
credentialsService = new CredentialsService(vaultTemplate, credentialsRepository);
|
||||
// Given
|
||||
Credentials credentials = new Credentials("login", "password");
|
||||
|
||||
// When
|
||||
Credentials savedCredentials = credentialsService.saveCredentials(credentials);
|
||||
|
||||
// Then
|
||||
assertNotNull(savedCredentials);
|
||||
assertEquals(credentials.getUsername(), savedCredentials.getUsername());
|
||||
assertEquals(credentials.getPassword(), savedCredentials.getPassword());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenId_whenFindById_thenReturnCredentials() {
|
||||
// Given
|
||||
Assume.assumeTrue("v1".equals(API_VERSION));
|
||||
Credentials expectedCredentials = new Credentials("login", "p@ssw@rd");
|
||||
credentialsService.saveCredentials(expectedCredentials);
|
||||
|
||||
// When
|
||||
Optional<Credentials> retrievedCredentials = credentialsService.findById(expectedCredentials.getUsername());
|
||||
|
||||
// Then
|
||||
assertNotNull(retrievedCredentials);
|
||||
assertNotNull(retrievedCredentials.get());
|
||||
assertEquals(expectedCredentials.getUsername(), retrievedCredentials.get()
|
||||
.getUsername());
|
||||
assertEquals(expectedCredentials.getPassword(), retrievedCredentials.get()
|
||||
.getPassword());
|
||||
}
|
||||
|
||||
}
|
@ -1,118 +0,0 @@
|
||||
package com.baeldung.springvault;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.junit.FixMethodOrder;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.MethodSorters;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.annotation.DirtiesContext.ClassMode;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
/**
|
||||
* These tests are requiring the {@code vault} command to be installed and available in the executing
|
||||
* platform. So, if you intend to run them in your environment, the please install the vault and then
|
||||
* run the ignored tests.
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = CredentialsService.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
@ContextConfiguration(classes = VaultTestConfiguration.class, loader = AnnotationConfigContextLoader.class)
|
||||
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
|
||||
@DirtiesContext(classMode = ClassMode.AFTER_CLASS)
|
||||
public class VaultIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private CredentialsService credentialsService;
|
||||
|
||||
@MockBean
|
||||
private CredentialsRepository credentialsRepository;
|
||||
|
||||
/**
|
||||
* Test to secure credentials.
|
||||
*
|
||||
* @throws URISyntaxException
|
||||
*/
|
||||
@Test
|
||||
@Ignore
|
||||
public void givenCredentials_whenSecureCredentials_thenCredentialsSecured() throws URISyntaxException {
|
||||
try {
|
||||
// Given
|
||||
Credentials credentials = new Credentials("username", "password");
|
||||
|
||||
// When
|
||||
credentialsService.secureCredentials(credentials);
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to access credentials
|
||||
*
|
||||
* @throws URISyntaxException
|
||||
*/
|
||||
@Test
|
||||
@Ignore
|
||||
public void whenAccessCredentials_thenCredentialsRetrieved() throws URISyntaxException {
|
||||
|
||||
// Given
|
||||
Credentials credentials = credentialsService.accessCredentials();
|
||||
|
||||
// Then
|
||||
assertNotNull(credentials);
|
||||
assertEquals("username", credentials.getUsername());
|
||||
assertEquals("password", credentials.getPassword());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void givenCredentials_whenSave_thenReturnCredentials() {
|
||||
// Given
|
||||
Credentials credentials = new Credentials("login", "password");
|
||||
Mockito.when(credentialsRepository.save(credentials))
|
||||
.thenReturn(credentials);
|
||||
|
||||
// When
|
||||
Credentials savedCredentials = credentialsService.saveCredentials(credentials);
|
||||
|
||||
// Then
|
||||
assertNotNull(savedCredentials);
|
||||
assertEquals(savedCredentials.getUsername(), credentials.getUsername());
|
||||
assertEquals(savedCredentials.getPassword(), credentials.getPassword());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void givenId_whenFindById_thenReturnCredentials() {
|
||||
// Given
|
||||
Credentials credentials = new Credentials("login", "p@ssw@rd");
|
||||
Mockito.when(credentialsRepository.findById("login"))
|
||||
.thenReturn(Optional.of(credentials));
|
||||
|
||||
// When
|
||||
Optional<Credentials> returnedCredentials = credentialsService.findById("login");
|
||||
|
||||
// Then
|
||||
assertNotNull(returnedCredentials);
|
||||
assertNotNull(returnedCredentials.get());
|
||||
assertEquals(returnedCredentials.get()
|
||||
.getUsername(), credentials.getUsername());
|
||||
assertEquals(returnedCredentials.get()
|
||||
.getPassword(), credentials.getPassword());
|
||||
}
|
||||
|
||||
}
|
@ -5,6 +5,7 @@ import java.net.URISyntaxException;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.vault.annotation.VaultPropertySource;
|
||||
import org.springframework.vault.authentication.TokenAuthentication;
|
||||
import org.springframework.vault.client.VaultEndpoint;
|
||||
import org.springframework.vault.core.VaultTemplate;
|
||||
@ -14,7 +15,7 @@ public class VaultTestConfiguration {
|
||||
|
||||
@Bean
|
||||
public VaultInitializer vaultInitializer() {
|
||||
VaultInitializer vaultInitializer = VaultInitializer.initializeValut();
|
||||
VaultInitializer vaultInitializer = VaultInitializer.initializeVault();
|
||||
return vaultInitializer;
|
||||
}
|
||||
|
||||
@ -24,6 +25,5 @@ public class VaultTestConfiguration {
|
||||
VaultInitializer vaultInitializer = vaultInitializer();
|
||||
VaultTemplate vaultTemplate = new VaultTemplate(VaultEndpoint.from(new URI("http://localhost:8200")), new TokenAuthentication(vaultInitializer.getRootToken()));
|
||||
return vaultTemplate;
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user