Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ | |
import org.apache.logging.log4j.Logger; | ||
import org.apache.logging.log4j.core.LogEvent; | ||
import org.apache.logging.log4j.core.config.ConfigurationAware; | ||
import org.apache.logging.log4j.core.net.JndiManager; | ||
import org.apache.logging.log4j.core.util.Loader; | ||
import org.apache.logging.log4j.util.ReflectionUtil; | ||
import org.apache.logging.log4j.plugins.util.PluginManager; | ||
|
@@ -77,7 +78,9 @@ public Interpolator(final StrLookup defaultLookup, final List<String> pluginPack | |
for (final Map.Entry<String, PluginType<?>> entry : plugins.entrySet()) { | ||
try { | ||
final Class<? extends StrLookup> clazz = entry.getValue().getPluginClass().asSubclass(StrLookup.class); | ||
strLookupMap.put(entry.getKey().toLowerCase(), ReflectionUtil.instantiate(clazz)); | ||
if (!clazz.getName().equals(JndiLookup.class.getName()) || JndiManager.isIsJndiEnabled()) { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
karaatanassov
|
||
strLookupMap.put(entry.getKey().toLowerCase(), ReflectionUtil.instantiate(clazz)); | ||
} | ||
} catch (final Throwable t) { | ||
handleError(entry.getKey(), t); | ||
} | ||
|
@@ -107,12 +110,14 @@ public Interpolator(final Map<String, String> properties) { | |
strLookupMap.put("lower", new LowerLookup()); | ||
strLookupMap.put("upper", new UpperLookup()); | ||
// JNDI | ||
try { | ||
// [LOG4J2-703] We might be on Android | ||
strLookupMap.put(LOOKUP_KEY_JNDI, | ||
Loader.newCheckedInstanceOf("org.apache.logging.log4j.core.lookup.JndiLookup", StrLookup.class)); | ||
} catch (final LinkageError | Exception e) { | ||
handleError(LOOKUP_KEY_JNDI, e); | ||
if (JndiManager.isIsJndiEnabled()) { | ||
try { | ||
// [LOG4J2-703] We might be on Android | ||
strLookupMap.put(LOOKUP_KEY_JNDI, | ||
Loader.newCheckedInstanceOf("org.apache.logging.log4j.core.lookup.JndiLookup", StrLookup.class)); | ||
} catch (final LinkageError | Exception e) { | ||
handleError(LOOKUP_KEY_JNDI, e); | ||
} | ||
} | ||
// JMX input args | ||
try { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,6 +73,10 @@ public class JndiManager extends AbstractManager { | |
|
||
private final DirContext context; | ||
|
||
public static boolean isIsJndiEnabled() { | ||
return PropertiesUtil.getProperties().getBooleanProperty("log4j2.enableJndi", false); | ||
} | ||
|
||
private JndiManager(final String name, final DirContext context, final List<String> allowedHosts, | ||
final List<String> allowedClasses, final List<String> allowedProtocols) { | ||
super(null, name); | ||
|
@@ -82,6 +86,14 @@ private JndiManager(final String name, final DirContext context, final List<Stri | |
this.allowedProtocols = allowedProtocols; | ||
} | ||
|
||
private JndiManager(final String name) { | ||
super(null, name); | ||
this.context = null; | ||
this.allowedProtocols = null; | ||
this.allowedClasses = null; | ||
this.allowedHosts = null; | ||
} | ||
|
||
/** | ||
* Gets the default JndiManager using the default {@link javax.naming.InitialContext}. | ||
* | ||
|
@@ -194,6 +206,9 @@ public static Properties createProperties(final String initialContextFactoryName | |
|
||
@Override | ||
protected boolean releaseSub(final long timeout, final TimeUnit timeUnit) { | ||
if (context != null) { | ||
return JndiCloser.closeSilently(this.context); | ||
} | ||
return JndiCloser.closeSilently(this.context); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
rgoers
Author
Member
|
||
} | ||
|
||
|
@@ -207,6 +222,9 @@ protected boolean releaseSub(final long timeout, final TimeUnit timeUnit) { | |
*/ | ||
@SuppressWarnings("unchecked") | ||
public synchronized <T> T lookup(final String name) throws NamingException { | ||
if (context == null) { | ||
return null; | ||
} | ||
try { | ||
URI uri = new URI(name); | ||
if (uri.getScheme() != null) { | ||
|
@@ -262,21 +280,25 @@ private static class JndiManagerFactory implements ManagerFactory<JndiManager, P | |
|
||
@Override | ||
public JndiManager createManager(final String name, final Properties data) { | ||
String hosts = data != null ? data.getProperty(ALLOWED_HOSTS) : null; | ||
String classes = data != null ? data.getProperty(ALLOWED_CLASSES) : null; | ||
String protocols = data != null ? data.getProperty(ALLOWED_PROTOCOLS) : null; | ||
List<String> allowedHosts = new ArrayList<>(); | ||
List<String> allowedClasses = new ArrayList<>(); | ||
List<String> allowedProtocols = new ArrayList<>(); | ||
addAll(hosts, allowedHosts, permanentAllowedHosts, ALLOWED_HOSTS, data); | ||
addAll(classes, allowedClasses, permanentAllowedClasses, ALLOWED_CLASSES, data); | ||
addAll(protocols, allowedProtocols, permanentAllowedProtocols, ALLOWED_PROTOCOLS, data); | ||
try { | ||
return new JndiManager(name, new InitialDirContext(data), allowedHosts, allowedClasses, | ||
allowedProtocols); | ||
} catch (final NamingException e) { | ||
LOGGER.error("Error creating JNDI InitialContext.", e); | ||
return null; | ||
if (isIsJndiEnabled()) { | ||
String hosts = data != null ? data.getProperty(ALLOWED_HOSTS) : null; | ||
String classes = data != null ? data.getProperty(ALLOWED_CLASSES) : null; | ||
String protocols = data != null ? data.getProperty(ALLOWED_PROTOCOLS) : null; | ||
List<String> allowedHosts = new ArrayList<>(); | ||
List<String> allowedClasses = new ArrayList<>(); | ||
List<String> allowedProtocols = new ArrayList<>(); | ||
addAll(hosts, allowedHosts, permanentAllowedHosts, ALLOWED_HOSTS, data); | ||
addAll(classes, allowedClasses, permanentAllowedClasses, ALLOWED_CLASSES, data); | ||
addAll(protocols, allowedProtocols, permanentAllowedProtocols, ALLOWED_PROTOCOLS, data); | ||
try { | ||
return new JndiManager(name, new InitialDirContext(data), allowedHosts, allowedClasses, | ||
allowedProtocols); | ||
} catch (final NamingException e) { | ||
LOGGER.error("Error creating JNDI InitialContext.", e); | ||
return null; | ||
} | ||
} else { | ||
return new JndiManager(name); | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache license, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the license for the specific language governing permissions and | ||
* limitations under the license. | ||
*/ | ||
package org.apache.logging.log4j.core.lookup; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.apache.logging.log4j.core.test.junit.JndiRule; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertNull; | ||
|
||
/** | ||
* JndiDisabledLookupTest | ||
* | ||
* Verifies the Lookups are disabled without the log4j2.enableJndi property set to true. | ||
*/ | ||
public class JndiDisabledLookupTest { | ||
|
||
private static final String TEST_CONTEXT_RESOURCE_NAME = "logging/context-name"; | ||
private static final String TEST_CONTEXT_NAME = "app-1"; | ||
private static final String TEST_INTEGRAL_NAME = "int-value"; | ||
private static final int TEST_INTEGRAL_VALUE = 42; | ||
private static final String TEST_STRINGS_NAME = "string-collection"; | ||
private static final Collection<String> TEST_STRINGS_COLLECTION = Arrays.asList("one", "two", "three"); | ||
|
||
@Rule | ||
public JndiRule jndiRule = new JndiRule(createBindings()); | ||
|
||
private Map<String, Object> createBindings() { | ||
final Map<String, Object> map = new HashMap<>(); | ||
map.put(JndiLookup.CONTAINER_JNDI_RESOURCE_PATH_PREFIX + TEST_CONTEXT_RESOURCE_NAME, TEST_CONTEXT_NAME); | ||
map.put(JndiLookup.CONTAINER_JNDI_RESOURCE_PATH_PREFIX + TEST_INTEGRAL_NAME, TEST_INTEGRAL_VALUE); | ||
map.put(JndiLookup.CONTAINER_JNDI_RESOURCE_PATH_PREFIX + TEST_STRINGS_NAME, TEST_STRINGS_COLLECTION); | ||
return map; | ||
} | ||
|
||
@Test | ||
public void testLookup() { | ||
final StrLookup lookup = new JndiLookup(); | ||
|
||
String contextName = lookup.lookup(TEST_CONTEXT_RESOURCE_NAME); | ||
assertNull(contextName); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -267,6 +267,10 @@ For example: | |
[#JndiLookup] | ||
== JNDI Lookup | ||
As of Log4j 2.15.1 JNDI operations require that `log4j2.enableJndi=tru`e be set as a system property or the | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
remkop
Contributor
|
||
corresponding environment variable for this lookup to function. See the | ||
link:./configuration.html#enableJndi[log4j2.enableJndi] system property. | ||
The JndiLookup allows variables to be retrieved via JNDI. By default the | ||
key will be prefixed with java:comp/env/, however if the key contains a | ||
":" no prefix will be added. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,7 +63,7 @@ substitution is enabled. From log4j 2.15.0, this behavior has been disabled by d | |
Mitigation: In releases >= 2.10, this behavior can be mitigated by setting either the system property | ||
`log4j2.formatMsgNoLookups` or the environment variable `LOG4J_FORMAT_MSG_NO_LOOKUPS` to `true`. | ||
For releases from 2.7 through 2.14.1 all PatternLayout patterns can be modified to specify the message converter as | ||
`%m{nnolookups}` instead of just `%m`. | ||
`%m{nolookups}` instead of just `%m`. | ||
This comment has been minimized.
Sorry, something went wrong.
guusdk
|
||
For releases from 2.0-beta9 to 2.7, the only mitigation is to remove the `JndiLookup` class from the classpath: | ||
`zip -q -d log4j-core-*.jar org/apache/logging/log4j/core/lookup/JndiLookup.class`. | ||
|
Would not
JndiLookup.class.getName()
cause regression of [LOG4J2-703] mentioned below?As well this will conflict with the guides recommending the deletion of the JndiLookup.class from the jar files.