Skip to content
This repository was archived by the owner on Jan 18, 2023. It is now read-only.

Commit eed139b

Browse files
committed
Support password
1 parent 8a762b6 commit eed139b

File tree

3 files changed

+54
-13
lines changed

3 files changed

+54
-13
lines changed

internal/provider/data_source_remotefile.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,22 @@ func dataSourceRemotefile() *schema.Resource {
3636
Required: true,
3737
Description: "The username on the target host.",
3838
},
39+
"password": {
40+
Type: schema.TypeString,
41+
Optional: true,
42+
Description: "The pasword for the user on the target host.",
43+
},
3944
"private_key": {
4045
Type: schema.TypeString,
41-
Required: true,
46+
Optional: true,
4247
Sensitive: true,
4348
Description: "The private key used to login to the target host.",
4449
},
50+
"private_key_path": {
51+
Type: schema.TypeString,
52+
Optional: true,
53+
Description: "The path of the private key used to login to the target host.",
54+
},
4555
},
4656
},
4757
},

internal/provider/provider.go

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package provider
33
import (
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

5960
func (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

internal/provider/resource_remotefile.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,22 @@ func resourceRemotefile() *schema.Resource {
4141
Required: true,
4242
Description: "The username on the target host.",
4343
},
44+
"password": {
45+
Type: schema.TypeString,
46+
Optional: true,
47+
Description: "The pasword for the user on the target host.",
48+
},
4449
"private_key": {
4550
Type: schema.TypeString,
46-
Required: true,
51+
Optional: true,
4752
Sensitive: true,
4853
Description: "The private key used to login to the target host.",
4954
},
55+
"private_key_path": {
56+
Type: schema.TypeString,
57+
Optional: true,
58+
Description: "The path of the private key used to login to the target host.",
59+
},
5060
},
5161
},
5262
},

0 commit comments

Comments
 (0)