某天我們有了要在exe檔上sign certificate的需求,而我們目前狀況如下:
基於以上需求,我開始思考有哪些做法:
使用第一種做法,我需要refactor build script,並且要考慮installer傳輸或放置在可抓取位置問題。而第二個做法呢? 考慮容易且可以傳輸檔案、執行指令的protocol:
為了簡單也不需要額外實作,我選擇了ssh server的方式。
在ssh server的選擇上,首先我使用了cygwin。在Signtool Server上安裝cygwin後,首先遇到類似以下的訊息
could not be located in the dynamic link library KERNEL32.dll嘗試過不同版本的cygwin依然無法解決,接著改用apache sshd。我使用的是1.2.0的版本,我遇到以下問題:
在成功啟動apache sshd後,可以透過putty連線測試一下。
(此部分需參考後記)
將程式註冊為service有不少方法,最簡單的方法是透過mssm。首先透過mssm執行:
mssm.exe install apache-sshd
接著會出現如下圖畫面,去選擇你sshd.bat:sc query apache-sshd sc start apache-sshd
ant scp的task會基於jsch,請記得先下載並放置於ANT_HOME/lib下。以下為我client side的ant script:
<project name="test-sign"> <target name="test-sign"> <property name="exe.file.name" value="test.exe"></property> <scp file="build/${exe.file.name}" todir="root:root@10.146.127.213:/apache-sshd-1.2.0/build" port="8000" trust="true"/> <sshexec command="sign.bat ${exe.file.name}" host="10.146.127.213" port="8000" username="root" password="root" trust="true"/> <scp file="root:root@10.146.127.213:/apache-sshd-1.2.0/build/${exe.file.name}" todir="dist" port="8000" trust="true"/> </target> </project>簡單說明一下我的ant script,
sign.bat首先會確認檔案是否存在,接著就是執行signtool:
@echo off cd /d "D:\\apache-sshd-1.2.0" if "%1" == "" ( echo Please enter the file name exit /b 1 ) set sign_file=D:\\apache-sshd-1.2.0\build\%1 if not exist "%sign_file%" ( echo Can't find %sign_file% exit /b 1 ) C:\WinDDK\6000\bin\setenv.bat C:\WinDDK\6000\ fre AMD64 WLH & signtool sign /v /ac D:\my.cer /s My /n "IssueToName" /t http://timestamp.verisign.com/scripts/timestamp.dll "%sign_file%"然而,事情並不如想像中順利,執行後發生以下錯誤:
<sshexec command="psexec -accepteula -u administrator -p 123456 \\localhost -w D:\apache-sshd-1.2.0 cmd.exe /c call sign.bat ${exe.file.name}" host="10.146.127.213" port="8000" username="root" password="root" trust="true" />
果然成功了:
某天正式開始啟用此功能後,發現傳大檔案都會發生問題。經過trace,發現是由於nssm註冊為service後,會一分鐘重新啟動一次:
詳細原因我沒有深究,但我把啟動的prcoess由bat改為exe就正常了:
使用的Arguments如下,請改成你自己對應路徑,我是echo bat內容複製貼上的:
-server -Xmx512M -Dcom.sun.management.jmxremote -classpath ";D:\apache-sshd-1.2.0\scripts\..\lib\bcpg-jdk15on-1.54.jar;D:\apache-sshd-1.2.0\scripts\..\lib\bcpkix-jdk15on-1.54.jar;D:\apache-sshd-1.2.0\scripts\..\lib\bcprov-jdk15on-1.54.jar;D:\apache-sshd-1.2.0\scripts\..\lib\slf4j-api-1.7.16.jar;D:\apache-sshd-1.2.0\scripts\..\lib\slf4j-jdk14-1.7.16.jar;D:\apache-sshd-1.2.0\scripts\..\lib\sshd-core-1.2.0.jar;D:\apache-sshd-1.2.0\scripts\..\lib\tomcat-apr-5.5.23.jar" -Dsshd.home="D:\apache-sshd-1.2.0\scripts\.." org.apache.sshd.server.SshServer