#166 Fix AWS JAVA ignoring shell commands in combination with a Makefile.

This commit is contained in:
Maximilian Paß
2022-06-19 20:48:31 +02:00
parent b0916a2485
commit ed5205236f
5 changed files with 138 additions and 101 deletions

View File

@ -67,9 +67,10 @@ public class App implements RequestHandler<APIGatewayV2WebSocketEvent, APIGatewa
return cmd[cmd.length - 1];
}
// Wrapps the passed command with "sh -c".
public static String[] wrapCommand(String cmd) {
return new String[]{"sh", "-c", cmd};
// Replaces the last element of the command with the new shell command.
public static String[] wrapCommand(String[] originalCommand, String shellCommand) {
originalCommand[originalCommand.length - 1] = shellCommand;
return originalCommand;
}
public APIGatewayProxyResponseEvent handleRequest(final APIGatewayV2WebSocketEvent input, final Context context) {
@ -89,7 +90,7 @@ public class App implements RequestHandler<APIGatewayV2WebSocketEvent, APIGatewa
String[] cmd = execution.cmd;
try {
SimpleMakefile make = new SimpleMakefile(execution.files);
cmd = wrapCommand(make.parseCommand(unwrapCommand(execution.cmd)));
cmd = wrapCommand(cmd, make.parseCommand(unwrapCommand(execution.cmd)));
} catch (NoMakefileFoundException | NoMakeCommandException | InvalidMakefileException ignored) {}
ProcessBuilder pb = new ProcessBuilder(cmd);

View File

@ -22,7 +22,7 @@ class InvalidMakefileException extends Exception {}
class SimpleMakefile {
// This pattern validates if a command is a make command.
private static final Pattern isMakeCommand = Pattern.compile("^(?:.* && )?make(?:\\s+(?<startRule>\\w*))?(?<assignments>(?:.*?=.*?)+)?(?: && .*)?$");
private static final Pattern isMakeCommand = Pattern.compile("^(?<before>.* && )?make(?:\\s+(?<startRule>\\w*))?(?<assignments>(?:.*?=.*?)+)?(?<after> && .*)?$");
// This pattern identifies the rules in a makefile.
private static final Pattern makeRules = Pattern.compile("(?<name>.*):\\r?\\n(?<commands>(?:\\t.+\\r?\\n?)*)");
@ -131,6 +131,15 @@ class SimpleMakefile {
String command = getCommand(ruleArgument);
String assignments = makeCommandMatcher.group("assignments");
return injectAssignments(command, (assignments != null) ? assignments : "");
command = injectAssignments(command, (assignments != null) ? assignments : "");
if (makeCommandMatcher.group("before") != null) {
command = makeCommandMatcher.group("before") + command;
}
if (makeCommandMatcher.group("after") != null) {
command = command + makeCommandMatcher.group("after");
}
return command;
}
}

View File

@ -53,7 +53,7 @@ public class AppTest {
@Test
public void successfulResponse() {
APIGatewayProxyResponseEvent result = getApiGatewayProxyResponse(RecursiveMathContent);
APIGatewayProxyResponseEvent result = getApiGatewayProxyResponseRecursiveMath(RecursiveMathContent);
assertEquals(200, result.getStatusCode().intValue());
}
@ -61,7 +61,7 @@ public class AppTest {
@Test
public void successfulMultilineResponse() {
ByteArrayOutputStream out = setupStdOutLogs();
APIGatewayProxyResponseEvent result = getApiGatewayProxyResponse(MultilineMathContent);
APIGatewayProxyResponseEvent result = getApiGatewayProxyResponseRecursiveMath(MultilineMathContent);
restoreStdOutLogs();
assertEquals(200, result.getStatusCode().intValue());
@ -75,7 +75,7 @@ public class AppTest {
@Test
public void outputWithoutTrailingNewline() {
ByteArrayOutputStream out = setupStdOutLogs();
APIGatewayProxyResponseEvent result = getApiGatewayProxyResponse(MathContentWithoutTrailingNewline);
APIGatewayProxyResponseEvent result = getApiGatewayProxyResponseRecursiveMath(MathContentWithoutTrailingNewline);
restoreStdOutLogs();
assertEquals(200, result.getStatusCode().intValue());
@ -85,6 +85,21 @@ public class AppTest {
assertEquals(expectedOutput, out.toString());
}
@Test
public void makefileJustReplacesShellCommand() {
ByteArrayOutputStream out = setupStdOutLogs();
APIGatewayProxyResponseEvent result = getApiGatewayProxyResponse("{\"action\":\"java11Exec\"," +
"\"cmd\":[\"env\", \"TEST_VAR=42\", \"sh\",\"-c\",\"make run\"]," +
"\"files\":{\"Makefile\":\"" + Base64.getEncoder().encodeToString(("run:\n\t@echo $TEST_VAR\n").getBytes(StandardCharsets.UTF_8)) + "\"}}");
restoreStdOutLogs();
assertEquals(200, result.getStatusCode().intValue());
String expectedOutput =
"{\"type\":\"stdout\",\"data\":\"42\\n\"}\n" +
"{\"type\":\"exit\",\"data\":0}\n";
assertEquals(expectedOutput, out.toString());
}
private PrintStream originalOut;
private ByteArrayOutputStream setupStdOutLogs() {
@ -98,7 +113,7 @@ public class AppTest {
System.setOut(originalOut);
}
private APIGatewayProxyResponseEvent getApiGatewayProxyResponse(String content) {
private APIGatewayProxyResponseEvent getApiGatewayProxyResponse(String body) {
App app = new App();
APIGatewayV2WebSocketEvent input = new APIGatewayV2WebSocketEvent();
APIGatewayV2WebSocketEvent.RequestContext ctx = new APIGatewayV2WebSocketEvent.RequestContext();
@ -108,8 +123,12 @@ public class AppTest {
Map<String, String> headers = new HashMap<>();
headers.put(App.disableOutputHeaderKey, "True");
input.setHeaders(headers);
input.setBody("{\"action\":\"java11Exec\",\"cmd\":[\"sh\",\"-c\",\"javac org/example/RecursiveMath.java && java org/example/RecursiveMath\"]," +
"\"files\":{\"org/example/RecursiveMath.java\":\"" + content + "\"}}");
input.setBody(body);
return app.handleRequest(input, null);
}
private APIGatewayProxyResponseEvent getApiGatewayProxyResponseRecursiveMath(String content) {
return getApiGatewayProxyResponse("{\"action\":\"java11Exec\",\"cmd\":[\"sh\",\"-c\",\"javac org/example/RecursiveMath.java && java org/example/RecursiveMath\"]," +
"\"files\":{\"org/example/RecursiveMath.java\":\"" + content + "\"}}");
}
}

View File

@ -148,4 +148,20 @@ public class SimpleMakefileTest {
fail();
} catch (InvalidMakefileException ignored) {}
}
@Test
public void withBeforeAndAfterStatements() {
Map<String, String> files = new HashMap<>();
files.put("Makefile", Base64.getEncoder().encodeToString(("run:\n\t@echo TRAAAIIN\n").getBytes(StandardCharsets.UTF_8)));
try {
String command = "echo \"Look it's a\" && sl && make run && echo WOW";
SimpleMakefile makefile = new SimpleMakefile(files);
String cmd = makefile.parseCommand(command);
assertEquals("echo \"Look it's a\" && sl && echo TRAAAIIN && echo WOW", cmd);
} catch (NoMakefileFoundException | InvalidMakefileException | NoMakeCommandException ignored) {
fail();
}
}
}