2424import static org .junit .Assert .fail ;
2525
2626import java .io .File ;
27+ import java .io .IOException ;
2728import java .net .BindException ;
2829import java .net .SocketException ;
2930import java .net .URI ;
3031import java .security .PrivilegedExceptionAction ;
3132import java .util .Properties ;
3233import javax .net .ssl .SSLException ;
34+ import javax .servlet .http .HttpServletResponse ;
3335import org .apache .commons .io .FileUtils ;
3436import org .apache .hadoop .HadoopIllegalArgumentException ;
3537import org .apache .hadoop .conf .Configuration ;
@@ -73,6 +75,8 @@ public class TestLogLevel {
7375 private static Configuration clientConf ;
7476 private static Configuration sslConf ;
7577 private static final String logName = TestLogLevel .class .getName ();
78+ private static final String protectedPrefix = "protected" ;
79+ private static final String protectedLogName = protectedPrefix + "." + logName ;
7680 private static final org .apache .logging .log4j .Logger log =
7781 org .apache .logging .log4j .LogManager .getLogger (logName );
7882 private final static String PRINCIPAL = "loglevel.principal" ;
@@ -89,6 +93,7 @@ public class TestLogLevel {
8993 @ BeforeClass
9094 public static void setUp () throws Exception {
9195 serverConf = new Configuration ();
96+ serverConf .setStrings (LogLevel .READONLY_LOGGERS_CONF_KEY , protectedPrefix );
9297 HTU = new HBaseCommonTestingUtil (serverConf );
9398
9499 File keystoreDir = new File (HTU .getDataTestDir ("keystore" ).toString ());
@@ -259,9 +264,17 @@ private HttpServer createServer(String protocol, boolean isSpnego) throws Except
259264 private void testDynamicLogLevel (final String bindProtocol , final String connectProtocol ,
260265 final boolean isSpnego ) throws Exception {
261266 testDynamicLogLevel (bindProtocol , connectProtocol , isSpnego ,
267+ logName ,
262268 org .apache .logging .log4j .Level .DEBUG .toString ());
263269 }
264270
271+ private void testDynamicLogLevel (final String bindProtocol , final String connectProtocol ,
272+ final boolean isSpnego , final String newLevel ) throws Exception {
273+ testDynamicLogLevel (bindProtocol , connectProtocol , isSpnego ,
274+ logName ,
275+ newLevel );
276+ }
277+
265278 /**
266279 * Run both client and server using the given protocol.
267280 * @param bindProtocol specify either http or https for server
@@ -270,13 +283,14 @@ private void testDynamicLogLevel(final String bindProtocol, final String connect
270283 * @throws Exception if client can't accesss server.
271284 */
272285 private void testDynamicLogLevel (final String bindProtocol , final String connectProtocol ,
273- final boolean isSpnego , final String newLevel ) throws Exception {
286+ final boolean isSpnego , final String loggerName , final String newLevel ) throws Exception {
274287 if (!LogLevel .isValidProtocol (bindProtocol )) {
275288 throw new Exception ("Invalid server protocol " + bindProtocol );
276289 }
277290 if (!LogLevel .isValidProtocol (connectProtocol )) {
278291 throw new Exception ("Invalid client protocol " + connectProtocol );
279292 }
293+ org .apache .logging .log4j .Logger log = org .apache .logging .log4j .LogManager .getLogger (loggerName );
280294 org .apache .logging .log4j .Level oldLevel = log .getLevel ();
281295 assertNotEquals ("Get default Log Level which shouldn't be ERROR." ,
282296 org .apache .logging .log4j .Level .ERROR , oldLevel );
@@ -305,8 +319,8 @@ private void testDynamicLogLevel(final String bindProtocol, final String connect
305319 try {
306320 clientUGI .doAs ((PrivilegedExceptionAction <Void >) () -> {
307321 // client command line
308- getLevel (connectProtocol , authority );
309- setLevel (connectProtocol , authority , newLevel );
322+ getLevel (connectProtocol , authority , loggerName );
323+ setLevel (connectProtocol , authority , loggerName , newLevel );
310324 return null ;
311325 });
312326 } finally {
@@ -324,7 +338,7 @@ private void testDynamicLogLevel(final String bindProtocol, final String connect
324338 * @param authority daemon's web UI address
325339 * @throws Exception if unable to connect
326340 */
327- private void getLevel (String protocol , String authority ) throws Exception {
341+ private void getLevel (String protocol , String authority , String logName ) throws Exception {
328342 String [] getLevelArgs = { "-getlevel" , authority , logName , "-protocol" , protocol };
329343 CLI cli = new CLI (protocol .equalsIgnoreCase ("https" ) ? sslConf : clientConf );
330344 cli .run (getLevelArgs );
@@ -336,13 +350,27 @@ private void getLevel(String protocol, String authority) throws Exception {
336350 * @param authority daemon's web UI address
337351 * @throws Exception if unable to run or log level does not change as expected
338352 */
339- private void setLevel (String protocol , String authority , String newLevel ) throws Exception {
353+ private void setLevel (String protocol , String authority , String logName , String newLevel ) throws Exception {
340354 String [] setLevelArgs = { "-setlevel" , authority , logName , newLevel , "-protocol" , protocol };
341355 CLI cli = new CLI (protocol .equalsIgnoreCase ("https" ) ? sslConf : clientConf );
342356 cli .run (setLevelArgs );
343357
358+ org .apache .logging .log4j .Logger logger = org .apache .logging .log4j .LogManager .getLogger (logName );
359+
344360 assertEquals ("new level not equal to expected: " , newLevel .toUpperCase (),
345- log .getLevel ().toString ());
361+ logger .getLevel ().toString ());
362+ }
363+
364+ @ Test
365+ public void testSettingProtectedLogLevel () throws Exception {
366+ try {
367+ testDynamicLogLevel (LogLevel .PROTOCOL_HTTP , LogLevel .PROTOCOL_HTTP , true , protectedLogName ,
368+ "DEBUG" );
369+ fail ("Expected IO exception due to protected logger" );
370+ } catch (IOException e ) {
371+ assertTrue (e .getMessage ().contains ("" + HttpServletResponse .SC_PRECONDITION_FAILED ));
372+ assertTrue (e .getMessage ().contains ("Modification of logger " + protectedLogName + " is disallowed in configuration." ));
373+ }
346374 }
347375
348376 /**
0 commit comments