Skip to content

Commit 6eaac1b

Browse files
committed
Merge branch 'patch-regex-fix' into 'main'
For cache addPatch, corrected the format test for patch IDs with the architecture suffix. See merge request weblogic-cloud/weblogic-image-tool!489
2 parents 0ac4c5a + 6878d00 commit 6eaac1b

File tree

6 files changed

+55
-36
lines changed

6 files changed

+55
-36
lines changed

imagetool/src/main/java/com/oracle/weblogic/imagetool/cli/cache/AddPatchEntry.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public String getKey() {
2525
public CommandResponse call() throws Exception {
2626
try {
2727
if (patchId != null && !patchId.isEmpty()) {
28-
Utils.validatePatchIds(Collections.singletonList(patchId), true);
28+
Utils.validatePatchIds(Collections.singletonList(patchId), false);
2929
return addToCache();
3030
} else {
3131
return CommandResponse.error("IMG-0076", "--patchId");

imagetool/src/main/java/com/oracle/weblogic/imagetool/cli/menu/CommonPatchingOptions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ void initializeOptions() throws IOException, InvalidCredentialException, Invalid
6363
throw new InvalidCredentialException();
6464
}
6565

66-
Utils.validatePatchIds(patches, false);
66+
Utils.validatePatchIds(patches, true);
6767
}
6868
}
6969

imagetool/src/main/java/com/oracle/weblogic/imagetool/cli/menu/UpdateImage.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,7 @@ public CommandResponse call() throws Exception {
121121
if (userId == null) {
122122
logger.info("IMG-0009");
123123
} else {
124-
if (!Utils.validatePatchIds(patches, false)) {
125-
return CommandResponse.error("Patch ID validation failed");
126-
}
124+
Utils.validatePatchIds(patches, true);
127125

128126
String oraclePatches = baseImageProperties.getProperty("oraclePatches", null);
129127
if (oraclePatches != null) {

imagetool/src/main/java/com/oracle/weblogic/imagetool/util/Constants.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ public final class Constants {
2323
public static final String DELETE_ALL_FOR_SURE = "deleteAll4Sure";
2424
public static final String HTTP = "http";
2525
public static final String HTTPS = "https";
26-
public static final String PATCH_ID_REGEX = "^(\\d{8})(?:[_][0-9][0-9](?:\\.[0-9]){3,8}\\.(\\d+))?";
27-
public static final String RIGID_PATCH_ID_REGEX = "^(\\d{8})[_][0-9][0-9](?:\\.[0-9]){3,8}\\.(\\d+)";
2826
public static final String BUSYBOX = "busybox";
2927
public static final List<String> BUSYBOX_OS_IDS = Collections.unmodifiableList(Arrays.asList("bb", "alpine"));
3028
public static final String ORACLE_LINUX = "ghcr.io/oracle/oraclelinux:8-slim";

imagetool/src/main/java/com/oracle/weblogic/imagetool/util/Utils.java

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import com.oracle.weblogic.imagetool.logging.LoggingFacade;
4444
import com.oracle.weblogic.imagetool.logging.LoggingFactory;
4545
import org.jetbrains.annotations.NonNls;
46+
import org.jetbrains.annotations.NotNull;
4647
import org.jetbrains.annotations.PropertyKey;
4748

4849
public class Utils {
@@ -582,38 +583,36 @@ public static String getBuildWorkingDir() throws IOException {
582583
return workingDir;
583584
}
584585

586+
private static void validatePatchIds(List<String> patches, Pattern pattern,
587+
String example) throws InvalidPatchIdFormatException {
588+
for (String patchId: patches) {
589+
Matcher matcher = pattern.matcher(patchId);
590+
if (!matcher.matches()) {
591+
throw new InvalidPatchIdFormatException(patchId, example);
592+
}
593+
}
594+
}
595+
585596
/**
586-
* validatePatchIds validate the format of the patch ids.
597+
* Validate the format of the patch ID.
598+
* Simple ID: 12345678
599+
* Full ID: {bug number}_{version}_{architecture}
587600
*
588-
* @param patches list of patch ids
589-
* @return true if all patch IDs are valid , false otherwise.
601+
* @param patches List of patch IDs to validate against the required format.
602+
* @param allowSimpleId Allow simple bug number without version or architecture
603+
* @throws InvalidPatchIdFormatException if any of the provide patch IDs are of invalid format
590604
*/
591-
592-
public static boolean validatePatchIds(List<String> patches, boolean rigid) throws InvalidPatchIdFormatException {
593-
Pattern patchIdPattern;
594-
if (rigid) {
595-
patchIdPattern = Pattern.compile(Constants.RIGID_PATCH_ID_REGEX);
605+
public static void validatePatchIds(@NotNull List<String> patches,
606+
boolean allowSimpleId) throws InvalidPatchIdFormatException {
607+
if (allowSimpleId) {
608+
validatePatchIds(patches,
609+
Pattern.compile("^(\\d{8,9})(?:_\\d\\d(?:\\.\\d){3,8}\\.(\\d+)(_.*)?)?"),
610+
"12345678[_12.2.1.3.0[_arm64]]");
596611
} else {
597-
patchIdPattern = Pattern.compile(Constants.PATCH_ID_REGEX);
598-
}
599-
if (patches != null && !patches.isEmpty()) {
600-
for (String patchId : patches) {
601-
logger.finer("pattern matching patchId: {0}", patchId);
602-
Matcher matcher = patchIdPattern.matcher(patchId);
603-
if (!matcher.matches()) {
604-
String errorFormat;
605-
if (rigid) {
606-
errorFormat = "12345678_12.2.1.3.0";
607-
} else {
608-
errorFormat = "12345678[_12.2.1.3.0]";
609-
}
610-
611-
throw new InvalidPatchIdFormatException(patchId, errorFormat);
612-
}
613-
}
612+
validatePatchIds(patches,
613+
Pattern.compile("^(\\d{8,9})_\\d\\d(?:\\.\\d){3,8}\\.(\\d+)(_.*)?"),
614+
"12345678_12.2.1.3.0[_arm64]");
614615
}
615-
616-
return true;
617616
}
618617

619618
/**

imagetool/src/test/java/com/oracle/weblogic/imagetool/cli/cache/AddPatchEntryTest.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,19 @@
55

66
import java.io.PrintWriter;
77
import java.io.StringWriter;
8+
import java.util.Arrays;
89

910
import com.oracle.weblogic.imagetool.api.model.CommandResponse;
11+
import com.oracle.weblogic.imagetool.util.InvalidPatchIdFormatException;
12+
import com.oracle.weblogic.imagetool.util.Utils;
1013
import org.junit.jupiter.api.Tag;
1114
import org.junit.jupiter.api.Test;
1215
import picocli.CommandLine;
1316

1417
import static org.junit.jupiter.api.Assertions.assertEquals;
1518
import static org.junit.jupiter.api.Assertions.assertNotNull;
1619
import static org.junit.jupiter.api.Assertions.assertNull;
20+
import static org.junit.jupiter.api.Assertions.assertThrows;
1721

1822
@Tag("unit")
1923
class AddPatchEntryTest {
@@ -33,15 +37,15 @@ void testMissingParameters() {
3337
// missing valid parameters should generate USAGE output
3438
int exitCode = cmd.execute("-x", "-y=123");
3539
CommandResponse result = cmd.getExecutionResult();
36-
assertNull(result, "Should not have a response, picoli should intercept usage error");
40+
assertNull(result, "Should not have a response, picocli should intercept usage error");
3741
assertEquals(CommandLine.ExitCode.USAGE, exitCode);
3842
}
3943

4044
@Test
4145
void testInvalidFileParameter() {
4246
CommandLine cmd = getCommand();
4347
// invalid file (file does not exist), should generate an error response
44-
int exitCode = cmd.execute("--patchId=12345678_12.2.1.3.0", "--path=/here/there");
48+
cmd.execute("--patchId=12345678_12.2.1.3.0", "--path=/here/there");
4549
CommandResponse result = cmd.getExecutionResult();
4650
assertNotNull(result, "Response missing from call to addPatch");
4751
assertEquals(1, result.getStatus());
@@ -56,4 +60,24 @@ void testInvalidPatchId() {
5660
assertNotNull(result, "Response missing from call to addPatch");
5761
assertEquals(1, result.getStatus());
5862
}
63+
64+
@Test
65+
void validPatchIds() throws InvalidPatchIdFormatException {
66+
String[] patchIds = {"12345678_12.2.1.4.0", "12345678_12.2.1.4.241001", "12345678_12.2.1.4.0_arm64"};
67+
Utils.validatePatchIds(Arrays.asList(patchIds), false);
68+
}
69+
70+
@Test
71+
void invalidPatchIds1() {
72+
String[] patchIds = {"12345678_12.2.1.4.0", "12345678", "12345678_12.2.1.4.0_arm64"};
73+
assertThrows(InvalidPatchIdFormatException.class,
74+
() -> Utils.validatePatchIds(Arrays.asList(patchIds), false));
75+
}
76+
77+
@Test
78+
void invalidPatchIds2() {
79+
String[] patchIds = {"12345678_12.2.1.4.0", "12345678_arm64", "12345678_12.2.1.4.0_arm64"};
80+
assertThrows(InvalidPatchIdFormatException.class,
81+
() -> Utils.validatePatchIds(Arrays.asList(patchIds), false));
82+
}
5983
}

0 commit comments

Comments
 (0)