@@ -3,6 +3,7 @@ package provider
33import (
44 "context"
55 "fmt"
6+ "io/ioutil"
67
78 "github.com/bramvdbogaerde/go-scp"
89 "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
@@ -57,21 +58,41 @@ func configure(version string, p *schema.Provider) func(context.Context, *schema
5758}
5859
5960func (c apiClient ) fromResourceData (d * schema.ResourceData ) (* apiClient , error ) {
60- signer , err := ssh .ParsePrivateKey ([]byte (d .Get ("conn.0.private_key" ).(string )))
61+ clientConfig := ssh.ClientConfig {
62+ User : d .Get ("conn.0.username" ).(string ),
63+ HostKeyCallback : ssh .InsecureIgnoreHostKey (),
64+ }
6165
62- if err != nil {
63- return nil , fmt .Errorf ("couldn't create a ssh client config: %s" , err .Error ())
66+ password , ok := d .GetOk ("conn.0.password" )
67+ if ok {
68+ clientConfig .Auth = append (clientConfig .Auth , ssh .Password (password .(string )))
69+ }
70+
71+ private_key , ok := d .GetOk ("conn.0.private_key" )
72+ if ok {
73+ signer , err := ssh .ParsePrivateKey ([]byte (private_key .(string )))
74+ if err != nil {
75+ return nil , fmt .Errorf ("couldn't create a ssh client config from private key: %s" , err .Error ())
76+ }
77+ clientConfig .Auth = append (clientConfig .Auth , ssh .PublicKeys (signer ))
78+ }
79+
80+ private_key_path , ok := d .GetOk ("conn.0.private_key_path" )
81+ if ok {
82+ content , err := ioutil .ReadFile (private_key_path .(string ))
83+ if err != nil {
84+ return nil , fmt .Errorf ("couldn't read private key: %s" , err .Error ())
85+ }
86+ signer , err := ssh .ParsePrivateKey (content )
87+ if err != nil {
88+ return nil , fmt .Errorf ("couldn't create a ssh client config from private key file: %s" , err .Error ())
89+ }
90+ clientConfig .Auth = append (clientConfig .Auth , ssh .PublicKeys (signer ))
6491 }
6592
6693 client := apiClient {
67- clientConfig : ssh.ClientConfig {
68- User : d .Get ("conn.0.username" ).(string ),
69- Auth : []ssh.AuthMethod {
70- ssh .PublicKeys (signer ),
71- },
72- HostKeyCallback : ssh .InsecureIgnoreHostKey (),
73- },
74- host : fmt .Sprintf ("%s:%d" , d .Get ("conn.0.host" ).(string ), d .Get ("conn.0.port" ).(int )),
94+ clientConfig : clientConfig ,
95+ host : fmt .Sprintf ("%s:%d" , d .Get ("conn.0.host" ).(string ), d .Get ("conn.0.port" ).(int )),
7596 }
7697
7798 return & client , nil
0 commit comments