Skip to content

Commit

Permalink
Merge branch 'patch-regex-fix' into 'main'
Browse files Browse the repository at this point in the history
For cache addPatch, corrected the format test for patch IDs with the architecture suffix.

See merge request weblogic-cloud/weblogic-image-tool!489
  • Loading branch information
ddsharpe committed Oct 9, 2024
2 parents 0ac4c5a + 6878d00 commit 6eaac1b
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void initializeOptions() throws IOException, InvalidCredentialException, Invalid
throw new InvalidCredentialException();
}

Utils.validatePatchIds(patches, false);
Utils.validatePatchIds(patches, true);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> BUSYBOX_OS_IDS = Collections.unmodifiableList(Arrays.asList("bb", "alpine"));
public static final String ORACLE_LINUX = "ghcr.io/oracle/oraclelinux:8-slim";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -582,38 +583,36 @@ public static String getBuildWorkingDir() throws IOException {
return workingDir;
}

private static void validatePatchIds(List<String> 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<String> patches, boolean rigid) throws InvalidPatchIdFormatException {
Pattern patchIdPattern;
if (rigid) {
patchIdPattern = Pattern.compile(Constants.RIGID_PATCH_ID_REGEX);
public static void validatePatchIds(@NotNull List<String> 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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@

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;

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 {
Expand All @@ -33,15 +37,15 @@ 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);
}

@Test
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());
Expand All @@ -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));
}
}

0 comments on commit 6eaac1b

Please sign in to comment.