Skip to content

Commit

Permalink
Merge pull request #36 from gomicro/ssl-in-files
Browse files Browse the repository at this point in the history
Ssl in files
  • Loading branch information
dan9186 authored Aug 18, 2020
2 parents b6e6444 + c188540 commit 52acba1
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@ routes:
reset: "/a/custom/path/for/reset" # Optional
status: "/a/custom/path/for/status" # Optional
cert: "cert for serving ssl" # Optional
cert_path: "path to file containing cert" # Optional
key: "key for serving ssl" # Optional
key_path: "path to file containing key" # Optional
ca: "a custom CA to include for SSL" # Optional
ca_path: "path to file containing CA(.)(.)" # Optional
```

## Running
Expand Down
27 changes: 27 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ type File struct {
Reset string `yaml:"reset"`
Status string `yaml:"status"`
Cert string `yaml:"cert"`
CertPath string `yaml:"cert_path"`
Key string `yaml:"key"`
KeyPath string `yaml:"key_path"`
CA string `yaml:"ca"`
CAPath string `yaml:"ca_path"`
proxies map[string]*httputil.ReverseProxy `yaml:"-"`
transport *http.Transport `yaml:"-"`
}
Expand Down Expand Up @@ -67,6 +70,30 @@ func ParseFromFile() (*File, error) {

conf.proxies = make(map[string]*httputil.ReverseProxy)

if conf.KeyPath != "" {
key, err := ioutil.ReadFile(conf.KeyPath)
if err != nil {
return nil, fmt.Errorf("Failed to read Key from file: %v", err.Error())
}
conf.Key = string(key)
}

if conf.CertPath != "" {
cert, err := ioutil.ReadFile(conf.CertPath)
if err != nil {
return nil, fmt.Errorf("Ffailed to read Cert from file: %v", err.Error())
}
conf.Cert = string(cert)
}

if conf.CAPath != "" {
ca, err := ioutil.ReadFile(conf.CAPath)
if err != nil {
return nil, fmt.Errorf("Failed to read CA(s) from file: %v", err.Error())
}
conf.CA = string(ca)
}

pool := x509.NewCertPool()
if conf.CA != "" {
ok := pool.AppendCertsFromPEM([]byte(conf.CA))
Expand Down
13 changes: 12 additions & 1 deletion config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package config_test

import (
"fmt"
"github.com/gomicro/avenues/config"
"net/http"
"net/http/httptest"
"os"
"testing"

"github.com/gomicro/avenues/config"

. "github.com/franela/goblin"
. "github.com/onsi/gomega"
)
Expand Down Expand Up @@ -45,6 +46,16 @@ func TestConfig(t *testing.T) {
Expect(err.Error()).To(ContainSubstring("Failed to read config file"))
Expect(c).To(BeNil())
})

g.It("should parse a config file with path settings", func() {
os.Setenv("AVENUES_CONFIG_FILE", "./routes_other.yaml")

c, err := config.ParseFromFile()
Expect(err).To(BeNil())

Expect(c.CertPath).To(Equal("dummy.cert"))
Expect(c.Cert).To(ContainSubstring("-----BEGIN CERTIFICATE-----"))
})
})

g.Describe("Serving", func() {
Expand Down
25 changes: 25 additions & 0 deletions config/dummy.cert
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
-----BEGIN CERTIFICATE-----
MIIESzCCAzOgAwIBAgIJAJAOazJZ41AYMA0GCSqGSIb3DQEBCwUAMIG7MQswCQYD
VQQGEwItLTESMBAGA1UECAwJU29tZVN0YXRlMREwDwYDVQQHDAhTb21lQ2l0eTEZ
MBcGA1UECgwQU29tZU9yZ2FuaXphdGlvbjEfMB0GA1UECwwWU29tZU9yZ2FuaXph
dGlvbmFsVW5pdDEeMBwGA1UEAwwVbG9jYWxob3N0LmxvY2FsZG9tYWluMSkwJwYJ
KoZIhvcNAQkBFhpyb290QGxvY2FsaG9zdC5sb2NhbGRvbWFpbjAeFw0yMDA4MTgy
MDMyNDFaFw0yMTA4MTgyMDMyNDFaMIG7MQswCQYDVQQGEwItLTESMBAGA1UECAwJ
U29tZVN0YXRlMREwDwYDVQQHDAhTb21lQ2l0eTEZMBcGA1UECgwQU29tZU9yZ2Fu
aXphdGlvbjEfMB0GA1UECwwWU29tZU9yZ2FuaXphdGlvbmFsVW5pdDEeMBwGA1UE
AwwVbG9jYWxob3N0LmxvY2FsZG9tYWluMSkwJwYJKoZIhvcNAQkBFhpyb290QGxv
Y2FsaG9zdC5sb2NhbGRvbWFpbjCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoC
ggEBANcrwzlqSypcFIjpEGoZrzVRW6Ke+xjMAcZkzg1mINg69yBHtJR0iRYJlK+U
GpHLNCi52BxadL/3K0hVbcpx+9qtSzGWqYBKEsKPjF/q1QvaNOC+agdEupvk6CUV
tV5IsGZycPZSVS47Ai6nF6XEp+uhxO7s5cMzAWaLva/jA54tlaczmcjvoymXAd53
4EC9+nMkSIDaFIAqsq60GTRdaHeusxiwX7wmjY3xIHaMiAtsQimt/y1Zr7bDNyuw
sgYYgS6/Gl9qGj2u+ugr17GEliv0s3FD4FRvlFnbqzSZr/SYNcXeNcyMHjRs+ZAL
mN/ggjcYdeLuzBAZ5hCYgSdhC50CAwEAAaNQME4wHQYDVR0OBBYEFEde6YglAzMg
mGI3wkLpiGF4KaRoMB8GA1UdIwQYMBaAFEde6YglAzMgmGI3wkLpiGF4KaRoMAwG
A1UdEwQFMAMBAf8wDQYJKoZIhvcNAQELBQADggEBAADV7qAUq4DojrVtYj0CTVOT
K/WhjK8/GMCV9jG6wIwF6fUM/ODwop0okp4I98SUO8Jdi+NNA2e0vLr6jOdm1FC/
dio83RasoWAUcyLGX0q/L4aV9ne84hCG0sDRFpwc2OzN+GwXutc/n4wz9ZaWuUok
BF+7VAMHdcJV6QbEFpc97ibDaBlrKknqLsLHuWqJIzWrKVngBz6yFgNy6ZaV/zjR
kFCFGEfihJefAfTKJqBx++hb+uvgblEE3zkMsEn9uaraLU51Jt7zSkv1HtM74S/S
4CiICFcx0KW+9ed9t2caYNmcOEgy2Psh4gT6V7aVAhFYap2ifnHbsLg2FMQoa04=
-----END CERTIFICATE-----
2 changes: 2 additions & 0 deletions config/routes_other.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
routes:
/v1/baz:
backend: http://baz:4567

cert_path: dummy.cert

0 comments on commit 52acba1

Please sign in to comment.