forked from openark/orchestrator
/
http.go
207 lines (183 loc) · 6.59 KB
/
http.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
/*
Copyright 2014 Outbrain Inc.
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
http://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.
*/
package app
import (
"net"
nethttp "net/http"
"strings"
"time"
"github.com/github/orchestrator/go/agent"
"github.com/github/orchestrator/go/collection"
"github.com/github/orchestrator/go/config"
"github.com/github/orchestrator/go/http"
"github.com/github/orchestrator/go/inst"
"github.com/github/orchestrator/go/logic"
"github.com/github/orchestrator/go/process"
"github.com/github/orchestrator/go/ssl"
"github.com/go-martini/martini"
"github.com/martini-contrib/auth"
"github.com/martini-contrib/gzip"
"github.com/martini-contrib/render"
"github.com/openark/golib/log"
)
const discoveryMetricsName = "DISCOVERY_METRICS"
var sslPEMPassword []byte
var agentSSLPEMPassword []byte
var discoveryMetrics *collection.Collection
// Http starts serving
func Http(continuousDiscovery bool) {
promptForSSLPasswords()
process.ContinuousRegistration(process.OrchestratorExecutionHttpMode, "")
martini.Env = martini.Prod
if config.Config.ServeAgentsHttp {
go agentsHttp()
}
standardHttp(continuousDiscovery)
}
// Iterate over the private keys and get passwords for them
// Don't prompt for a password a second time if the files are the same
func promptForSSLPasswords() {
if ssl.IsEncryptedPEM(config.Config.SSLPrivateKeyFile) {
sslPEMPassword = ssl.GetPEMPassword(config.Config.SSLPrivateKeyFile)
}
if ssl.IsEncryptedPEM(config.Config.AgentSSLPrivateKeyFile) {
if config.Config.AgentSSLPrivateKeyFile == config.Config.SSLPrivateKeyFile {
agentSSLPEMPassword = sslPEMPassword
} else {
agentSSLPEMPassword = ssl.GetPEMPassword(config.Config.AgentSSLPrivateKeyFile)
}
}
}
// standardHttp starts serving HTTP or HTTPS (api/web) requests, to be used by normal clients
func standardHttp(continuousDiscovery bool) {
m := martini.Classic()
switch strings.ToLower(config.Config.AuthenticationMethod) {
case "basic":
{
if config.Config.HTTPAuthUser == "" {
// Still allowed; may be disallowed in future versions
log.Warning("AuthenticationMethod is configured as 'basic' but HTTPAuthUser undefined. Running without authentication.")
}
m.Use(auth.Basic(config.Config.HTTPAuthUser, config.Config.HTTPAuthPassword))
}
case "multi":
{
if config.Config.HTTPAuthUser == "" {
// Still allowed; may be disallowed in future versions
log.Fatal("AuthenticationMethod is configured as 'multi' but HTTPAuthUser undefined")
}
m.Use(auth.BasicFunc(func(username, password string) bool {
if username == "readonly" {
// Will be treated as "read-only"
return true
}
return auth.SecureCompare(username, config.Config.HTTPAuthUser) && auth.SecureCompare(password, config.Config.HTTPAuthPassword)
}))
}
default:
{
// We inject a dummy User object because we have function signatures with User argument in api.go
m.Map(auth.User(""))
}
}
m.Use(gzip.All())
// Render html templates from templates directory
m.Use(render.Renderer(render.Options{
Directory: "resources",
Layout: "templates/layout",
HTMLContentType: "text/html",
}))
m.Use(martini.Static("resources/public", martini.StaticOptions{Prefix: config.Config.URLPrefix}))
if config.Config.UseMutualTLS {
m.Use(ssl.VerifyOUs(config.Config.SSLValidOUs))
}
inst.SetMaintenanceOwner(process.ThisHostname)
if continuousDiscovery {
// start to expire metric collection info
discoveryMetrics = collection.CreateOrReturnCollection(discoveryMetricsName)
discoveryMetrics.SetExpirePeriod(time.Duration(config.Config.DiscoveryCollectionRetentionSeconds) * time.Second)
log.Info("Starting Discovery")
go logic.ContinuousDiscovery()
}
log.Info("Registering endpoints")
http.API.URLPrefix = config.Config.URLPrefix
http.Web.URLPrefix = config.Config.URLPrefix
http.API.RegisterRequests(m)
http.Web.RegisterRequests(m)
// Serve
if config.Config.ListenSocket != "" {
log.Infof("Starting HTTP listener on unix socket %v", config.Config.ListenSocket)
unixListener, err := net.Listen("unix", config.Config.ListenSocket)
if err != nil {
log.Fatale(err)
}
defer unixListener.Close()
if err := nethttp.Serve(unixListener, m); err != nil {
log.Fatale(err)
}
} else if config.Config.UseSSL {
log.Info("Starting HTTPS listener")
tlsConfig, err := ssl.NewTLSConfig(config.Config.SSLCAFile, config.Config.UseMutualTLS)
if err != nil {
log.Fatale(err)
}
tlsConfig.InsecureSkipVerify = config.Config.SSLSkipVerify
if err = ssl.AppendKeyPairWithPassword(tlsConfig, config.Config.SSLCertFile, config.Config.SSLPrivateKeyFile, sslPEMPassword); err != nil {
log.Fatale(err)
}
if err = ssl.ListenAndServeTLS(config.Config.ListenAddress, m, tlsConfig); err != nil {
log.Fatale(err)
}
} else {
log.Infof("Starting HTTP listener on %+v", config.Config.ListenAddress)
if err := nethttp.ListenAndServe(config.Config.ListenAddress, m); err != nil {
log.Fatale(err)
}
}
log.Info("Web server started")
}
// agentsHttp startes serving agents HTTP or HTTPS API requests
func agentsHttp() {
m := martini.Classic()
m.Use(gzip.All())
m.Use(render.Renderer())
if config.Config.AgentsUseMutualTLS {
m.Use(ssl.VerifyOUs(config.Config.AgentSSLValidOUs))
}
log.Info("Starting agents listener")
agent.InitHttpClient()
go logic.ContinuousAgentsPoll()
http.AgentsAPI.URLPrefix = config.Config.URLPrefix
http.AgentsAPI.RegisterRequests(m)
// Serve
if config.Config.AgentsUseSSL {
log.Info("Starting agent HTTPS listener")
tlsConfig, err := ssl.NewTLSConfig(config.Config.AgentSSLCAFile, config.Config.AgentsUseMutualTLS)
if err != nil {
log.Fatale(err)
}
tlsConfig.InsecureSkipVerify = config.Config.AgentSSLSkipVerify
if err = ssl.AppendKeyPairWithPassword(tlsConfig, config.Config.AgentSSLCertFile, config.Config.AgentSSLPrivateKeyFile, agentSSLPEMPassword); err != nil {
log.Fatale(err)
}
if err = ssl.ListenAndServeTLS(config.Config.AgentsServerPort, m, tlsConfig); err != nil {
log.Fatale(err)
}
} else {
log.Info("Starting agent HTTP listener")
if err := nethttp.ListenAndServe(config.Config.AgentsServerPort, m); err != nil {
log.Fatale(err)
}
}
log.Info("Agent server started")
}