From: Michael Rasmussen Date: Tue, 11 Nov 2025 02:39:37 +0000 (+0100) Subject: Initial push X-Git-Url: http://git.datanom.net/vault.git/commitdiff_plain/aa08c20b765a36e4483c4e02440aa179eec2e7ce?ds=inline Initial push --- aa08c20b765a36e4483c4e02440aa179eec2e7ce diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..667aaef --- /dev/null +++ b/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..e7276e0 --- /dev/null +++ b/pom.xml @@ -0,0 +1,97 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 3.5.7 + + + dk.cbs.soa.soasi.secret + vault + 0.0.1-SNAPSHOT + vault + Secret vault to exchange secrets with external partners + + + + + + + + + + + + + + + 21 + + + + org.springframework.boot + spring-boot-starter-thymeleaf + + + org.springframework.boot + spring-boot-starter-validation + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.session + spring-session-jdbc + + + + org.postgresql + postgresql + runtime + + + + org.projectlombok + lombok + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + + + org.projectlombok + lombok + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + org.projectlombok + lombok + + + + + + + + diff --git a/src/main/java/dk/cbs/soa/soasi/secret/vault/VaultApplication.java b/src/main/java/dk/cbs/soa/soasi/secret/vault/VaultApplication.java new file mode 100644 index 0000000..892d02c --- /dev/null +++ b/src/main/java/dk/cbs/soa/soasi/secret/vault/VaultApplication.java @@ -0,0 +1,13 @@ +package dk.cbs.soa.soasi.secret.vault; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class VaultApplication { + + public static void main(String[] args) { + SpringApplication.run(VaultApplication.class, args); + } + +} diff --git a/src/main/java/dk/cbs/soa/soasi/secret/vault/configuration/JdbcConfig.java b/src/main/java/dk/cbs/soa/soasi/secret/vault/configuration/JdbcConfig.java new file mode 100644 index 0000000..6d37606 --- /dev/null +++ b/src/main/java/dk/cbs/soa/soasi/secret/vault/configuration/JdbcConfig.java @@ -0,0 +1,30 @@ +package dk.cbs.soa.soasi.secret.vault.configuration; + +import javax.sql.DataSource; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.jdbc.datasource.DriverManagerDataSource; + +@Configuration +public class JdbcConfig { + + @Value("${vault.datasource.url}") + private String url; + @Value("${vault.datasource.username}") + private String username; + @Value("${vault.datasource.password}") + private String password; + + @Bean + public DataSource postgresqlDataSource() { + DriverManagerDataSource dataSource = new DriverManagerDataSource(); + dataSource.setDriverClassName("org.postgresql.Driver"); + dataSource.setUrl(url); + dataSource.setUsername(username); + dataSource.setPassword(password); + + return dataSource; + } +} diff --git a/src/main/java/dk/cbs/soa/soasi/secret/vault/controller/ProjectController.java b/src/main/java/dk/cbs/soa/soasi/secret/vault/controller/ProjectController.java new file mode 100644 index 0000000..ac4fa90 --- /dev/null +++ b/src/main/java/dk/cbs/soa/soasi/secret/vault/controller/ProjectController.java @@ -0,0 +1,88 @@ +package dk.cbs.soa.soasi.secret.vault.controller; + +import java.util.Optional; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.validation.BindingResult; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; + +import dk.cbs.soa.soasi.secret.vault.model.Project; +import dk.cbs.soa.soasi.secret.vault.repository.ProjectRepository; +import jakarta.validation.Valid; +import lombok.extern.slf4j.Slf4j; + +@Controller +@Slf4j +public class ProjectController { + + @Autowired + private ProjectRepository projectRepository; + + @GetMapping("/info") + public String showRegisterProject(Project project) { + return "create-project"; + } + + @PostMapping("/showinfo") + public String showInfo(@Valid Project project, BindingResult result, Model model) { + log.info(project == null ? "null" : project.toString()); + + if (result.hasErrors()) { + return "create-project"; + } + + Optional newProject = projectRepository.add(project); + if (newProject.isEmpty()) + throw new IllegalArgumentException("Project is empty"); + + log.info(project.toString()); + model.addAttribute("project", newProject.get()); + + + return "project-info"; + } + + @GetMapping("/project") + public String showCreateProject(Project project) { + return "add-project"; + } + + @PostMapping("/addproject") + public String createProject(@Valid Project project, BindingResult result, Model model) { + log.info(project == null ? "null" : project.toString()); + + if (result.hasErrors()) { + return "add-project"; + } + Optional p = projectRepository.get(project.getToken()); + if (p.isEmpty()) { + throw new IllegalArgumentException("Invalid token" + project.getToken()); + } + + model.addAttribute("project", p.get()); + + return "create-info"; + } + + @GetMapping("/") + public String getSecret(Project project) { + return "get-secret"; + } + + @PostMapping("/secret") + public String receiveSecret(@Valid Project project, BindingResult result, Model model) { + log.info(project == null ? "null" : project.toString()); + + if (result.hasErrors()) { + return "get-secret"; + } + + projectRepository.update(project); + + return "redirect:/goodbye.html"; + } + +} diff --git a/src/main/java/dk/cbs/soa/soasi/secret/vault/model/Project.java b/src/main/java/dk/cbs/soa/soasi/secret/vault/model/Project.java new file mode 100644 index 0000000..76b53a7 --- /dev/null +++ b/src/main/java/dk/cbs/soa/soasi/secret/vault/model/Project.java @@ -0,0 +1,14 @@ +package dk.cbs.soa.soasi.secret.vault.model; + +import lombok.Builder; +import lombok.Data; +import lombok.ToString; + +@Data +@Builder +@ToString +public class Project { + private String id; + private String token; + private String secret; +} diff --git a/src/main/java/dk/cbs/soa/soasi/secret/vault/repository/ProjectRepository.java b/src/main/java/dk/cbs/soa/soasi/secret/vault/repository/ProjectRepository.java new file mode 100644 index 0000000..64d41bb --- /dev/null +++ b/src/main/java/dk/cbs/soa/soasi/secret/vault/repository/ProjectRepository.java @@ -0,0 +1,94 @@ +package dk.cbs.soa.soasi.secret.vault.repository; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Optional; +import java.util.UUID; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.RowMapper; +import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; +import org.springframework.stereotype.Repository; + +import dk.cbs.soa.soasi.secret.vault.model.Project; +import jakarta.validation.Valid; +import lombok.extern.slf4j.Slf4j; + +@Repository +@Slf4j +public class ProjectRepository { + + @Autowired + private NamedParameterJdbcTemplate jdbcNamedTemplate; + + @Autowired + private JdbcTemplate jdbcTemplate; + + // Get project by token and remove project + public Optional get(String token) { + String sqlGet = "select id,token,secret from projects.project where token = :token"; + String sqlDelete = "delete from projects.project where token = ?"; + + MapSqlParameterSource namedParameters = new MapSqlParameterSource(); + namedParameters.addValue("token", token); + + Project p = jdbcNamedTemplate.queryForObject(sqlGet, namedParameters, new RowMapper() { + + @Override + public Project mapRow(ResultSet rs, int rowNum) throws SQLException { + return Project.builder() + .id(rs.getString("id")) + .secret(rs.getString("secret")) + .token(rs.getString("token")) + .build(); + } + + }); + + log.info("Add: " + p.toString()); + + if (p != null) { + jdbcTemplate.update(sqlDelete, token); + } + + return Optional.ofNullable(p); + } + + // Add project and return project with created token + public Optional add(@Valid Project project) { + String sqlInsert = "insert into projects.project (id, token) values (:id, :token)"; + + log.info("Create: " + project.toString()); + + UUID token = getRandomUuid(); + project.setToken(token.toString()); + + MapSqlParameterSource namedParameters = new MapSqlParameterSource(); + namedParameters.addValue("token", project.getToken()); + namedParameters.addValue("id", project.getId()); + + jdbcNamedTemplate.update(sqlInsert, namedParameters); + + return Optional.ofNullable(project); + } + + // Update project with received secret + public void update(@Valid Project project) { + // Update project with secret + log.info("Update: " + project.toString()); + + String sqlUpdate = "update projects.project set secret = :secret where token = :token"; + + MapSqlParameterSource namedParameters = new MapSqlParameterSource(); + namedParameters.addValue("token", project.getToken()); + namedParameters.addValue("secret", project.getSecret()); + + jdbcNamedTemplate.update(sqlUpdate, namedParameters); + } + + private UUID getRandomUuid() { + return UUID.randomUUID(); + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties new file mode 100644 index 0000000..9edb2da --- /dev/null +++ b/src/main/resources/application.properties @@ -0,0 +1,9 @@ +spring.application.name=vault + +logging.level.org.springframework.web=DEBUG + +vault.datasource.url=jdbc:postgresql://localhost:5432/vault +vault.datasource.username=vault +vault.datasource.password=vault + +spring.session.jdbc.initialize-schema=always diff --git a/src/main/resources/database/createdb.sql b/src/main/resources/database/createdb.sql new file mode 100644 index 0000000..e18df57 --- /dev/null +++ b/src/main/resources/database/createdb.sql @@ -0,0 +1,10 @@ +create user vault with password 'vault'; +create database vault with owner vault; +create schema projects AUTHORIZATION vault; + +create table projects.project ( +id varchar UNIQUE, +token varchar UNIQUE, +secret varchar +); + diff --git a/src/main/resources/static/goodbye.html b/src/main/resources/static/goodbye.html new file mode 100644 index 0000000..b5efa60 --- /dev/null +++ b/src/main/resources/static/goodbye.html @@ -0,0 +1,10 @@ + + + + +GoodBye + + +

GoodBye

+ + \ No newline at end of file diff --git a/src/main/resources/templates/add-project.html b/src/main/resources/templates/add-project.html new file mode 100644 index 0000000..60b8313 --- /dev/null +++ b/src/main/resources/templates/add-project.html @@ -0,0 +1,7 @@ +
+ + + +

+ +
\ No newline at end of file diff --git a/src/main/resources/templates/create-info.html b/src/main/resources/templates/create-info.html new file mode 100644 index 0000000..3185749 --- /dev/null +++ b/src/main/resources/templates/create-info.html @@ -0,0 +1,25 @@ + + + + +Project Info + + + + + + + + + + + + + + +
IdTokenSecret
+ + +
+ + diff --git a/src/main/resources/templates/create-project.html b/src/main/resources/templates/create-project.html new file mode 100644 index 0000000..d00b6b2 --- /dev/null +++ b/src/main/resources/templates/create-project.html @@ -0,0 +1,7 @@ +
+ + + +

+ +
\ No newline at end of file diff --git a/src/main/resources/templates/get-secret.html b/src/main/resources/templates/get-secret.html new file mode 100644 index 0000000..c0c2fc0 --- /dev/null +++ b/src/main/resources/templates/get-secret.html @@ -0,0 +1,11 @@ +
+ + + +
+ + + +

+ +
\ No newline at end of file diff --git a/src/main/resources/templates/project-info.html b/src/main/resources/templates/project-info.html new file mode 100644 index 0000000..85d0850 --- /dev/null +++ b/src/main/resources/templates/project-info.html @@ -0,0 +1,23 @@ + + + + +Project Info + + + + + + + + + + + + + +
IdToken
+ +
+ + diff --git a/src/test/java/dk/cbs/soa/soasi/secret/vault/VaultApplicationTests.java b/src/test/java/dk/cbs/soa/soasi/secret/vault/VaultApplicationTests.java new file mode 100644 index 0000000..572611a --- /dev/null +++ b/src/test/java/dk/cbs/soa/soasi/secret/vault/VaultApplicationTests.java @@ -0,0 +1,13 @@ +package dk.cbs.soa.soasi.secret.vault; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class VaultApplicationTests { + + @Test + void contextLoads() { + } + +}