-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from certifaction/encryption-key-validation
Encryption key validation
- Loading branch information
Showing
5 changed files
with
205 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
name: Build and Release JAR | ||
|
||
on: | ||
push: | ||
tags: | ||
- 'v*' | ||
|
||
env: | ||
KEYCLOAK_VERSION: 25.0.6 | ||
|
||
jobs: | ||
build-and-release: | ||
runs-on: ubuntu-24.04 | ||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v4 | ||
|
||
- name: Set up JDK 17 | ||
uses: actions/setup-java@v3 | ||
with: | ||
java-version: '17' | ||
distribution: 'temurin' | ||
|
||
- name: Install Git | ||
run: sudo apt-get update && sudo apt-get install -y git | ||
|
||
- name: Build with Maven | ||
run: mvn clean package -Dkeycloak.version=$KEYCLOAK_VERSION -Drevision=${{ github.ref_name }} | ||
env: | ||
KEYCLOAK_VERSION: ${{ env.KEYCLOAK_VERSION }} | ||
|
||
- name: Upload JAR artifact | ||
uses: actions/upload-artifact@v4 | ||
with: | ||
name: pii-encryption-provider | ||
path: target/*.jar | ||
|
||
- name: Create GitHub Release | ||
uses: softprops/action-gh-release@v1 | ||
if: startsWith(github.ref, 'refs/tags/') | ||
with: | ||
files: target/*.jar | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
src/test/java/my/unifi/eset/keycloak/piidataencryption/utils/EncryptionUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package my.unifi.eset.keycloak.piidataencryption.utils; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
|
||
import javax.crypto.SecretKey; | ||
import javax.crypto.spec.SecretKeySpec; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import uk.org.webcompere.systemstubs.environment.EnvironmentVariables; | ||
import uk.org.webcompere.systemstubs.jupiter.SystemStub; | ||
import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension; | ||
|
||
import java.security.NoSuchAlgorithmException; | ||
|
||
import static my.unifi.eset.keycloak.piidataencryption.utils.EncryptionUtils.algorithm; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
@ExtendWith(SystemStubsExtension.class) | ||
class EncryptionUtilsTest { | ||
|
||
@SystemStub | ||
private EnvironmentVariables environmentVariables; | ||
|
||
protected final String envVarKey = "KC_PII_ENCKEY"; | ||
protected final String validEncKey = "1234567891123456"; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
EncryptionUtils.key = null; | ||
|
||
environmentVariables.set(envVarKey, validEncKey); | ||
} | ||
|
||
@Test | ||
void testEncryptValue() throws NoSuchAlgorithmException { | ||
String encryptedValue = EncryptionUtils.encryptValue("test"); | ||
assertNotEquals("test", encryptedValue); | ||
|
||
String decryptedValue = EncryptionUtils.decryptValue(encryptedValue); | ||
assertEquals("test", decryptedValue); | ||
} | ||
|
||
@Test | ||
void testDecryptValue() throws NoSuchAlgorithmException { | ||
SecretKey key = EncryptionUtils.getEncryptionKey(); | ||
|
||
String decryptedValue = EncryptionUtils.decryptValue("$$$GTaogsGC8vbgE098AN9kC+UCHD8vYzVgFF0hFDnuKIw="); | ||
|
||
assertEquals("test", decryptedValue); | ||
} | ||
|
||
@Test | ||
void testWrongKeySize() { | ||
environmentVariables.set(envVarKey, "invalid"); | ||
|
||
Throwable thrown = assertThrows(RuntimeException.class, () -> { | ||
EncryptionUtils.getEncryptionKey(); | ||
}); | ||
|
||
assertEquals("Invalid encryption key for algorithm " + algorithm, thrown.getMessage()); | ||
|
||
|
||
environmentVariables.set(envVarKey, validEncKey); | ||
assertDoesNotThrow(() -> EncryptionUtils.getEncryptionKey(), "should work once the key is correct and not reuse the previous one"); | ||
} | ||
|
||
@Test | ||
void testValidKey() { | ||
byte[] validKeyBytes = validEncKey.getBytes(); | ||
SecretKeySpec validKey = new SecretKeySpec(validKeyBytes, "AES"); | ||
|
||
assertDoesNotThrow(() -> EncryptionUtils.validateKey(validKey)); | ||
} | ||
|
||
@Test | ||
void testValidateAnInvalidKey() { | ||
byte[] invalidKeyBytes = "invalid_size".getBytes(); | ||
SecretKeySpec invalidKey = new SecretKeySpec(invalidKeyBytes, "AES"); | ||
|
||
IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, () -> { | ||
EncryptionUtils.validateKey(invalidKey); | ||
}); | ||
|
||
String expectedMessage = "Invalid encryption key for algorithm " + algorithm; | ||
assertThrows(IllegalArgumentException.class, () -> EncryptionUtils.validateKey(invalidKey)); | ||
assert(thrown.getMessage().contains(expectedMessage)); | ||
} | ||
} |