From ef1e3b3665816c6d6892617a4fac0b8c8c5a9ec4 Mon Sep 17 00:00:00 2001 From: Elyes Cherfa Date: Fri, 23 Aug 2024 11:39:11 +0200 Subject: [PATCH] Issue #297: Manage WMI Empty Username * Ignored WMI Password when WMI Username isn't provided * Added test cases. * Tested the results using the CLI and the Agent. --- .../extension/wmi/WmiRequestExecutor.java | 31 ++++++++++++------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/metricshub-wmi-extension/src/main/java/org/sentrysoftware/metricshub/extension/wmi/WmiRequestExecutor.java b/metricshub-wmi-extension/src/main/java/org/sentrysoftware/metricshub/extension/wmi/WmiRequestExecutor.java index 89b513932..8339e9ee3 100644 --- a/metricshub-wmi-extension/src/main/java/org/sentrysoftware/metricshub/extension/wmi/WmiRequestExecutor.java +++ b/metricshub-wmi-extension/src/main/java/org/sentrysoftware/metricshub/extension/wmi/WmiRequestExecutor.java @@ -78,6 +78,14 @@ public List> executeWmi( @SpanAttribute("wmi.query") @NonNull final String wbemQuery, @SpanAttribute("wmi.namespace") @NonNull final String namespace ) throws ClientException { + final String username = wmiConfig.getUsername(); + // If the username is not provided, null will be used instead of the provided password. + final char[] password = username == null ? null : wmiConfig.getPassword(); + + if (username == null) { + log.warn("Hostname {}. Username not provided.", hostname); + } + // Where to connect to? // Local: namespace // Remote: hostname\namespace @@ -91,7 +99,7 @@ public List> executeWmi( "- Namespace: {}\n- Timeout: {} s\n", hostname, networkResource, - wmiConfig.getUsername(), + username, wbemQuery, namespace, wmiConfig.getTimeout() @@ -99,13 +107,7 @@ public List> executeWmi( ); // Go! - try ( - WmiWbemServices wbemServices = WmiWbemServices.getInstance( - networkResource, - wmiConfig.getUsername(), - wmiConfig.getPassword() - ) - ) { + try (WmiWbemServices wbemServices = WmiWbemServices.getInstance(networkResource, username, password)) { final long startTime = System.currentTimeMillis(); // Execute the WQL and get the result @@ -125,7 +127,7 @@ public List> executeWmi( "- Namespace: {}\n- Timeout: {} s\n- Result:\n{}\n- response-time: {}\n", hostname, networkResource, - wmiConfig.getUsername(), + username, wbemQuery, namespace, wmiConfig.getTimeout(), @@ -270,11 +272,18 @@ public String executeWinRemoteCommand( String command, List embeddedFiles ) throws ClientException { + final String username = winConfiguration.getUsername(); + // If the username is not provided, null will be used instead of the provided password. + final char[] password = username == null ? null : winConfiguration.getPassword(); + + if (username == null) { + log.warn("Hostname {}. Username not provided.", hostname); + } return executeWmiRemoteCommand( command, hostname, - winConfiguration.getUsername(), - winConfiguration.getPassword(), + username, + password, winConfiguration.getTimeout().intValue(), embeddedFiles );