macos-sign-binaries.sh 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/usr/bin/env bash
  2. # intended to run on MacOS only
  3. # signs executables and runtime libraries found in $RELX_TEMP_DIR with developer certificate
  4. # required variables:
  5. # APPLE_DEVELOPER_IDENTITY: "Developer ID Application: <company name> (<hex id>)"
  6. # APPLE_DEVELOPER_ID_BUNDLE: base64-encoded content of apple developer id certificate bundle in pksc12 format
  7. # APPLE_DEVELOPER_ID_BUNDLE_PASSWORD: password used when exporting the bundle
  8. # note: 'bundle' in apple terminology is 'identity'
  9. set -euo pipefail
  10. if [ "$(uname)" != 'Darwin' ]; then
  11. echo 'Not macOS, exiting';
  12. exit 0;
  13. fi
  14. if [ "${APPLE_SIGN_BINARIES:-0}" == 0 ]; then
  15. n echo "Signing Apple binaries is disabled, exiting"
  16. exit 0
  17. fi
  18. if [[ "${APPLE_DEVELOPER_ID_BUNDLE:-0}" == 0 || "${APPLE_DEVELOPER_ID_BUNDLE_PASSWORD:-0}" == 0 ]]; then
  19. echo "Apple developer certificate is not configured, skip signing"
  20. exit 0
  21. fi
  22. pushd "${RELX_TEMP_DIR}"
  23. PKSC12_FILE="$HOME/developer-id-application.p12"
  24. base64 --decode > "${PKSC12_FILE}" <<<"${APPLE_DEVELOPER_ID_BUNDLE}"
  25. KEYCHAIN="emqx-$(date +%s).keychain-db"
  26. KEYCHAIN_PASSWORD="$(openssl rand -base64 32)"
  27. trap cleanup EXIT
  28. function cleanup {
  29. set +e
  30. security delete-keychain "${KEYCHAIN}" 2>/dev/null
  31. }
  32. security create-keychain -p "${KEYCHAIN_PASSWORD}" "${KEYCHAIN}"
  33. security set-keychain-settings "${KEYCHAIN}"
  34. security unlock-keychain -p "${KEYCHAIN_PASSWORD}" "${KEYCHAIN}"
  35. security import "${PKSC12_FILE}" -P "${APPLE_DEVELOPER_ID_BUNDLE_PASSWORD}" -t cert -f pkcs12 -k "${KEYCHAIN}" -T /usr/bin/codesign
  36. security set-key-partition-list -S "apple-tool:,apple:,codesign:" -s -k "${KEYCHAIN_PASSWORD}" "${KEYCHAIN}"
  37. security verify-cert -k "${KEYCHAIN}" -c "${PKSC12_FILE}"
  38. security find-identity -p codesigning "${KEYCHAIN}"
  39. # add new keychain into the search path for codesign, otherwise the stuff does not work
  40. keychains=$(security list-keychains -d user)
  41. keychain_names=();
  42. for keychain in ${keychains}; do
  43. basename=$(basename "${keychain}")
  44. keychain_name=${basename::${#basename}-4}
  45. keychain_names+=("${keychain_name}")
  46. done
  47. security -v list-keychains -s "${keychain_names[@]}" "${KEYCHAIN}"
  48. # known runtime executables and binaries
  49. codesign -s "${APPLE_DEVELOPER_IDENTITY}" -f --verbose=4 --timestamp --options=runtime \
  50. erts-*/bin/{beam.smp,dyn_erl,epmd,erl,erl_call,erl_child_setup,erlexec,escript,heart,inet_gethost,run_erl,to_erl}
  51. codesign -s "${APPLE_DEVELOPER_IDENTITY}" -f --verbose=4 --timestamp --options=runtime \
  52. lib/runtime_tools-*/priv/lib/{dyntrace.so,trace_ip_drv.so,trace_file_drv.so}
  53. codesign -s "${APPLE_DEVELOPER_IDENTITY}" -f --verbose=4 --timestamp --options=runtime \
  54. lib/os_mon-*/priv/bin/{cpu_sup,memsup}
  55. codesign -s "${APPLE_DEVELOPER_IDENTITY}" -f --verbose=4 --timestamp --options=runtime \
  56. lib/jq-*/priv/{jq_nif1.so,libjq.1.dylib,libonig.5.dylib,erlang_jq_port}
  57. # other files from runtime and dependencies
  58. for f in \
  59. asn1rt_nif.so \
  60. bcrypt_nif.so \
  61. crc32cer_nif.so \
  62. crypto.so \
  63. crypto_callback.so \
  64. ezstd_nif.so \
  65. jiffy.so \
  66. liberocksdb.so \
  67. libquicer_nif.so \
  68. odbcserver \
  69. otp_test_engine.so \
  70. sasl_auth.so \
  71. snappyer.so \
  72. ; do
  73. find lib/ -name "$f" -exec codesign -s "${APPLE_DEVELOPER_IDENTITY}" -f --verbose=4 --timestamp --options=runtime {} \;
  74. done
  75. popd
  76. cleanup