dongyiyu3953 2019-01-08 22:17
浏览 56

结合使用加密/ ssh和代理身份验证,同时回退到加密密钥身份验证

I am implementing an SSH client which will use agent authentication (if available) and fallback to public key authentication if that fails. I have found that I can do this using multiple signers like the following:

sshConfig := &ssh.ClientConfig{
    User:            getUsername(username, currentUser),
    HostKeyCallback: ssh.InsecureIgnoreHostKey(),
    Auth: []ssh.AuthMethod{
        ssh.PublicKeysCallback(getSigners),
    },
}

func getSigners() ([]ssh.Signer, error) {
    signers := make([]ssh.Signer, 0)
    currentUser, _ := user.Current()
    if os.Getenv("SSH_AUTH_SOCK") != "" {
        sshAgent, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK"))
        if err == nil {
            agentSigners, _ := agent.NewClient(sshAgent).Signers()
            signers = append(signers, agentSigners...)
        }
    }

    // default to id_rsa
    keyPath := path.Join(path.Join(currentUser.HomeDir, ".ssh/id_rsa"))

    buffer, errI := ioutil.ReadFile(keyPath)
    if errI != nil {
        fmt.Println(errI)
        return signers, errI
    }

    block, _ := pem.Decode(buffer)

    var key ssh.Signer
    if strings.Contains(block.Headers["Proc-Type"], "ENCRYPTED") {
        fmt.Print("SSH Passphrase: ")
        bytePassword, _ := terminal.ReadPassword(int(syscall.Stdin))
        key, _ = ssh.ParsePrivateKeyWithPassphrase(buffer, bytePassword)
    } else {
        key, _ = ssh.ParsePrivateKey(buffer)
    }

    signers = append(signers, key)
    return signers, nil
}

The issue with this method is that it will always prompt for a passphrase, since the passphrase prompt occurs before any authentication attempts have been made. Is it possible to delay when the public key gets decrypted so that agent authentication can be attempted before the user is asked to enter a passphrase?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
    • ¥20 西门子S7-Graph,S7-300,梯形图
    • ¥50 用易语言http 访问不了网页
    • ¥50 safari浏览器fetch提交数据后数据丢失问题
    • ¥15 matlab不知道怎么改,求解答!!
    • ¥15 永磁直线电机的电流环pi调不出来
    • ¥15 用stata实现聚类的代码
    • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
    • ¥20 docker里部署springboot项目,访问不到扬声器
    • ¥15 netty整合springboot之后自动重连失效