-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmain.go
259 lines (215 loc) · 6.54 KB
/
main.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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package main
import (
"flag"
"fmt"
"os"
"runtime"
"syscall"
"github.com/blang/semver"
"github.com/contiv/auth_proxy/auth"
"github.com/contiv/auth_proxy/common"
"github.com/contiv/auth_proxy/proxy"
"github.com/contiv/auth_proxy/state"
log "github.com/Sirupsen/logrus"
)
const (
// DefaultVersion is the version string used when a BUILD_VERSION is not passed to the build.
DefaultVersion = "devbuild"
)
var (
// flags
dataStoreDriver string // driver of the data store used by netmaster
dataStoreAddress string // address of the data store used by netmaster
debug bool // if set, log level is set to `debug`
listenAddress string // address we listen on
netmasterAddress string // address of the netmaster we proxy to
tlsKeyFile string // path to TLS key
tlsCertificate string // path to TLS certificate
// ProgramName is used in logging output and the X-Forwarded-By header.
ProgramName = "Auth Proxy"
// ProgramVersion is used in logging output and the X-Forwarded-By header.
// it is overridden at compile time via -ldflags
ProgramVersion = DefaultVersion
// the three timeouts we support. See proxy.Config for comments
netmasterRequestTimeout int64
clientReadTimeout int64
clientWriteTimeout int64
)
func processFlags() {
flag.Int64Var(
&netmasterRequestTimeout,
"netmaster-timeout",
proxy.DefaultNetmasterRequestTimeout,
"time (in seconds) to allow auth_proxy to spend forwarding a request to netmaster",
)
flag.Int64Var(
&clientReadTimeout,
"client-read-timeout",
proxy.DefaultClientReadTimeout,
"time (in seconds) to allow a client to send its complete request to auth_proxy",
)
flag.Int64Var(
&clientWriteTimeout,
"client-write-timeout",
proxy.DefaultClientWriteTimeout,
"time (in seconds) to allow for auth_proxy to send a response after receiving a request from a client",
)
flag.StringVar(
&listenAddress,
"listen-address",
":10000",
"address to listen to HTTP requests on",
)
flag.StringVar(
&netmasterAddress,
"netmaster-address",
"localhost:9999",
"address of the upstream netmaster",
)
flag.StringVar(
&tlsKeyFile,
"tls-key-file",
"local.key",
"path to TLS key",
)
flag.StringVar(
&tlsCertificate,
"tls-certificate",
"cert.pem",
"path to TLS certificate",
)
flag.BoolVar(
&debug,
"debug",
false,
"if set, log level is set to debug",
)
flag.StringVar(
&dataStoreAddress,
"data-store-address",
"",
"address of the state store used by netmaster",
)
flag.StringVar(
&dataStoreDriver,
"data-store-driver",
"",
"driver of the state store used by netmaster",
)
flag.Parse()
}
// We perform two checks here:
// 1. that the version of the netmaster we're pointed at is a compatible version,
// i.e., its major version is the same and the minor version of netmaster is
// greater than or equal to the minor version of auth_proxy.
// 2. by nature of 1., that the netmaster is actually reachable at all
//
// If this is a devbuild (i.e., build version = default version), we will still
// ensure that netmaster is reachable but we won't check its version.
func netmasterStartupCheck() error {
// this envvar is used by systemtests to get around the fact that auth_proxy
// expects netmaster to have already been started, but the actual systemtests
// code (which runs the MockServer) is started *after* the proxy containers are
// started so that it can receive the IPs/ports of the proxy containers.
//
// we won't be advertising this envvar in our docs or anywhere else.
if len(os.Getenv("NO_NETMASTER_STARTUP_CHECK")) != 0 {
log.Println("Skipping netmaster startup check")
return nil
}
log.Info("Testing connectivity to netmaster at " + netmasterAddress)
netmasterVersion, err := common.GetNetmasterVersion(netmasterAddress)
if err != nil {
return err
}
log.Infof("Found netmaster version '%s'", netmasterVersion)
// if this is a dev build, just exit
if DefaultVersion == ProgramVersion {
log.Infof("%s version is default (%s), skipping netmaster version compatibility check",
ProgramName,
DefaultVersion,
)
return nil
}
// this envvar is useful for running systemtests against an arbitrary
// version of netmaster while still ensuring netmaster is up.
if len(os.Getenv("NO_NETMASTER_SEMVER_CHECK")) != 0 {
log.Println("Skipping netmaster semver check")
return nil
}
// compare the semvers of the proxy and netmaster
// (only major and minor, we will allow patch level differences)
proxyVer, err := semver.Make(ProgramVersion)
if err != nil {
return fmt.Errorf(
"failed to create semver from proxy version '%s': %s",
ProgramVersion,
err.Error(),
)
}
netmasterVer, err := semver.Make(netmasterVersion)
if err != nil {
return fmt.Errorf(
"failed to create semver from netmaster version '%s': %s",
netmasterVersion,
err.Error(),
)
}
compatible := netmasterVer.Major == proxyVer.Major && netmasterVer.Minor >= proxyVer.Minor
if !compatible {
return fmt.Errorf(
"%s and netmaster versions are incompatible (%s: %q, netmaster: %q)",
ProgramName,
ProgramName,
ProgramVersion,
netmasterVersion,
)
}
return nil
}
func main() {
// prevent this process from being swapped out to disk
syscall.Mlockall(syscall.MCL_CURRENT | syscall.MCL_FUTURE)
log.Println(ProgramName, ProgramVersion, "starting up...")
if DefaultVersion == ProgramVersion {
log.Println("====================================================")
log.Println(" DEV BUILD - DO NOT RELEASE ")
log.Println("====================================================")
}
processFlags()
if debug {
log.SetLevel(log.DebugLevel)
}
// Initialize data store
if err := state.InitializeStateDriver(dataStoreDriver, dataStoreAddress); err != nil {
log.Fatalln(err)
return
}
// Add built-in users
log.Println("Adding default users with default passwords")
if err := auth.AddDefaultUsers(); err != nil {
log.Fatalln(err)
return
}
if err := netmasterStartupCheck(); err != nil {
log.Fatalln(err)
return
}
if err := common.Global().Set("tls_key_file", tlsKeyFile); err != nil {
log.Fatalln(err)
return
}
p := proxy.NewServer(&proxy.Config{
Name: ProgramName,
Version: ProgramVersion,
NetmasterAddress: netmasterAddress,
ListenAddress: listenAddress,
TLSCertificate: tlsCertificate,
TLSKeyFile: tlsKeyFile,
NetmasterRequestTimeout: netmasterRequestTimeout,
ClientReadTimeout: clientReadTimeout,
ClientWriteTimeout: clientWriteTimeout,
})
go p.Serve()
runtime.Goexit()
}