Fixes for constrained powershell

This commit is contained in:
crschnick 2024-07-17 12:45:21 +00:00
parent f3f63aa0f7
commit 58533aba5b
3 changed files with 22 additions and 8 deletions

View file

@ -5,6 +5,7 @@ import io.xpipe.app.util.LocalShell;
import io.xpipe.core.process.OsType;
import io.xpipe.core.process.ProcessControlProvider;
import io.xpipe.core.process.ProcessOutputException;
import lombok.Value;
import java.util.Optional;
@ -17,15 +18,19 @@ public class AppShellCheck {
.getEffectiveLocalDialect()
.equals(ProcessControlProvider.get().getFallbackDialect());
if (err.isPresent() && canFallback) {
var msg = formatMessage(err.get());
var msg = formatMessage(err.get().getMessage());
ErrorEvent.fromThrowable(new IllegalStateException(msg)).handle();
enableFallback();
err = selfTestErrorCheck();
}
if (err.isPresent()) {
var msg = formatMessage(err.get());
ErrorEvent.fromThrowable(new IllegalStateException(msg)).handle();
var msg = formatMessage(err.get().getMessage());
var event = ErrorEvent.fromThrowable(new IllegalStateException(msg));
if (!err.get().isCanContinue()) {
event.term();
}
event.handle();
}
}
@ -71,17 +76,24 @@ public class AppShellCheck {
LocalShell.init();
}
private static Optional<String> selfTestErrorCheck() {
private static Optional<FailureResult> selfTestErrorCheck() {
try (var command = LocalShell.getShell().command("echo test").complex().start()) {
var out = command.readStdoutOrThrow();
if (!out.equals("test")) {
return Optional.of("Expected \"test\", got \"" + out + "\"");
return Optional.of(new FailureResult("Expected \"test\", got \"" + out + "\"", true));
}
} catch (ProcessOutputException ex) {
return Optional.of(ex.getOutput() != null ? ex.getOutput() : ex.toString());
return Optional.of(new FailureResult(ex.getOutput() != null ? ex.getOutput() : ex.toString(), true));
} catch (Throwable t) {
return Optional.of(t.getMessage() != null ? t.getMessage() : t.toString());
return Optional.of(new FailureResult(t.getMessage() != null ? t.getMessage() : t.toString(), false));
}
return Optional.empty();
}
@Value
private static class FailureResult {
String message;
boolean canContinue;
}
}

View file

@ -82,7 +82,7 @@ public class SentryErrorHandler implements ErrorHandler {
causeField.set(copy, adjustCopy(throwable.getCause(), true));
return copy;
} catch (Exception e) {
} catch (Throwable e) {
// This can fail for example when the underlying exception is not serializable
// and comes from some third party library
if (AppLogs.get() != null) {

View file

@ -27,6 +27,7 @@ public class ShellDialects {
public static ShellDialect CISCO;
public static ShellDialect MIKROTIK;
public static ShellDialect RBASH;
public static ShellDialect CONSTRAINED_POWERSHELL;
public static ShellDialect OVH_BASTION;
public static List<ShellDialect> getStartableDialects() {
@ -85,6 +86,7 @@ public class ShellDialects {
CISCO = byId("cisco");
MIKROTIK = byId("mikrotik");
RBASH = byId("rbash");
CONSTRAINED_POWERSHELL = byId("constrainedPowershell");
OVH_BASTION = byId("ovhBastion");
}
}