Skip to content

Commit

Permalink
Add Jasypt SecretKeysHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez committed Nov 4, 2022
1 parent 70b4f62 commit 0c84602
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 0 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
<module>utils/events</module>
<module>utils/cdi-provider</module>
<module>utils/crypto</module>
<module>utils/jasypt</module>
<module>testsuite</module>
<module>documentation</module>
<module>examples</module>
Expand Down
42 changes: 42 additions & 0 deletions utils/jasypt/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>smallrye-config-parent</artifactId>
<groupId>io.smallrye.config</groupId>
<version>2.12.2-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>

<artifactId>smallrye-config-jasypt</artifactId>

<name>SmallRye Config: Jasypt</name>

<dependencies>
<dependency>
<groupId>io.smallrye.config</groupId>
<artifactId>smallrye-config</artifactId>
</dependency>
<dependency>
<groupId>org.jasypt</groupId>
<artifactId>jasypt</artifactId>
<version>1.9.3</version>
</dependency>

<!-- Test -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
</dependency>
<dependency>
<groupId>io.smallrye.testing</groupId>
<artifactId>smallrye-testing-utilities</artifactId>
</dependency>
<dependency>
<groupId>jakarta.annotation</groupId>
<artifactId>jakarta.annotation-api</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.smallrye.config.jasypt;

import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.jasypt.iv.RandomIvGenerator;
import org.jasypt.properties.PropertyValueEncryptionUtils;

import io.smallrye.config.SecretKeysHandler;

public class JasyptSecretKeysHandler implements SecretKeysHandler {
@Override
public String handleSecret(final String secret) {
// TODO - We need to be able to configure this in the Handler.
// Option to configure it in the constructor or retrieve config on the fly?
StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
encryptor.setPassword("jasypt");
encryptor.setAlgorithm("PBEWithHMACSHA512AndAES_256");
encryptor.setIvGenerator(new RandomIvGenerator());
encryptor.initialize();
return PropertyValueEncryptionUtils.decrypt(secret, encryptor);
}

@Override
public String getName() {
return "jasypt";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.smallrye.config.jasypt.JasyptSecretKeysHandler
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.smallrye.config.jasypt;

import static org.junit.jupiter.api.Assertions.*;

import java.util.HashMap;
import java.util.Map;

import org.junit.jupiter.api.Test;

import io.smallrye.config.PropertiesConfigSource;
import io.smallrye.config.SmallRyeConfig;
import io.smallrye.config.SmallRyeConfigBuilder;

class JasyptSecretKeysHandlerTest {
@Test
void jasypt() {
Map<String, String> properties = new HashMap<>();
properties.put("my.secret", "${jasypt::ENC(wqp8zDeiCQ5JaFvwDtoAcr2WMLdlD0rjwvo8Rh0thG5qyTQVGxwJjBIiW26y0dtU)}");

SmallRyeConfig config = new SmallRyeConfigBuilder()
.addDefaultInterceptors()
.addDiscoveredSources()
.withSources(new PropertiesConfigSource(properties, "", 0))
.build();

assertEquals("12345678", config.getRawValue("my.secret"));
}
}

0 comments on commit 0c84602

Please sign in to comment.