1

How to reuse the credentials without masking results in withCredentials.usernamePassword?

Background

  •  I'm using scripted pipelines and Shared Libraries. All the implementation we have is under the src directory where I reuse the reference in pipelines:
def static myFunction(steps, context) {
steps.withCredentials([steps.usernamePassword(credentialsId: context.git.deploy.credentialsId, usernameVariable: 'GITHUB_USERNAME', passwordVariable: 'GITHUB_PASSWORD')]) {
// use of steps.env.GITHUB_PASSWORD
}
}
  • I need to make 2 API calls to Github Enterprise with the same Credentials, which is set as the UsernamePassword credentials
  •  Although the first call works as expected, the second call fails because of the env.PASSWORD value is masked

Details

  def teamMembersApi = sh(curl -H 'Authorization: token ${env.PASSWORD}' ${githubRepoApi}")
  •  The response of this call is another API URL, which I created another URL with the "teamMembersApi"
  def teamMembers = sh("curl -H 'Authorization: token ${env.PASSWORD}' ${teamMembersApi}")
  • At this point, the value of ${env.PASSWORD} is masked and, as a consequence, the second call fails because of invalid credentials

Questions

As I understand, this is a result of the "masking" of values when accessed via any method that will cause a "toString()" will make it not available for reuse in Strings..

  • How can I reuse the same credentials, even when they are eligible to be masked?

I tried using the httpRequest step to not refer to variable and I get the following error with a MalformedURLException with a clearly well-formed URL... I made sure the URL was in String format and has the protocol...

java.net.MalformedURLException: no protocol: https://github.company.com/org/repo

Thank you...

0 comments

Please sign in to leave a comment.