From c30fc51b035e0bc1f8d35cb10b4bec5c7d1c6820 Mon Sep 17 00:00:00 2001 From: Kiril Vladimirov Date: Sat, 21 Feb 2015 19:22:16 +0200 Subject: [PATCH 1/2] fix(datasource/CloudSigma): Make sure public ssh key is not empty Even when public ssh key is not set by the user, CloudSigma's server context has a key `meta.ssh_public_key` which is just an empty string. So instead of just relying on the "comma ok" idiom I make sure the value is not an empty string. --- datasource/metadata/cloudsigma/server_context.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/datasource/metadata/cloudsigma/server_context.go b/datasource/metadata/cloudsigma/server_context.go index b618e45..d1a5015 100644 --- a/datasource/metadata/cloudsigma/server_context.go +++ b/datasource/metadata/cloudsigma/server_context.go @@ -108,7 +108,7 @@ func (scs *serverContextService) FetchMetadata() (metadata datasource.Metadata, } metadata.SSHPublicKeys = map[string]string{} - if key, ok := inputMetadata.Meta["ssh_public_key"]; ok { + if key, _ := inputMetadata.Meta["ssh_public_key"]; len(key) > 0 { splitted := strings.Split(key, " ") metadata.SSHPublicKeys[splitted[len(splitted)-1]] = key } From be53013431eddf412e29777e7b33d718531fc0d4 Mon Sep 17 00:00:00 2001 From: Kiril Vladimirov Date: Sun, 22 Feb 2015 05:31:38 +0200 Subject: [PATCH 2/2] fix(datasource/CloudSigma): Add a test for an empty ssh key --- .../metadata/cloudsigma/server_context.go | 2 ++ .../cloudsigma/server_context_test.go | 21 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/datasource/metadata/cloudsigma/server_context.go b/datasource/metadata/cloudsigma/server_context.go index d1a5015..855ae97 100644 --- a/datasource/metadata/cloudsigma/server_context.go +++ b/datasource/metadata/cloudsigma/server_context.go @@ -108,6 +108,8 @@ func (scs *serverContextService) FetchMetadata() (metadata datasource.Metadata, } metadata.SSHPublicKeys = map[string]string{} + // CloudSigma uses an empty string, rather than no string, + // to represent the lack of a SSH key if key, _ := inputMetadata.Meta["ssh_public_key"]; len(key) > 0 { splitted := strings.Split(key, " ") metadata.SSHPublicKeys[splitted[len(splitted)-1]] = key diff --git a/datasource/metadata/cloudsigma/server_context_test.go b/datasource/metadata/cloudsigma/server_context_test.go index 726425b..4f29d7f 100644 --- a/datasource/metadata/cloudsigma/server_context_test.go +++ b/datasource/metadata/cloudsigma/server_context_test.go @@ -43,6 +43,27 @@ func (f *fakeCepgoClient) FetchRaw(key string) ([]byte, error) { return f.raw, f.err } +func TestServerContextWithEmptyPublicSSHKey(t *testing.T) { + client := new(fakeCepgoClient) + scs := NewServerContextService() + scs.client = client + client.raw = []byte(`{ + "meta": { + "base64_fields": "cloudinit-user-data", + "cloudinit-user-data": "I2Nsb3VkLWNvbmZpZwoKaG9zdG5hbWU6IGNvcmVvczE=", + "ssh_public_key": "" + } + }`) + metadata, err := scs.FetchMetadata() + if err != nil { + t.Error(err.Error()) + } + + if len(metadata.SSHPublicKeys) != 0 { + t.Error("There should be no Public SSH Keys provided") + } +} + func TestServerContextFetchMetadata(t *testing.T) { client := new(fakeCepgoClient) scs := NewServerContextService()