diff --git a/imagetool/src/main/java/com/oracle/weblogic/imagetool/cli/cache/AddPatchEntry.java b/imagetool/src/main/java/com/oracle/weblogic/imagetool/cli/cache/AddPatchEntry.java index ff882d4b..5445696e 100644 --- a/imagetool/src/main/java/com/oracle/weblogic/imagetool/cli/cache/AddPatchEntry.java +++ b/imagetool/src/main/java/com/oracle/weblogic/imagetool/cli/cache/AddPatchEntry.java @@ -25,7 +25,7 @@ public String getKey() { public CommandResponse call() throws Exception { try { if (patchId != null && !patchId.isEmpty()) { - Utils.validatePatchIds(Collections.singletonList(patchId), true); + Utils.validatePatchIds(Collections.singletonList(patchId), false); return addToCache(); } else { return CommandResponse.error("IMG-0076", "--patchId"); diff --git a/imagetool/src/main/java/com/oracle/weblogic/imagetool/cli/menu/CommonPatchingOptions.java b/imagetool/src/main/java/com/oracle/weblogic/imagetool/cli/menu/CommonPatchingOptions.java index 775ab06d..c81909e2 100644 --- a/imagetool/src/main/java/com/oracle/weblogic/imagetool/cli/menu/CommonPatchingOptions.java +++ b/imagetool/src/main/java/com/oracle/weblogic/imagetool/cli/menu/CommonPatchingOptions.java @@ -63,7 +63,7 @@ void initializeOptions() throws IOException, InvalidCredentialException, Invalid throw new InvalidCredentialException(); } - Utils.validatePatchIds(patches, false); + Utils.validatePatchIds(patches, true); } } diff --git a/imagetool/src/main/java/com/oracle/weblogic/imagetool/cli/menu/UpdateImage.java b/imagetool/src/main/java/com/oracle/weblogic/imagetool/cli/menu/UpdateImage.java index c79c75a0..8ad02fb1 100644 --- a/imagetool/src/main/java/com/oracle/weblogic/imagetool/cli/menu/UpdateImage.java +++ b/imagetool/src/main/java/com/oracle/weblogic/imagetool/cli/menu/UpdateImage.java @@ -121,9 +121,7 @@ public CommandResponse call() throws Exception { if (userId == null) { logger.info("IMG-0009"); } else { - if (!Utils.validatePatchIds(patches, false)) { - return CommandResponse.error("Patch ID validation failed"); - } + Utils.validatePatchIds(patches, true); String oraclePatches = baseImageProperties.getProperty("oraclePatches", null); if (oraclePatches != null) { diff --git a/imagetool/src/main/java/com/oracle/weblogic/imagetool/util/Constants.java b/imagetool/src/main/java/com/oracle/weblogic/imagetool/util/Constants.java index 81e57719..dd0245f6 100644 --- a/imagetool/src/main/java/com/oracle/weblogic/imagetool/util/Constants.java +++ b/imagetool/src/main/java/com/oracle/weblogic/imagetool/util/Constants.java @@ -23,8 +23,6 @@ public final class Constants { public static final String DELETE_ALL_FOR_SURE = "deleteAll4Sure"; public static final String HTTP = "http"; public static final String HTTPS = "https"; - public static final String PATCH_ID_REGEX = "^(\\d{8})(?:[_][0-9][0-9](?:\\.[0-9]){3,8}\\.(\\d+))?"; - public static final String RIGID_PATCH_ID_REGEX = "^(\\d{8})[_][0-9][0-9](?:\\.[0-9]){3,8}\\.(\\d+)"; public static final String BUSYBOX = "busybox"; public static final List BUSYBOX_OS_IDS = Collections.unmodifiableList(Arrays.asList("bb", "alpine")); public static final String ORACLE_LINUX = "ghcr.io/oracle/oraclelinux:8-slim"; diff --git a/imagetool/src/main/java/com/oracle/weblogic/imagetool/util/Utils.java b/imagetool/src/main/java/com/oracle/weblogic/imagetool/util/Utils.java index 5aef7cd3..0c752f5d 100644 --- a/imagetool/src/main/java/com/oracle/weblogic/imagetool/util/Utils.java +++ b/imagetool/src/main/java/com/oracle/weblogic/imagetool/util/Utils.java @@ -43,6 +43,7 @@ import com.oracle.weblogic.imagetool.logging.LoggingFacade; import com.oracle.weblogic.imagetool.logging.LoggingFactory; import org.jetbrains.annotations.NonNls; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.PropertyKey; public class Utils { @@ -582,38 +583,36 @@ public static String getBuildWorkingDir() throws IOException { return workingDir; } + private static void validatePatchIds(List patches, Pattern pattern, + String example) throws InvalidPatchIdFormatException { + for (String patchId: patches) { + Matcher matcher = pattern.matcher(patchId); + if (!matcher.matches()) { + throw new InvalidPatchIdFormatException(patchId, example); + } + } + } + /** - * validatePatchIds validate the format of the patch ids. + * Validate the format of the patch ID. + * Simple ID: 12345678 + * Full ID: {bug number}_{version}_{architecture} * - * @param patches list of patch ids - * @return true if all patch IDs are valid , false otherwise. + * @param patches List of patch IDs to validate against the required format. + * @param allowSimpleId Allow simple bug number without version or architecture + * @throws InvalidPatchIdFormatException if any of the provide patch IDs are of invalid format */ - - public static boolean validatePatchIds(List patches, boolean rigid) throws InvalidPatchIdFormatException { - Pattern patchIdPattern; - if (rigid) { - patchIdPattern = Pattern.compile(Constants.RIGID_PATCH_ID_REGEX); + public static void validatePatchIds(@NotNull List patches, + boolean allowSimpleId) throws InvalidPatchIdFormatException { + if (allowSimpleId) { + validatePatchIds(patches, + Pattern.compile("^(\\d{8,9})(?:_\\d\\d(?:\\.\\d){3,8}\\.(\\d+)(_.*)?)?"), + "12345678[_12.2.1.3.0[_arm64]]"); } else { - patchIdPattern = Pattern.compile(Constants.PATCH_ID_REGEX); - } - if (patches != null && !patches.isEmpty()) { - for (String patchId : patches) { - logger.finer("pattern matching patchId: {0}", patchId); - Matcher matcher = patchIdPattern.matcher(patchId); - if (!matcher.matches()) { - String errorFormat; - if (rigid) { - errorFormat = "12345678_12.2.1.3.0"; - } else { - errorFormat = "12345678[_12.2.1.3.0]"; - } - - throw new InvalidPatchIdFormatException(patchId, errorFormat); - } - } + validatePatchIds(patches, + Pattern.compile("^(\\d{8,9})_\\d\\d(?:\\.\\d){3,8}\\.(\\d+)(_.*)?"), + "12345678_12.2.1.3.0[_arm64]"); } - - return true; } /** diff --git a/imagetool/src/test/java/com/oracle/weblogic/imagetool/cli/cache/AddPatchEntryTest.java b/imagetool/src/test/java/com/oracle/weblogic/imagetool/cli/cache/AddPatchEntryTest.java index 107bbf27..e0538b40 100644 --- a/imagetool/src/test/java/com/oracle/weblogic/imagetool/cli/cache/AddPatchEntryTest.java +++ b/imagetool/src/test/java/com/oracle/weblogic/imagetool/cli/cache/AddPatchEntryTest.java @@ -5,8 +5,11 @@ import java.io.PrintWriter; import java.io.StringWriter; +import java.util.Arrays; import com.oracle.weblogic.imagetool.api.model.CommandResponse; +import com.oracle.weblogic.imagetool.util.InvalidPatchIdFormatException; +import com.oracle.weblogic.imagetool.util.Utils; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; import picocli.CommandLine; @@ -14,6 +17,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; @Tag("unit") class AddPatchEntryTest { @@ -33,7 +37,7 @@ void testMissingParameters() { // missing valid parameters should generate USAGE output int exitCode = cmd.execute("-x", "-y=123"); CommandResponse result = cmd.getExecutionResult(); - assertNull(result, "Should not have a response, picoli should intercept usage error"); + assertNull(result, "Should not have a response, picocli should intercept usage error"); assertEquals(CommandLine.ExitCode.USAGE, exitCode); } @@ -41,7 +45,7 @@ void testMissingParameters() { void testInvalidFileParameter() { CommandLine cmd = getCommand(); // invalid file (file does not exist), should generate an error response - int exitCode = cmd.execute("--patchId=12345678_12.2.1.3.0", "--path=/here/there"); + cmd.execute("--patchId=12345678_12.2.1.3.0", "--path=/here/there"); CommandResponse result = cmd.getExecutionResult(); assertNotNull(result, "Response missing from call to addPatch"); assertEquals(1, result.getStatus()); @@ -56,4 +60,24 @@ void testInvalidPatchId() { assertNotNull(result, "Response missing from call to addPatch"); assertEquals(1, result.getStatus()); } + + @Test + void validPatchIds() throws InvalidPatchIdFormatException { + String[] patchIds = {"12345678_12.2.1.4.0", "12345678_12.2.1.4.241001", "12345678_12.2.1.4.0_arm64"}; + Utils.validatePatchIds(Arrays.asList(patchIds), false); + } + + @Test + void invalidPatchIds1() { + String[] patchIds = {"12345678_12.2.1.4.0", "12345678", "12345678_12.2.1.4.0_arm64"}; + assertThrows(InvalidPatchIdFormatException.class, + () -> Utils.validatePatchIds(Arrays.asList(patchIds), false)); + } + + @Test + void invalidPatchIds2() { + String[] patchIds = {"12345678_12.2.1.4.0", "12345678_arm64", "12345678_12.2.1.4.0_arm64"}; + assertThrows(InvalidPatchIdFormatException.class, + () -> Utils.validatePatchIds(Arrays.asList(patchIds), false)); + } }