gpt4 book ai didi

jenkins - 使用 Jenkins 凭证插件以纯文本形式显示密码

转载 作者:行者123 更新时间:2023-12-02 03:31:07 25 4
gpt4 key购买 nike

我正在尝试使用 Jenkins Credentials 插件来获取用户输入并在 Jenkinsfile 中使用它进行处理。由于密码字段高度敏感,我希望凭据插件能够屏蔽密码,使其不显示在控制台输出中。但是似乎密码以纯文本形式显示。我注意到一个现有问题 https://issues.jenkins-ci.org/browse/JENKINS-38181其中讨论了 withCredentials block 之外的 echo 语句,以纯文本形式显示密码,这是预期的。但就我而言,即使 withCredentials block 内的 echo 语句也会显示为普通。

我在这里做错了什么吗?我应该避免使用 echo 吗?

凭证绑定(bind)插件:1.12
凭证插件:2.1.16

 node('someagent') {
stage 'input'
def userNameInput = input(
id: 'UserName', message: 'input your username: ', ok: 'ok', parameters: [string(defaultValue: 'user', description: '.....', name: 'DB_USER')]
)
def userPasswordInput = input(
id: 'Password', message: 'input your password: ', ok: 'ok', parameters: [string(defaultValue: 'password', description: '.....', name: 'DB_PASS')]
)
withCredentials(bindings: [usernamePassword(credentialsId: 'CREDS', usernameVariable: userNameInput, variable: userPasswordInput)]) {
echo ("My Username: ${userNameInput}")
echo ("My Password: ${userPasswordInput}")
}
}

控制台输出:

[Pipeline] {
[Pipeline] stage (input)
Using the ‘stage’ step without a block argument is deprecated
Entering stage input
Proceeding
[Pipeline] input
Input requested
Approved by UserId
[Pipeline] input
Input requested
Approved by UserId
[Pipeline] withCredentials
[Pipeline] {
[Pipeline] echo
My Username: user
[Pipeline] echo
My Password: password
[Pipeline] }
[Pipeline] // withCredentials
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

最佳答案

您没有正确理解 withCredentials 的用法。当使用withCredentials时,意味着凭证已添加到Jenkins中,withCredentials可以将用户和密码值提取到Shell变量中,然后您可以通过引用shell变量来使用它们。

因此无法将用户和密码提取到预定义的 Groovy 变量中。

withCredentials的正确用法:

withCredentials([usernamePassword(
credentialsId: 'CREDS', // the CREDS should be exist in Jenkins
passwordVariable: 'pwd', // you need to use a string, not a Groovy variable
usernameVariable: 'user') // you need to use a string, not a Groovy variable
]) {
sh '''
echo UserName: ${user} // the user and pwd injected into Shell Context as Environment variable
echo Password: ${pwd} // will show as *** in jenkins console
'''

// If you user `"` or `"""` to wrapper a string,
// Groovy will execute string substitution with Groovy variable if
// same name variable exist in Groovy Context
// otherwise the string keep nothing change

sh """
echo ${user}
echo ${pwd} // will show as *** in jenkins console
"""
// because the user and pwd not exist Groovy context
// so substitution will fail and the string keep no change
// then execute the string in Shell by sh(),
// the user and pwd can be found in Shell context
}

实际上,下面的代码将首先通过 Groovy 执行字符串替换。这就是为什么您会在 jenkins 控制台中看到密码以纯文本形式显示

 echo ("My Username: ${userNameInput}") 
echo ("My Password: ${userPasswordInput}")

// because userNameInput and userPasswordInput exist in Groovy variables,
// so the ${userNameInput} and ${userPasswordInput} will be replaced to
// the value of Groovy variables before echo, as result ${userNameInput}
// and ${userPasswordInput} used variable from Groovy, rather then from Shell

关于jenkins - 使用 Jenkins 凭证插件以纯文本形式显示密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51834768/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com