-
Notifications
You must be signed in to change notification settings - Fork 293
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for environment variables in other platforms
Signed-off-by: Peter Nied <[email protected]>
- Loading branch information
Showing
2 changed files
with
75 additions
and
3 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
71 changes: 71 additions & 0 deletions
71
src/test/java/org/opensearch/security/support/SecurityUtilsTest.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,71 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
package org.opensearch.security.support; | ||
|
||
import java.util.List; | ||
import java.util.Collection; | ||
import org.junit.Test; | ||
import org.hamcrest.Matcher; | ||
import java.util.function.Predicate; | ||
|
||
import static org.opensearch.security.support.SecurityUtils.ENV_PATTERN; | ||
import static org.opensearch.security.support.SecurityUtils.ENVBC_PATTERN; | ||
import static org.opensearch.security.support.SecurityUtils.ENVBASE64_PATTERN; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.equalTo; | ||
|
||
public class SecurityUtilsTest { | ||
|
||
private final Collection<String> interestingEnvKeyNames = List.of( | ||
"=ExitCode", | ||
"=C:", | ||
"ProgramFiles(x86)", | ||
"INPUT_GRADLE-HOME-CACHE-CLEANUP" | ||
); | ||
private final Collection<String> namesFromThisRuntimeEnvironment = System.getenv().keySet(); | ||
|
||
@Test | ||
public void checkInterestingNamesForEnvPattern() { | ||
checkKeysWithPredicate(interestingEnvKeyNames, "env", ENV_PATTERN.asMatchPredicate()); | ||
} | ||
|
||
@Test | ||
public void checkRuntimeKeyNamesForEnvPattern() { | ||
checkKeysWithPredicate(namesFromThisRuntimeEnvironment, "env", ENV_PATTERN.asMatchPredicate()); | ||
} | ||
|
||
@Test | ||
public void checkInterestingNamesForEnvbcPattern() { | ||
checkKeysWithPredicate(interestingEnvKeyNames, "envbc", ENVBC_PATTERN.asMatchPredicate()); | ||
} | ||
|
||
@Test | ||
public void checkInterestingNamesForEnvBase64Pattern() { | ||
checkKeysWithPredicate(interestingEnvKeyNames, "envbase64", ENVBASE64_PATTERN.asMatchPredicate()); | ||
} | ||
|
||
private void checkKeysWithPredicate(Collection<String> keys, String predicateName, Predicate<String> predicate) { | ||
keys.forEach(envKeyName -> { | ||
final String prefixWithKeyName = "${" + predicateName + "." + envKeyName; | ||
|
||
final String baseKeyName = prefixWithKeyName + "}"; | ||
assertThat("Testing " + envKeyName + ", " + baseKeyName, | ||
predicate.test(baseKeyName), | ||
equalTo(true)); | ||
|
||
final String baseKeyNameWithDefault = prefixWithKeyName + ":-tTt}"; | ||
assertThat("Testing " + envKeyName + " with defaultValue, " + baseKeyNameWithDefault, | ||
predicate.test(baseKeyNameWithDefault), | ||
equalTo(true)); | ||
}); | ||
} | ||
} |