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 in a package.
Download client tools
Sign in to DigiCert ONE.
Navigate to DigiCert® KeyLocker > Resources > Client tool repository.
Select your operating system.
Click the download icon next to DigiCert® KeyLocker clients.
Create PKCS11 configuration file
To create a configuration file with the path to this 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
DigiCert® KeyLocker enforces multifactor authentication for security. To access keypairs, certificates, and sign code, you need to set up two types of credentials: an API token and an authentication certificate.
Create an API token
The API token is an authentication method used to verify you as a user and your permissions assigned in DigiCert ONE. The API token provides the first factor authentication.
Follow these steps to generate an API token:
Sign in to DigiCert ONE.
Select the profile icon (top-right).
Select Admin Profile.
Scroll down to API Tokens.
Select Create API token.
注記
The API token is only shown once, securely store the API key to use it later.
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.
Follow these steps to create a client authentication certificate:
Sign in to DigiCert ONE.
Select the profile icon (top-right).
Select Admin Profile.
Scroll down to Authentication certificates.
Select Create authentication certificate.
注記
The client authentication certificate password shown after creating an client authentication certificate cannot be accessed again, download the certificate and securely store the password to use it later.
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 PKCS11 configuration file. |
SM_TLS_SKIP_VERIFY | Enter true to disable or false to enable TLS verification on 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" } } } }