1 回答
TA贡献1883条经验 获得超3个赞
这可以通过使用下面的代码片段/功能来实现。
GenerateRSAKeyPairE: func RSAKeyPairFromFile(fpath string) (*terrassh.KeyPair, error) { // import crypto/x509 // import enter code hereio/ioutil // import encoding/pem // import "golang.org/x/crypto/ssh" / /terrassh“github.com/gruntwork-io/terratest/modules/ssh”
pemBytes, err := ioutil.ReadFile(fpath)
if err != nil {
return nil, err
}
pemBlock, _ := pem.Decode(pemBytes)
if pemBlock == nil {
return nil, fmt.Errorf("failed to decode PEM block containing private key")
}
privKey, err := x509.ParsePKCS1PrivateKey(pemBlock.Bytes)
if err != nil {
return nil, err
}
sshPubKey, err := ssh.NewPublicKey(privKey.Public())
if err != nil {
return nil, err
}
sshPubKeyBytes := ssh.MarshalAuthorizedKey(sshPubKey)
sshPubKeyStr := string(sshPubKeyBytes)
return &terrassh.KeyPair{PublicKey: sshPubKeyStr, PrivateKey: string(pemBytes)}, nil
}
- 1 回答
- 0 关注
- 204 浏览
添加回答
举报
