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 1/4] 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") + } +} From 18f72b1cb772313924a1f809bb0030dbbbb58e02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A1bio=20Matavelli?= Date: Wed, 12 Feb 2020 10:06:30 +0000 Subject: [PATCH 2/4] Fix communicator test --- communicator/ssh/communicator_test.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/communicator/ssh/communicator_test.go b/communicator/ssh/communicator_test.go index bbe821363..c9db19df0 100644 --- a/communicator/ssh/communicator_test.go +++ b/communicator/ssh/communicator_test.go @@ -691,6 +691,7 @@ func TestScriptPath(t *testing.T) { Ephemeral: terraform.EphemeralState{ ConnInfo: map[string]string{ "type": "ssh", + "host": "127.0.0.1", "script_path": tc.Input, }, }, @@ -715,7 +716,14 @@ func TestScriptPath_randSeed(t *testing.T) { // Pre GH-4186 fix, this value was the deterministic start the pseudorandom // chain of unseeded math/rand values for Int31(). staticSeedPath := "/tmp/terraform_1298498081.sh" - c, err := New(&terraform.InstanceState{}) + c, err := New(&terraform.InstanceState{ + Ephemeral: terraform.EphemeralState{ + ConnInfo: map[string]string{ + "type": "ssh", + "host": "127.0.0.1", + }, + }, + }) if err != nil { t.Fatalf("err: %s", err) } From 48c187efe8598dd7f845a02d5da75120d66f7cd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A1bio=20Matavelli?= Date: Wed, 12 Feb 2020 10:07:17 +0000 Subject: [PATCH 3/4] Add invalid host test --- communicator/ssh/communicator_test.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/communicator/ssh/communicator_test.go b/communicator/ssh/communicator_test.go index c9db19df0..445fbebb9 100644 --- a/communicator/ssh/communicator_test.go +++ b/communicator/ssh/communicator_test.go @@ -149,6 +149,25 @@ func TestNew_Invalid(t *testing.T) { } } +func TestNew_InvalidHost(t *testing.T) { + r := &terraform.InstanceState{ + Ephemeral: terraform.EphemeralState{ + ConnInfo: map[string]string{ + "type": "ssh", + "user": "user", + "password": "i-am-invalid", + "port": "22", + "timeout": "30s", + }, + }, + } + + _, err := New(r) + if err == nil { + t.Fatal("should have had an error creating communicator") + } +} + func TestStart(t *testing.T) { address := newMockLineServer(t, nil, testClientPublicKey) parts := strings.Split(address, ":") From aefdef1044d46bec9aa42278df375a7155abd47a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A1bio=20Matavelli?= Date: Wed, 12 Feb 2020 10:49:58 +0000 Subject: [PATCH 4/4] Add missing host to communicator test --- communicator/communicator_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/communicator/communicator_test.go b/communicator/communicator_test.go index 659222421..e20d0368b 100644 --- a/communicator/communicator_test.go +++ b/communicator/communicator_test.go @@ -16,6 +16,7 @@ func TestCommunicator_new(t *testing.T) { Ephemeral: terraform.EphemeralState{ ConnInfo: map[string]string{ "type": "telnet", + "host": "127.0.0.1", }, }, }