From 52e3de807d7385a85d3e339c549b18991c71a202 Mon Sep 17 00:00:00 2001 From: Amund Tenstad Date: Fri, 30 Apr 2021 14:22:16 +0200 Subject: [PATCH] Use key instead of path --- internal/provider/data_source_remotefile.go | 7 ++++--- internal/provider/provider.go | 14 ++++++++++---- internal/provider/resource_remotefile.go | 7 ++++--- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/internal/provider/data_source_remotefile.go b/internal/provider/data_source_remotefile.go index 5bdb372..4107ae1 100644 --- a/internal/provider/data_source_remotefile.go +++ b/internal/provider/data_source_remotefile.go @@ -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, diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 820c977..64cb2a0 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -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" @@ -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 diff --git a/internal/provider/resource_remotefile.go b/internal/provider/resource_remotefile.go index 7e6eb23..09cf2aa 100644 --- a/internal/provider/resource_remotefile.go +++ b/internal/provider/resource_remotefile.go @@ -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,