Skip to content

Commit 9a86e34

Browse files
committed
devonfw#103: rephrase interaction, mapUtil, LICENCE
also SecurityRiskInteraction returns configured version and latest version when possible. conversion between cpe and ulr version more rebust by using map and inverse function where map fails. Added asciidoc
1 parent 1b9224b commit 9a86e34

File tree

10 files changed

+441
-100
lines changed

10 files changed

+441
-100
lines changed

cli/src/main/java/com/devonfw/tools/ide/tool/ToolCommandlet.java

+24-39
Original file line numberDiff line numberDiff line change
@@ -173,16 +173,6 @@ public boolean install(boolean silent) {
173173
return doInstall(silent);
174174
}
175175

176-
protected String securityRiskInteractionQuestion(String question, String... options) {
177-
178-
question += "Do you want to";
179-
for (int i = 0; i < options.length - 1; i++) {
180-
options[i] += " or";
181-
}
182-
options[options.length - 1] += "?";
183-
return this.context.question(question, options);
184-
}
185-
186176
/**
187177
* Checks if the given {@link VersionIdentifier} has a matching security warning in the {@link UrlSecurityJsonFile}.
188178
*
@@ -193,16 +183,10 @@ protected String securityRiskInteractionQuestion(String question, String... opti
193183
*/
194184
protected VersionIdentifier securityRiskInteraction(VersionIdentifier configuredVersion) {
195185

196-
// TODO maybe instead of returning current return configuredVersion if the users chooses "stay"
197-
198-
// TODO webpage:\nhttps://github.com/devonfw/ide/blob/master/documentation/vulnerabilities.asciidoc\n\n";
199-
200186
UrlSecurityJsonFile securityFile = this.context.getUrls().getEdition(this.tool, this.getEdition())
201187
.getSecurityJsonFile();
202188

203189
VersionIdentifier current = this.context.getUrls().getVersion(this.tool, this.getEdition(), configuredVersion);
204-
// TODO oder doch eher sowas wie VersionIdentifier resolvedVersion = toolRepository.resolveVersion(this.tool,
205-
// edition, selectedVersion); sollte immer das selbe ergeben
206190

207191
if (!securityFile.contains(current, true, this.context)) {
208192
return configuredVersion;
@@ -212,7 +196,6 @@ protected VersionIdentifier securityRiskInteraction(VersionIdentifier configured
212196
VersionIdentifier latest = allVersions.get(0);
213197

214198
int currentVersionIndex = allVersions.indexOf(current);
215-
216199
VersionIdentifier nextSafe = null;
217200
for (int i = currentVersionIndex - 1; i >= 0; i--) {
218201
if (!securityFile.contains(allVersions.get(i), true, this.context)) {
@@ -232,10 +215,12 @@ protected VersionIdentifier securityRiskInteraction(VersionIdentifier configured
232215
String currentIsUnsafe = "Currently, version " + current + " of " + this.getName() + " is selected, "
233216
+ "which is has one or more vulnerabilities:\n\n" + cves + "\n\n(See also " + securityFile.getPath() + ")\n\n";
234217

235-
String stay = "stay with the current unsafe version (" + current + ")";
236-
String installLatestSafe = "install the latest safe version (" + latestSafe + ")";
237-
String installSafeLatest = "install the (safe) latest version (" + latest + ")";
238-
String installNextSafe = "install the next safe version (" + nextSafe + ")";
218+
String ask = "Which version do you want to install?";
219+
220+
String stay = "Stay with the current unsafe version (" + current + ").";
221+
String installLatestSafe = "Install the latest safe version (" + latestSafe + ").";
222+
String installSafeLatest = "Install the (safe) latest version (" + latest + ").";
223+
String installNextSafe = "Install the next safe version (" + nextSafe + ").";
239224
// I don't need to offer "install latest which is unsafe" as option since the user can set to the latest and choose
240225
// "stay"
241226

@@ -245,38 +230,38 @@ protected VersionIdentifier securityRiskInteraction(VersionIdentifier configured
245230
}
246231

247232
if (current.equals(latest)) {
248-
String answer = securityRiskInteractionQuestion(currentIsUnsafe + "There are no updates available.", stay,
233+
String answer = this.context.question(currentIsUnsafe + "There are no updates available. " + ask, stay,
249234
installLatestSafe);
250-
return answer.startsWith(stay) ? current : latestSafe;
235+
return answer.equals(stay) ? configuredVersion : latestSafe;
251236

252-
} else if (nextSafe == null) {
253-
String answer = securityRiskInteractionQuestion(currentIsUnsafe + " All newer versions are also not safe.", stay,
237+
} else if (nextSafe == null) { // install an older version that is safe or stay with the current unsafe version
238+
String answer = this.context.question(currentIsUnsafe + " All newer versions are also not safe. " + ask, stay,
254239
installLatestSafe);
255-
return answer.startsWith(stay) ? current : latestSafe;
240+
return answer.equals(stay) ? configuredVersion : latestSafe;
256241

257242
} else if (nextSafe.equals(latest)) {
258-
String answer = securityRiskInteractionQuestion(
259-
currentIsUnsafe + " Of the newer versions, only the latest is safe.", stay, installSafeLatest);
260-
return answer.startsWith(stay) ? current : latestSafe;
243+
String answer = this.context.question(currentIsUnsafe + " Of the newer versions, only the latest is safe. " + ask,
244+
stay, installSafeLatest);
245+
return answer.equals(stay) ? configuredVersion : VersionIdentifier.LATEST;
261246

262247
} else if (nextSafe.equals(latestSafe)) {
263-
String answer = securityRiskInteractionQuestion(currentIsUnsafe + " Of the newer versions, only the version "
264-
+ nextSafe + " is safe, Which is not the latest.", stay, "Install the safe version (" + nextSafe + ")");
265-
return answer.startsWith(stay) ? current : nextSafe;
248+
String answer = this.context.question(
249+
currentIsUnsafe + " Of the newer versions, only the version " + nextSafe
250+
+ " is safe, which is however not the latest." + ask,
251+
stay, "Install the safe version (" + nextSafe + ")");
252+
return answer.equals(stay) ? configuredVersion : nextSafe;
266253

267254
} else {
268255
if (latestSafe.equals(latest)) {
269-
String answer = securityRiskInteractionQuestion(currentIsUnsafe, stay, installNextSafe, installSafeLatest);
270-
return answer.startsWith(stay) ? current : answer.startsWith(installNextSafe) ? nextSafe : latestSafe;
256+
String answer = this.context.question(currentIsUnsafe + ask, stay, installNextSafe, installSafeLatest);
257+
return answer.equals(stay) ? configuredVersion
258+
: answer.equals(installNextSafe) ? nextSafe : VersionIdentifier.LATEST;
271259

272260
} else {
273-
String answer = securityRiskInteractionQuestion(currentIsUnsafe, stay, installNextSafe, installLatestSafe);
274-
return answer.startsWith(stay) ? current : answer.startsWith(installNextSafe) ? nextSafe : latestSafe;
261+
String answer = this.context.question(currentIsUnsafe + ask, stay, installNextSafe, installLatestSafe);
262+
return answer.equals(stay) ? configuredVersion : answer.equals(installNextSafe) ? nextSafe : latestSafe;
275263
}
276264
}
277-
278-
// VersionIdentifier chosenVersion = securityRiskInteraction(configuredVersion);
279-
// setVersion(chosenVersion, silent);
280265
}
281266

282267
/**

cli/src/main/java/com/devonfw/tools/ide/tool/terraform/TerraformUrlUpdater.java

-3
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,4 @@ public String getCpeProduct() {
5353

5454
return "terraform";
5555
}
56-
// add matche cpe the the warning and print it in ide, to to wether the vul maybe oinly applies to the enterprise
57-
// edition
58-
// or can I filter this enterpsrise version by adding overriding the eidtion methiod with the normal edition string?
5956
}

cli/src/main/java/com/devonfw/tools/ide/url/model/file/UrlSecurityJsonFile.java

+7-5
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@
2828
*/
2929
public class UrlSecurityJsonFile extends AbstractUrlFile<UrlEdition> {
3030

31-
/***
31+
/**
3232
* A simple container with the information about a security warning.
33-
*
3433
*/
3534
public static class UrlSecurityWarning {
3635

@@ -62,6 +61,8 @@ public UrlSecurityWarning() {
6261
*
6362
* @param versionRange the version range, specifying the versions of the tool to which the security risk applies.
6463
* @param matchedCpe the matched CPE.
64+
* @param interval the interval of vulnerability that was used to determine the {@link VersionRange}. This is used
65+
* to check if the mapping from CPE version to UrlVersion was correct.
6566
* @param severity the severity of the security risk.
6667
* @param severityVersion Indicating from which version the {@code severity} was obtained. As of December 2023, this
6768
* is either v2 or v3.
@@ -258,12 +259,13 @@ public boolean addSecurityWarning(VersionRange versionRange) {
258259
return added;
259260
}
260261

261-
/***
262+
/**
262263
* Adds a new security warning to the security json file.
263264
*
264265
* @param versionRange the version range, specifying the versions of the tool to which the security risk applies.
265266
* @param matchedCpe the matched CPE.
266-
* @param interval the interval of vulnerability that was used to determine the version range.
267+
* @param interval the interval of vulnerability that was used to determine the {@link VersionRange}. This is used to
268+
* check if the mapping from CPE version to UrlVersion was correct.
267269
* @param severity the severity of the security risk.
268270
* @param severityVersion Indicating from which version the {@code severity} was obtained. As of December 2023, this
269271
* is either v2 or v3.
@@ -283,7 +285,7 @@ public boolean addSecurityWarning(VersionRange versionRange, String matchedCpe,
283285
return added;
284286
}
285287

286-
/***
288+
/**
287289
* For a given version, returns whether there is a security risk by locking at the warnings in the security json file.
288290
*
289291
* @param version the version to check for security risks.

cli/src/main/java/com/devonfw/tools/ide/url/updater/AbstractUrlUpdater.java

+10-5
Original file line numberDiff line numberDiff line change
@@ -97,24 +97,23 @@ protected final String getToolWithEdition() {
9797
return tool + "/" + edition;
9898
}
9999

100-
/***
101-
*
100+
/**
102101
* @return the vendor of the tool as specified in the CPE (Common Platform Enumeration)
103102
*/
104103
public String getCpeVendor() {
105104

106105
return null;
107106
}
108107

109-
/***
108+
/**
110109
* @return the product name of the tool as specified in the CPE (Common Platform Enumeration)
111110
*/
112111
public String getCpeProduct() {
113112

114113
return null;
115114
}
116115

117-
/***
116+
/**
118117
* @return the edition of the tool as specified in the CPE (Common Platform Enumeration)
119118
*/
120119
public String getCpeEdition() {
@@ -132,7 +131,13 @@ public String mapUrlVersionToCpeVersion(String version) {
132131
}
133132

134133
/**
135-
* @return maps the cpe version to the version as specified in the CPE (Common Platform Enumeration).
134+
* This method is only used as fallback if the passed version is not in the image of
135+
* {@link #mapUrlVersionToCpeVersion(String)}. This doesn't have to be inverse of
136+
* {@link #mapUrlVersionToCpeVersion(String)}. It must only be sufficient to get the correct VersionRange from the
137+
* matched vulnerable software.
138+
*
139+
* @return maps the version as specified in the CPE (Common Platform Enumeration) to the version as specified by the
140+
* directory name in the url repository
136141
*/
137142
public String mapCpeVersionToUrlVersion(String version) {
138143

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.devonfw.tools.ide.util;
2+
3+
import java.util.HashMap;
4+
import java.util.Iterator;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
/**
9+
* Utility class for operations on maps.
10+
*/
11+
public class MapUtil {
12+
13+
/**
14+
* Creates a {@link HashMap} with the given {@code keys} and {@code values} which are passed as {@link List lists}.
15+
* The map is populated by iterating through both lists simultaneously until one of the list is exhausted.
16+
*/
17+
public static <K, V> Map<K, V> createMapWithLists(List<K> keys, List<V> values) {
18+
19+
Map<K, V> resultMap = new HashMap<>();
20+
21+
// Create iterators for both lists
22+
Iterator<K> keysIterator = keys.iterator();
23+
Iterator<V> valuesIterator = values.iterator();
24+
25+
// Iterate through both iterators simultaneously
26+
while (keysIterator.hasNext() && valuesIterator.hasNext()) {
27+
K key = keysIterator.next();
28+
V value = valuesIterator.next();
29+
resultMap.put(key, value);
30+
}
31+
32+
return resultMap;
33+
}
34+
}

cli/src/test/java/com/devonfw/tools/ide/tool/ToolCommandletTest.java

+17-19
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
import com.devonfw.tools.ide.version.VersionIdentifier;
1313
import com.devonfw.tools.ide.version.VersionRange;
1414

15-
/***
15+
/**
1616
* Test of {@link ToolCommandlet}.
1717
*/
1818
public class ToolCommandletTest extends AbstractIdeContextTest {
1919

20-
/***
20+
/**
2121
* Test of {@link ToolCommandlet#securityRiskInteraction(VersionIdentifier)} where no safe version is available. But
2222
* there is a warning that affects all versions. This warning is then ignored, but the other warnings are considered.
2323
*/
@@ -26,7 +26,7 @@ public void testSecurityRiskInteractionAllVersionAffectedBySingleWarning() {
2626

2727
// arrange
2828
Class<? extends ToolCommandlet> dummyTool = Azure.class;
29-
String[] answers = {"1", "2", "3"};
29+
String[] answers = { "1", "2", "3" };
3030
IdeContext context = getContextForSecurityJsonTests(dummyTool, answers);
3131
ToolCommandlet tool = context.getCommandletManager().getCommandlet(dummyTool);
3232
UrlSecurityJsonFile securityFile = context.getUrls().getEdition(tool.getName(), tool.getEdition())
@@ -44,11 +44,10 @@ public void testSecurityRiskInteractionAllVersionAffectedBySingleWarning() {
4444
// answer to the interaction is 2
4545
assertThat(tool.securityRiskInteraction(VersionIdentifier.of("2"))).isEqualTo(VersionIdentifier.of("6"));
4646
// answer to the interaction is 3
47-
assertThat(tool.securityRiskInteraction(VersionIdentifier.of("2"))).isEqualTo(VersionIdentifier.of("9"));
47+
assertThat(tool.securityRiskInteraction(VersionIdentifier.of("2"))).isEqualTo(VersionIdentifier.of("*"));
4848
}
4949

50-
51-
/***
50+
/**
5251
* Test of {@link ToolCommandlet#securityRiskInteraction(VersionIdentifier)} where no safe version is available. Only
5352
* the warnings all considered together cover all versions and there is no single warning that affects all versions.
5453
*/
@@ -70,7 +69,8 @@ public void testSecurityRiskInteractionAllVersionAffectedByMultipleWarning() {
7069
assertThat(tool.securityRiskInteraction(VersionIdentifier.of("9"))).isEqualTo(VersionIdentifier.of("9"));
7170
assertThat(tool.securityRiskInteraction(VersionIdentifier.of("*"))).isEqualTo(VersionIdentifier.of("*"));
7271
}
73-
/***
72+
73+
/**
7474
* Test of {@link ToolCommandlet#securityRiskInteraction(VersionIdentifier)} where the set version is the latest but
7575
* vulnerable.
7676
*/
@@ -89,12 +89,12 @@ public void testSecurityRiskInteractionCurrentIsLatest() {
8989

9090
// act & assert
9191
// answer to the interaction is 1
92-
assertThat(tool.securityRiskInteraction(VersionIdentifier.of("*"))).isEqualTo(VersionIdentifier.of("9"));
92+
assertThat(tool.securityRiskInteraction(VersionIdentifier.of("*"))).isEqualTo(VersionIdentifier.of("*"));
9393
// answer to the interaction is 2
9494
assertThat(tool.securityRiskInteraction(VersionIdentifier.of("*"))).isEqualTo(VersionIdentifier.of("6"));
9595
}
9696

97-
/***
97+
/**
9898
* Test of {@link ToolCommandlet#securityRiskInteraction(VersionIdentifier)} where there are no newer versions that
9999
* are safe, but there is a previous version that is safe.
100100
*/
@@ -119,7 +119,7 @@ public void testSecurityRiskInteractionNextSafeIsNull() {
119119
assertThat(tool.securityRiskInteraction(VersionIdentifier.of("6"))).isEqualTo(VersionIdentifier.of("5"));
120120
}
121121

122-
/***
122+
/**
123123
* Test of {@link ToolCommandlet#securityRiskInteraction(VersionIdentifier)} where the next safe version is also the
124124
* latest.
125125
*/
@@ -141,10 +141,10 @@ public void testSecurityRiskInteractionNextSafeIsLatest() {
141141
// answer to the interaction is 1
142142
assertThat(tool.securityRiskInteraction(VersionIdentifier.of("7"))).isEqualTo(VersionIdentifier.of("7"));
143143
// answer to the interaction is 2
144-
assertThat(tool.securityRiskInteraction(VersionIdentifier.of("7"))).isEqualTo(VersionIdentifier.of("9"));
144+
assertThat(tool.securityRiskInteraction(VersionIdentifier.of("7"))).isEqualTo(VersionIdentifier.of("*"));
145145
}
146146

147-
/***
147+
/**
148148
* Test of {@link ToolCommandlet#securityRiskInteraction(VersionIdentifier)} where the next safe version is also the
149149
* latest safe version, and the overall latest version is not safe.
150150
*/
@@ -169,7 +169,7 @@ public void testSecurityRiskInteractionNextSafeIsLatestSafe() {
169169
assertThat(tool.securityRiskInteraction(VersionIdentifier.of("5"))).isEqualTo(VersionIdentifier.of("7"));
170170
}
171171

172-
/***
172+
/**
173173
* Test of {@link ToolCommandlet#securityRiskInteraction(VersionIdentifier)} where the next safe version differs from
174174
* the latest safe, which is also the overall latest version.
175175
*/
@@ -193,10 +193,10 @@ public void testSecurityRiskInteractionLatestSafeDiffersFromNextSafeButIsLatest(
193193
// answer to the interaction is 2
194194
assertThat(tool.securityRiskInteraction(VersionIdentifier.of("5"))).isEqualTo(VersionIdentifier.of("7"));
195195
// answer to the interaction is 3
196-
assertThat(tool.securityRiskInteraction(VersionIdentifier.of("5"))).isEqualTo(VersionIdentifier.of("9"));
196+
assertThat(tool.securityRiskInteraction(VersionIdentifier.of("5"))).isEqualTo(VersionIdentifier.of("*"));
197197
}
198198

199-
/***
199+
/**
200200
* Test of {@link ToolCommandlet#securityRiskInteraction(VersionIdentifier)} where the next safe version differs from
201201
* the latest safe, and the overall latest version is not safe.
202202
*/
@@ -223,7 +223,7 @@ public void testSecurityRiskInteractionLatestSafeDiffersFromNextSafeAndLatest()
223223
assertThat(tool.securityRiskInteraction(VersionIdentifier.of("3"))).isEqualTo(VersionIdentifier.of("7"));
224224
}
225225

226-
/***
226+
/**
227227
* Test of {@link ToolCommandlet#securityRiskInteraction(VersionIdentifier)} where set version is safe.
228228
*/
229229
@Test
@@ -243,9 +243,7 @@ public void testSecurityRiskInteractionCurrentVersionIsSafe() {
243243
assertThat(tool.securityRiskInteraction(VersionIdentifier.of("9"))).isEqualTo(VersionIdentifier.of("9"));
244244
}
245245

246-
247-
248-
/***
246+
/**
249247
* Creates the context and data for the tests of {@link ToolCommandlet#securityRiskInteraction(VersionIdentifier)}.
250248
*
251249
* @param dummyTool the dummy tool to be used for the tests. The

0 commit comments

Comments
 (0)