@@ -49,7 +49,7 @@ import_certificate() {
4949 echo -n -e ${CERTIFICATE_ENV_VAR} | base64 -D > ${CERTIFICATE_FILE_NAME}
5050 # we set || true here and for every `security import invoke` because the "duplicate SecKeychainItemImport" error
5151 # will cause set -e to exit 1. It is okay we do this because we deliberately handle this error in the lines below.
52- local out=$( security import ${CERTIFICATE_FILE_NAME} -A 2>&1 ) || true
52+ local out=$( security import ${CERTIFICATE_FILE_NAME} -T /usr/bin/pkgbuild - A 2>&1 ) || true
5353 local exitcode=$?
5454 # delete the certificate from disk
5555 rm -rf ${CERTIFICATE_FILE_NAME}
@@ -68,6 +68,28 @@ import_certificate() {
6868 fi
6969}
7070
71+ create_cloudflared_build_keychain () {
72+ # Reusing the private key password as the keychain key
73+ local PRIVATE_KEY_PASS=$1
74+
75+ # Create keychain only if it doesn't already exist
76+ if [ ! -f " $HOME /Library/Keychains/cloudflared_build_keychain.keychain-db" ]; then
77+ security create-keychain -p " $PRIVATE_KEY_PASS " cloudflared_build_keychain
78+ else
79+ echo " Keychain already exists: cloudflared_build_keychain"
80+ fi
81+
82+ # Append temp keychain to the user domain
83+ security list-keychains -d user -s cloudflared_build_keychain $( security list-keychains -d user | sed s/\" //g)
84+
85+ # Remove relock timeout
86+ security set-keychain-settings cloudflared_build_keychain
87+
88+ # Unlock keychain so it doesn't require password
89+ security unlock-keychain -p " $PRIVATE_KEY_PASS " cloudflared_build_keychain
90+
91+ }
92+
7193# Imports private keys to the Apple KeyChain
7294import_private_keys () {
7395 local PRIVATE_KEY_NAME=$1
@@ -83,7 +105,7 @@ import_private_keys() {
83105 echo -n -e ${PRIVATE_KEY_ENV_VAR} | base64 -D > ${PRIVATE_KEY_FILE_NAME}
84106 # we set || true here and for every `security import invoke` because the "duplicate SecKeychainItemImport" error
85107 # will cause set -e to exit 1. It is okay we do this because we deliberately handle this error in the lines below.
86- local out=$( security import ${PRIVATE_KEY_FILE_NAME} -A -P " ${PRIVATE_KEY_PASS} " 2>&1 ) || true
108+ local out=$( security import ${PRIVATE_KEY_FILE_NAME} -k cloudflared_build_keychain -P " $PRIVATE_KEY_PASS " -T /usr/bin/pkgbuild - A -P " ${PRIVATE_KEY_PASS} " 2>&1 ) || true
87109 local exitcode=$?
88110 rm -rf ${PRIVATE_KEY_FILE_NAME}
89111 if [ -n " $out " ]; then
@@ -100,6 +122,9 @@ import_private_keys() {
100122 fi
101123}
102124
125+ # Create temp keychain only for this build
126+ create_cloudflared_build_keychain " ${CFD_CODE_SIGN_PASS} "
127+
103128# Add Apple Root Developer certificate to the key chain
104129import_certificate " Apple Developer CA" " ${APPLE_DEV_CA_CERT} " " ${APPLE_CA_CERT} "
105130
@@ -119,8 +144,8 @@ import_certificate "Developer ID Installer" "${CFD_INSTALLER_CERT}" "${INSTALLER
119144if [[ ! -z " $CFD_CODE_SIGN_NAME " ]]; then
120145 CODE_SIGN_NAME=" ${CFD_CODE_SIGN_NAME} "
121146else
122- if [[ -n " $( security find-certificate -c " Developer ID Application" | cut -d' "' -f 4 -s | grep " Developer ID Application:" | head -1) " ]]; then
123- CODE_SIGN_NAME=$( security find-certificate -c " Developer ID Application" | cut -d' "' -f 4 -s | grep " Developer ID Application:" | head -1)
147+ if [[ -n " $( security find-certificate -c " Developer ID Application" cloudflared_build_keychain | cut -d' "' -f 4 -s | grep " Developer ID Application:" | head -1) " ]]; then
148+ CODE_SIGN_NAME=$( security find-certificate -c " Developer ID Application" cloudflared_build_keychain | cut -d' "' -f 4 -s | grep " Developer ID Application:" | head -1)
124149 else
125150 CODE_SIGN_NAME=" "
126151 fi
130155if [[ ! -z " $CFD_INSTALLER_NAME " ]]; then
131156 PKG_SIGN_NAME=" ${CFD_INSTALLER_NAME} "
132157else
133- if [[ -n " $( security find-certificate -c " Developer ID Installer" | cut -d' "' -f 4 -s | grep " Developer ID Installer:" | head -1) " ]]; then
134- PKG_SIGN_NAME=$( security find-certificate -c " Developer ID Installer" | cut -d' "' -f 4 -s | grep " Developer ID Installer:" | head -1)
158+ if [[ -n " $( security find-certificate -c " Developer ID Installer" cloudflared_build_keychain | cut -d' "' -f 4 -s | grep " Developer ID Installer:" | head -1) " ]]; then
159+ PKG_SIGN_NAME=$( security find-certificate -c " Developer ID Installer" cloudflared_build_keychain | cut -d' "' -f 4 -s | grep " Developer ID Installer:" | head -1)
135160 else
136161 PKG_SIGN_NAME=" "
137162 fi
@@ -142,9 +167,16 @@ rm -rf "${TARGET_DIRECTORY}"
142167export TARGET_OS=" darwin"
143168GOCACHE=" $PWD /../../../../" GOPATH=" $PWD /../../../../" CGO_ENABLED=1 make cloudflared
144169
170+
171+ # This allows apple tools to use the certificates in the keychain without requiring password input.
172+ # This command always needs to run after the certificates have been loaded into the keychain
173+ if [[ ! -z " $CFD_CODE_SIGN_PASS " ]]; then
174+ security set-key-partition-list -S apple-tool:,apple: -s -k " ${CFD_CODE_SIGN_PASS} " cloudflared_build_keychain
175+ fi
176+
145177# sign the cloudflared binary
146178if [[ ! -z " $CODE_SIGN_NAME " ]]; then
147- codesign -s " ${CODE_SIGN_NAME} " -f -v --timestamp -- options runtime ${BINARY_NAME}
179+ codesign --keychain $HOME /Library/Keychains/cloudflared_build_keychain.keychain-db - s " ${CODE_SIGN_NAME} " -fv -- options runtime --timestamp ${BINARY_NAME}
148180
149181 # notarize the binary
150182 # TODO: TUN-5789
@@ -165,11 +197,13 @@ tar czf "$FILENAME" "${BINARY_NAME}"
165197
166198# build the installer package
167199if [[ ! -z " $PKG_SIGN_NAME " ]]; then
200+
168201 pkgbuild --identifier com.cloudflare.${PRODUCT} \
169202 --version ${VERSION} \
170203 --scripts ${ARCH_TARGET_DIRECTORY} /scripts \
171204 --root ${ARCH_TARGET_DIRECTORY} /contents \
172205 --install-location /usr/local/bin \
206+ --keychain cloudflared_build_keychain \
173207 --sign " ${PKG_SIGN_NAME} " \
174208 ${PKGNAME}
175209
187221# cleanup build directory because this script is not ran within containers,
188222# which might lead to future issues in subsequent runs.
189223rm -rf " ${TARGET_DIRECTORY} "
224+
225+ # cleanup the keychain
226+ security default-keychain -d user -s login.keychain-db
227+ security list-keychains -d user -s login.keychain-db
228+ security delete-keychain cloudflared_build_keychain
0 commit comments