Skip to content
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

Clear environment variable before tests. #16

Merged
merged 1 commit into from
Dec 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
20 changes: 17 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,34 @@ 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.
* <p>
* TODO - remove workaround after fix and release https://issues.apache.org/jira/browse/MINVOKER-273
*
* @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))
mkarg marked this conversation as resolved.
Show resolved Hide resolved
.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");
}
}