forked from poseidon/matchbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
104 lines (94 loc) · 3.82 KB
/
server.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
package http
import (
"net/http"
"github.com/Sirupsen/logrus"
"github.com/coreos/matchbox/matchbox/server"
"github.com/coreos/matchbox/matchbox/sign"
)
// Config configures a Server.
type Config struct {
Core server.Server
Logger *logrus.Logger
// Path to static assets
AssetsPath string
// config signers (.sig and .asc)
Signer sign.Signer
ArmoredSigner sign.Signer
}
// Server serves boot and provisioning configs to machines via HTTP.
type Server struct {
core server.Server
logger *logrus.Logger
assetsPath string
signer sign.Signer
armoredSigner sign.Signer
}
// NewServer returns a new Server.
func NewServer(config *Config) *Server {
return &Server{
core: config.Core,
logger: config.Logger,
assetsPath: config.AssetsPath,
signer: config.Signer,
armoredSigner: config.ArmoredSigner,
}
}
// HTTPHandler returns a HTTP handler for the server.
func (s *Server) HTTPHandler() http.Handler {
mux := http.NewServeMux()
chain := func(next ContextHandler) http.Handler {
return s.logRequest(NewHandler(next))
}
// matchbox version
mux.Handle("/", s.logRequest(homeHandler()))
// Boot via GRUB
mux.Handle("/grub", chain(s.selectProfile(s.core, s.grubHandler())))
// Boot via iPXE
mux.Handle("/boot.ipxe", chain(ipxeInspect()))
mux.Handle("/boot.ipxe.0", chain(ipxeInspect()))
mux.Handle("/ipxe", chain(s.selectProfile(s.core, s.ipxeHandler())))
// Boot via Pixiecore
mux.Handle("/pixiecore/v1/boot/", chain(s.pixiecoreHandler(s.core)))
// Ignition Config
mux.Handle("/ignition", chain(s.selectGroup(s.core, s.ignitionHandler(s.core))))
// Cloud-Config
mux.Handle("/cloud", chain(s.selectGroup(s.core, s.cloudHandler(s.core))))
// Generic template
mux.Handle("/generic", chain(s.selectGroup(s.core, s.genericHandler(s.core))))
// Metadata
mux.Handle("/metadata", chain(s.selectGroup(s.core, s.metadataHandler())))
// Signatures
if s.signer != nil {
signerChain := func(next ContextHandler) http.Handler {
return s.logRequest(sign.SignatureHandler(s.signer, NewHandler(next)))
}
mux.Handle("/grub.sig", signerChain(s.selectProfile(s.core, s.grubHandler())))
mux.Handle("/boot.ipxe.sig", signerChain(ipxeInspect()))
mux.Handle("/boot.ipxe.0.sig", signerChain(ipxeInspect()))
mux.Handle("/ipxe.sig", signerChain(s.selectProfile(s.core, s.ipxeHandler())))
mux.Handle("/pixiecore/v1/boot.sig/", signerChain(s.pixiecoreHandler(s.core)))
mux.Handle("/ignition.sig", signerChain(s.selectGroup(s.core, s.ignitionHandler(s.core))))
mux.Handle("/cloud.sig", signerChain(s.selectGroup(s.core, s.cloudHandler(s.core))))
mux.Handle("/generic.sig", signerChain(s.selectGroup(s.core, s.genericHandler(s.core))))
mux.Handle("/metadata.sig", signerChain(s.selectGroup(s.core, s.metadataHandler())))
}
if s.armoredSigner != nil {
signerChain := func(next ContextHandler) http.Handler {
return s.logRequest(sign.SignatureHandler(s.armoredSigner, NewHandler(next)))
}
mux.Handle("/grub.asc", signerChain(s.selectProfile(s.core, s.grubHandler())))
mux.Handle("/boot.ipxe.asc", signerChain(ipxeInspect()))
mux.Handle("/boot.ipxe.0.asc", signerChain(ipxeInspect()))
mux.Handle("/ipxe.asc", signerChain(s.selectProfile(s.core, s.ipxeHandler())))
mux.Handle("/pixiecore/v1/boot.asc/", signerChain(s.pixiecoreHandler(s.core)))
mux.Handle("/ignition.asc", signerChain(s.selectGroup(s.core, s.ignitionHandler(s.core))))
mux.Handle("/cloud.asc", signerChain(s.selectGroup(s.core, s.cloudHandler(s.core))))
mux.Handle("/generic.asc", signerChain(s.selectGroup(s.core, s.genericHandler(s.core))))
mux.Handle("/metadata.asc", signerChain(s.selectGroup(s.core, s.metadataHandler())))
}
// kernel, initrd, and TLS assets
if s.assetsPath != "" {
mux.Handle("/assets/", s.logRequest(http.StripPrefix("/assets/", http.FileServer(http.Dir(s.assetsPath)))))
}
return mux
}