forked from contiv/auth_proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
230 lines (195 loc) · 5.71 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
package main
import (
"flag"
"fmt"
"os"
"time"
"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
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
initialSetup bool // if set, run the initial proxy setup (adding default users, etc.)
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
)
func performInitialSetup() {
log.Println("Performing initial setup")
log.Println("Adding default users with default passwords")
if err := auth.AddDefaultUsers(); err != nil {
log.Fatalln(err)
// exit with a non-zero error code.
// this can be used by installers, etc. to determine whether the
// setup successfully completed or not.
os.Exit(1)
}
log.Println("Initial setup is complete. Exiting.")
os.Exit(0)
}
func processFlags() {
// TODO: add a flag for LDAP host + port
flag.BoolVar(
&initialSetup,
"initial-setup",
false,
"if set, run the initial proxy setup (adding default users, etc.)",
)
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.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
}
// 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() {
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(dataStoreAddress); err != nil {
log.Fatalln(err)
return
}
// if --initial-setup is specified, just perform setup and exit immediately
if initialSetup {
performInitialSetup()
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,
})
go p.Serve()
for range time.Tick(time.Second) {
}
}