forked from remind101/empire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
149 lines (121 loc) · 3.31 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
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
// Package server provides an http.Handler implementation that includes the
// Heroku Platform API compatibility layer, GitHub Deployments integration and a
// simple health check.
package server
import (
"io"
"net/http"
"net/url"
"github.com/remind101/empire"
"github.com/remind101/empire/internal/saml"
"github.com/remind101/empire/server/github"
"github.com/remind101/empire/server/heroku"
)
var (
DefaultOptions = Options{}
)
type Options struct {
GitHub struct {
// Deployments
Webhooks struct {
Secret string
}
Deployments struct {
Environments []string
ImageBuilder github.ImageBuilder
TugboatURL string
}
}
}
// Server composes the Heroku API compatibility layer, the GitHub Webhooks
// handlers and a health check as a single http.Handler.
type Server struct {
// Base host for the server.
URL *url.URL
// The underlying Heroku http.Handler.
Heroku *heroku.Server
GitHubWebhooks http.Handler
Health *HealthHandler
// If provided, enables the SAML integration.
ServiceProvider *saml.ServiceProvider
}
func New(e *empire.Empire, options Options) *Server {
s := &Server{}
if options.GitHub.Webhooks.Secret != "" {
// Mount GitHub webhooks
s.GitHubWebhooks = github.New(e, github.Options{
Secret: options.GitHub.Webhooks.Secret,
Environments: options.GitHub.Deployments.Environments,
Deployer: newDeployer(e, options),
})
}
s.Heroku = heroku.New(e)
s.Health = NewHealthHandler(e)
return s
}
func (s *Server) Handler(r *http.Request) http.Handler {
h := s.handler(r)
if h == nil {
h = http.NotFoundHandler()
}
return h
}
func (s *Server) handler(r *http.Request) http.Handler {
if r.Header.Get("X-GitHub-Event") != "" {
return s.GitHubWebhooks
}
// Route to Heroku API.
if r.Header.Get("Accept") == heroku.AcceptHeader {
return s.Heroku
}
switch r.URL.Path {
case "/saml/login":
return http.HandlerFunc(s.SAMLLogin)
case "/saml/acs":
return http.HandlerFunc(s.SAMLACS)
case "/health":
return s.Health
}
return nil
}
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h := s.Handler(r)
h.ServeHTTP(w, r)
}
// HealthHandler is an http.Handler that returns the health of empire.
type HealthHandler struct {
// A function that returns true if empire is healthy.
IsHealthy func() error
}
// NewHealthHandler returns a new HealthHandler using the IsHealthy method from
// an Empire instance.
func NewHealthHandler(e *empire.Empire) *HealthHandler {
return &HealthHandler{
IsHealthy: e.IsHealthy,
}
}
func (h *HealthHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
err := h.IsHealthy()
if err == nil {
w.WriteHeader(http.StatusOK)
return
}
w.WriteHeader(http.StatusServiceUnavailable)
io.WriteString(w, err.Error())
}
// newDeployer generates a new github.Deployer implementation for the given
// options.
func newDeployer(e *empire.Empire, options Options) github.Deployer {
ed := github.NewEmpireDeployer(e)
ed.ImageBuilder = options.GitHub.Deployments.ImageBuilder
var d github.Deployer = ed
// Enables the Tugboat integration, which will send logs to a Tugboat
// instance.
if url := options.GitHub.Deployments.TugboatURL; url != "" {
d = github.NotifyTugboat(d, url)
}
// Perform the deployment within a go routine so we don't timeout
// githubs webhook requests.
d = github.DeployAsync(d)
return d
}