1. 程式人生 > >Jenkins 憑證管理 - 看這一篇就夠了~

Jenkins 憑證管理 - 看這一篇就夠了~

[TOC] 許多三方網站和應用可以與Jenkins互動,如Artifact倉庫,基於雲的儲存系統和服務等. 在Jenkins中新增/配置credentials,Pipeline專案就可以使用 credentials 與三方應用互動 ![](https://gitee.com/owen2016/pic-hub/raw/master/material/jenkins.jpg) ## Credential 型別 參考: **Jenkins可以儲存以下型別的credentials:** - Secret text - API token之類的token (如GitHub個人訪問token) - Username and password - 可以為獨立的欄位,也可以為冒號分隔的字串:username:password(更多資訊請參照 處理 credentials) - Secret file - 儲存在檔案中的加密內容 - SSH Username with private key - SSH 公鑰/私鑰對 - Certificate - a PKCS#12 證書檔案 和可選密碼 - Docker Host Certificate Authentication credentials. ## Credential 安全 為了最大限度地提高安全性,在Jenins中配置的 credentials 以加密形式儲存在Jenkins 主節點上(用Jenkins ID加密),並且 `只能通過 credentials ID` 在Pipeline專案中獲取 這最大限度地減少了向Jenkins使用者公開credentials真實內容的可能性,並且阻止了將credentials複製到另一臺Jenkins例項 ## Credential 建立 - 選擇適合的憑證型別 ![](https://gitee.com/owen2016/pic-hub/raw/master/pics/20201027222335.png) - 建立 “Username and password” 憑證 ![](https://gitee.com/owen2016/pic-hub/raw/master/pics/20201027223010.png) - 建立 “SSH Username with private key” 憑證 ![](https://gitee.com/owen2016/pic-hub/raw/master/pics/20201027222917.png) ### Credential ID 定義 - 在 ID 欄位中,必須指定一個有意義的`Credential ID`- 例如 jenkins-user-for-xyz-artifact-repository。注意: 該欄位是可選的。 如果您沒有指定值, Jenkins 則Jenkins會分配一個全域性唯一ID(GUID)值。 - **請記住:** 一旦設定了credential ID,就不能再進行更改。 ## Credential 使用 參考:
儲存在Jenkins中的credentials可以被使用: 1. 適用於Jenkins的任何地方 (即全域性 credentials), 2. 通過特定的Pipeline專案/專案 (在 處理 credentials 和 使用Jenkinsfile部分了解更多資訊), 3. 由特定的Jenkins使用者 (如 Pipeline 專案中[建立 Blue Ocean](https://jenkins.io/zh/doc/book/blueocean/creating-pipelines/)的情況). - Blue Ocean 自動生成一個 SSH 公共/私有金鑰對, 確保 SSH 公共/私有祕鑰對在繼續之前已經被註冊到你的Git伺服器 實際使用中,下面幾個場景會用到creential - gitlab 訪問、API呼叫 - jenkins slave 建立 ### Credential 相關外掛 **注意:** 上述 Credential 型別都依賴於 jenkins外掛,同樣jenkins pipeline 也需要這些外掛的安裝以支援程式碼片段 - Credentials Binding:
- **For secret text, usernames and passwords, and secret files** ```bash environment { MAGE_REPO_CREDENTIALS = credentials('COMPOSER_REPO_MAGENTO') COMPOSER_AUTH = """{ "http-basic": { "repo.magento.com": { "username": "${env.MAGE_REPO_CREDENTIALS_USR}", "password": "${env.MAGE_REPO_CREDENTIALS_PSW}" } } }""" } ``` - **For other credential types** ``` bash withCredentials([usernamePassword(credentialsId: 'amazon', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) { // available as an env variable, but will be masked if you try to print it out any which way // note: single quotes prevent Groovy interpolation; expansion is by Bourne Shell, which is what you want sh 'echo $PASSWORD' // also available as a Groovy variable echo USERNAME // or inside double quotes for string interpolation echo "username is $USERNAME" } ``` - Jenkins Plain Credentials Plugin:
![](https://gitee.com/owen2016/pic-hub/raw/master/pics/20201027224420.png) - SSH Credentials: ## 最佳實踐 - 為了便於管理和使用, 強烈建議使用統一的約定來指定credential ID - 建議使用類似下面的format做為credential ID, 便於jenkinsfile開發時直接使用,同時在”描述“裡寫清楚credential的作用 `gitlab-api-token、gitlab-private-key、gitlab-userpwd-pair、harbor-xxx-xxx` ![](https://gitee.com/owen2016/pic-hub/raw/master/pics/20201027221956.png) **實踐:** - 如下所示,將憑證使用統一的ID命名之後,便於複用,憑證定義一次,可多次,多個地方統一使用,無論是後期維護,複用都非常方便! ``` shell environment { // HARBOR="harbor.devopsing.site" HARBOR_ACCESS_KEY = credentials('harbor-userpwd-pair') SERVER_ACCESS_KEY = credentials('deploy-userpwd-pair') } ..... docker login --username=${HARBOR_ACCESS_KEY_USR} --password=${HARBOR_ACCESS_KEY_PSW} ${HARBOR} sshpass -p "${SERVER_ACCESS_KEY_PSW}" ssh -o StrictHostKeyChecking=no ${SERVER_ACCESS_KEY_USR}@${DEPLOY_SERVER} "$runCmd"