From 25764601e2683f0775fa871cf74dd9b3638f02e5 Mon Sep 17 00:00:00 2001 From: Thomas Neidhart Date: Mon, 8 Jan 2024 13:41:10 +0100 Subject: [PATCH] feat: improve error handling during submission (#294) --- .../notarization/process/NativeProcess.java | 4 ++++ .../xcrun/notarytool/NotarytoolNotarizer.java | 19 +++++++++++++-- .../notarytool/NotarytoolNotarizerTest.java | 23 +++++++++++++++++-- .../xcrun/notarytool/submission-empty.log | 0 .../xcrun/notarytool/submission-failure.log | 1 + 5 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 src/test/resources/org/eclipse/cbi/ws/macos/notarization/xcrun/notarytool/submission-empty.log create mode 100644 src/test/resources/org/eclipse/cbi/ws/macos/notarization/xcrun/notarytool/submission-failure.log diff --git a/src/main/java/org/eclipse/cbi/ws/macos/notarization/process/NativeProcess.java b/src/main/java/org/eclipse/cbi/ws/macos/notarization/process/NativeProcess.java index 0a3b628..e61997f 100644 --- a/src/main/java/org/eclipse/cbi/ws/macos/notarization/process/NativeProcess.java +++ b/src/main/java/org/eclipse/cbi/ws/macos/notarization/process/NativeProcess.java @@ -122,6 +122,10 @@ public InputStream stdoutAsStream() throws IOException { return Files.newInputStream(stdout, StandardOpenOption.READ); } + public InputStream stderrAsStream() throws IOException { + return Files.newInputStream(stderr, StandardOpenOption.READ); + } + Result log() { LOGGER.trace(this.toString()); if (exitValue == 0) { diff --git a/src/main/java/org/eclipse/cbi/ws/macos/notarization/xcrun/notarytool/NotarytoolNotarizer.java b/src/main/java/org/eclipse/cbi/ws/macos/notarization/xcrun/notarytool/NotarytoolNotarizer.java index 8d63f21..aa84b9d 100644 --- a/src/main/java/org/eclipse/cbi/ws/macos/notarization/xcrun/notarytool/NotarytoolNotarizer.java +++ b/src/main/java/org/eclipse/cbi/ws/macos/notarization/xcrun/notarytool/NotarytoolNotarizer.java @@ -7,7 +7,9 @@ *******************************************************************************/ package org.eclipse.cbi.ws.macos.notarization.xcrun.notarytool; +import com.google.common.base.Charsets; import com.google.common.collect.ImmutableList; +import com.google.common.io.CharStreams; import org.eclipse.cbi.ws.macos.notarization.process.NativeProcess; import org.eclipse.cbi.ws.macos.notarization.xcrun.common.*; import org.slf4j.Logger; @@ -15,6 +17,7 @@ import org.xml.sax.SAXException; import java.io.IOException; +import java.io.InputStreamReader; import java.nio.file.Path; import java.util.*; import java.util.concurrent.ExecutionException; @@ -36,7 +39,7 @@ protected List getUploadCommand(String appleIDUsername, String appleIDPa } @Override - protected NotarizerResult analyzeSubmissionResult(NativeProcess.Result nativeProcessResult, Path fileToNotarize) throws ExecutionException { + protected NotarizerResult analyzeSubmissionResult(NativeProcess.Result nativeProcessResult, Path fileToNotarize) { NotarizerResultBuilder resultBuilder = NotarizerResult.builder(); try { PListDict plist = PListDict.fromXML(nativeProcessResult.stdoutAsStream()); @@ -66,7 +69,19 @@ protected NotarizerResult analyzeSubmissionResult(NativeProcess.Result nativePro } } catch (IOException | SAXException e) { LOGGER.error("Error while parsing the output after the upload of '" + fileToNotarize + "' to the Apple notarization service", e); - throw new ExecutionException("Error while parsing the output after the upload of the file to be notarized", e); + + String errorMessage; + + try { + errorMessage = + CharStreams.toString(new InputStreamReader(nativeProcessResult.stderrAsStream(), Charsets.UTF_8)); + } catch (IOException ex) { + errorMessage = "unknown error. See server log for more details."; + } + + resultBuilder + .status(NotarizerResult.Status.UPLOAD_FAILED) + .message("Failed to notarize the requested file. Reason: " + errorMessage); } return resultBuilder.build(); } diff --git a/src/test/java/org/eclipse/cbi/ws/macos/notarization/xcrun/notarytool/NotarytoolNotarizerTest.java b/src/test/java/org/eclipse/cbi/ws/macos/notarization/xcrun/notarytool/NotarytoolNotarizerTest.java index 41124ca..7e5314d 100644 --- a/src/test/java/org/eclipse/cbi/ws/macos/notarization/xcrun/notarytool/NotarytoolNotarizerTest.java +++ b/src/test/java/org/eclipse/cbi/ws/macos/notarization/xcrun/notarytool/NotarytoolNotarizerTest.java @@ -17,8 +17,7 @@ import java.nio.file.Path; import java.util.concurrent.ExecutionException; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.*; public class NotarytoolNotarizerTest { @@ -49,6 +48,26 @@ public void analyzeSuccessfulSubmission() throws ExecutionException { assertEquals("Successfully uploaded file", result.message()); } + @Test + public void analyzeFailureSubmissionDueToRequiredAgreement() throws ExecutionException { + Path stdout = Path.of(this.getClass().getResource("submission-empty.log").getPath()); + Path stderr = Path.of(this.getClass().getResource("submission-failure.log").getPath()); + + NativeProcess.Result r = + NativeProcess.Result.builder() + .exitValue(0) + .arg0("") + .stdout(stdout) + .stderr(stderr) + .build(); + + NotarizerResult result = tool.analyzeSubmissionResult(r, Path.of("SuperDuper.dmg")); + + assertEquals(NotarizerResult.Status.UPLOAD_FAILED, result.status()); + assertTrue(result.message().startsWith("Failed to notarize the requested file. Reason: Error: " + + "HTTP status code: 403. A required agreement is missing or has expired.")); + } + @Test public void analyzeInfoInProgress() throws ExecutionException { Path stdout = Path.of(this.getClass().getResource("info-in-progress.log").getPath()); diff --git a/src/test/resources/org/eclipse/cbi/ws/macos/notarization/xcrun/notarytool/submission-empty.log b/src/test/resources/org/eclipse/cbi/ws/macos/notarization/xcrun/notarytool/submission-empty.log new file mode 100644 index 0000000..e69de29 diff --git a/src/test/resources/org/eclipse/cbi/ws/macos/notarization/xcrun/notarytool/submission-failure.log b/src/test/resources/org/eclipse/cbi/ws/macos/notarization/xcrun/notarytool/submission-failure.log new file mode 100644 index 0000000..4a879e8 --- /dev/null +++ b/src/test/resources/org/eclipse/cbi/ws/macos/notarization/xcrun/notarytool/submission-failure.log @@ -0,0 +1 @@ +Error: HTTP status code: 403. A required agreement is missing or has expired. This request requires an in-effect agreement that has not been signed or has expired. Ensure your team has signed the necessary legal agreements and that they are not expired. \ No newline at end of file