This repository has been archived by the owner on Feb 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathconfig.go
99 lines (81 loc) · 2.29 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package butteredscones
import (
"crypto/tls"
"crypto/x509"
"encoding/json"
"encoding/pem"
"fmt"
"io/ioutil"
"os"
)
type Configuration struct {
State string `json:"state"`
Network NetworkConfiguration `json:"network"`
Statistics StatisticsConfiguration `json:"statistics"`
Files []FileConfiguration `json:"files"`
MaxLength int `json:"max_length"`
}
type NetworkConfiguration struct {
Servers []ServerConfiguration `json:"servers"`
Certificate string `json:"certificate"`
Key string `json:"key"`
CA string `json:"ca"`
Timeout int `json:"timeout"`
SpoolSize int `json:"spool_size"`
}
type ServerConfiguration struct {
Addr string `json:"addr"`
Name string `json:"name"`
}
type StatisticsConfiguration struct {
Addr string `json:"addr"`
}
type FileConfiguration struct {
Paths []string `json:"paths"`
Fields map[string]string `json:"fields"`
}
func LoadConfiguration(configFile string) (*Configuration, error) {
file, err := os.Open(configFile)
if err != nil {
return nil, err
}
defer file.Close()
decoder := json.NewDecoder(file)
configuration := new(Configuration)
err = decoder.Decode(configuration)
if err != nil {
return nil, err
}
return configuration, nil
}
func (c *Configuration) BuildTLSConfig() (*tls.Config, error) {
if c.Network.Certificate == "" || c.Network.Key == "" {
return nil, fmt.Errorf("certificate and key not specified")
}
cert, err := tls.LoadX509KeyPair(c.Network.Certificate, c.Network.Key)
if err != nil {
return nil, err
}
tlsConfig := new(tls.Config)
tlsConfig.Certificates = []tls.Certificate{cert}
if c.Network.CA != "" {
tlsConfig.RootCAs = x509.NewCertPool()
data, err := ioutil.ReadFile(c.Network.CA)
if err != nil {
return nil, err
}
block, _ := pem.Decode(data)
if block == nil {
return nil, fmt.Errorf("CA file %q did not contain PEM encoded data", c.Network.CA)
}
if block.Type != "CERTIFICATE" {
return nil, fmt.Errorf("CA file %q did not contain certificate data", c.Network.CA)
}
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return nil, err
}
tlsConfig.RootCAs.AddCert(cert)
}
return tlsConfig, nil
}