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

2024-04-24

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

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

凭证绑定插件: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 执行字符串替换。 这就是为什么您在詹金斯控制台中看到密码以纯文本形式显示的原因

 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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Jenkins 凭证插件以纯文本形式显示密码 的相关文章

随机推荐

  • Web 服务器可以处理多少个套接字连接?

    假设我要获得共享 虚拟或专用托管 我在某处读到服务器 计算机一次只能处理 64 000 个 TCP 连接 这是真的吗 无论带宽如何 任何类型的托管可以处理多少个 我假设 HTTP 通过 TCP 工作 这是否意味着只有 64 000 个用户可
  • 如何合并两个 Git 存储库?

    考虑以下场景 我在自己的 Git 存储库中开发了一个小型实验项目 A 它现在已经成熟了 我希望 A 成为更大的项目 B 的一部分 该项目有自己的大存储库 我现在想将 A 添加为 B 的子目录 如何将 A 合并到 B 中而不丢失任何一方的历史
  • Django:简单的速率限制

    我的许多视图都获取外部资源 我想确保在重负载下我不会炸毁远程站点 和 或被禁止 我只有 1 个爬行器 所以有一个中央锁就可以了 我希望每秒最多允许对主机进行 3 次查询 并让其余的块最多持续 15 秒 我怎样才能做到这一点 简单 Use d
  • Jquery Onchange 事件没有被触发 - Primefaces - inputText

    未调用以下事件 inputTxt的代码如下
  • commit失败是否需要回滚?

    这似乎是一个足够简单的问题 但我找不到任何特定于 MySQL 的明确答案 看这个 mysqli gt autocommit false Start the transaction success true do a bunch of ins
  • MS Access 和 ODBC。过滤器对话框不显示

    我的 Microsoft Access 数据库有问题通过 ODBC 连接到 Postgresql 数据库 过滤器对话框 用于使用多个可选复选框过滤数据 不起作用 编辑 不起作用 我的意思是没有可供选择的复选框 仅提供用于 A Z 和 Z A
  • 作为后台进程/服务运行命令

    我有一个 Shell 命令 我想在后台运行 并且我读到这可以通过添加后缀来完成 到导致它作为后台进程运行的命令 但我需要一些更多的功能 并且想知道如何去做 我希望每次系统重新启动时该命令都在后台启动并运行 我希望能够像人们一样在需要时启动和
  • 停止 Intellij / Android Studio Preview 在格式化匿名内部类参数时添加换行符

    我试图阻止 Android Studio Preview 即 IntelliJ 在匿名内部类定义为方法参数时在其后添加换行符 回车符 它改变了这一点 runOnUiThread new Runnable Override public vo
  • Service Worker 是否持续向服务器请求、响应?

    我正在使用服务器发送事件来显示通知 我创建了一个服务工作线程 并在运行项目后使用 EventSource 与服务器连接 在我的例子中 我使用了 servlet 一切正常 但事件内的内容会被多次执行 我想知道为什么 我的另一个问题是 一旦我关
  • 如何从 nltk 分类器获得精度和召回率?

    import nltk from nltk corpus import movie reviews from nltk tokenize import word tokenize documents list movie reviews w
  • Ruby 中 const_get 的行为令人困惑?

    根据文档mod const get sym 返回 mod 中指定常量的值 我也知道const get默认情况下可以查找接收者的继承链 所以以下工作 class A HELLO hello end class B lt A end B con
  • YUV 422、YUV 420、YUV 444

    例如 我有 4 4 图像 我想分别提取 Y U 和 V 分量 如果图像是 YUV 422 YUV 420 和 YUV444 该怎么办 我有兴趣了解 Y U 和 V 的数组结构如何存储在 422 420 和 444 中 以便可以访问它 Thi
  • MVC3 删除了“required”但不断获取 data-val-required 属性

    My Model DataType DataType Text Display Name Number of Employee public int NumberOfEmployee get set DataType DataType Da
  • 使用 dplyr 进行探索性绘图

    我经常使用 d ply 来绘制探索图 一个简单的例子 require plyr plot species lt function species data p lt qplot data species data x Sepal Lengt
  • C++ 中的向量存储

    我希望存储一个大的 d 维点向量 d 固定且小 如果我定义一个Point as vector
  • Notepad++ 在每行添加数字

    我在文本文件中有以下数据 p 1 p 1 p 1 p 1 等等 我在 Notepad 中打开该文件 我该如何将其更改为 p 1 p 2 p 3 p 4 etc You can use the Column Editor mode First
  • PopupView 没有显示?

    这是 XML 只是一个网络视图
  • Mysql InnoDB性能优化和索引

    我有 2 个数据库 需要链接两个大表之间的信息 每个表超过 300 万个条目 并且不断增长 第一个数据库有一个表 pages 用于存储有关网页的各种信息 并包括每个页面的 URL URL 列是 varchar 512 并且没有索引 第二个数
  • C 语言标准集合在哪里?

    我现在致力于学习 C 我擅长 Python PHP Bash 但我决定我不能流利地使用 C 但是我无法想象在没有列表和哈希的语言中工作 也许我 我只是有点操之过急 但肯定有 标准 集合库 我在 GNU 标准库中没有看到任何内容 有什么建议吗
  • 使用 Jenkins 凭证插件以纯文本形式显示密码

    我正在尝试使用 Jenkins Credentials 插件来获取用户输入并在 Jenkinsfile 中使用它进行处理 由于密码字段高度敏感 我希望凭据插件能够屏蔽密码 使其不显示在控制台输出中 但是似乎密码以纯文本形式显示 我注意到一个