forked from cesanta/docker_auth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
178 lines (159 loc) · 4.72 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
/*
Copyright 2015 Cesanta Software Ltd.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
//go:generate ./gen_version.py
package main // import "github.com/cesanta/docker_auth/auth_server"
import (
"crypto/tls"
"flag"
"math/rand"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/cesanta/docker_auth/auth_server/server"
"github.com/facebookgo/httpdown"
"github.com/golang/glog"
fsnotify "gopkg.in/fsnotify.v1"
)
type RestartableServer struct {
configFile string
hd *httpdown.HTTP
authServer *server.AuthServer
hs httpdown.Server
}
func ServeOnce(c *server.Config, cf string, hd *httpdown.HTTP) (*server.AuthServer, httpdown.Server) {
glog.Infof("Config from %s (%d users, %d ACL static entries)", cf, len(c.Users), len(c.ACL))
as, err := server.NewAuthServer(c)
if err != nil {
glog.Exitf("Failed to create auth server: %s", err)
}
var tlsConfig *tls.Config
if c.Server.CertFile != "" || c.Server.KeyFile != "" {
// Check for partial configuration.
if c.Server.CertFile == "" || c.Server.KeyFile == "" {
glog.Exitf("Failed to load certificate and key: both were not provided")
}
tlsConfig = &tls.Config{
MinVersion: tls.VersionTLS10,
PreferServerCipherSuites: true,
CipherSuites: []uint16{
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
tls.TLS_RSA_WITH_AES_128_CBC_SHA,
tls.TLS_RSA_WITH_AES_256_CBC_SHA,
},
NextProtos: []string{"http/1.1"},
Certificates: make([]tls.Certificate, 1),
}
glog.Infof("Cert file: %s", c.Server.CertFile)
glog.Infof("Key file : %s", c.Server.KeyFile)
tlsConfig.Certificates[0], err = tls.LoadX509KeyPair(c.Server.CertFile, c.Server.KeyFile)
if err != nil {
glog.Exitf("Failed to load certificate and key: %s", err)
}
} else {
glog.Warning("Running without TLS")
}
hs := &http.Server{
Addr: c.Server.ListenAddress,
Handler: as,
TLSConfig: tlsConfig,
}
s, err := hd.ListenAndServe(hs)
if err != nil {
glog.Exitf("Failed to set up listener: %s", err)
}
glog.Infof("Serving on %s", c.Server.ListenAddress)
return as, s
}
func (rs *RestartableServer) Serve(c *server.Config) {
rs.authServer, rs.hs = ServeOnce(c, rs.configFile, rs.hd)
rs.WatchConfig()
}
func (rs *RestartableServer) WatchConfig() {
w, err := fsnotify.NewWatcher()
if err != nil {
glog.Fatalf("Failed to create watcher: %s", err)
}
defer w.Close()
stopSignals := make(chan os.Signal, 1)
signal.Notify(stopSignals, syscall.SIGTERM, syscall.SIGINT)
err = w.Add(rs.configFile)
watching, needRestart := (err == nil), false
for {
select {
case <-time.After(1 * time.Second):
if !watching {
err = w.Add(rs.configFile)
if err != nil {
glog.Errorf("Failed to set up config watcher: %s", err)
} else {
watching, needRestart = true, true
}
} else if needRestart {
rs.MaybeRestart()
needRestart = false
}
case ev := <-w.Events:
if ev.Op == fsnotify.Remove {
glog.Warningf("Config file disappeared, serving continues")
w.Remove(rs.configFile)
watching, needRestart = false, false
} else if ev.Op == fsnotify.Write {
needRestart = true
}
case s := <-stopSignals:
signal.Stop(stopSignals)
glog.Infof("Signal: %s", s)
rs.hs.Stop()
rs.authServer.Stop()
glog.Exitf("Exiting")
}
}
}
func (rs *RestartableServer) MaybeRestart() {
glog.Infof("Restarting server")
c, err := server.LoadConfig(rs.configFile)
if err != nil {
glog.Errorf("Failed to reload config (server not restarted): %s", err)
return
}
glog.Infof("New config loaded")
rs.hs.Stop()
rs.authServer.Stop()
rs.authServer, rs.hs = ServeOnce(c, rs.configFile, rs.hd)
}
func main() {
flag.Parse()
rand.Seed(time.Now().UnixNano())
glog.CopyStandardLogTo("INFO")
glog.Infof("docker_auth %s build %s", Version, BuildId)
cf := flag.Arg(0)
if cf == "" {
glog.Exitf("Config file not specified")
}
c, err := server.LoadConfig(cf)
if err != nil {
glog.Exitf("Failed to load config: %s", err)
}
rs := RestartableServer{
configFile: cf,
hd: &httpdown.HTTP{},
}
rs.Serve(c)
}