1 package org.metricshub.winrm;
2
3 /*-
4 * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
5 * WinRM Java Client
6 * ჻჻჻჻჻჻
7 * Copyright 2023 - 2024 Metricshub
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 import org.metricshub.winrm.exceptions.WqlQuerySyntaxException;
29
30 public abstract class WmiHelper {
31
32 /**
33 * Private constructor, as this class cannot be instantiated (it's pure static)
34 */
35 private WmiHelper() {}
36
37 public static final String DEFAULT_NAMESPACE = "ROOT\\CIMV2";
38
39 /**
40 * Pattern to detect a simple WQL select query.
41 */
42 private static final Pattern WQL_SIMPLE_SELECT_PATTERN = Pattern.compile(
43 "^\\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+.*)?$",
44 Pattern.CASE_INSENSITIVE
45 );
46
47 /**
48 * Check if the WQL Query respect the simple syntax in the form of
49 * <code>Select * from (where)</code> or <code>Select a,b,c from (where)</code>
50 * is valid.
51 *
52 * @param wqlQuery
53 * @return whether specified WQL query's syntax is valid or not
54 */
55 public static boolean isValidWql(final String wqlQuery) {
56 return WQL_SIMPLE_SELECT_PATTERN.matcher(wqlQuery).find();
57 }
58
59 /**
60 * The "network resource" is either just the namespace (for localhost), or \\hostname\\namespace.
61 *
62 * @param hostname Host to connect to.
63 * @param namespace The Namespace.
64 * @return resource
65 */
66 public static String createNetworkResource(final String hostname, final String namespace) {
67 Utils.checkNonNull(namespace, "namespace");
68 return hostname == null || hostname.isEmpty() ? namespace : String.format("\\\\%s\\%s", hostname, namespace);
69 }
70
71 /**
72 * @param networkResource Network resource string to test
73 * @return whether specified networkResource is local or not
74 */
75 public static boolean isLocalNetworkResource(final String networkResource) {
76 Utils.checkNonNull(networkResource, "networkResource");
77 return (
78 !networkResource.startsWith("\\\\") ||
79 networkResource.startsWith("\\\\localhost\\") ||
80 networkResource.startsWith("\\\\127.0.0.1\\") ||
81 networkResource.startsWith("\\\\0:0:0:0:0:0:0:1\\") ||
82 networkResource.startsWith("\\\\::1\\") ||
83 networkResource.startsWith("\\\\0000:0000:0000:0000:0000:0000:0000:0001\\") ||
84 networkResource.toLowerCase().startsWith("\\\\" + Utils.getComputerName().toLowerCase() + "\\")
85 );
86 }
87
88 /**
89 * Extract the exact name of the properties from a WMI result.
90 *
91 * The interest is to retrieve the exact case of the property names, instead of
92 * the lowercase that we have at this stage.
93 *
94 * @param resultRows The result whose first row will be parsed
95 * @param wql The WQL query that was used (so we make sure to return the properties in the same order)
96 * @return a list of property names
97 * @throws IllegalStateException if the specified WQL is invalid
98 */
99 public static List<String> extractPropertiesFromResult(final List<Map<String, Object>> resultRows, final String wql) {
100 try {
101 return extractPropertiesFromResult(resultRows, WqlQuery.newInstance(wql));
102 } catch (WqlQuerySyntaxException e) {
103 throw new IllegalStateException(e);
104 }
105 }
106
107 /**
108 * Extract the exact name of the properties from a WMI result.
109 *
110 * The interest is to retrieve the exact case of the property names, instead of
111 * the lowercase that we have at this stage.
112 *
113 * Note: The exact case cannot be retrieved if result is empty, in which case all
114 * names are reported in lower case
115 *
116 * @param resultRows The result whose first row will be parsed
117 * @param wqlQuery The WQL query that was used (so we make sure to return the properties in the same order)
118 * @return a list of property names
119 */
120 public static List<String> extractPropertiesFromResult(
121 final List<Map<String, Object>> resultRows,
122 final WqlQuery wqlQuery
123 ) {
124 // If resultRows is empty, we won't be able to retrieve the actual property names
125 // with the correct case. So, we simply return the list of specified properties in the
126 // WQL query
127 if (resultRows.isEmpty()) {
128 return wqlQuery.getSelectedProperties();
129 }
130
131 // Extract the actual property names
132 final String[] resultPropertyArray = resultRows.get(0).keySet().toArray(new String[0]);
133
134 // First case: we don't have any specified properties in the WQL Query, so we just
135 // return the properties from the result set in alphabetical order
136 if (wqlQuery.getSelectedProperties().isEmpty()) {
137 Arrays.sort(resultPropertyArray, String.CASE_INSENSITIVE_ORDER);
138 return Arrays.asList(resultPropertyArray);
139 }
140
141 // Create a new list based on queryPropertyArray (with its order), but with the values
142 // from resultPropertyArray
143 final List<String> queryProperties = wqlQuery.getSelectedProperties();
144 final Map<String, String> resultProperties = Arrays
145 .asList(resultPropertyArray)
146 .stream()
147 .collect(Collectors.toMap(String::toLowerCase, property -> property));
148 return queryProperties
149 .stream()
150 .map(property -> resultProperties.getOrDefault(property.toLowerCase(), property))
151 .collect(Collectors.toList());
152 }
153 }