Skip to content

Commit

Permalink
[Branch 2.7][fix][broker] Upgrade log4j2 version to 2.18.0 (#16884) (#…
Browse files Browse the repository at this point in the history
…16942)

* cherry-pick 16884

* fix test
  • Loading branch information
Jason918 authored Aug 5, 2022
1 parent 44696df commit e75b403
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 65 deletions.
10 changes: 5 additions & 5 deletions distribution/server/src/assemble/LICENSE.bin.txt
Original file line number Diff line number Diff line change
Expand Up @@ -388,11 +388,11 @@ The Apache Software License, Version 2.0
- jakarta.validation-jakarta.validation-api-2.0.2.jar
- javax.validation-validation-api-1.1.0.Final.jar
* Log4J
- org.apache.logging.log4j-log4j-api-2.17.1.jar
- org.apache.logging.log4j-log4j-core-2.17.1.jar
- org.apache.logging.log4j-log4j-slf4j-impl-2.17.1.jar
- org.apache.logging.log4j-log4j-web-2.17.1.jar
- org.apache.logging.log4j-log4j-1.2-api-2.17.1.jar
- org.apache.logging.log4j-log4j-api-2.18.0.jar
- org.apache.logging.log4j-log4j-core-2.18.0.jar
- org.apache.logging.log4j-log4j-slf4j-impl-2.18.0.jar
- org.apache.logging.log4j-log4j-web-2.18.0.jar
- org.apache.logging.log4j-log4j-1.2-api-2.18.0.jar
* Java Native Access JNA -- net.java.dev.jna-jna-4.2.0.jar
* BookKeeper
- org.apache.bookkeeper-bookkeeper-common-4.12.1.jar
Expand Down
10 changes: 9 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ flexible messaging model and an intuitive client API.</description>
<rocksdb.version>6.10.2</rocksdb.version>
<slf4j.version>1.7.25</slf4j.version>
<commons.collections.version>3.2.2</commons.collections.version>
<log4j2.version>2.17.1</log4j2.version>
<log4j2.version>2.18.0</log4j2.version>
<bouncycastle.version>1.68</bouncycastle.version>
<bouncycastlefips.version>1.0.2</bouncycastlefips.version>
<jackson.version>2.11.1</jackson.version>
Expand All @@ -127,6 +127,7 @@ flexible messaging model and an intuitive client API.</description>
<grpc.version>1.18.0</grpc.version>
<protoc-gen-grpc-java.version>${grpc.version}</protoc-gen-grpc-java.version>
<gson.version>2.8.9</gson.version>
<system-lambda.version>1.2.1</system-lambda.version>
<sketches.version>0.8.3</sketches.version>
<hbc-core.version>2.2.0</hbc-core.version>
<cassandra-driver-core.version>3.6.0</cassandra-driver-core.version>
Expand Down Expand Up @@ -996,6 +997,13 @@ flexible messaging model and an intuitive client API.</description>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.github.stefanbirkner</groupId>
<artifactId>system-lambda</artifactId>
<version>${system-lambda.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-testng</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,49 +21,17 @@

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNull;

import java.lang.reflect.Field;
import java.util.Map;

import com.github.stefanbirkner.systemlambda.SystemLambda;
import org.testng.annotations.Test;

public class EnvironmentBasedSecretsProviderTest {
@Test
public void testConfigValidation() throws Exception {
EnvironmentBasedSecretsProvider provider = new EnvironmentBasedSecretsProvider();
assertNull(provider.provideSecret("mySecretName", "Ignored"));
injectEnvironmentVariable("mySecretName", "SecretValue");
assertEquals(provider.provideSecret("mySecretName", "Ignored"), "SecretValue");
}

private static void injectEnvironmentVariable(String key, String value)
throws Exception {

Class<?> processEnvironment = Class.forName("java.lang.ProcessEnvironment");

Field unmodifiableMapField = getAccessibleField(processEnvironment, "theUnmodifiableEnvironment");
Object unmodifiableMap = unmodifiableMapField.get(null);
injectIntoUnmodifiableMap(key, value, unmodifiableMap);

Field mapField = getAccessibleField(processEnvironment, "theEnvironment");
Map<String, String> map = (Map<String, String>) mapField.get(null);
map.put(key, value);
}

private static Field getAccessibleField(Class<?> clazz, String fieldName)
throws NoSuchFieldException {

Field field = clazz.getDeclaredField(fieldName);
field.setAccessible(true);
return field;
SystemLambda.withEnvironmentVariable("mySecretName", "SecretValue").execute(() -> {
assertEquals(provider.provideSecret("mySecretName", "Ignored"), "SecretValue");
});
}

private static void injectIntoUnmodifiableMap(String key, String value, Object map)
throws ReflectiveOperationException {

Class unmodifiableMap = Class.forName("java.util.Collections$UnmodifiableMap");
Field field = getAccessibleField(unmodifiableMap, "m");
Object obj = field.get(map);
((Map<String, String>) obj).put(key, value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@
*/
package org.apache.pulsar.io.flume.node;

import junit.framework.Assert;
import static org.testng.Assert.assertEquals;
import com.github.stefanbirkner.systemlambda.SystemLambda;
import java.io.File;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.EnvironmentVariables;

import java.io.File;

public class TestEnvVarResolverProperties {
private static final File TESTFILE = new File(
public final class TestEnvVarResolverProperties {
private static final File TEST_FILE = new File(
TestEnvVarResolverProperties.class.getClassLoader()
.getResource("flume-conf-with-envvars.properties").getFile());

Expand All @@ -36,35 +36,38 @@ public class TestEnvVarResolverProperties {
private PropertiesFileConfigurationProvider provider;

@Before
public void setUp() throws Exception {
provider = new PropertiesFileConfigurationProvider("a1", TESTFILE);
public void setUp() {
provider = new PropertiesFileConfigurationProvider("a1", TEST_FILE);
}

@Test
public void resolveEnvVar() throws Exception {
environmentVariables.set("VARNAME", "varvalue");
String resolved = EnvVarResolverProperties.resolveEnvVars("padding ${VARNAME} padding");
Assert.assertEquals("padding varvalue padding", resolved);
SystemLambda.withEnvironmentVariable("VARNAME", "varvalue").execute(() -> {
String resolved = EnvVarResolverProperties.resolveEnvVars("padding ${VARNAME} padding");
assertEquals(resolved, "padding varvalue padding");
});
}

@Test
public void resolveEnvVars() throws Exception {
environmentVariables.set("VARNAME1", "varvalue1");
environmentVariables.set("VARNAME2", "varvalue2");
String resolved = EnvVarResolverProperties
.resolveEnvVars("padding ${VARNAME1} ${VARNAME2} padding");
Assert.assertEquals("padding varvalue1 varvalue2 padding", resolved);
SystemLambda.withEnvironmentVariable("VARNAME1", "varvalue1")
.and("VARNAME2", "varvalue2")
.execute(() -> {
String resolved = EnvVarResolverProperties.resolveEnvVars(
"padding ${VARNAME1} ${VARNAME2} padding");
assertEquals(resolved, "padding varvalue1 varvalue2 padding");
});
}

@Test
public void getProperty() throws Exception {
String NC_PORT = "6667";
environmentVariables.set("NC_PORT", NC_PORT);
System.setProperty("propertiesImplementation",
"org.apache.pulsar.io.flume.node.EnvVarResolverProperties");
SystemLambda.withEnvironmentVariable("NC_PORT", "6667").execute(() -> {
System.setProperty("propertiesImplementation",
"org.apache.pulsar.io.flume.node.EnvVarResolverProperties");

Assert.assertEquals(NC_PORT, provider.getFlumeConfiguration()
.getConfigurationFor("a1")
.getSourceContext().get("r1").getParameters().get("port"));
assertEquals(provider.getFlumeConfiguration()
.getConfigurationFor("a1")
.getSourceContext().get("r1").getParameters().get("port"), "6667");
});
}
}
}

0 comments on commit e75b403

Please sign in to comment.