-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathconfig.go
More file actions
130 lines (107 loc) · 2.65 KB
/
config.go
File metadata and controls
130 lines (107 loc) · 2.65 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package haproxy
import (
"io/ioutil"
"os"
"path"
"runtime"
"text/template"
"github.com/criteo/haproxy-consul-connect/lib"
log "github.com/sirupsen/logrus"
)
const (
dataplaneUser = "haproxy"
dataplanePass = "pass"
)
var baseCfgTmpl = `
global
master-worker
stats socket {{.SocketPath}} mode 600 level admin expose-fd listeners
stats timeout 2m
tune.ssl.default-dh-param 1024
nbproc 1
nbthread {{.NbThread}}
userlist controller
user {{.DataplaneUser}} insecure-password {{.DataplanePass}}
`
const spoeConfTmpl = `
[intentions]
spoe-agent intentions-agent
messages check-intentions
option var-prefix connect
timeout hello 3000ms
timeout idle 3000s
timeout processing 3000ms
use-backend spoe_back
spoe-message check-intentions
args ip=src cert=ssl_c_der
event on-frontend-tcp-request
`
type baseParams struct {
NbThread int
SocketPath string
DataplaneUser string
DataplanePass string
LogsPath string
}
type haConfig struct {
Base string
HAProxy string
SPOE string
SPOESock string
StatsSock string
DataplaneSock string
DataplaneTransactionDir string
LogsSock string
}
func newHaConfig(baseDir string, sd *lib.Shutdown) (*haConfig, error) {
cfg := &haConfig{}
sd.Add(1)
base, err := ioutil.TempDir(baseDir, "haproxy-connect-")
if err != nil {
sd.Done()
return nil, err
}
go func() {
defer sd.Done()
<-sd.Stop
log.Info("cleaning config...")
os.RemoveAll(base)
}()
cfg.Base = base
cfg.HAProxy = path.Join(base, "haproxy.conf")
cfg.SPOE = path.Join(base, "spoe.conf")
cfg.SPOESock = path.Join(base, "spoe.sock")
cfg.StatsSock = path.Join(base, "haproxy.sock")
cfg.DataplaneSock = path.Join(base, "dataplane.sock")
cfg.DataplaneTransactionDir = path.Join(base, "dataplane-transactions")
cfg.LogsSock = path.Join(base, "logs.sock")
tmpl, err := template.New("cfg").Parse(baseCfgTmpl)
if err != nil {
return nil, err
}
cfgFile, err := os.OpenFile(cfg.HAProxy, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0600)
if err != nil {
return nil, err
}
defer cfgFile.Close()
err = tmpl.Execute(cfgFile, baseParams{
NbThread: runtime.GOMAXPROCS(0),
SocketPath: cfg.StatsSock,
LogsPath: cfg.LogsSock,
DataplaneUser: dataplaneUser,
DataplanePass: dataplanePass,
})
if err != nil {
return nil, err
}
spoeCfgFile, err := os.OpenFile(cfg.SPOE, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0600)
if err != nil {
return nil, err
}
defer spoeCfgFile.Close()
_, err = spoeCfgFile.WriteString(spoeConfTmpl)
if err != nil {
return nil, err
}
return cfg, nil
}