简体   繁体   English

如何将我的 AWS EC2 密钥对 PEM 文件加载到我的 terratest 脚本以执行 AWS EC2 SSH 连接验证

[英]How can I load my AWS EC2 Key pair PEM file to my terratest script to perform AWS EC2 SSH connection validation

I am writing Go terratest script to validate SSH connection for AWS EC2 instance我正在编写 Go terratest 脚本来验证 AWS EC2 实例的 SSH 连接

I already have AWS EC2 keypair PEM file in my local我的本地已经有 AWS EC2 密钥对 PEM 文件

I am able to launch and destroy EC2 instance using terraform.TgApplyAll() and terraform.TgDestroyAll() methods and fetch the output variables using terraform.Output() method I am able to launch and destroy EC2 instance using terraform.TgApplyAll() and terraform.TgDestroyAll() methods and fetch the output variables using terraform.Output() method

My local AWS EC2 keypair PEM file is used for creating EC2 instance in AWS我的本地 AWS EC2 密钥对 PEM 文件用于在 AWS 中创建 EC2 实例

Now I am trying to validate SSH connection Programmatically from terratest Go script.现在我正在尝试从 terratest Go 脚本以编程方式验证 SSH 连接。

I am NOT able to load my local AWS EC2 Key pair to sshKeyPair variable in Go terratest script无法将本地 AWS EC2 密钥对加载到 Go terratest 脚本中的sshKeyPair变量

I used below code snippet but no use我使用了下面的代码片段但没有用

https://github.com/gruntwork-io/module-asg/blob/067647b3aaeb24151badbc5a2d9a6b5381dd2041/test/server_group_test.go#L78 https://github.com/gruntwork-io/module-asg/blob/067647b3aaeb24151badbc5a2d9a6b5381dd2041/test/server_group_test.go#L78

I also tried script present in at https://github.com/gruntwork-io/terratest/blob/907c09f0696083a5ada580debb66bb5c00c19c32/modules/test-structure/save_test_data.go#L66 to load my local EC2 key pair using LoadEc2KeyPair and test EC2 SSH using fmt.Sprintf("SSH to public host %s", publicIP) But getting error while reading EC2 keypair from local in LoadTestData(t testing.TestingT, path string, value interface{}) method at json.Unmarshal(bytes, value) built in call I also tried script present in at https://github.com/gruntwork-io/terratest/blob/907c09f0696083a5ada580debb66bb5c00c19c32/modules/test-structure/save_test_data.go#L66 to load my local EC2 key pair using LoadEc2KeyPair and test EC2 SSH using fmt.Sprintf("SSH to public host %s", publicIP)但是在json.Unmarshal(bytes, value)LoadTestData(t testing.TestingT, path string, value interface{})方法中从本地读取 EC2 密钥对时出错内置调用

Error message: Failed to parse JSON for value D:\AWS\KeyPair\pandukeypair.pem: invalid character '-' in numeric literal I am getting this error as I am trying to ream.pem file and code is trying to do json.umarshal on the.pem file错误消息:无法解析 JSON 的值 D:\AWS\KeyPair\pandukeypair.pem:数字文字中的无效字符 '-' 我在尝试 ream.pem 文件和代码尝试执行 json 时收到此错误。 .pem 文件上的 umarshal

All code snippets available in github/terratest modules talks about creating new keypair and loading AWS EC2 JSON keypair as but i am not getting any approach/Logic for my scenario where already existing keypair JSON is present I just want to load and use it. github/terratest 模块中可用的所有代码片段都讨论了创建新的密钥对和加载 AWS EC2 JSON密钥对,但我没有得到任何方法/逻辑,因为我的场景已经存在密钥对 JSON 我只想加载和使用它。

Full Code is present in below link完整代码在下面的链接中

https://www.dropbox.com/sh/dl2mpesidsxitdu/AAAOi4Nmp41CHMSPcyU7a2qva?dl=0 https://www.dropbox.com/sh/dl2mpesidsxitdu/AAAOi4Nmp41CHMSPcyU7a2qva?dl=0

This can be achieved by using below Code snippet/functio..这可以通过使用下面的代码片段/功能来实现。

GenerateRSAKeyPairE: func RSAKeyPairFromFile(fpath string) (*terrassh.KeyPair, error) { // import crypto/x509 // import enter code here io/ioutil // import encoding/pem // import "golang.org/x/crypto/ssh" // terrassh "github.com/gruntwork-io/terratest/modules/ssh" GenerateRSAKeyPairE: func RSAKeyPairFromFile(fpath string) (*terrassh.KeyPair, error) { // import crypto/x509 // import enter code here io/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

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM