Skip to content

Commit

Permalink
Support for environment variables in other platforms
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Nied <[email protected]>
  • Loading branch information
peternied committed Oct 25, 2022
1 parent 3fb3d1a commit 2a4ce68
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@
public final class SecurityUtils {

protected final static Logger log = LogManager.getLogger(SecurityUtils.class);
private static final Pattern ENV_PATTERN = Pattern.compile("\\$\\{env\\.([\\w]+)((\\:\\-)?[\\w]*)\\}");
private static final Pattern ENVBC_PATTERN = Pattern.compile("\\$\\{envbc\\.([\\w]+)((\\:\\-)?[\\w]*)\\}");
private static final Pattern ENVBASE64_PATTERN = Pattern.compile("\\$\\{envbase64\\.([\\w]+)((\\:\\-)?[\\w]*)\\}");
private static final String ENV_PATTERN_SUFFIX = "\\.([\\w=():]+)((\\:\\-)?[\\w=():]*)\\}";
static final Pattern ENV_PATTERN = Pattern.compile("\\$\\{env" + ENV_PATTERN_SUFFIX);
static final Pattern ENVBC_PATTERN = Pattern.compile("\\$\\{envbc" + ENV_PATTERN_SUFFIX);
static final Pattern ENVBASE64_PATTERN = Pattern.compile("\\$\\{envbase64" + ENV_PATTERN_SUFFIX);
public static Locale EN_Locale = forEN();


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* 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.Collection;
import java.util.List;
import java.util.function.Predicate;

import org.junit.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.opensearch.security.support.SecurityUtils.ENVBASE64_PATTERN;
import static org.opensearch.security.support.SecurityUtils.ENVBC_PATTERN;
import static org.opensearch.security.support.SecurityUtils.ENV_PATTERN;

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));
});
}
}

0 comments on commit 2a4ce68

Please sign in to comment.