【问题标题】:How to hide parameters such as APIKey from Jenkins pipeline console output如何从 Jenkins 管道控制台输出中隐藏 APIKey 等参数
【发布时间】:2021-03-19 18:13:14
【问题描述】:

当我在舞台内执行脚本时,如何隐藏某些参数或****它们。

产生我想隐藏的输出的命令是:

sh "./wsagent_execute.sh -s -apiKey ${WHITESOURCE_API_KEY} -projectToken ${WHITESOURCE_PROJECT_TOKEN} -C ${configPath} -d ${directoryPath} -logLevel info"

我要隐藏的参数是-apiKey 和-projectToken。我该怎么做?

【问题讨论】:

  • 您需要将它们分配为环境变量,并在 shell 解释器中而不是在 Groovy 中解析它们。但是,您的字符串中有两个 Groovy 变量,因此您尝试执行的操作是不可能的。
  • 这是脚本标签内阶段步骤内的管道代码。 Jenkins 构建了这个管道,因此不涉及 shell 解释器

标签: linux shell unix jenkins jenkins-pipeline


【解决方案1】:

如果您从保险库中获取凭据,则可以使用 Mask Passwords 插件。它没有说它支持 Pipelines,但实际上它确实支持。


pipeline {
    agent any

    stages {
      stage('doing something') {
        steps {
          script {
            def current_nano = "1616407597607795668"
            sh label: "Now you see it", script: "echo ${current_nano}"
            maskPasswords(varPasswordPairs: [[password: current_nano, var: 'IGNORE']]) {
              sh label: "Now you don't", script: "echo ${current_nano}"
            }
          }
        }
      }
    }
}

输出:

[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/
[Pipeline] {
[Pipeline] stage
[Pipeline] { (doing something)
[Pipeline] script
[Pipeline] {
[Pipeline] sh (Now you see it)
+ echo 1616407597607795668
1616407597607795668
[Pipeline] maskPasswords
[Pipeline] {
[Pipeline] sh (Now you don't)
+ echo ********
********
[Pipeline] }
[Pipeline] // maskPasswords
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

【讨论】:

  • 我尝试了你的步骤,但它给了我一个错误:在步骤中没有找到这样的 DSL 方法'maskPasswords'
  • 你需要安装插件才能使用它的方法。
  • 嘿,它有效,但由于它是声明性的,所以使用了 wrap。你也可以添加多个变量来掩码
  • 您可以:varPasswordPairs: [[password: current_nano, var: 'IGNORE'], [password: '1234', var: "IGNORE2"],[password: 'hunter2', var: 'IGNORE3']]
【解决方案2】:

这些键是在管道中计算的还是静态的? 您可以尝试在 Jenkins 中像凭据一样使用它,但在日志中看不到它们的值。

【讨论】:

  • 这些键是在管道的环境部分定义的。 ProjectToken 来自 PipelineParams 参数,APIToken 来自 vault: path ...。我如何将它们用作凭据,因为它们的定义不同
  • 显然,您不能使用 Jenkins 凭据来存储它。
【解决方案3】:

如果您在 Jenkins 凭证中使用秘密定义,Jenkins 会自动为您屏蔽它,并在日志中显示为 ****

假设您在 jenkins 凭证中定义了您的 apiKey,其 ID 为:apikey。比你可以像下面的例子一样在你的管道上使用它。

更多详情可以查看here

node() {
   withCredentials([string(credentialsId: 'apikey', variable: 'TOKEN')]) {
      sh "./wsagent_execute.sh -s -apiKey $TOKEN -projectToken ${WHITESOURCE_PROJECT_TOKEN} -C ${configPath} -d ${directoryPath} -logLevel info"
   }
}

如果您对此不满意,请使用 mask password 或类似的插件

【讨论】:

    猜你喜欢
    • 2019-04-11
    • 1970-01-01
    • 1970-01-01
    • 2023-02-08
    • 1970-01-01
    • 1970-01-01
    • 2015-12-25
    • 2020-06-05
    • 2011-12-05
    相关资源
    最近更新 更多