-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
384 lines (325 loc) · 11.2 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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*
* This file is part of Windermere (EGIL SCIM Server).
*
* Copyright (C) 2019-2021 Föreningen Sambruk
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"context"
"crypto/tls"
"errors"
"flag"
"log"
"net/http"
_ "net/http/pprof"
"os"
"os/signal"
"syscall"
"time"
"github.com/Sambruk/windermere/windermere"
"github.com/joesiltberg/bowness/fedtls"
"github.com/joesiltberg/bowness/server"
"github.com/spf13/viper"
"golang.org/x/time/rate"
)
func must(err error) {
if err != nil {
log.Fatal(err.Error())
}
}
// Verifies that certain parameters are set in the configuration file
func verifyRequired(keys ...string) {
for _, key := range keys {
if !viper.IsSet(key) {
log.Fatalf("Missing required configuration setting: %s", key)
}
}
}
// Convenience method for getting a configuration parameter that
// specifies a duration in seconds.
func configuredSeconds(setting string) time.Duration {
return time.Duration(viper.GetInt(setting)) * time.Second
}
// Blocks until we get a signal to shut down
func waitForShutdownSignal() {
signals := make(chan os.Signal)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
<-signals
}
// Configuration parameters
const (
CNFMDURL = "MetadataURL"
CNFMDDefaultCacheTTL = "MetadataDefaultCacheTTL"
CNFMDNetworkRetry = "MetadataNetworkRetry"
CNFMDBadContentRetry = "MetadataBadContentRetry"
CNFMDCachePath = "MetadataCachePath"
CNFReadHeaderTimeout = "ReadHeaderTimeout"
CNFReadTimeout = "ReadTimeout"
CNFWriteTimeout = "WriteTimeout"
CNFIdleTimeout = "IdleTimeout"
CNFBackendTimeout = "BackendTimeout"
CNFEnableLimiting = "EnableLimiting"
CNFLimitRequestsPerSecond = "LimitRequestsPerSecond"
CNFLimitBurst = "LimitBurst"
CNFStorageType = "StorageType"
CNFStorageSource = "StorageSource"
CNFAccessLogPath = "AccessLogPath"
CNFJWKSPath = "JWKSPath"
CNFCert = "Cert"
CNFKey = "Key"
CNFListenAddress = "ListenAddress"
CNFAdminListenAddress = "AdminListenAddress"
CNFMDEntityID = "MetadataEntityID"
CNFMDBaseURI = "MetadataBaseURI"
CNFMDOrganization = "MetadataOrganization"
CNFMDOrganizationID = "MetadataOrganizationID"
CNFValidateUUID = "ValidateUUID"
CNFValidateSchoolUnitCode = "ValidateSchoolUnitCode"
CNFSkolsynkListenAddress = "SkolsynkListenAddress"
CNFSkolsynkAuthHeader = "SkolsynkAuthHeader"
CNFSkolsynkCert = "SkolsynkCert"
CNFSkolsynkKey = "SkolsynkKey"
CNFSkolsynkClients = "SkolsynkClients"
)
// Parses the config value for clients to a map[string]string
// (from tenant name to API key)
func parseClients(value interface{}) (map[string]string, error) {
res := make(map[string]string)
err := errors.New("invalid clients specification")
arr, ok := value.([]interface{})
if !ok {
return nil, err
}
for i := range arr {
client, ok := arr[i].(map[string]interface{})
if !ok {
return nil, err
}
getString := func(m map[string]interface{}, s string) (string, error) {
val, ok := m[s]
if !ok {
return "", err
}
res, ok := val.(string)
if !ok {
return "", err
}
return res, nil
}
name, e := getString(client, "name")
if e != nil {
return nil, e
}
key, e := getString(client, "key")
if e != nil {
return nil, e
}
res[name] = key
}
return res, nil
}
func main() {
// Configuration defaults
defaults := map[string]interface{}{
CNFMDURL: "https://fed.skolfederation.se/prod/md/kontosynk.jws",
CNFMDDefaultCacheTTL: 3600,
CNFMDNetworkRetry: 60,
CNFMDBadContentRetry: 3600,
CNFReadHeaderTimeout: 5,
CNFReadTimeout: 20,
CNFWriteTimeout: 40,
CNFIdleTimeout: 60,
CNFBackendTimeout: 30,
CNFEnableLimiting: false,
CNFLimitRequestsPerSecond: 10.0,
CNFLimitBurst: 50,
CNFStorageType: "file",
CNFStorageSource: "SS12000.json",
CNFAccessLogPath: "",
CNFAdminListenAddress: "",
CNFValidateUUID: true,
CNFValidateSchoolUnitCode: true,
CNFSkolsynkAuthHeader: "X-API-Key",
}
for key, value := range defaults {
viper.SetDefault(key, value)
}
flag.Parse()
if flag.NArg() < 1 {
log.Fatal("Missing configuration file path")
}
configPath := flag.Arg(0)
viper.SetConfigFile(configPath)
must(viper.ReadInConfig())
verifyRequired(CNFCert, CNFKey)
if viper.IsSet(CNFListenAddress) {
verifyRequired(CNFJWKSPath, CNFMDCachePath,
CNFMDEntityID, CNFMDBaseURI, CNFMDOrganization, CNFMDOrganizationID)
} else if viper.IsSet(CNFSkolsynkListenAddress) {
verifyRequired(CNFSkolsynkClients)
} else {
log.Fatalf("No listen address configured (configure at least one of %s or %s", CNFListenAddress, CNFSkolsynkListenAddress)
}
certFile := viper.GetString(CNFCert)
keyFile := viper.GetString(CNFKey)
// Windermere needs a function to get the currently authenticated
// SCIM tenant from the current Context.
tenantGetter := func(c context.Context) string {
tenant := APIKeyAuthenticatedTenantFromContext(c)
if tenant != nil {
return *tenant
}
return server.EntityIDFromContext(c)
}
// Configurable validation of SS12000 objects
validator := windermere.CreateOptionalValidator(
viper.GetBool(CNFValidateUUID),
viper.GetBool(CNFValidateSchoolUnitCode),
)
// Create the Windermere SCIM handler
wind, err := windermere.New(viper.GetString(CNFStorageType), viper.GetString(CNFStorageSource), tenantGetter, validator)
if err != nil {
log.Fatalf("Failed to initialize Windermere: %v", err)
}
// Setup various middlware handlers between Windermere and the http.Server
var handler http.Handler
handler = wind
enableLimiting := viper.GetBool(CNFEnableLimiting)
if enableLimiting {
handler = Limiter(handler, tenantGetter,
rate.Limit(viper.GetFloat64(CNFLimitRequestsPerSecond)),
viper.GetInt(CNFLimitBurst))
}
beTimeout := configuredSeconds(CNFBackendTimeout)
if beTimeout >= 1*time.Second {
handler = PanicReportTimeoutHandler(handler, beTimeout, "Backend timeout")
}
accessLogPath := viper.GetString(CNFAccessLogPath)
if accessLogPath != "" {
handler = accessLogHandler(handler, accessLogPath, tenantGetter)
}
var fedtlsServer *http.Server
var mdstore *fedtls.MetadataStore
// Possibly setup EGIL server with Federated TLS authentication
if viper.IsSet(CNFListenAddress) {
// Setup federated TLS metadata store
mdstore = fedtls.NewMetadataStore(
viper.GetString(CNFMDURL),
viper.GetString(CNFJWKSPath),
viper.GetString(CNFMDCachePath),
fedtls.DefaultCacheTTL(configuredSeconds(CNFMDDefaultCacheTTL)),
fedtls.NetworkRetry(configuredSeconds(CNFMDNetworkRetry)),
fedtls.BadContentRetry(configuredSeconds(CNFMDBadContentRetry)))
// The TLS config manager is used by the tls.Listener below to configure
// TLS according to the TLS federation.
mdTLSConfigManager, err := server.NewMetadataTLSConfigManager(certFile, keyFile, mdstore)
if err != nil {
log.Fatalf("Failed to create TLS configuration: %v", err)
}
// Create the HTTP server
fedtlsServer = &http.Server{
// Wrap the HTTP handler with authentication middleware.
Handler: server.AuthMiddleware(handler, mdstore),
// In order to use the authentication middleware, the server needs
// to have a ConnContext configured so the middleware can access
// connection specific information.
ConnContext: server.ContextModifier(),
ReadHeaderTimeout: configuredSeconds(CNFReadHeaderTimeout),
ReadTimeout: configuredSeconds(CNFReadTimeout),
WriteTimeout: configuredSeconds(CNFWriteTimeout),
IdleTimeout: configuredSeconds(CNFIdleTimeout),
}
// Set up a TLS listener with certificate authorities loaded from
// federation metadata (and dynamically updated as metadata gets refreshed).
address := viper.GetString(CNFListenAddress)
listener, err := tls.Listen("tcp", address, mdTLSConfigManager.Config())
if err != nil {
log.Fatalf("Failed to listen to %s (%v)", address, err)
}
// Start the main HTTP server
go func() {
err := fedtlsServer.Serve(listener)
if err != http.ErrServerClosed {
log.Fatalf("Unexpected server exit: %v", err)
}
}()
}
var skolsynkServer *http.Server
// Possibly start Skolsynk HTTP server (API-key based authentication)
if viper.IsSet(CNFSkolsynkListenAddress) {
clients, err := parseClients(viper.Get(CNFSkolsynkClients))
if err != nil {
log.Fatalf("Failed to parse clients from config: %v", err)
}
// Create the HTTP server
skolsynkServer = &http.Server{
// Wrap the HTTP handler with authentication middleware.
Handler: APIKeyAuthMiddleware(handler,
viper.GetString(CNFSkolsynkAuthHeader),
clients),
Addr: viper.GetString(CNFSkolsynkListenAddress),
ReadHeaderTimeout: configuredSeconds(CNFReadHeaderTimeout),
ReadTimeout: configuredSeconds(CNFReadTimeout),
WriteTimeout: configuredSeconds(CNFWriteTimeout),
IdleTimeout: configuredSeconds(CNFIdleTimeout),
}
skolsynkCertFile := certFile
skolsynkKeyFile := keyFile
if viper.IsSet(CNFSkolsynkCert) {
skolsynkCertFile = viper.GetString(CNFSkolsynkCert)
}
if viper.IsSet(CNFSkolsynkKey) {
skolsynkKeyFile = viper.GetString(CNFSkolsynkKey)
}
go func() {
err := skolsynkServer.ListenAndServeTLS(skolsynkCertFile, skolsynkKeyFile)
if err != http.ErrServerClosed {
log.Fatalf("Unexpected Skolsynk server exit: %v", err)
}
}()
}
// Possibly start the admin HTTP server
adminAddress := viper.GetString(CNFAdminListenAddress)
if adminAddress != "" {
http.Handle("/metadata", metadataHandler(certFile,
viper.GetString(CNFMDEntityID), viper.GetString(CNFMDBaseURI),
viper.GetString(CNFMDOrganization), viper.GetString(CNFMDOrganizationID)))
go func() {
log.Println(http.ListenAndServeTLS(adminAddress, certFile, keyFile, nil))
}()
}
waitForShutdownSignal()
log.Printf("Shutting down, waiting for active requests to finish...")
if fedtlsServer != nil {
err = fedtlsServer.Shutdown(context.Background())
if err != nil {
log.Printf("Failed to gracefully shutdown federated TLS server: %v", err)
}
}
if skolsynkServer != nil {
err = skolsynkServer.Shutdown(context.Background())
if err != nil {
log.Printf("Failed to gracefully shutdown Skolsynk server: %v", err)
}
}
err = wind.Shutdown()
if err != nil {
log.Printf("Failed to gracefully shutdown Windermere: %v", err)
}
if mdstore != nil {
log.Printf("Server closed, waiting for metadata store to close...")
mdstore.Quit()
}
log.Printf("Done.")
}