From 809e3d87175d86ca46bc1922fdde3182a2f43613 Mon Sep 17 00:00:00 2001 From: Hean Chhinling Date: Tue, 5 Nov 2024 15:06:10 +0100 Subject: [PATCH 1/3] HadoopYARN-11726: Add logging statements for successful and unsucessful password retrieval operations --- .../java/org/apache/hadoop/yarn/webapp/util/WebAppUtils.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/webapp/util/WebAppUtils.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/webapp/util/WebAppUtils.java index ff04ffe47a2ad..99f3706174a4e 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/webapp/util/WebAppUtils.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/webapp/util/WebAppUtils.java @@ -47,6 +47,8 @@ import org.apache.hadoop.yarn.webapp.NotFoundException; import org.apache.http.NameValuePair; import org.apache.http.client.utils.URLEncodedUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import javax.servlet.http.HttpServletRequest; @@ -61,6 +63,7 @@ public class WebAppUtils { "ssl.server.keystore.keypassword"; public static final String HTTPS_PREFIX = "https://"; public static final String HTTP_PREFIX = "http://"; + public static final Logger LOG = LoggerFactory.getLogger(WebAppUtils.class); public static void setRMWebAppPort(Configuration conf, int port) { String hostname = getRMWebAppURLWithoutScheme(conf); @@ -509,10 +512,12 @@ static String getPassword(Configuration conf, String alias) { char[] passchars = conf.getPassword(alias); if (passchars != null) { password = new String(passchars); + LOG.debug("Successful password retrieval for alias: {}", alias); } } catch (IOException ioe) { password = null; + LOG.error("Unable to retrieve password for alias: {}", alias, ioe); } return password; } From d0ba380f156e8ec40667a9e1351eaa563b122739 Mon Sep 17 00:00:00 2001 From: Hean Chhinling Date: Wed, 6 Nov 2024 08:47:52 +0100 Subject: [PATCH 2/3] YARN-11726: Adding test cases for IOException and change log from debug to info --- .../hadoop/yarn/webapp/util/WebAppUtils.java | 7 ++++++- .../yarn/webapp/util/TestWebAppUtils.java | 21 ++++++++++++++----- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/webapp/util/WebAppUtils.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/webapp/util/WebAppUtils.java index 99f3706174a4e..e79c7ff3f6ff8 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/webapp/util/WebAppUtils.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/webapp/util/WebAppUtils.java @@ -512,13 +512,18 @@ static String getPassword(Configuration conf, String alias) { char[] passchars = conf.getPassword(alias); if (passchars != null) { password = new String(passchars); - LOG.debug("Successful password retrieval for alias: {}", alias); + LOG.info("Successful password retrieval for alias: {}", alias); } } catch (IOException ioe) { password = null; LOG.error("Unable to retrieve password for alias: {}", alias, ioe); } + + if (password == null) { + LOG.error("Password does not exist for alias: {}", alias); + } + return password; } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/webapp/util/TestWebAppUtils.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/webapp/util/TestWebAppUtils.java index 2d9c39aba8b8d..648994ebf1ed5 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/webapp/util/TestWebAppUtils.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/webapp/util/TestWebAppUtils.java @@ -25,6 +25,7 @@ import java.util.Map; import javax.servlet.http.HttpServletRequest; +import org.apache.hadoop.yarn.util.TestBoundedAppender; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @@ -43,6 +44,7 @@ import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; +import static org.mockito.Mockito.when; public class TestWebAppUtils { private static final String RM1_NODE_ID = "rm1"; @@ -109,6 +111,15 @@ void testGetPassword() throws Exception { assertNull(WebAppUtils.getPassword(conf, "invalid-alias")); } + @Test + void testGetPasswordIOException() throws Exception { + Configuration mockConf = Mockito.mock(Configuration.class); + + when(mockConf.getPassword("error-alias")).thenThrow(new IOException("Simulated IO error")); + + assertNull(WebAppUtils.getPassword(mockConf, "error-alias")); + } + @Test void testLoadSslConfiguration() throws Exception { Configuration conf = provisionCredentialsForSSL(); @@ -186,7 +197,7 @@ protected Configuration provisionCredentialsForSSL() throws IOException, void testAppendQueryParams() throws Exception { HttpServletRequest request = Mockito.mock(HttpServletRequest.class); String targetUri = "/test/path"; - Mockito.when(request.getCharacterEncoding()).thenReturn(null); + when(request.getCharacterEncoding()).thenReturn(null); Map paramResultMap = new HashMap<>(); paramResultMap.put("param1=x", targetUri + "?" + "param1=x"); paramResultMap @@ -195,7 +206,7 @@ void testAppendQueryParams() throws Exception { targetUri + "?" + "param1=x¶m2=y¶m3=x+y"); for (Map.Entry entry : paramResultMap.entrySet()) { - Mockito.when(request.getQueryString()).thenReturn(entry.getKey()); + when(request.getQueryString()).thenReturn(entry.getKey()); String uri = WebAppUtils.appendQueryParams(request, targetUri); assertEquals(entry.getValue(), uri); } @@ -205,8 +216,8 @@ void testAppendQueryParams() throws Exception { void testGetHtmlEscapedURIWithQueryString() throws Exception { HttpServletRequest request = Mockito.mock(HttpServletRequest.class); String targetUri = "/test/path"; - Mockito.when(request.getCharacterEncoding()).thenReturn(null); - Mockito.when(request.getRequestURI()).thenReturn(targetUri); + when(request.getCharacterEncoding()).thenReturn(null); + when(request.getRequestURI()).thenReturn(targetUri); Map paramResultMap = new HashMap<>(); paramResultMap.put("param1=x", targetUri + "?" + "param1=x"); paramResultMap @@ -215,7 +226,7 @@ void testGetHtmlEscapedURIWithQueryString() throws Exception { targetUri + "?" + "param1=x&param2=y&param3=x+y"); for (Map.Entry entry : paramResultMap.entrySet()) { - Mockito.when(request.getQueryString()).thenReturn(entry.getKey()); + when(request.getQueryString()).thenReturn(entry.getKey()); String uri = WebAppUtils.getHtmlEscapedURIWithQueryString(request); assertEquals(entry.getValue(), uri); } From 8f7c101b7547b5f58710119a8519ffd36e1c7850 Mon Sep 17 00:00:00 2001 From: Hean Chhinling Date: Sat, 14 Dec 2024 11:55:02 +0100 Subject: [PATCH 3/3] Updating the log level to debug and warn instead of info level --- .../hadoop/yarn/webapp/util/WebAppUtils.java | 11 ++++------- .../hadoop/yarn/webapp/util/TestWebAppUtils.java | 14 ++++++-------- 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/webapp/util/WebAppUtils.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/webapp/util/WebAppUtils.java index e79c7ff3f6ff8..6667b2d67474a 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/webapp/util/WebAppUtils.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/webapp/util/WebAppUtils.java @@ -47,11 +47,10 @@ import org.apache.hadoop.yarn.webapp.NotFoundException; import org.apache.http.NameValuePair; import org.apache.http.client.utils.URLEncodedUtils; +import javax.servlet.http.HttpServletRequest; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import javax.servlet.http.HttpServletRequest; - @Private @Evolving public class WebAppUtils { @@ -512,7 +511,9 @@ static String getPassword(Configuration conf, String alias) { char[] passchars = conf.getPassword(alias); if (passchars != null) { password = new String(passchars); - LOG.info("Successful password retrieval for alias: {}", alias); + LOG.debug("Successful password retrival for alias: {}", alias); + } else { + LOG.warn("Password retrieval failed, no password found for alias: {}", alias); } } catch (IOException ioe) { @@ -520,10 +521,6 @@ static String getPassword(Configuration conf, String alias) { LOG.error("Unable to retrieve password for alias: {}", alias, ioe); } - if (password == null) { - LOG.error("Password does not exist for alias: {}", alias); - } - return password; } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/webapp/util/TestWebAppUtils.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/webapp/util/TestWebAppUtils.java index 648994ebf1ed5..166a4841ac6f9 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/webapp/util/TestWebAppUtils.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/webapp/util/TestWebAppUtils.java @@ -25,7 +25,6 @@ import java.util.Map; import javax.servlet.http.HttpServletRequest; -import org.apache.hadoop.yarn.util.TestBoundedAppender; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @@ -44,7 +43,6 @@ import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; -import static org.mockito.Mockito.when; public class TestWebAppUtils { private static final String RM1_NODE_ID = "rm1"; @@ -115,7 +113,7 @@ void testGetPassword() throws Exception { void testGetPasswordIOException() throws Exception { Configuration mockConf = Mockito.mock(Configuration.class); - when(mockConf.getPassword("error-alias")).thenThrow(new IOException("Simulated IO error")); + Mockito.when(mockConf.getPassword("error-alias")).thenThrow(new IOException("Simulated IO error")); assertNull(WebAppUtils.getPassword(mockConf, "error-alias")); } @@ -197,7 +195,7 @@ protected Configuration provisionCredentialsForSSL() throws IOException, void testAppendQueryParams() throws Exception { HttpServletRequest request = Mockito.mock(HttpServletRequest.class); String targetUri = "/test/path"; - when(request.getCharacterEncoding()).thenReturn(null); + Mockito.when(request.getCharacterEncoding()).thenReturn(null); Map paramResultMap = new HashMap<>(); paramResultMap.put("param1=x", targetUri + "?" + "param1=x"); paramResultMap @@ -206,7 +204,7 @@ void testAppendQueryParams() throws Exception { targetUri + "?" + "param1=x¶m2=y¶m3=x+y"); for (Map.Entry entry : paramResultMap.entrySet()) { - when(request.getQueryString()).thenReturn(entry.getKey()); + Mockito.when(request.getQueryString()).thenReturn(entry.getKey()); String uri = WebAppUtils.appendQueryParams(request, targetUri); assertEquals(entry.getValue(), uri); } @@ -216,8 +214,8 @@ void testAppendQueryParams() throws Exception { void testGetHtmlEscapedURIWithQueryString() throws Exception { HttpServletRequest request = Mockito.mock(HttpServletRequest.class); String targetUri = "/test/path"; - when(request.getCharacterEncoding()).thenReturn(null); - when(request.getRequestURI()).thenReturn(targetUri); + Mockito.when(request.getCharacterEncoding()).thenReturn(null); + Mockito.when(request.getRequestURI()).thenReturn(targetUri); Map paramResultMap = new HashMap<>(); paramResultMap.put("param1=x", targetUri + "?" + "param1=x"); paramResultMap @@ -226,7 +224,7 @@ void testGetHtmlEscapedURIWithQueryString() throws Exception { targetUri + "?" + "param1=x&param2=y&param3=x+y"); for (Map.Entry entry : paramResultMap.entrySet()) { - when(request.getQueryString()).thenReturn(entry.getKey()); + Mockito.when(request.getQueryString()).thenReturn(entry.getKey()); String uri = WebAppUtils.getHtmlEscapedURIWithQueryString(request); assertEquals(entry.getValue(), uri); }