4. Jenkins 憑證管理
[作者:Surpassme]隨着網絡環境的變化,如果在Jenkins中使用明文密碼會造成一些安全隱患。爲此Jenkins也提供憑證管理功能,本章節來系統學習一下。
4.1 憑證定義
[作者:Surpassme]憑證(Credentials)是Jenkins在進行一些受限操作時的鑰匙
。例如SSH登錄時的密碼、Gitlab相關賬戶信息等,這些重要信息是不能以明文形式配置在Jenkins中,因此需要對這些憑證進行統一管理和使用。
爲了最大限度的提高安全性,在Jenkins master節點上對憑證進行加密存儲,然後通過憑證ID
在pipeline中使用。
4.2 創建憑證
[作者:Surpassme]因爲Jenkins本身也有一定的權限控制,因此在創建憑證時,請確保使用的賬戶具有創建憑證的權限。其創建憑證的操作步驟如下所示:
- 1.Manage Jenkins -> Manage Credentials ,如下所示:
- 2.Credentials -> System -> Global credentials(unrestricted) -> Add credentials,如下所示
-
- 進入創建憑證後的界面如下所示:
其主要參數如下所示:
-
類型:創建憑證的類型
-
範圍:憑證的作用域,主要有兩種
Global
: 全局作用域。若要在pipeline中使用,則使用該類型作用域
System
: 如果憑證用於Jenkins本身系統管理,則使用該類型作用域 -
ID:使用該憑證的唯一標識,如果創建時,不填寫則由Jenkins自動生成。
-
描述: 對該憑證的聲明式描述,建議填寫,方便區分和查找憑證
原文地址:作者:Surpassme:https://www.cnblogs.com/surpassme/p/16995400.html
4.3 常用憑證
[作者:Surpassme]在pipeline中使用憑證時,需要檢查插件Credentials Binding Plugin
(https://plugins.jenkins.io/credentials-binding/)是否安裝。
4.3.1 Username with password
Username with password 是比較常用的憑證,是指用戶和密碼憑證
,添加方法如下所示:
在pipeline中的使用示例如下所示:
pipeline{
agent any
options{
timestamps()
}
stages{
stage("credential demo"){
steps{
withCredentials([
usernamePassword(
credentialsId:"surpass-123",
usernameVariable:"username",
passwordVariable:"passwd"
)
]){
echo "username is ${username} \n password is ${passwd}"
}
}
}
}
}
4.3.2 Secret file
[作者:Surpassme]Secret file 是指需要保密的文件,添加方法如下所示:
在pipeline中的使用示例如下所示:
pipeline{
agent any
options{
timestamps()
}
stages{
stage("credential demo"){
steps{
withCredentials([
file(credentialsId:"secret-file-123",variable:"secretContent")]){
echo "secret contetn is: ${secretContent}"
}
}
}
}
}
4.3.3 Secret text
[作者:Surpassme]Secret text 是一串需要加密的文件,例如各種token信息等,添加方法如下所示:
在pipeline中的使用示例如下所示:
pipeline{
agent any
options{
timestamps()
}
stages{
stage("credential demo"){
steps{
withCredentials([
string(credentialsId:"secret-text-123",variable:"secretText")]){
echo "secret text is: ${secretText}"
}
}
}
}
}
4.3.4 SSH Username with private key
[作者:Surpassme]SSH Username with private key是指一對SSH用戶名和密鑰,添加方法如下所示:
在pipeline中的使用示例如下所示:
pipeline{
agent any
options{
timestamps()
}
stages{
stage("credential demo"){
steps{
withCredentials([
sshUserPrivateKey(
credentialsId:"ssh-username-key-123",
usernameVariable:"username",
keyFileVariable:"keyFile"
)
]){
echo "username is ${username}\nkey is: ${keyFile}"
}
}
}
}
}
4.4 憑證使用擴展
[作者:Surpassme]看到前面每次使用憑證比較麻煩,爲此Jenkins也提供另外一種擴展方法來使用憑證,使用credentials hepler(僅允許在environment中使用)
來簡化憑證使用。而credentials hepler也僅支持Secret text、Username with password和Secret file三種憑證
4.4.1 Username with password
在pipeline中的使用示例如下所示:
pipeline{
agent any
options{
timestamps()
}
environment{
GET_USERNAME_PASSWD=credentials("surpass-123")
}
stages{
stage("credential demo"){
steps{
echo "username is ${GET_USERNAME_PASSWD_USR}\npassword is ${GET_USERNAME_PASSWD_PSW}"
}
}
}
}
GET_USERNAME_PASSWD_USR的值是一個字符串,其格式爲:
<用戶名>
:<密碼>
4.4.2 Secret file
在pipeline中的使用示例如下所示:
pipeline{
agent any
options{
timestamps()
}
environment{
SECRET_FILE=credentials("secret-file-123")
}
stages{
stage("credential demo"){
steps{
echo "secret file is ${SECRET_FILE}"
}
}
}
}
4.4.3 Secret text
[作者:Surpassme]在pipeline中的使用示例如下所示:
pipeline{
agent any
options{
timestamps()
}
environment{
SECRET_TEXT=credentials("secret-text-123")
}
stages{
stage("credential demo"){
steps{
echo "secret text is ${SECRET_TEXT}"
}
}
}
}
原文地址:https://www.jianshu.com/p/e99708a14aa0
本文同步在微信訂閱號上發佈,如各位小夥伴們喜歡我的文章,也可以關注我的微信訂閱號:woaitest,或掃描下面的二維碼添加關注: