Skip to content
This repository has been archived by the owner on Jul 20, 2021. It is now read-only.

Commit

Permalink
Removed hardcoded use of private key files with ssh agent.
Browse files Browse the repository at this point in the history
  • Loading branch information
john-terrell committed May 22, 2020
1 parent c34869a commit dc96b13
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 34 deletions.
20 changes: 18 additions & 2 deletions provider.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package main

import (
"net"
"os"

"github.com/hashicorp/terraform/helper/schema"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
)

func Provider() *schema.Provider {
Expand Down Expand Up @@ -43,9 +48,20 @@ func providerDataSources() map[string]*schema.Resource {
}

func providerConfigure(d *schema.ResourceData) (interface{}, error) {
sshSocket := os.Getenv("SSH_AUTH_SOCK")
agentConnection, err := net.Dial("unix", sshSocket)
if err != nil {
return nil, err
}

authMethods := []ssh.AuthMethod{}
authMethods = append(authMethods, ssh.PublicKeysCallback(agent.NewClient(agentConnection).Signers))

client := SmartOSClient{
host: d.Get("host").(string),
user: d.Get("user").(string),
host: d.Get("host").(string),
user: d.Get("user").(string),
agentConnection: agentConnection,
authMethods: authMethods,
}

return &client, nil
Expand Down
43 changes: 11 additions & 32 deletions smartos_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os/user"
"path"
"net"
"regexp"

"github.com/google/uuid"
"golang.org/x/crypto/ssh"
)

type SmartOSClient struct {
host string
user string
client *ssh.Client
host string
user string
client *ssh.Client
agentConnection net.Conn
authMethods []ssh.AuthMethod
}

func (c *SmartOSClient) Connect() error {
Expand All @@ -27,41 +27,20 @@ func (c *SmartOSClient) Connect() error {
return nil
}

log.Println("Creating client")
user, err := user.Current()
if err != nil {
return err
}

keyPath := path.Join(user.HomeDir, ".ssh", "id_rsa")
log.Println("Loading private key from ", keyPath)
keyBytes, err := ioutil.ReadFile(keyPath)
if err != nil {
return err
}

log.Println("Parsing private key")
signer, err := ssh.ParsePrivateKey(keyBytes)
if err != nil {
return err
}

config := &ssh.ClientConfig{
User: c.user,
Auth: []ssh.AuthMethod{
ssh.PublicKeys(signer),
},
User: c.user,
Auth: c.authMethods,
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}

log.Println("Connecting to host: ", c.host)
log.Println("SSH: Connecting to host: ", c.host)
c.client, err = ssh.Dial("tcp", c.host, config)
if err != nil {
log.Println("Connection failed: ", err.Error())
log.Println("SSH: Connection failed: ", err.Error())
return err
}

log.Println("Connected successfully")
log.Println("SSH: Connected successfully")
return nil
}

Expand Down

0 comments on commit dc96b13

Please sign in to comment.