first commit

This commit is contained in:
Azhwani 2022-06-14 23:12:55 +02:00
parent 1a1d011466
commit be8bed47d3
5 changed files with 93 additions and 6 deletions

View File

@ -15,6 +15,18 @@
<version>0.0.1-SNAPSHOT</version> <version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-boot-2</relativePath> <relativePath>../parent-boot-2</relativePath>
</parent> </parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-releasetrain</artifactId>
<version>Moore-SR1</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies> <dependencies>
<dependency> <dependency>
@ -26,6 +38,10 @@
<artifactId>spring-vault-core</artifactId> <artifactId>spring-vault-core</artifactId>
<version>${spring.vault.core.version}</version> <version>${spring.vault.core.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-keyvalue</artifactId>
</dependency>
<dependency> <dependency>
<groupId>com.fasterxml.jackson.core</groupId> <groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId> <artifactId>jackson-databind</artifactId>
@ -38,7 +54,7 @@
</dependencies> </dependencies>
<properties> <properties>
<spring.vault.core.version>2.1.1.RELEASE</spring.vault.core.version> <spring.vault.core.version>2.2.0.RELEASE</spring.vault.core.version>
</properties> </properties>
</project> </project>

View File

@ -1,7 +1,12 @@
package com.baeldung.springvault; package com.baeldung.springvault;
import org.springframework.data.annotation.Id;
import org.springframework.vault.repository.mapping.Secret;
@Secret(backend = "credentials", value = "myapp")
public class Credentials { public class Credentials {
@Id
private String username; private String username;
private String password; private String password;

View File

@ -0,0 +1,9 @@
package com.baeldung.springvault;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface CredentialsRepository extends CrudRepository<Credentials, String> {
}

View File

@ -1,6 +1,7 @@
package com.baeldung.springvault; package com.baeldung.springvault;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -18,6 +19,9 @@ public class CredentialsService {
@Autowired @Autowired
private VaultTemplate vaultTemplate; private VaultTemplate vaultTemplate;
@Autowired
private CredentialsRepository credentialsRepository;
/** /**
* To Secure Credentials * To Secure Credentials
@ -40,5 +44,15 @@ public class CredentialsService {
VaultResponseSupport<Credentials> response = vaultTemplate.read("credentials/myapp", Credentials.class); VaultResponseSupport<Credentials> response = vaultTemplate.read("credentials/myapp", Credentials.class);
return response.getData(); return response.getData();
} }
public Credentials saveCredentials(Credentials credentials) {
return credentialsRepository.save(credentials);
}
public Optional<Credentials> findById(String username) {
return credentialsRepository.findById(username);
}
} }

View File

@ -1,23 +1,26 @@
package com.baeldung.springvault; 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.FixMethodOrder;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters; import org.junit.runners.MethodSorters;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; 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;
import org.springframework.test.annotation.DirtiesContext.ClassMode; import org.springframework.test.annotation.DirtiesContext.ClassMode;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader; import org.springframework.test.context.support.AnnotationConfigContextLoader;
import java.net.URISyntaxException;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
/** /**
* These tests are requiring the {@code vault} command to be installed and available in the executing * 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 * platform. So, if you intend to run them in your environment, the please install the vault and then
@ -32,6 +35,9 @@ public class VaultIntegrationTest {
@Autowired @Autowired
private CredentialsService credentialsService; private CredentialsService credentialsService;
@MockBean
private CredentialsRepository credentialsRepository;
/** /**
* Test to secure credentials. * Test to secure credentials.
@ -71,5 +77,42 @@ public class VaultIntegrationTest {
assertEquals("username", credentials.getUsername()); assertEquals("username", credentials.getUsername());
assertEquals("password", credentials.getPassword()); 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());
}
} }