|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "crypto/tls" |
| 5 | + "crypto/x509" |
| 6 | + "fmt" |
| 7 | + "io/ioutil" |
| 8 | + "log" |
| 9 | + "net/http" |
| 10 | + "os" |
| 11 | + "time" |
| 12 | + |
4 | 13 | "github.com/miniclip/gonsul/app" |
5 | 14 | "github.com/miniclip/gonsul/internal/config" |
6 | 15 | "github.com/miniclip/gonsul/internal/exporter" |
7 | 16 | "github.com/miniclip/gonsul/internal/importer" |
8 | 17 | "github.com/miniclip/gonsul/internal/util" |
9 | | - |
10 | | - "fmt" |
11 | | - "net/http" |
12 | | - "os" |
13 | | - "time" |
14 | 18 | ) |
15 | 19 |
|
16 | 20 | func main() { |
@@ -41,9 +45,31 @@ func start() { |
41 | 45 | return |
42 | 46 | } |
43 | 47 |
|
| 48 | + var certificate tls.Certificate |
| 49 | + var caCertPool *x509.CertPool |
| 50 | + if len(cfg.GetKeyFile()) != 0 && len(cfg.GetCaFile()) != 0 && len(cfg.GetCertFile()) != 0 { |
| 51 | + cert, err := ioutil.ReadFile(cfg.GetCaFile()) |
| 52 | + if err != nil { |
| 53 | + log.Fatalf("could not open certificate file: %v", err) |
| 54 | + } |
| 55 | + caCertPool = x509.NewCertPool() |
| 56 | + caCertPool.AppendCertsFromPEM(cert) |
| 57 | + |
| 58 | + certificate, err = tls.LoadX509KeyPair(cfg.GetCertFile(), cfg.GetKeyFile()) |
| 59 | + if err != nil { |
| 60 | + log.Fatalf("could not load certificate: %v", err) |
| 61 | + } |
| 62 | + } |
| 63 | + |
44 | 64 | // Build all dependencies for our application |
45 | 65 | hookHttpServer := app.NewHookHttp(cfg, logger) |
46 | | - httpClient := &http.Client{Timeout: time.Second * time.Duration(cfg.GetTimeout())} |
| 66 | + httpClient := &http.Client{Transport: &http.Transport{ |
| 67 | + TLSClientConfig: &tls.Config{ |
| 68 | + RootCAs: caCertPool, |
| 69 | + Certificates: []tls.Certificate{certificate}, |
| 70 | + }, |
| 71 | + }, Timeout: time.Second * time.Duration(cfg.GetTimeout())} |
| 72 | + |
47 | 73 | exp := exporter.NewExporter(cfg, logger) |
48 | 74 | imp := importer.NewImporter(cfg, logger, httpClient) |
49 | 75 | sigChannel := make(chan os.Signal) |
|
0 commit comments