Jenkins と PKCS11の統合
STMクライアントツールをセットアップし、Jenkins と統合して、CI/CDパイプラインに自動化できるようにします。
前提条件
Jenkins ビルドシステム
Jenkins 上で Javaを サポートする OS を持つ任意のエージェント
エージェントにインストールされた JDK
DigiCert® Software Trust Manager アクセスの設定(下記参照)
特定の OS 用の DigiCert® Software Trust Manager クライアント(クライアントツール)
DigiCert® Software Trust Manager PKCS11ライブラリ (クライアントツール)
注記
クライアントツールは、DigiCert Oneポータルの DigiCert® Software Trust Manager -> リソース -> クライアントツールでダウンロードすることができます
Client tools
DigiCert® KeyLocker clients can be downloaded as a package.
Download client tools
In the KeyLocker menu, go to Resources > Client tool repository.
Select your operating system, and then select the corresponding download (
) icon next to the desired client.
Create PKCS11 configuration file
To create a configuration file with the path to the shared library:
Open an integrated development environment (IDE) or plain text editor.
Copy and paste the following text into the editor:
Save the file as pkcs11properties.cfg.
Move the pkcs11properties.cfg file to the same location as the PKCS11 library.
Set PATH environment variables
Operating systems use the environment variable called PATH to determine where executable files are stored on your system. Use the PATH environment variable to store the file path to your signing tools to ensure that the CLI can reference these signing tools.
User authentication
KeyLocker enforces multi-factor authentication for security purposes. To access keypairs, certificates, and sign code, you need to set up two types of credentials: An API key and an authentication certificate.
Create an API key
The API key is an authentication method used to verify you as a user and your permissions assigned in DigiCert ONE. The API key provides the first factor authentication.
In DigiCert ONE, select the profile (
) icon, and then select Admin Profile.Under API keys, select Create API key.
For Name, enter a descriptive name for the key.
For End date (optional), enter the date when the key should expire.
Select Create. The API key appears this one time and can't be accessed again. Securely store the API key for future use.
Create an authentication certificate
The client authentication certificate is an authentication method used to verify you as a user and your permissions assigned in DigiCert ONE. The client authentication certificate provides the second factor authentication.
In DigiCert ONE, select the profile (
) icon, and then select Admin Profile.Under Client authentication certificates, select Create client authentication certificate.
For Nickname, enter a descriptive name for the key.
For End date, enter the date when the certificate should expire.
Select the desired Encryption and Signature hash algorithm.
Select Generate certificate. The password appears this one time and can't be accessed again. Download the certificate and securely store the password for future use.
DigiCert® Software Trust Managerのセットアップ
システム変数として設定することで、環境変数が永続的に残るようにしてください。
Variable | Description |
|---|---|
SM_API_KEY | Provide your API token. |
SM_CLIENT_CERT_FILE | Provide your client authentication certificate. |
SM_CLIENT_CERT_PASSWORD | Provide your client certificate password. |
SM_HOST | Provide your host environment. |
PKCS11_CONFIG | Provide the path to the PKCS#11 configuration file. |
SM_TLS_SKIP_VERIFY | Enter true to disable or false to enable TLS verification on the client side. |
Integration with Jenkins
Environment variables setup for pipeline
The client tools need these environment variables to connect with DigiCert® KeyLocker to provide its service. They can be integrated as environment variables that are part of the pipeline as shown in the example below or they can be configured at an OS environment level.
pipeline {
environment{
SM_CLIENT_CERT_PASSWORD='**********'
SM_CLIENT_CERT_FILE='<Path to client authentication certificate file>'
SM_HOST='<host url>'
SM_API_KEY='<API token>'
PKCS11_CONFIG='<Path to pkcs11properties.cfg>'
}
}Sign
Example for a Jenkins stage for signing a jar:
stage('sign') {
steps {
//For Windows
bat 'jarsigner -keystore NONE -storepass NONE -storetype PKCS11 -sigalg SHA256withRSA -providerClass sun.security.pkcs11.SunPKCS11 -providerArg %PKCS11_CONFIG% -signedjar <Path to Output Signed Jar> <Path to the Jar to be Signed> <Keypair Certificate Name/Alias > -tsa http://timestamp.digicert.com'
//For Linux
sh 'jarsigner -keystore NONE -storepass NONE -storetype PKCS11 -sigalg SHA256withRSA -providerClass sun.security.pkcs11.SunPKCS11 -providerArg $PKCS11_CONFIG -signedjar <Path to Output Signed Jar> <Path to the Jar to be Signed> <Keypair Certificate Name/Alias> -tsa http://timestamp.digicert.com'
}
}この例の入力パラメーターは、署名された jarを出力する必要があるパス、署名する必要がある jar へのパス、署名に使用する必要がある証明書の名前/エイリアスです。
注記
PKCS11の設定ファイルは、パイプラインの環境に定義されていました。定義されていない場合は、設定ファイルへのパスで代用可能です。
Verify signature
Example of a Jenkins stage that verifies a signed jar:
stage('verify') {
steps {
//For Windows
bat 'jarsigner -verify <Path to Signed Jar>'
//For Linux
sh 'jarsigner -verify <Path to Signed Jar>'
}
}この段階での入力は、検証が必要な署名付き jarのパスのみです。
サンプルパイプライン
pipeline {
agent{ label 'windows' }
tools {
gradle 'Gradle'
}
environment {
SM_CLIENT_CERT_PASSWORD="gxmiK9Oe72Pn"
SM_CLIENT_CERT_FILE="C:\\Workspace\\smtools-windows\\local_pkcs12.p12"
SM_HOST="https://clientauth.one.digicert.com"
SM_API_KEY="01ff018928e385329550b6e7a7_9b38fc5fbb4ef99ceeb7d61e6984107efa4283583cb7a64f5e292582cd3ed848"
PKCS11_CONFIG="C:\\Workspace\\smtools-windows\\pkcs11properties.cfg"
KEYPAIR_CERT_NAME="jenkins_test_rsa_2048_${env.BUILD_NUMBER}"
}
stages {
stage('checkout') {
steps {
checkout([$class: 'GitSCM', branches: [[name: '*/main']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'Siddharth-Srinivas', url: 'https://github.com/Siddharth-Srinivas/jenkins-test-repo.git']]])
}
}
stage('clean') {
steps {
bat "./gradlew clean"
}
}
stage('test') {
steps {
withGradle {
bat "./gradlew test"
}
}
post {
success {
junit "**/build/test-results/test/TEST-*.xml"
}
}
}
stage('build') {
steps {
withGradle {
bat "./gradlew build"
}
}
}
stage('sign') {
steps {
bat "jarsigner -keystore NONE -storepass NONE -storetype PKCS11 -sigalg SHA256withRSA -providerClass sun.security.pkcs11.SunPKCS11 -providerArg %PKCS11_CONFIG% -signedjar app/build/libs/signed_app.jar app/build/libs/app.jar %KEYPAIR_CERT_NAME% -tsa http://timestamp.digicert.com"
}
post {
success {
archiveArtifacts "app/build/libs/signed_app.jar"
}
}
}
stage('verify') {
steps {
bat "jarsigner -verify app/build/libs/signed_app.jar"
}
}
}
}