View Javadoc
1   package org.sentrysoftware.winrm;
2   
3   /*-
4    * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
5    * WinRM Java Client
6    * ჻჻჻჻჻჻
7    * Copyright 2023 - 2024 Sentry Software
8    * ჻჻჻჻჻჻
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   *
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   *
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
21   */
22  
23  import java.util.Arrays;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.regex.Pattern;
27  import java.util.stream.Collectors;
28  
29  
30  import org.sentrysoftware.winrm.exceptions.WqlQuerySyntaxException;
31  
32  public abstract class WmiHelper {
33  
34  
35  	/**
36  	 * Private constructor, as this class cannot be instantiated (it's pure static)
37  	 */
38  	private WmiHelper() {}
39  
40  	public static final String DEFAULT_NAMESPACE = "ROOT\\CIMV2";
41  
42  	/**
43  	 * Pattern to detect a simple WQL select query.
44  	 */
45  	private static final Pattern WQL_SIMPLE_SELECT_PATTERN = Pattern.compile(
46  			"^\\s*SELECT\\s+(\\*|(?!SELECT|FROM|WHERE)[a-z0-9._]+|((?!SELECT|FROM|WHERE)[a-z0-9._]+\\s*,\\s*)+((?!SELECT|FROM|WHERE)[a-z0-9._]+))\\s+FROM\\s+((?!WHERE|FROM)\\w+)\\s*(WHERE\\s+.*)?$",
47  			Pattern.CASE_INSENSITIVE
48  	);
49  
50  	/**
51  	 * Check if the WQL Query respect the simple syntax in the form of
52  	 * <code>Select * from (where)</code> or <code>Select a,b,c from (where)</code>
53  	 * is valid.
54  	 *
55  	 * @param wqlQuery
56  	 * @return whether specified WQL query's syntax is valid or not
57  	 */
58  	public static boolean isValidWql(final String wqlQuery) {
59  		return WQL_SIMPLE_SELECT_PATTERN.matcher(wqlQuery).find();
60  	}
61  
62  	/**
63  	 * The "network resource" is either just the namespace (for localhost), or \\hostname\\namespace.
64  	 *
65  	 * @param hostname Host to connect to.
66  	 * @param namespace The Namespace.
67  	 * @return resource
68  	 */
69  	public static String createNetworkResource(
70  			final String hostname,
71  			final String namespace) {
72  		Utils.checkNonNull(namespace, "namespace");
73  		return hostname == null || hostname.isEmpty() ?
74  				namespace :
75  				String.format("\\\\%s\\%s", hostname, namespace);
76  	}
77  
78  	/**
79  	 * @param networkResource Network resource string to test
80  	 * @return whether specified networkResource is local or not
81  	 */
82  	public static boolean isLocalNetworkResource(final String networkResource) {
83  		Utils.checkNonNull(networkResource, "networkResource");
84  		return !networkResource.startsWith("\\\\") ||
85  				networkResource.startsWith("\\\\localhost\\") ||
86  				networkResource.startsWith("\\\\127.0.0.1\\") ||
87  				networkResource.startsWith("\\\\0:0:0:0:0:0:0:1\\") ||
88  				networkResource.startsWith("\\\\::1\\") ||
89  				networkResource.startsWith("\\\\0000:0000:0000:0000:0000:0000:0000:0001\\") ||
90  				networkResource.toLowerCase().startsWith("\\\\" + Utils.getComputerName().toLowerCase() + "\\");
91  	}
92  
93  	/**
94  	 * Extract the exact name of the properties from a WMI result.
95  	 * 
96  	 * The interest is to retrieve the exact case of the property names, instead of
97  	 * the lowercase that we have at this stage.
98  	 * 
99  	 * @param resultRows The result whose first row will be parsed
100 	 * @param wql The WQL query that was used (so we make sure to return the properties in the same order)
101 	 * @return a list of property names
102 	 * @throws IllegalStateException if the specified WQL is invalid
103 	 */
104 	public static List<String> extractPropertiesFromResult(final List<Map<String, Object>> resultRows, final String wql) {
105 
106 		try {
107 			return extractPropertiesFromResult(resultRows, WqlQuery.newInstance(wql));
108 		} catch (WqlQuerySyntaxException e) {
109 			throw new IllegalStateException(e);
110 		}
111 
112 	}
113 
114 	/**
115 	 * Extract the exact name of the properties from a WMI result.
116 	 * 
117 	 * The interest is to retrieve the exact case of the property names, instead of
118 	 * the lowercase that we have at this stage.
119 	 * 
120 	 * Note: The exact case cannot be retrieved if result is empty, in which case all
121 	 * names are reported in lower case
122 	 * 
123 	 * @param resultRows The result whose first row will be parsed
124 	 * @param wqlQuery The WQL query that was used (so we make sure to return the properties in the same order)
125 	 * @return a list of property names
126 	 */
127 	public static List<String> extractPropertiesFromResult(final List<Map<String, Object>> resultRows, final WqlQuery wqlQuery) {
128 
129 		// If resultRows is empty, we won't be able to retrieve the actual property names
130 		// with the correct case. So, we simply return the list of specified properties in the
131 		// WQL query
132 		if (resultRows.isEmpty()) {
133 			return wqlQuery.getSelectedProperties();
134 		}
135 
136 		// Extract the actual property names
137 		final String[] resultPropertyArray = resultRows.get(0).keySet().toArray(new String[0]);
138 
139 		// First case: we don't have any specified properties in the WQL Query, so we just
140 		// return the properties from the result set in alphabetical order
141 		if (wqlQuery.getSelectedProperties().isEmpty()) {
142 			Arrays.sort(resultPropertyArray, String.CASE_INSENSITIVE_ORDER);
143 			return Arrays.asList(resultPropertyArray);
144 		}
145 
146 		// Create a new list based on queryPropertyArray (with its order), but with the values
147 		// from resultPropertyArray
148 		final List<String> queryProperties = wqlQuery.getSelectedProperties();
149 		final Map<String, String> resultProperties = Arrays.asList(resultPropertyArray).stream()
150 				.collect(Collectors.toMap(String::toLowerCase, property -> property));
151 		return queryProperties.stream()
152 				.map(property -> resultProperties.getOrDefault(property.toLowerCase(), property))
153 				.collect(Collectors.toList());
154 
155 	}
156 
157 }