Skip to content

Commit

Permalink
Clear environment variable before tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
slawekjaranowski committed Dec 28, 2020
1 parent 779fa06 commit 4fc7696
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
17 changes: 17 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,18 @@

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<excludedEnvironmentVariables>
<!-- exclude configuration variable from test environment -->
<excludedVariable>SIGN_KEY</excludedVariable>
<excludedVariable>SIGN_KEY_ID</excludedVariable>
<excludedVariable>SIGN_KEY_PASS</excludedVariable>
</excludedEnvironmentVariables>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
Expand Down Expand Up @@ -358,6 +370,11 @@
<settingsFile>src/it/settings.xml</settingsFile>
<showErrors>true</showErrors>
<showVersion>true</showVersion>
<environmentVariables>
<SIGN_KEY/>
<SIGN_KEY_ID/>
<SIGN_KEY_PASS/>
</environmentVariables>
</configuration>
<executions>
<execution>
Expand Down
17 changes: 14 additions & 3 deletions src/main/java/org/simplify4u/plugins/sign/openpgp/PGPKeyInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,31 @@ public class PGPKeyInfo {
@Builder
private PGPKeyInfo(String keyId, String keyPass, File keyFile) {

id = Optional.ofNullable(Optional.ofNullable(System.getenv(SIGN_KEY_ID_ENV)).orElse(keyId))
id = Optional.ofNullable(stringFromEnv(SIGN_KEY_ID_ENV).orElse(keyId))
.map(PGPKeyInfo::parseKeyId)
.orElse(null);

pass = Optional.ofNullable(Optional.ofNullable(System.getenv(SIGN_KEY_PASS_ENV)).orElse(keyPass))
pass = Optional.ofNullable(stringFromEnv(SIGN_KEY_PASS_ENV).orElse(keyPass))
.map(String::toCharArray)
.orElse(null);

key = Optional.ofNullable(System.getenv(SIGN_KEY_ENV))
key = stringFromEnv(SIGN_KEY_ENV)
.map(String::trim)
.map(PGPKeyInfo::keyFromString)
.orElseGet(() -> keyFromFile(keyFile));
}

/**
* Read environment variable and filter by "null" string - this value is set be invoker-maven-plugin.
*
* @param environmentName a environment variable name
* @return content of environment variable or empty if not exist.
*/
private static Optional<String> stringFromEnv(String environmentName) {
return Optional.ofNullable(System.getenv(environmentName))
.filter(s -> !"null".equals(s));
}

private static InputStream keyFromFile(File keyFile) {

if (!keyFile.exists()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,18 @@ void invalidKeyIdThrowException() {
.hasMessageStartingWith("Invalid keyId: For input string: \"xxx\"")
.hasNoCause();
}

@Test
@SetEnvironmentVariable(key = "SIGN_KEY", value = "signKey from environment")
@SetEnvironmentVariable(key = "SIGN_KEY_ID", value = "null")
void nullStringInEnvironmentValueShouldBeFiltered() {
// when
PGPKeyInfo keyInfo = PGPKeyInfo.builder()
.build();

// then
assertThat(keyInfo.getId()).isNull();
assertThat(keyInfo.getPass()).isNull();
assertThat(keyInfo.getKey()).hasContent("signKey from environment");
}
}

0 comments on commit 4fc7696

Please sign in to comment.