|
1 | 1 | #!/bin/sh |
2 | 2 |
|
3 | | -# Notarize a generated node-<version>.pkg file as an Apple requirement for installation on macOS Catalina and later, as validated by Gatekeeper. |
| 3 | +# Notarize a macOS release artifact as an Apple requirement for running or |
| 4 | +# installing on macOS Catalina and later, as validated by Gatekeeper. |
4 | 5 | # Uses notarytool and requires Xcode >= 13.0. |
| 6 | +# |
| 7 | +# Usage: osx-notarize.sh <path> |
| 8 | +# |
| 9 | +# <path> ending in .pkg: the installer is submitted as is, assessed with |
| 10 | +# spctl and the notarization ticket is stapled to it. |
| 11 | +# any other <path>: a Mach-O binary, such as bin/node from the release |
| 12 | +# tarball. Apple only accepts zip, dmg or pkg submissions, so the binary |
| 13 | +# is zipped for the upload and the zip is discarded afterwards. A bare |
| 14 | +# binary cannot carry a stapled ticket; Gatekeeper fetches it online by |
| 15 | +# the code directory hash, so the binary itself is left untouched and the |
| 16 | +# published checksums do not change. |
| 17 | +# |
| 18 | +# For backwards compatibility a bare version id (for example v26.13.0) is |
| 19 | +# accepted and resolved to node-<version>.pkg. |
5 | 20 |
|
6 | | -pkgid="$1" |
| 21 | +# The notarization ticket can take a while to become available from Apple's |
| 22 | +# servers after notarytool reports "Accepted", so retry with backoff. |
| 23 | +retry() { |
| 24 | + delay=15 |
| 25 | + attempt=1 |
| 26 | + while ! "$@"; do |
| 27 | + if [ "$attempt" -ge 6 ]; then |
| 28 | + return 1 |
| 29 | + fi |
| 30 | + echo "Attempt $attempt failed, retrying in ${delay}s..." |
| 31 | + sleep "$delay" |
| 32 | + attempt=$((attempt + 1)) |
| 33 | + delay=$((delay * 2)) |
| 34 | + done |
| 35 | +} |
| 36 | + |
| 37 | +submit() { |
| 38 | + echo "Submitting $1 for notarization..." |
| 39 | + if xcrun notarytool submit \ |
| 40 | + --keychain-profile "NODE_RELEASE_PROFILE" \ |
| 41 | + --wait \ |
| 42 | + "$1"; then |
| 43 | + echo "Notarization $1 submitted successfully." |
| 44 | + else |
| 45 | + echo "Notarization $1 failed." |
| 46 | + return 1 |
| 47 | + fi |
| 48 | +} |
| 49 | + |
| 50 | +# Remove the upload zip on every exit path, including a failed ditto or an |
| 51 | +# interrupted build, the same way the dep_updaters scripts clean their |
| 52 | +# workspace. $zip is only set by notarize_binary. |
| 53 | +cleanup() { |
| 54 | + EXIT_CODE=$? |
| 55 | + if [ -n "$zip" ]; then |
| 56 | + rm -f "$zip" |
| 57 | + fi |
| 58 | + exit $EXIT_CODE |
| 59 | +} |
| 60 | + |
| 61 | +notarize_pkg() { |
| 62 | + submit "$1" || exit 1 |
| 63 | + |
| 64 | + if ! retry xcrun spctl --assess --type install --context context:primary-signature --ignore-cache --verbose=2 "$1"; then |
| 65 | + echo "error: Signature will not be accepted by Gatekeeper!" 1>&2 |
| 66 | + exit 1 |
| 67 | + fi |
| 68 | + echo "Verification was successful." |
| 69 | + |
| 70 | + if ! retry xcrun stapler staple "$1"; then |
| 71 | + echo "error: Stapling $1 failed." 1>&2 |
| 72 | + exit 1 |
| 73 | + fi |
| 74 | + echo "Stapler was successful." |
| 75 | +} |
| 76 | + |
| 77 | +notarize_binary() { |
| 78 | + # ditto preserves the code signature and resource forks, which plain zip |
| 79 | + # does not, so it is the archiver Apple documents for notarization uploads. |
| 80 | + # The zip only exists for the upload and is removed by cleanup. |
| 81 | + zip="$1.notarize.zip" |
| 82 | + rm -f "$zip" |
| 83 | + if ! ditto -c -k --keepParent "$1" "$zip"; then |
| 84 | + echo "error: Could not create $zip for submission." 1>&2 |
| 85 | + exit 1 |
| 86 | + fi |
| 87 | + |
| 88 | + submit "$zip" || exit 1 |
| 89 | + rm -f "$zip" |
| 90 | + |
| 91 | + # There is nothing to staple to a bare binary, so the only way to confirm |
| 92 | + # the ticket is live is to have codesign look it up. spctl cannot be used |
| 93 | + # here: it rejects command line tools with "does not seem to be an app". |
| 94 | + if ! retry codesign --verify --verbose=2 -R=notarized "$1"; then |
| 95 | + echo "error: $1 will not be accepted by Gatekeeper!" 1>&2 |
| 96 | + exit 1 |
| 97 | + fi |
| 98 | + echo "Verification was successful." |
| 99 | +} |
7 | 100 |
|
8 | | -if [ -z "$pkgid" ]; then |
9 | | - echo "Usage: $0 <pkgid>" |
| 101 | +target="$1" |
| 102 | + |
| 103 | +if [ -z "$target" ]; then |
| 104 | + echo "Usage: $0 <path-to-pkg-or-binary>" |
10 | 105 | exit 1 |
11 | 106 | fi |
12 | 107 |
|
@@ -26,6 +121,16 @@ if [ -z "$NOTARIZATION_TEAM_ID" ]; then |
26 | 121 | exit 0 |
27 | 122 | fi |
28 | 123 |
|
| 124 | +# Legacy call form: a version id instead of a path. |
| 125 | +if [ ! -e "$target" ] && [ -e "node-$target.pkg" ]; then |
| 126 | + target="node-$target.pkg" |
| 127 | +fi |
| 128 | + |
| 129 | +if [ ! -e "$target" ]; then |
| 130 | + echo "error: $target does not exist." 1>&2 |
| 131 | + exit 1 |
| 132 | +fi |
| 133 | + |
29 | 134 | echo "Notarization process is done with Notarytool." |
30 | 135 |
|
31 | 136 | if ! command -v xcrun notarytool > /dev/null |
|
34 | 139 | exit 1 |
35 | 140 | fi |
36 | 141 |
|
37 | | -echo "Submitting node-$pkgid.pkg for notarization..." |
38 | | - |
39 | | -xcrun notarytool submit \ |
40 | | - --keychain-profile "NODE_RELEASE_PROFILE" \ |
41 | | - --wait \ |
42 | | - "node-$pkgid.pkg" |
43 | | - |
44 | | -if [ $? -eq 0 ]; then |
45 | | - echo "Notarization node-$pkgid.pkg submitted successfully." |
46 | | -else |
47 | | - echo "Notarization node-$pkgid.pkg failed." |
48 | | - exit 1 |
49 | | -fi |
50 | | - |
51 | | -# The notarization ticket can take a while to become available from Apple's |
52 | | -# servers after notarytool reports "Accepted", so retry with backoff. |
53 | | -retry() { |
54 | | - delay=15 |
55 | | - attempt=1 |
56 | | - while ! "$@"; do |
57 | | - if [ "$attempt" -ge 6 ]; then |
58 | | - return 1 |
59 | | - fi |
60 | | - echo "Attempt $attempt failed, retrying in ${delay}s..." |
61 | | - sleep "$delay" |
62 | | - attempt=$((attempt + 1)) |
63 | | - delay=$((delay * 2)) |
64 | | - done |
65 | | -} |
66 | | - |
67 | | -if ! retry xcrun spctl --assess --type install --context context:primary-signature --ignore-cache --verbose=2 "node-$pkgid.pkg"; then |
68 | | - echo "error: Signature will not be accepted by Gatekeeper!" 1>&2 |
69 | | - exit 1 |
70 | | -else |
71 | | - echo "Verification was successful." |
72 | | -fi |
| 142 | +trap cleanup INT TERM EXIT |
73 | 143 |
|
74 | | -if ! retry xcrun stapler staple "node-$pkgid.pkg"; then |
75 | | - echo "error: Stapling node-$pkgid.pkg failed." 1>&2 |
76 | | - exit 1 |
77 | | -fi |
78 | | -echo "Stapler was successful." |
| 144 | +case "$target" in |
| 145 | + *.pkg) notarize_pkg "$target" ;; |
| 146 | + *) notarize_binary "$target" ;; |
| 147 | +esac |