Skip to content

Commit

Permalink
Use key instead of path
Browse files Browse the repository at this point in the history
  • Loading branch information
tenstad committed Apr 30, 2021
1 parent 632cd9a commit 52e3de8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
7 changes: 4 additions & 3 deletions internal/provider/data_source_remotefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ func dataSourceRemotefile() *schema.Resource {
DefaultFunc: schema.EnvDefaultFunc("REMOTEFILE_USERNAME", nil),
Description: "The username on the target host. May alternatively be set via the `REMOTEFILE_USERNAME` environment variable.",
},
"private_key_path": {
"private_key": {
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("REMOTEFILE_PRIVATE_KEY_PATH", nil),
Description: "The path to the private key used to login to target host. May alternatively be set via the `REMOTEFILE_PRIVATE_KEY_PATH` environment variable.",
Sensitive: true,
DefaultFunc: schema.EnvDefaultFunc("REMOTEFILE_PRIVATE_KEY", nil),
Description: "The private key used to login to target host. May alternatively be set via the `REMOTEFILE_PRIVATE_KEY` environment variable.",
},
"host": {
Type: schema.TypeString,
Expand Down
14 changes: 10 additions & 4 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"

"github.com/bramvdbogaerde/go-scp"
"github.com/bramvdbogaerde/go-scp/auth"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/pkg/sftp"
Expand Down Expand Up @@ -58,14 +57,21 @@ func configure(version string, p *schema.Provider) func(context.Context, *schema
}

func (c apiClient) fromResourceData(d *schema.ResourceData) (*apiClient, error) {
clientConfig, err := auth.PrivateKey(d.Get("username").(string), d.Get("private_key_path").(string), ssh.InsecureIgnoreHostKey())
signer, err := ssh.ParsePrivateKey([]byte(d.Get("private_key").(string)))

if err != nil {
return nil, fmt.Errorf("couldn't create a ssh client config: %s", err.Error())
}

client := apiClient{
clientConfig: clientConfig,
host: fmt.Sprintf("%s:%d", d.Get("host").(string), d.Get("port").(int)),
clientConfig: ssh.ClientConfig{
User: d.Get("username").(string),
Auth: []ssh.AuthMethod{
ssh.PublicKeys(signer),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
},
host: fmt.Sprintf("%s:%d", d.Get("host").(string), d.Get("port").(int)),
}

return &client, nil
Expand Down
7 changes: 4 additions & 3 deletions internal/provider/resource_remotefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ func resourceRemotefile() *schema.Resource {
DefaultFunc: schema.EnvDefaultFunc("REMOTEFILE_USERNAME", nil),
Description: "The username on the target host. May alternatively be set via the `REMOTEFILE_USERNAME` environment variable.",
},
"private_key_path": {
"private_key": {
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("REMOTEFILE_PRIVATE_KEY_PATH", nil),
Description: "The path to the private key used to login to target host. May alternatively be set via the `REMOTEFILE_PRIVATE_KEY_PATH` environment variable.",
Sensitive: true,
DefaultFunc: schema.EnvDefaultFunc("REMOTEFILE_PRIVATE_KEY", nil),
Description: "The private key used to login to target host. May alternatively be set via the `REMOTEFILE_PRIVATE_KEY` environment variable.",
},
"host": {
Type: schema.TypeString,
Expand Down

0 comments on commit 52e3de8

Please sign in to comment.