forked from cloudfoundry/bosh-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
https_handler.go
181 lines (150 loc) · 4.41 KB
/
https_handler.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
package mbus
import (
"bufio"
"io"
"io/ioutil"
"net/http"
"net/url"
"path"
"github.com/cloudfoundry/bosh-agent/platform"
"github.com/cloudfoundry/bosh-agent/settings"
boshagentblobstore "github.com/cloudfoundry/bosh-agent/agent/blobstore"
boshhandler "github.com/cloudfoundry/bosh-agent/handler"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
)
const httpsHandlerLogTag = "https_handler"
type HTTPSHandler struct {
parsedURL *url.URL
blobManager boshagentblobstore.BlobManagerInterface
logger boshlog.Logger
dispatcher *HTTPSDispatcher
auditLogger platform.AuditLogger
}
func NewHTTPSHandler(
parsedURL *url.URL,
keyPair settings.CertKeyPair,
blobManager boshagentblobstore.BlobManagerInterface,
logger boshlog.Logger,
auditLogger platform.AuditLogger,
) HTTPSHandler {
return HTTPSHandler{
parsedURL: parsedURL,
logger: logger,
blobManager: blobManager,
dispatcher: NewHTTPSDispatcher(parsedURL, keyPair, logger),
auditLogger: auditLogger,
}
}
func (h HTTPSHandler) Run(handlerFunc boshhandler.Func) error {
err := h.Start(handlerFunc)
if err != nil {
return bosherr.WrapError(err, "Starting https handler")
}
return nil
}
func (h HTTPSHandler) Start(handlerFunc boshhandler.Func) error {
h.dispatcher.AddRoute("/agent", h.agentHandler(handlerFunc))
h.dispatcher.AddRoute("/blobs/", h.blobsHandler())
return h.dispatcher.Start()
}
func (h HTTPSHandler) Stop() {
h.dispatcher.Stop()
}
func (h HTTPSHandler) RegisterAdditionalFunc(handlerFunc boshhandler.Func) {
panic("HTTPSHandler does not support registering additional handler funcs")
}
func (h HTTPSHandler) Send(target boshhandler.Target, topic boshhandler.Topic, message interface{}) error {
return nil
}
func (h HTTPSHandler) agentHandler(handlerFunc boshhandler.Func) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
w.WriteHeader(404)
h.generateCEFLog(r, 404, "")
return
}
rawJSONPayload, err := ioutil.ReadAll(r.Body)
if err != nil {
err = bosherr.WrapError(err, "Reading http body")
h.logger.Error(httpsHandlerLogTag, err.Error())
w.WriteHeader(400)
h.generateCEFLog(r, 400, "")
return
}
respBytes, _, err := boshhandler.PerformHandlerWithJSON(
rawJSONPayload,
handlerFunc,
boshhandler.UnlimitedResponseLength,
h.logger,
)
if err != nil {
err = bosherr.WrapError(err, "Running handler in a nice JSON sandwich")
h.logger.Error(httpsHandlerLogTag, err.Error())
w.WriteHeader(500)
h.generateCEFLog(r, 500, "")
return
}
_, err = w.Write(respBytes)
if err != nil {
err = bosherr.WrapError(err, "Writing response")
h.logger.Error(httpsHandlerLogTag, err.Error())
}
h.generateCEFLog(r, 200, "")
}
}
func (h HTTPSHandler) blobsHandler() (blobsHandler func(http.ResponseWriter, *http.Request)) {
blobsHandler = func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
h.getBlob(w, r)
case "PUT":
h.putBlob(w, r)
default:
w.WriteHeader(404)
h.generateCEFLog(r, 404, "")
}
return
}
return
}
func (h HTTPSHandler) putBlob(w http.ResponseWriter, r *http.Request) {
_, blobID := path.Split(r.URL.Path)
err := h.blobManager.Write(blobID, r.Body)
if err != nil {
w.WriteHeader(500)
h.generateCEFLog(r, 500, "")
if _, wErr := w.Write([]byte(err.Error())); wErr != nil {
h.logger.Error(httpsHandlerLogTag, "Failed to write response body: %s", wErr.Error())
}
return
}
w.WriteHeader(201)
h.generateCEFLog(r, 201, "")
}
func (h HTTPSHandler) getBlob(w http.ResponseWriter, r *http.Request) {
_, blobID := path.Split(r.URL.Path)
file, statusCode, err := h.blobManager.Fetch(blobID)
if err != nil {
h.logger.Error(httpsHandlerLogTag, "Failed to fetch blob: %s", err.Error())
w.WriteHeader(statusCode)
} else {
defer func() {
_ = file.Close()
}()
reader := bufio.NewReader(file)
if _, wErr := io.Copy(w, reader); wErr != nil {
h.logger.Error(httpsHandlerLogTag, "Failed to write response body: %s", wErr.Error())
}
}
h.generateCEFLog(r, statusCode, "")
}
func (h HTTPSHandler) generateCEFLog(r *http.Request, respStatusCode int, respJSON string) {
cef := boshhandler.NewCommonEventFormat()
cefString, err := cef.ProduceHTTPRequestEventLog(r, respStatusCode, respJSON)
if err != nil {
h.logger.Error(httpsHandlerLogTag, err.Error())
return
}
h.auditLogger.Debug(cefString)
}