2020-06-18 16:44:22 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
set -e
|
|
|
|
|
|
|
|
[[ $(uname) != 'Darwin' ]] && { echo 'This only works on macOS.' >&2; exit 1; }
|
|
|
|
[[ $# -lt 2 ]] && { echo 'sign-macos-bundle.sh <file_to_sign> <sign_identity>' >&2; exit 1; }
|
|
|
|
|
|
|
|
# First is the target file/directory to sign
|
|
|
|
TARGET="${1}"
|
|
|
|
# Second argument is the signing identity
|
|
|
|
CODESIGN_ID="${2}"
|
|
|
|
# Rest are extra command line flags for codesign
|
|
|
|
shift 2
|
|
|
|
CODESIGN_OPTS_EXTRA=("${@}")
|
|
|
|
|
|
|
|
[[ ! -e "${TARGET}" ]] && { echo 'Target file does not exist.' >&2; exit 1; }
|
|
|
|
|
|
|
|
function clean_up {
|
|
|
|
STATUS=$?
|
2023-08-10 17:57:33 +00:00
|
|
|
if [[ "${STATUS}" -ne 0 ]]; then
|
2021-08-04 15:41:31 +00:00
|
|
|
echo -e "\n###### ERROR: See above for details."
|
|
|
|
fi
|
2020-06-18 16:44:22 +00:00
|
|
|
set +e
|
|
|
|
|
|
|
|
echo -e "\n###### Cleaning up..."
|
|
|
|
echo -e "\n### Locking keychain..."
|
|
|
|
security lock-keychain "${MACOS_KEYCHAIN_FILE}"
|
|
|
|
echo -e "\n### Restoring default keychain search list..."
|
2020-08-10 13:31:54 +00:00
|
|
|
security list-keychains -s ${ORIG_KEYCHAIN_LIST}
|
2020-06-18 16:44:22 +00:00
|
|
|
security list-keychains
|
|
|
|
|
|
|
|
exit $STATUS
|
|
|
|
}
|
|
|
|
|
|
|
|
# Flags for codesign
|
|
|
|
CODESIGN_OPTS=(
|
|
|
|
"--sign ${CODESIGN_ID}"
|
|
|
|
"--options runtime"
|
|
|
|
"--verbose=4"
|
|
|
|
"--force"
|
|
|
|
)
|
|
|
|
# Add extra flags provided via command line
|
|
|
|
CODESIGN_OPTS+=(
|
|
|
|
${CODESIGN_OPTS_EXTRA[@]}
|
|
|
|
)
|
|
|
|
|
|
|
|
# Setting MACOS_KEYCHAIN_FILE nd MACOS_KEYCHAIN_PASS is not required because
|
|
|
|
# MACOS_CODESIGN_IDENT can be found in e.g. your login keychain.
|
|
|
|
# Those would normally be specified only in CI.
|
|
|
|
if [[ -n "${MACOS_KEYCHAIN_FILE}" ]]; then
|
|
|
|
if [[ -z "${MACOS_KEYCHAIN_PASS}" ]]; then
|
|
|
|
echo "Unable to unlock the keychain without MACOS_KEYCHAIN_PASS!" >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
2020-08-10 13:31:54 +00:00
|
|
|
echo -e "\n### Storing original keychain search list..."
|
2020-09-07 17:39:26 +00:00
|
|
|
# We want to restore the normal keychains and ignore Jenkis created ones
|
|
|
|
ORIG_KEYCHAIN_LIST=$(security list-keychains | grep -v -e "^/private" -e "secretFiles" | xargs)
|
2023-10-03 15:43:35 +00:00
|
|
|
|
2020-06-18 16:44:22 +00:00
|
|
|
# The keychain file needs to be locked afterwards
|
|
|
|
trap clean_up EXIT ERR
|
|
|
|
|
|
|
|
echo -e "\n### Adding keychain to search list..."
|
|
|
|
security list-keychains -s ${ORIG_KEYCHAIN_LIST} "${MACOS_KEYCHAIN_FILE}"
|
|
|
|
security list-keychains
|
|
|
|
echo -e "\n### Unlocking keychain..."
|
|
|
|
security unlock-keychain -p "${MACOS_KEYCHAIN_PASS}" "${MACOS_KEYCHAIN_FILE}"
|
|
|
|
|
|
|
|
# Add a flag to use the unlocked keychain
|
|
|
|
CODESIGN_OPTS+=("--keychain ${MACOS_KEYCHAIN_FILE}")
|
|
|
|
fi
|
|
|
|
|
|
|
|
# If 'TARGET' is a directory, we assume it's an app
|
|
|
|
# bundle, otherwise we consider it to be a dmg.
|
|
|
|
if [[ -d "${TARGET}" ]]; then
|
|
|
|
CODESIGN_OPTS+=("--deep")
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo -e "\n### Signing target..."
|
|
|
|
codesign ${CODESIGN_OPTS[@]} "${TARGET}"
|
|
|
|
|
|
|
|
echo -e "\n### Verifying signature..."
|
|
|
|
codesign --verify --strict=all --deep --verbose=4 "${TARGET}"
|
|
|
|
|
|
|
|
echo -e "\n###### DONE"
|