如何将用户输入的密码与凭据密码进行比较 [英] How do i compare user inputed password to credentials passphrase

查看:127
本文介绍了如何将用户输入的密码与凭据密码进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本化的管道,该管道使用input函数请求用户密码,并将其与用户保存的凭据密码短语进行比较.如果用户输入的类型为string,则它起作用.但是,当我将输入类型更改为password时(因此当用户键入它时,它在屏幕上不可见),它会失败并显示断言错误.

I have a scripted pipeline that requests the user's password using the input function and compares it with the user's saved credentials passphrase. If the user input is a type string then it works. However, when I changed the input type to password (so it isn't visible on the screen when the user types it), it fails with an assert error.

我确保输入与凭证文件中的密码匹配的密码.

I made sure to enter the password that matches the one in the credentials file.

我正在将Jenkins 2.83与最新的Credentials插件和Credentials绑定插件一起使用.

I am using Jenkins 2.83 with latest Credentials Plugin and Credentials Binding Plugin.

脚本:

node 
{
    stage ("Collect User Input")
    {
        userInput = input(  id: 'Input-username', 
                            message: 'Select username', 
                            ok: 'Continue', 
                            parameters: [choice(choices: 'user1\nuser2\nuser3', description: '', name: 'username'),
                                         password(defaultValue: '', description: 'Enter your private key passphrase ', name: 'password')
                                         ], 
                            submitterParameter: 'approver')


        println("User Input is: " + userInput)

        withCredentials(bindings: [sshUserPrivateKey(credentialsId: 'my-test-key', 
                                                   keyFileVariable: 'cred_keyfile',
                                                passphraseVariable: 'cred_passphrase',
                                                  usernameVariable: 'cred_username' )])
        {
            assert userInput.password==cred_passphrase
        }
    }
}

我得到了错误:

hudson.remoting.ProxyException: Assertion failed: 

assert userInput.password==cred_passphrase

    at org.codehaus.groovy.runtime.InvokerHelper.assertFailed(InvokerHelper.java:404)
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.assertFailed(ScriptBytecodeAdapter.java:650)
    at com.cloudbees.groovy.cps.impl.AssertBlock$ContinuationImpl.fail(AssertBlock.java:47)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
    at com.cloudbees.groovy.cps.impl.ConstantBlock.eval(ConstantBlock.java:21)
    at com.cloudbees.groovy.cps.Next.step(Next.java:83)
    at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:174)
    at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:163)
    at org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.use(GroovyCategorySupport.java:122)
    at org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:261)
    at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:163)
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access$101(SandboxContinuable.java:34)
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.lambda$run0$0(SandboxContinuable.java:59)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovySandbox.runInSandbox(GroovySandbox.java:108)
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:58)
    at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:174)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:332)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$200(CpsThreadGroup.java:83)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:244)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:232)
    at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:64)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:112)
    at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:748)
Finished: FAILURE

但是,如果我将password输入参数更改为string,它将起作用:

However, if I change the password input parameter to string, it works:

string(defaultValue: '', description: 'Enter your private key passphrase here', name: 'password')

所以我的问题是,我应该如何访问/处理输入参数password变量/名称?

So my question is, how am I supposed to access/handle the input paramter password variable/name?

推荐答案

您的断言失败,因为输入类型password返回的不是字符串,而是

Your assertions fails, because input type password returns not a String, but hudson.util.Secret object. If you want to compare input password with cred_passphrase you should do something like this:

hudson.util.Secret.fromString(cred_passphrase) == userInput.password

使用

It's important to convert cred_passphrase with Secret.fromString(data) to a hudson.util.Secret object, because variable cred_passphrase holds your passphrase as a plain text in a String.

下面您可以找到完整的示例.

Below you can find a full example.

node {
    stage ("Collect User Input") {
        userInput = input(  id: 'Input-username', 
                            message: 'Select username', 
                            ok: 'Continue', 
                            parameters: [choice(choices: 'user1\nuser2\nuser3', description: '', name: 'username'),
                                         password(defaultValue: '', description: 'Enter your private key passphrase ', name: 'password')
                                         ], 
                            submitterParameter: 'approver')


        println("User Input is: " + userInput)

        withCredentials(bindings: [sshUserPrivateKey(credentialsId: 'my-test-key', 
                                                   keyFileVariable: 'cred_keyfile',
                                                passphraseVariable: 'cred_passphrase',
                                                  usernameVariable: 'cred_username' )])
        {
            assert hudson.util.Secret.fromString(cred_passphrase) == userInput.password
        }
    }
}

这篇关于如何将用户输入的密码与凭据密码进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆