mirror of
https://github.com/DerDavidBohl/dirigent-spring.git
synced 2026-01-04 09:29:44 -06:00
Refacotred Secrets
This commit is contained in:
@@ -2,10 +2,8 @@ package org.davidbohl.dirigent.sercrets;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.ElementCollection;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
@@ -19,13 +17,13 @@ import lombok.Setter;
|
||||
@Entity
|
||||
public class Secret {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
private String key;
|
||||
|
||||
private String environmentVariable;
|
||||
|
||||
private String encryptedValue;
|
||||
|
||||
@ElementCollection
|
||||
private List<String> deployments;
|
||||
|
||||
}
|
||||
@@ -3,12 +3,17 @@ package org.davidbohl.dirigent.sercrets;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@RestController()
|
||||
@RequestMapping(path = "/api/v1/secrets")
|
||||
@Slf4j
|
||||
public class SecretController {
|
||||
|
||||
private final SecretService secretService;
|
||||
@@ -18,9 +23,9 @@ public class SecretController {
|
||||
this.secretService = secretService;
|
||||
}
|
||||
|
||||
@PutMapping
|
||||
public void saveSecret(SecretDto secret) {
|
||||
this.secretService.saveSecret(secret.environmentVariable(), secret.value(), secret.deployments());
|
||||
@PutMapping("{key}")
|
||||
public void saveSecret(@RequestBody SecretDto secret, @PathVariable String key) {
|
||||
this.secretService.saveSecret(key, secret.environmentVariable(), secret.value(), secret.deployments());
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
|
||||
@@ -2,7 +2,7 @@ package org.davidbohl.dirigent.sercrets;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public record SecretDto(String environmentVariable, String value, List<String> deployments) {
|
||||
public record SecretDto(String key, String environmentVariable, String value, List<String> deployments) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1,14 +1,12 @@
|
||||
package org.davidbohl.dirigent.sercrets;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface SecretRepository extends JpaRepository<Secret, Long> {
|
||||
public interface SecretRepository extends JpaRepository<Secret, String> {
|
||||
|
||||
Optional<Secret> findByKey(String key);
|
||||
|
||||
List<Secret> findByDeploymentsContaining(String deployment);
|
||||
List<Secret> findAllByDeploymentsContaining(String deployment);
|
||||
List<Secret> findAllByEnvironmentVariableAndDeploymentsContaining(String environmentVariable, String deployment);
|
||||
|
||||
}
|
||||
@@ -11,8 +11,11 @@ import javax.crypto.spec.SecretKeySpec;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
public class SecretService {
|
||||
|
||||
private static final String ALGORITHM = "AES";
|
||||
@@ -30,10 +33,11 @@ public class SecretService {
|
||||
this.secretRepository = secretRepository;
|
||||
}
|
||||
|
||||
public void saveSecret(String environmentVariable, String value, List<String> deployments) {
|
||||
public void saveSecret(String key, String environmentVariable, String value, List<String> deployments) {
|
||||
try {
|
||||
|
||||
String encrypted = encrypt(value);
|
||||
Secret secret = new Secret(null, environmentVariable, encrypted, deployments);
|
||||
Secret secret = new Secret(key, environmentVariable, encrypted, deployments);
|
||||
secretRepository.save(secret);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Saving Secret failed", e);
|
||||
@@ -41,11 +45,16 @@ public class SecretService {
|
||||
}
|
||||
|
||||
public Map<String, String> getAllSecretsAsEnvironmentVariableMapByDeployment(String deployment) {
|
||||
List<Secret> secrets = secretRepository.findByDeploymentsContaining(deployment);
|
||||
List<Secret> secrets = secretRepository.findAllByDeploymentsContaining(deployment);
|
||||
Map<String, String> result = new HashMap<>();
|
||||
|
||||
for (Secret secret : secrets) {
|
||||
result.put(secret.getEnvironmentVariable(), getSecret(secret.getEncryptedValue()));
|
||||
try {
|
||||
result.put(secret.getEnvironmentVariable(), decrypt(secret.getEncryptedValue()));
|
||||
} catch(Exception ex) {
|
||||
log.error("Failed to decrypt secret <" + secret.getKey() + "> for Env Var <" + secret.getEnvironmentVariable() + "> and Deployment <" + deployment + ">.");
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
@@ -53,19 +62,10 @@ public class SecretService {
|
||||
|
||||
public List<SecretDto> getAllSecretsWithoutValues() {
|
||||
return secretRepository.findAll().stream().map(
|
||||
s -> new SecretDto(s.getEnvironmentVariable(), null, s.getDeployments())
|
||||
s -> new SecretDto(s.getKey(), s.getEnvironmentVariable(), null, s.getDeployments())
|
||||
).toList();
|
||||
}
|
||||
|
||||
private String getSecret(String key) {
|
||||
try {
|
||||
Secret secret = secretRepository.findByKey(key).orElseThrow();
|
||||
return decrypt(secret.getEncryptedValue());
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Reading Secret failed", e);
|
||||
}
|
||||
}
|
||||
|
||||
private String encrypt(String value) throws Exception {
|
||||
SecretKeySpec keySpec = new SecretKeySpec(encryptionKey.getBytes(), ALGORITHM);
|
||||
Cipher cipher = Cipher.getInstance(ALGORITHM);
|
||||
|
||||
Reference in New Issue
Block a user