|
19 | 19 | package org.elasticsearch.common.logging;
|
20 | 20 |
|
21 | 21 | import com.carrotsearch.randomizedtesting.generators.CodepointSetGenerator;
|
22 |
| - |
23 | 22 | import org.apache.logging.log4j.LogManager;
|
| 23 | +import org.apache.logging.log4j.Logger; |
| 24 | +import org.apache.logging.log4j.simple.SimpleLoggerContext; |
| 25 | +import org.apache.logging.log4j.simple.SimpleLoggerContextFactory; |
| 26 | +import org.apache.logging.log4j.spi.ExtendedLogger; |
| 27 | +import org.apache.logging.log4j.spi.LoggerContext; |
| 28 | +import org.apache.logging.log4j.spi.LoggerContextFactory; |
24 | 29 | import org.elasticsearch.common.settings.Settings;
|
25 | 30 | import org.elasticsearch.common.util.concurrent.ThreadContext;
|
26 | 31 | import org.elasticsearch.test.ESTestCase;
|
27 | 32 | import org.elasticsearch.test.hamcrest.RegexMatcher;
|
28 | 33 | import org.hamcrest.core.IsSame;
|
29 | 34 |
|
30 | 35 | import java.io.IOException;
|
| 36 | +import java.net.URI; |
| 37 | +import java.nio.charset.StandardCharsets; |
| 38 | +import java.security.AccessControlContext; |
| 39 | +import java.security.AccessController; |
| 40 | +import java.security.Permissions; |
| 41 | +import java.security.PrivilegedAction; |
| 42 | +import java.security.ProtectionDomain; |
31 | 43 | import java.util.Collections;
|
32 | 44 | import java.util.HashSet;
|
33 | 45 | import java.util.List;
|
34 | 46 | import java.util.Locale;
|
35 | 47 | import java.util.Map;
|
36 | 48 | import java.util.Set;
|
| 49 | +import java.util.concurrent.atomic.AtomicBoolean; |
37 | 50 | import java.util.stream.IntStream;
|
38 |
| -import java.nio.charset.StandardCharsets; |
39 | 51 |
|
40 | 52 | import static org.elasticsearch.common.logging.DeprecationLogger.WARNING_HEADER_PATTERN;
|
41 | 53 | import static org.elasticsearch.test.hamcrest.RegexMatcher.matches;
|
42 | 54 | import static org.hamcrest.Matchers.containsString;
|
43 | 55 | import static org.hamcrest.Matchers.equalTo;
|
44 | 56 | import static org.hamcrest.Matchers.hasSize;
|
45 | 57 | import static org.hamcrest.Matchers.not;
|
| 58 | +import static org.hamcrest.core.Is.is; |
| 59 | +import static org.mockito.Mockito.doAnswer; |
| 60 | +import static org.mockito.Mockito.mock; |
| 61 | +import static org.mockito.Mockito.when; |
46 | 62 |
|
47 | 63 | /**
|
48 | 64 | * Tests {@link DeprecationLogger}
|
@@ -303,6 +319,49 @@ public void testWarningHeaderSizeSetting() throws IOException{
|
303 | 319 | }
|
304 | 320 | }
|
305 | 321 |
|
| 322 | + public void testLogPermissions() { |
| 323 | + AtomicBoolean supplierCalled = new AtomicBoolean(false); |
| 324 | + |
| 325 | + // mocking the logger used inside DeprecationLogger requires heavy hacking... |
| 326 | + Logger parentLogger = mock(Logger.class); |
| 327 | + when(parentLogger.getName()).thenReturn("logger"); |
| 328 | + ExtendedLogger mockLogger = mock(ExtendedLogger.class); |
| 329 | + doAnswer(invocationOnMock -> { |
| 330 | + supplierCalled.set(true); |
| 331 | + createTempDir(); // trigger file permission, like rolling logs would |
| 332 | + return null; |
| 333 | + }).when(mockLogger).warn("foo", new Object[] {"bar"}); |
| 334 | + final LoggerContext context = new SimpleLoggerContext() { |
| 335 | + @Override |
| 336 | + public ExtendedLogger getLogger(String name) { |
| 337 | + return mockLogger; |
| 338 | + } |
| 339 | + }; |
| 340 | + |
| 341 | + final LoggerContextFactory originalFactory = LogManager.getFactory(); |
| 342 | + try { |
| 343 | + LogManager.setFactory(new SimpleLoggerContextFactory() { |
| 344 | + @Override |
| 345 | + public LoggerContext getContext(String fqcn, ClassLoader loader, Object externalContext, boolean currentContext, |
| 346 | + URI configLocation, String name) { |
| 347 | + return context; |
| 348 | + } |
| 349 | + }); |
| 350 | + DeprecationLogger deprecationLogger = new DeprecationLogger(parentLogger); |
| 351 | + |
| 352 | + AccessControlContext noPermissionsAcc = new AccessControlContext( |
| 353 | + new ProtectionDomain[]{new ProtectionDomain(null, new Permissions())} |
| 354 | + ); |
| 355 | + AccessController.doPrivileged((PrivilegedAction<Void>) () -> { |
| 356 | + deprecationLogger.deprecated("foo", "bar"); |
| 357 | + return null; |
| 358 | + }, noPermissionsAcc); |
| 359 | + assertThat("supplier called", supplierCalled.get(), is(true)); |
| 360 | + } finally { |
| 361 | + LogManager.setFactory(originalFactory); |
| 362 | + } |
| 363 | + } |
| 364 | + |
306 | 365 | private String range(int lowerInclusive, int upperInclusive) {
|
307 | 366 | return IntStream
|
308 | 367 | .range(lowerInclusive, upperInclusive + 1)
|
|
0 commit comments