From 3887f85ad6e47933d8172cba61fff7561d53ad17 Mon Sep 17 00:00:00 2001 From: Isobel Redelmeier Date: Fri, 21 Jul 2017 16:55:22 -0700 Subject: [PATCH] Handle new lines in CA provided as string [Finishes #146384923] User should be able to provide contents of CA instead of file reference (github CLI issue #4) --- config/config.go | 3 ++- config/config_test.go | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/config/config.go b/config/config.go index 38bc21f6..4474e269 100644 --- a/config/config.go +++ b/config/config.go @@ -5,6 +5,7 @@ import ( "io/ioutil" "os" "path" + "strings" ) const AuthClient = "credhub_cli" @@ -66,7 +67,7 @@ func (cfg *Config) UpdateTrustedCAs(caCerts []string) error { _, err := os.Stat(cert) if err != nil { - certs = append(certs, string(cert)) + certs = append(certs, strings.Replace(cert, "\\n", "\n", -1)) } else { certContents, err := ioutil.ReadFile(cert) diff --git a/config/config_test.go b/config/config_test.go index 767fcfdf..3ab7e1bf 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -59,6 +59,16 @@ var _ = Describe("Config", func() { Expect(cfg.CaCerts).To(ConsistOf([]string{string(ca1), ca2})) }) + It("handles new lines in certificate strings", func() { + caWithNewLines := `-----BEGIN CERTIFICATE-----\nFAKE CERTIFICATE CONTENTS\n-----END CERTIFICATE-----` + expectedCa := "-----BEGIN CERTIFICATE-----\nFAKE CERTIFICATE CONTENTS\n-----END CERTIFICATE-----" + + err := cfg.UpdateTrustedCAs([]string{caWithNewLines}) + + Expect(err).To(BeNil()) + Expect(cfg.CaCerts).To(ConsistOf([]string{expectedCa})) + }) + It("returns an error if a file can't be read", func() { invalidCaFile, err := ioutil.TempFile("", "no-read-access") Expect(err).To(BeNil())