From c9ea93308aa6cb12971bfcb7a9441233e0ddd63a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A1bio=20Matavelli?= Date: Tue, 11 Feb 2020 21:18:04 +0000 Subject: [PATCH] ssh: return error if host is empty --- communicator/ssh/provisioner.go | 8 +++++++- communicator/ssh/provisioner_test.go | 21 +++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/communicator/ssh/provisioner.go b/communicator/ssh/provisioner.go index 1c9d6f501..b6fe80a4a 100644 --- a/communicator/ssh/provisioner.go +++ b/communicator/ssh/provisioner.go @@ -16,7 +16,7 @@ import ( "github.com/hashicorp/terraform/communicator/shared" "github.com/hashicorp/terraform/terraform" "github.com/mitchellh/mapstructure" - "github.com/xanzy/ssh-agent" + sshagent "github.com/xanzy/ssh-agent" "golang.org/x/crypto/ssh" "golang.org/x/crypto/ssh/agent" "golang.org/x/crypto/ssh/knownhosts" @@ -93,6 +93,12 @@ func parseConnectionInfo(s *terraform.InstanceState) (*connectionInfo, error) { connInfo.User = DefaultUser } + // Check if host is empty. + // Otherwise return error. + if connInfo.Host == "" { + return nil, fmt.Errorf("host for provisioner cannot be empty") + } + // Format the host if needed. // Needed for IPv6 support. connInfo.Host = shared.IpFormat(connInfo.Host) diff --git a/communicator/ssh/provisioner_test.go b/communicator/ssh/provisioner_test.go index 9eb5af7c8..f8e0f77d8 100644 --- a/communicator/ssh/provisioner_test.go +++ b/communicator/ssh/provisioner_test.go @@ -131,3 +131,24 @@ func TestProvisioner_connInfoHostname(t *testing.T) { t.Fatalf("bad %v", conf) } } + +func TestProvisioner_connInfoEmptyHostname(t *testing.T) { + r := &terraform.InstanceState{ + Ephemeral: terraform.EphemeralState{ + ConnInfo: map[string]string{ + "type": "ssh", + "user": "root", + "password": "supersecret", + "private_key": "someprivatekeycontents", + "host": "", + "port": "22", + "timeout": "30s", + }, + }, + } + + _, err := parseConnectionInfo(r) + if err == nil { + t.Fatalf("bad: should not allow empty host") + } +}