Repository navigation
Expand file tree
/
Copy pathOsUpdateResponseMapper.java
More file actions
198 lines (183 loc) · 10.3 KB
/
Copy pathOsUpdateResponseMapper.java
File metadata and controls
198 lines (183 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package com.openframe.api.mapper;
import com.openframe.api.dto.rmm.osupdate.OsUpdateDeviceCounts;
import com.openframe.api.dto.rmm.osupdate.OsUpdateDeviceFilters;
import com.openframe.api.dto.rmm.osupdate.OsUpdateOnDeviceResponse;
import com.openframe.api.dto.rmm.osupdate.OsUpdateFilterOption;
import com.openframe.api.dto.rmm.osupdate.OsUpdateFilters;
import com.openframe.api.dto.rmm.osupdate.OsUpdateResponse;
import com.openframe.api.dto.rmm.osupdate.OsUpdateSeverityFilter;
import com.openframe.data.document.rmm.osupdate.OsUpdate;
import com.openframe.data.document.rmm.osupdate.OsUpdateClassification;
import com.openframe.data.document.rmm.osupdate.OsUpdateOnDevice;
import com.openframe.data.document.rmm.osupdate.OsUpdateOnDeviceStatus;
import com.openframe.data.document.rmm.osupdate.OsUpdateSeverity;
import com.openframe.data.repository.rmm.osupdate.OsUpdateDeviceFilter;
import com.openframe.data.repository.rmm.osupdate.OsUpdateOnDeviceStatusCount;
import com.openframe.data.repository.rmm.osupdate.OsUpdateOptionCount;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.EnumMap;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static java.util.Objects.requireNonNullElse;
import static org.springframework.util.CollectionUtils.isEmpty;
@Component
@ConditionalOnProperty(name = "openframe.rmm.os-update.enabled", havingValue = "true")
public class OsUpdateResponseMapper {
private static final String KB_ARTICLE_URL = "https://support.microsoft.com/help/";
private static final Pattern KB_ARTICLE_ID = Pattern.compile("(?i)(?:KB)?(\\d+)");
private static final Set<OsUpdateOnDeviceStatus> PENDING_STATUSES = EnumSet.of(
OsUpdateOnDeviceStatus.NOT_INSTALLED, OsUpdateOnDeviceStatus.FAILED, OsUpdateOnDeviceStatus.RESTART_PENDING);
public OsUpdateResponse toResponse(OsUpdate osUpdate, String cursor) {
List<String> kbArticleIds = requireNonNullElse(osUpdate.getKbArticleIds(), List.of());
String releaseNotesUrl = toReleaseNotesUrl(kbArticleIds);
return OsUpdateResponse.builder()
.id(osUpdate.getId())
.platform(osUpdate.getPlatform())
.identifier(osUpdate.getIdentifier())
.displayName(osUpdate.getDisplayName())
.displayVersion(osUpdate.getDisplayVersion())
.kbArticleIds(kbArticleIds)
.releaseNotesUrl(releaseNotesUrl)
.severity(osUpdate.getSeverity())
.categories(requireNonNullElse(osUpdate.getCategories(), List.of()))
.categoryIds(requireNonNullElse(osUpdate.getCategoryIds(), List.of()))
.updateType(osUpdate.getUpdateType())
.classification(osUpdate.getClassification())
.cveIds(requireNonNullElse(osUpdate.getCveIds(), List.of()))
.publishedAt(osUpdate.getPublishedAt())
.sizeBytes(osUpdate.getSizeBytes())
.rebootBehavior(osUpdate.getRebootBehavior())
.browseOnly(osUpdate.getBrowseOnly())
.exploited(osUpdate.getExploited())
.enrichedAt(osUpdate.getEnrichedAt())
.firstSeenAt(osUpdate.getFirstSeenAt())
.cursor(cursor)
.build();
}
// WARNING: a severity option carries the name of the stored severity, and the client sends it back as
// OsUpdateSeverityFilter: the names must match.
public OsUpdateFilters toOsUpdateFilters(List<OsUpdateOptionCount> classificationCounts,
List<OsUpdateOptionCount> severityCounts, long filteredCount) {
Map<String, Long> updateCountByClassification = toUpdateCountByValue(classificationCounts);
Map<String, Long> updateCountBySeverity = toUpdateCountByValue(severityCounts);
List<OsUpdateFilterOption> classifications = Arrays.stream(OsUpdateClassification.values())
.map(OsUpdateClassification::name)
.map(classification -> toFilterOption(classification, updateCountByClassification))
.toList();
List<OsUpdateFilterOption> severities = new ArrayList<>();
Arrays.stream(OsUpdateSeverity.values())
.map(OsUpdateSeverity::name)
.map(severity -> toFilterOption(severity, updateCountBySeverity))
.forEach(severities::add);
String withoutSeverityValue = OsUpdateSeverityFilter.NONE.name();
Long updateCountWithoutSeverity = updateCountBySeverity.get(null);
OsUpdateFilterOption withoutSeverity = toFilterOption(withoutSeverityValue, updateCountWithoutSeverity);
severities.add(withoutSeverity);
int filteredUpdateCount = Math.toIntExact(filteredCount);
return OsUpdateFilters.builder()
.classifications(classifications)
.severities(severities)
.filteredCount(filteredUpdateCount)
.build();
}
// lastScanAt null = the device has no OS state or never scanned.
public OsUpdateOnDeviceResponse toDeviceResponse(OsUpdateOnDevice osUpdateOnDevice, Instant lastScanAt, String cursor) {
return OsUpdateOnDeviceResponse.builder()
.machineId(osUpdateOnDevice.getMachineId())
.status(osUpdateOnDevice.getStatus())
.pendingSince(osUpdateOnDevice.getPendingSince())
.installedAt(osUpdateOnDevice.getInstalledAt())
.lastInstallFailedAt(osUpdateOnDevice.getLastInstallFailedAt())
.lastInstallErrorCode(osUpdateOnDevice.getLastInstallErrorCode())
.lastScanAt(lastScanAt)
.cursor(cursor)
.build();
}
// Every status, the ones no device has too. filteredCount = the devices in the statuses of the filter.
public OsUpdateDeviceFilters toOsUpdateDeviceFilters(List<OsUpdateOnDeviceStatusCount> statusCounts,
OsUpdateDeviceFilter osUpdateDeviceFilter,
long notManageableDeviceCount) {
Map<OsUpdateOnDeviceStatus, Long> deviceCountByStatus = toDeviceCountByStatus(statusCounts);
List<OsUpdateFilterOption> statuses = Arrays.stream(OsUpdateOnDeviceStatus.values())
.map(status -> toFilterOption(status.name(), deviceCountByStatus.get(status)))
.toList();
List<OsUpdateOnDeviceStatus> requestedStatuses = osUpdateDeviceFilter.getStatuses();
List<OsUpdateOnDeviceStatus> filteredStatuses = isEmpty(requestedStatuses)
? Arrays.asList(OsUpdateOnDeviceStatus.values())
: requestedStatuses;
long filteredDeviceCount = filteredStatuses.stream()
.distinct()
.mapToLong(status -> deviceCountByStatus.getOrDefault(status, 0L))
.sum();
return OsUpdateDeviceFilters.builder()
.statuses(statuses)
.filteredCount(Math.toIntExact(filteredDeviceCount))
.notManageableDeviceCount(Math.toIntExact(notManageableDeviceCount))
.build();
}
// The status counts of one update. Pending = every status but INSTALLED.
public OsUpdateDeviceCounts toOsUpdateDeviceCounts(List<OsUpdateOnDeviceStatusCount> statusCounts) {
Map<OsUpdateOnDeviceStatus, Long> deviceCountByStatus = toDeviceCountByStatus(statusCounts);
long pendingDevices = PENDING_STATUSES.stream()
.mapToLong(status -> deviceCountByStatus.getOrDefault(status, 0L))
.sum();
long installedDevices = deviceCountByStatus.getOrDefault(OsUpdateOnDeviceStatus.INSTALLED, 0L);
long failedDevices = deviceCountByStatus.getOrDefault(OsUpdateOnDeviceStatus.FAILED, 0L);
int pendingDeviceCount = Math.toIntExact(pendingDevices);
int installedDeviceCount = Math.toIntExact(installedDevices);
int failedDeviceCount = Math.toIntExact(failedDevices);
return OsUpdateDeviceCounts.builder()
.pendingDeviceCount(pendingDeviceCount)
.installedDeviceCount(installedDeviceCount)
.failedDeviceCount(failedDeviceCount)
.build();
}
private static Map<OsUpdateOnDeviceStatus, Long> toDeviceCountByStatus(
List<OsUpdateOnDeviceStatusCount> statusCounts) {
Map<OsUpdateOnDeviceStatus, Long> deviceCountByStatus = new EnumMap<>(OsUpdateOnDeviceStatus.class);
statusCounts.forEach(
statusCount -> deviceCountByStatus.put(statusCount.getStatus(), statusCount.getDeviceCount()));
return deviceCountByStatus;
}
// The updates that have no value are counted under the null key.
private static Map<String, Long> toUpdateCountByValue(List<OsUpdateOptionCount> optionCounts) {
Map<String, Long> updateCountByValue = new HashMap<>();
optionCounts.forEach(
optionCount -> updateCountByValue.put(optionCount.getValue(), optionCount.getUpdateCount()));
return updateCountByValue;
}
private static OsUpdateFilterOption toFilterOption(String value, Map<String, Long> updateCountByValue) {
Long updateCount = updateCountByValue.get(value);
return toFilterOption(value, updateCount);
}
private static OsUpdateFilterOption toFilterOption(String value, Long updateCount) {
int count = updateCount == null ? 0 : Math.toIntExact(updateCount);
return OsUpdateFilterOption.builder()
.value(value)
.label(value)
.count(count)
.build();
}
// Null = the update names no KB article (a driver, for one): there is nothing to link to.
private static String toReleaseNotesUrl(List<String> kbArticleIds) {
if (kbArticleIds.isEmpty()) {
return null;
}
String firstKbArticleId = kbArticleIds.get(0).trim();
Matcher kbArticleId = KB_ARTICLE_ID.matcher(firstKbArticleId);
if (!kbArticleId.matches()) {
return null;
}
String kbNumber = kbArticleId.group(1);
return KB_ARTICLE_URL + kbNumber;
}
}