Skip to content

Commit

Permalink
updated error message
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewChubatiuk committed Dec 25, 2021
1 parent fd1b05c commit 52f2c49
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 30 deletions.
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ func main() {
flag.Parse()
if ppid == 0 {
if ppid, err = strconv.Atoi(os.Getenv("TF_SSH_PROVIDER_TUNNEL_PPID")); err != nil {
log.Fatalf("[ERROR] Parent process pid wasn't set")
log.Fatalf("[ERROR] parent process pid wasn't set")
}
}
if addr == "" {
log.Fatalf("[ERROR] RPC server address wasn't set")
}
var sshTunnel ssh.SSHTunnel
if err := sshTunnel.Run(proto, addr, ppid); err != nil {
log.Fatalf("[ERROR] Failed to start SSH Tunnel:\n%s", err)
log.Fatalf("[ERROR] failed to start SSH Tunnel:\n%s", err)
}
}
}
31 changes: 16 additions & 15 deletions provider/data_source_ssh_tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ import (
"bufio"
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/stefansundin/terraform-provider-ssh/ssh"
"io"
"log"
"net"
Expand All @@ -16,13 +12,18 @@ import (
"os/exec"
"os/user"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/stefansundin/terraform-provider-ssh/ssh"
)

func dataSourceSSHTunnel() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceSSHTunnelRead,
Schema: map[string]*schema.Schema{
"user": &schema.Schema{
"user": {
Type: schema.TypeString,
Optional: true,
Description: "SSH connection username",
Expand All @@ -37,7 +38,7 @@ func dataSourceSSHTunnel() *schema.Resource {
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"sock": &schema.Schema{
"sock": {
Type: schema.TypeString,
Optional: true,
Description: "Attempt to use the SSH agent (using the SSH_AUTH_SOCK environment variable)",
Expand All @@ -59,13 +60,13 @@ func dataSourceSSHTunnel() *schema.Resource {
Description: "The private SSH key",
Sensitive: true,
},
"password": &schema.Schema{
"password": {
Type: schema.TypeString,
Optional: true,
Description: "The private SSH key password",
Sensitive: true,
},
"certificate": &schema.Schema{
"certificate": {
Type: schema.TypeString,
Optional: true,
Description: "A signed SSH certificate",
Expand All @@ -74,7 +75,7 @@ func dataSourceSSHTunnel() *schema.Resource {
},
},
},
"password": &schema.Schema{
"password": {
Type: schema.TypeString,
Optional: true,
Description: "The private SSH key password",
Expand Down Expand Up @@ -287,11 +288,11 @@ func dataSourceSSHTunnelRead(ctx context.Context, d *schema.ResourceData, m inte
cmd := exec.Command("sh", "-c", os.Args[0])
stdout, err := cmd.StdoutPipe()
if err != nil {
return diag.FromErr(fmt.Errorf("Failed to get stdout of SSH Tunnel:\n%v", err))
return diag.FromErr(fmt.Errorf("failed to get stdout of SSH Tunnel:\n%v", err))
}
stderr, err := cmd.StderrPipe()
if err != nil {
return diag.FromErr(fmt.Errorf("Failed to get stderr of SSH Tunnel:\n%v", err))
return diag.FromErr(fmt.Errorf("failed to get stderr of SSH Tunnel:\n%v", err))
}
env := []string{
fmt.Sprintf("TF_SSH_PROVIDER_TUNNEL_PROTO=%s", proto),
Expand All @@ -307,7 +308,7 @@ func dataSourceSSHTunnelRead(ctx context.Context, d *schema.ResourceData, m inte
redirectStd := func(std io.ReadCloser) {
in := bufio.NewScanner(std)
for in.Scan() {
log.Printf(in.Text())
log.Println(in.Text())
}
if err := in.Err(); err != nil {
log.Printf("[ERROR] %s", err)
Expand All @@ -329,11 +330,11 @@ func dataSourceSSHTunnelRead(ctx context.Context, d *schema.ResourceData, m inte

go func() {
<-timer.C
commandError = fmt.Errorf("Timed out during a tunnel setup")
commandError = fmt.Errorf("timed out during a tunnel setup")
}()

for !tunnelServer.Ready {
log.Printf("[DEBUG] Waiting for local port availability")
log.Printf("[DEBUG] waiting for local port availability")
if commandError != nil {
return diag.FromErr(commandError)
}
Expand All @@ -342,7 +343,7 @@ func dataSourceSSHTunnelRead(ctx context.Context, d *schema.ResourceData, m inte

tunnelServerInbound.Close()

log.Printf("[DEBUG] Local port: %v", sshTunnel.Local.Port)
log.Printf("[DEBUG] local port: %v", sshTunnel.Local.Port)
d.Set("local", flattenEndpoint(sshTunnel.Local))
d.SetId(sshTunnel.Local.Address())

Expand Down
26 changes: 13 additions & 13 deletions ssh/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,25 +88,25 @@ func (pk SSHPrivateKey) Enabled() bool {
func (pk SSHPrivateKey) Authenticate() (methods []ssh.AuthMethod, err error) {
var signer ssh.Signer
if pk.Password != "" {
log.Println("[DEBUG] Using private key with password for authentication")
log.Println("[DEBUG] using private key with password for authentication")
signer, err = ssh.ParsePrivateKeyWithPassphrase([]byte(pk.PrivateKey), []byte(pk.Password))
} else {
log.Println("[DEBUG] Using private key without password for authentication")
log.Println("[DEBUG] using private key without password for authentication")
signer, err = ssh.ParsePrivateKey([]byte(pk.PrivateKey))
}
if err != nil {
return nil, fmt.Errorf("Failed to parse private key:\n%v", err)
return nil, fmt.Errorf("failed to parse private key:\n%v", err)
}
methods = append(methods, ssh.PublicKeys(signer))
if pk.Certificate != "" {
log.Println("[DEBUG] Using client certificate for authentication")
log.Println("[DEBUG] using client certificate for authentication")
pcert, _, _, _, err := ssh.ParseAuthorizedKey([]byte(pk.Certificate))
if err != nil {
return nil, fmt.Errorf("Failed to parse certificate %q: %s", pk.Certificate, err)
return nil, fmt.Errorf("failed to parse certificate %q: %s", pk.Certificate, err)
}
certSigner, err := ssh.NewCertSigner(pcert.(*ssh.Certificate), signer)
if err != nil {
return nil, fmt.Errorf("Failed to create cert signer %q: %s", certSigner, err)
return nil, fmt.Errorf("failed to create cert signer %q: %s", certSigner, err)
}
methods = append(methods, ssh.PublicKeys(certSigner))
}
Expand All @@ -127,20 +127,20 @@ type SSHTunnel struct {
}

func (st *SSHTunnel) Run(proto, serverAddress string, ppid int) error {
log.Println("[DEBUG] Creating SSH Tunnel")
log.Println("[DEBUG] creating SSH Tunnel")
var ack bool
gob.Register(SSHPrivateKey{})
gob.Register(SSHAuthSock{})
gob.Register(SSHPassword{})
client, err := rpc.Dial("tcp", serverAddress)
if err != nil {
log.Fatal("[ERROR] Failed to connect to RPC server:\n", err)
log.Fatal("[ERROR] failed to connect to RPC server:\n", err)
}

defer client.Close()
err = client.Call("SSHTunnelServer.GetSSHTunnel", &ack, &st)
if err != nil {
log.Fatal("[ERROR] Failed to execute a RPC call:\n", err)
log.Fatal("[ERROR] failed to execute a RPC call:\n", err)
}

sshConf := &ssh.ClientConfig{
Expand Down Expand Up @@ -193,19 +193,19 @@ func (st *SSHTunnel) Run(proto, serverAddress string, ppid int) error {

err = client.Call("SSHTunnelServer.PutSSHReady", st.Local.Port, &ack)
if err != nil {
log.Fatal("[ERROR] Failed to execute a RPC call:\n", err)
log.Fatal("[ERROR] failed to execute a RPC call:\n", err)
}

go func(pid int) {
for {
process, err := os.FindProcess(pid)
if err != nil {
log.Printf("Failed to find process. Closing server: %s\n", err)
log.Printf("failed to find process. Closing server: %s\n", err)
localListener.Close()
return
}
if err := process.Signal(syscall.Signal(0)); err != nil {
log.Printf("Process %d is not alive anymore: %v\n", pid, err)
log.Printf("process %d is not alive anymore: %v\n", pid, err)
localListener.Close()
return
}
Expand All @@ -216,7 +216,7 @@ func (st *SSHTunnel) Run(proto, serverAddress string, ppid int) error {
localConn, err := localListener.Accept()
if err != nil {
if errors.Is(err, net.ErrClosed) {
log.Printf("Stopping connection loop")
log.Printf("stopping connection loop")
break
}
log.Printf("error accepting connection: %s", err)
Expand Down

0 comments on commit 52f2c49

Please sign in to comment.