-
-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Encryption key validation #3
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d54716d
Merge pull request #1 from certifaction/fix-postgres
ianaz 0316b5b
Add encryption key validation
ianaz 7b4549e
Added `validateKey` tests
ianaz a535479
Moved tests together with verify
ianaz 5ad9355
DT-836: Create the release GH workflow (#3)
mobohram 6efcdba
Merge branch 'main' into encryption-key-validation
mobohram 3d037b4
Requested changes
ianaz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was there a reason to have the offline parameter?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, the reason being the mvn command is splitted into two to optimize Docker builds. The
mvn verify
afterCOPY pom.xml
will first download all dependencies as defined by the pom.xml. Themvn package -o
after theCOPY src ./src
will compile the source code using the dependencies already downloaded bymvn verify
. Without the offline parameter, maven somehow still attempts to check and re-download dependencies even though the dependencies are all there.With Docker build automatically re-uses the caches if no files have changed, we don't want to re-download dependencies when we just modify files inside the
src
folder.So, yes the offline flag is very intentionally added to optimize Docker builds. Please revert.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In a535479 i moved the tests to be ran together with verify and skipped them in the
package
step. This because tests required some more packages to be downloaded.I also removed the
--fail-never
in verify because we don't want failed tests to be ignored, you agree? The question is why do we have that in verify.. ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The unit tests require the
src
folder, don't they? If so, then they cannot be run with themvn verify
command because at that point onlypom.xml
is copied over into the Docker build layer. Only after theCOPY src ./src
command that thesrc
folder is available inside the layer for the unit tests to execute.Whatever packages needed by the unit tests are declared inside
pom.xml
so I thinkmvn verify
should already download the packages. If it doesn't then maybe we need to tweak themvn verify
parameters to also download<scope>test</scope>
Here was basically my original intention:
pom.xml
into Docker build layerpom.xml
including any plugins needed for packagingsrc
folderThis way, if I don't change anything inside
pom.xml
then Docker build engine will not need to perform step 1 & 2 and thus saving a lot of time by re-using the Docker build caches, skipping the re-download of dependencies.