alopresto commented on a change in pull request #4154: NIFI-7271 Make command
timeout configurable for ShellUserGroupProvider
URL: https://github.com/apache/nifi/pull/4154#discussion_r396794204
##########
File path:
nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-shell-authorizer/src/main/java/org/apache/nifi/authorization/util/ShellRunner.java
##########
@@ -23,60 +23,105 @@
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
-
import java.util.ArrayList;
import java.util.List;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
public class ShellRunner {
private final static Logger logger =
LoggerFactory.getLogger(ShellRunner.class);
static String SHELL = "sh";
static String OPTS = "-c";
- static Integer TIMEOUT = 60;
- public static List<String> runShell(String command) throws IOException {
+ private final int timeoutSeconds;
+ private final ExecutorService executor;
+
+ public ShellRunner(final int timeoutSeconds) {
+ this.timeoutSeconds = timeoutSeconds;
+ this.executor = Executors.newFixedThreadPool(1, new ThreadFactory() {
+ @Override
+ public Thread newThread(final Runnable r) {
+ final Thread t = Executors.defaultThreadFactory().newThread(r);
+ t.setName("ShellRunner");
+ t.setDaemon(true);
+ return t;
+ }
+ });
+ }
+
+ public List<String> runShell(String command) throws IOException {
return runShell(command, "<unknown>");
}
- public static List<String> runShell(String command, String description)
throws IOException {
+ public List<String> runShell(String command, String description) throws
IOException {
final ProcessBuilder builder = new ProcessBuilder(SHELL, OPTS,
command);
+ builder.redirectErrorStream(true);
+
final List<String> builderCommand = builder.command();
+ logger.debug("Run Command '{}': {}", new Object[]{description,
builderCommand});
- logger.debug("Run Command '" + description + "': " + builderCommand);
final Process proc = builder.start();
+ final List<String> lines = new ArrayList<>();
+ executor.submit(() -> {
+ try {
+ int lineCount = 0;
+ try (final Reader stdin = new
InputStreamReader(proc.getInputStream());
+ final BufferedReader reader = new BufferedReader(stdin)) {
+ logger.trace("Reading process input stream...");
+
+ String line;
+ while ((line = reader.readLine()) != null) {
+ if (logger.isTraceEnabled()) {
+ logger.trace((lineCount++) + " - " + line);
Review comment:
I'm fine leaving this zero-indexed, but if someone is trying to debug the
output after the fact, should we prefix the increment operator (`++lineCount`)
to let the printed line number match up with the output?
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services