jenkins - 如何在 Jenkins 控制台中屏蔽变量,以避免泄露我的令牌?
问题描述
在我的 Jenkins 管道中,我将withCredentials
插件作为一个函数,这样我就可以传递变量来检索不同的秘密并让我的舞台管道干净。下面是我的代码。
def getCredentialsString(dsl, credentialsId) {
def r;
dsl.withCredentials([string(credentialsId: credentialsId, variable: 'CredentialsString')]) {
r = "${env.CredentialsString}"
}
return r
}
阶段管道,my-jenkins-token
并且my-jenkins-api-token
是 jenkins 中用于存储我的 API 令牌的秘密。我隐藏了必要的命令并显示如下。
def project_token = credentials.getCredentialsString(this, "my-jenkins-token")
def api_token = credentials.getCredentialsString(this, "my-jenkins-api-token")
sh "mvn -Dsettings.security=${mavenSecuritySetting} -DprojectToken=${project_token} -DapiToken=${api_token} -Dcommit=${COMMIT_ID} -s ${mavenSetting}"
问题是,如果我选择分离withCredentials
,令牌将打印为纯文本,我想知道一种掩盖变量的方法。只要它有助于屏蔽控制台中的输出,我就可以将变量设为 ENV 变量。
解决方案
获取withCredentils
块外秘密的值违背了安全存储秘密的目的。如果您拥有动态范围的凭据,我建议您使用以下方法。
node('master'){
Map creds= [slack_token: 'Slack', aws_token: 'AWS'] // key is the local variable and value is the credentialsId from jenkins store
runWithCredentials(creds){
echo "${slack_token} and ${aws_token}"
}
}
def runWithCredentials(Map credentials, def body){
List credBlock = []
credentials.each {credValue, credId ->
credBlock.add(string(credentialsId: credId, variable: credValue))
}
withCredentials(credBlock){
body()
}
}
推荐阅读
- vba - Lock/Unlock of the parameter (Check), Check the value of parameter is set from KWA Rule (Edit Window for CADPRT)
- python - 如何输入作为字符串一部分的变量的文件路径(Python)
- c# - 显示用户名
- symfony - LexikjwtAuthenticator Symfony 定义身份验证路由
- bash - 如何从 ubuntu 机器安全地将数据传输到外部硬盘驱动器?
- php - 在 PHP header() vs setcookie() 中设置 cookie 过期时间的问题
- google-apps-script - 如何通过 GOOGLE APPS SCRIPT 自动将多个谷歌表格中的数据合并到另一个谷歌表格
- mysql - 如果这一天(今天的日期)条目将被计算在内,那么明天的日期将再次以 0 开始
- javascript - 从另一个 js 文件访问和操作自定义元素
- kubernetes - 如何处理 RWO 卷服务的滚动更新部署策略?