@@ -49,7 +49,7 @@ import_certificate() {
49
49
echo -n -e ${CERTIFICATE_ENV_VAR} | base64 -D > ${CERTIFICATE_FILE_NAME}
50
50
# we set || true here and for every `security import invoke` because the "duplicate SecKeychainItemImport" error
51
51
# 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
53
53
local exitcode=$?
54
54
# delete the certificate from disk
55
55
rm -rf ${CERTIFICATE_FILE_NAME}
@@ -68,6 +68,28 @@ import_certificate() {
68
68
fi
69
69
}
70
70
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
+
71
93
# Imports private keys to the Apple KeyChain
72
94
import_private_keys () {
73
95
local PRIVATE_KEY_NAME=$1
@@ -83,7 +105,7 @@ import_private_keys() {
83
105
echo -n -e ${PRIVATE_KEY_ENV_VAR} | base64 -D > ${PRIVATE_KEY_FILE_NAME}
84
106
# we set || true here and for every `security import invoke` because the "duplicate SecKeychainItemImport" error
85
107
# 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
87
109
local exitcode=$?
88
110
rm -rf ${PRIVATE_KEY_FILE_NAME}
89
111
if [ -n " $out " ]; then
@@ -100,6 +122,9 @@ import_private_keys() {
100
122
fi
101
123
}
102
124
125
+ # Create temp keychain only for this build
126
+ create_cloudflared_build_keychain " ${CFD_CODE_SIGN_PASS} "
127
+
103
128
# Add Apple Root Developer certificate to the key chain
104
129
import_certificate " Apple Developer CA" " ${APPLE_DEV_CA_CERT} " " ${APPLE_CA_CERT} "
105
130
@@ -119,8 +144,8 @@ import_certificate "Developer ID Installer" "${CFD_INSTALLER_CERT}" "${INSTALLER
119
144
if [[ ! -z " $CFD_CODE_SIGN_NAME " ]]; then
120
145
CODE_SIGN_NAME=" ${CFD_CODE_SIGN_NAME} "
121
146
else
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)
124
149
else
125
150
CODE_SIGN_NAME=" "
126
151
fi
130
155
if [[ ! -z " $CFD_INSTALLER_NAME " ]]; then
131
156
PKG_SIGN_NAME=" ${CFD_INSTALLER_NAME} "
132
157
else
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)
135
160
else
136
161
PKG_SIGN_NAME=" "
137
162
fi
@@ -142,9 +167,16 @@ rm -rf "${TARGET_DIRECTORY}"
142
167
export TARGET_OS=" darwin"
143
168
GOCACHE=" $PWD /../../../../" GOPATH=" $PWD /../../../../" CGO_ENABLED=1 make cloudflared
144
169
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
+
145
177
# sign the cloudflared binary
146
178
if [[ ! -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}
148
180
149
181
# notarize the binary
150
182
# TODO: TUN-5789
@@ -165,11 +197,13 @@ tar czf "$FILENAME" "${BINARY_NAME}"
165
197
166
198
# build the installer package
167
199
if [[ ! -z " $PKG_SIGN_NAME " ]]; then
200
+
168
201
pkgbuild --identifier com.cloudflare.${PRODUCT} \
169
202
--version ${VERSION} \
170
203
--scripts ${ARCH_TARGET_DIRECTORY} /scripts \
171
204
--root ${ARCH_TARGET_DIRECTORY} /contents \
172
205
--install-location /usr/local/bin \
206
+ --keychain cloudflared_build_keychain \
173
207
--sign " ${PKG_SIGN_NAME} " \
174
208
${PKGNAME}
175
209
187
221
# cleanup build directory because this script is not ran within containers,
188
222
# which might lead to future issues in subsequent runs.
189
223
rm -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