/
server.go
236 lines (206 loc) · 5.94 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
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
package server
import (
"fmt"
"net"
"net/http"
"os"
"path"
"regexp"
"github.com/gorilla/mux"
"github.com/sirupsen/logrus"
"github.com/libopenstorage/openstorage/pkg/auth"
)
// Route is a specification and handler for a REST endpoint.
type Route struct {
verb string
path string
fn func(http.ResponseWriter, *http.Request)
}
// AnonRegex defines a regex replace for a string
type AnonRegex struct {
anonymizeRegx *regexp.Regexp
replaceString string
}
// anonIDRegxes[] used to hide ID content when logging.
var anonIDRegxes = []AnonRegex{
{
// Anonymize the token=... and replace with token=***..*
// JWT Regex below pulled from - https://www.regextester.com/105777
anonymizeRegx: regexp.MustCompile(`token=[A-Za-z0-9-_=]+\.[A-Za-z0-9-_=]+\.?[A-Za-z0-9-_.+/=]*`),
replaceString: `token=********`,
},
}
func (r *Route) GetVerb() string {
return r.verb
}
func (r *Route) GetPath() string {
return r.path
}
func (r *Route) GetFn() func(http.ResponseWriter, *http.Request) {
return r.fn
}
// StartGraphAPI starts a REST server to receive GraphDriver commands from
// the Linux container engine.
func StartGraphAPI(name string, restBase string) error {
graphPlugin := newGraphPlugin(name)
if _, _, err := startServer(name, restBase, 0, graphPlugin); err != nil {
return err
}
return nil
}
// StartVolumeMgmtAPI starts a REST server to receive volume management API commands
func StartVolumeMgmtAPI(
name, sdkUds string,
mgmtBase string,
mgmtPort uint16,
auth bool,
authenticators map[string]auth.Authenticator,
) (*http.Server, *http.Server, error) {
var (
unixServer, portServer *http.Server
err error
)
volMgmtApi := newVolumeAPI(name, sdkUds)
if auth {
unixServer, portServer, err = startServerWithAuth(
name,
mgmtBase,
mgmtPort,
volMgmtApi,
authenticators,
)
} else {
unixServer, portServer, err = startServer(
name,
mgmtBase,
mgmtPort,
volMgmtApi,
)
}
return unixServer, portServer, err
}
// StartVolumePluginAPI starts a REST server to receive volume API commands
// from the linux container engine
func StartVolumePluginAPI(
name, sdkUds string,
pluginBase string,
pluginPort uint16,
) error {
volPluginApi := newVolumePlugin(name, sdkUds)
if _, _, err := startServer(
name,
pluginBase,
pluginPort,
volPluginApi,
); err != nil {
return err
}
return nil
}
// StartClusterAPI starts a REST server to receive driver configuration commands
// from the CLI/UX to control the OSD cluster.
func StartClusterAPI(clusterApiBase string, clusterPort uint16, authenticators map[string]auth.Authenticator) error {
clusterApi := newClusterAPI()
// start server as before
if _, _, err := startServerWithAuth("osd", clusterApiBase, clusterPort, clusterApi, authenticators); err != nil {
return err
}
return nil
}
func GetClusterAPIRoutes() []*Route {
clusterApi := newClusterAPI()
return clusterApi.Routes()
}
func SetClusterAPIRoutesWithAuth(router *mux.Router, authenticators map[string]auth.Authenticator) (*mux.Router, error) {
clusterApi := newClusterAPI()
return clusterApi.SetupRoutesWithAuth(router, authenticators)
}
func startServerWithAuth(
name, sockBase string,
port uint16,
rs restServer,
authenticators map[string]auth.Authenticator,
) (*http.Server, *http.Server, error) {
var err error
router := mux.NewRouter()
router.NotFoundHandler = http.HandlerFunc(notFound)
router, err = rs.SetupRoutesWithAuth(router, authenticators)
if err != nil {
return nil, nil, err
}
return startServerCommon(name, sockBase, port, rs, router)
}
func startServer(name string, sockBase string, port uint16, rs restServer) (*http.Server, *http.Server, error) {
router := mux.NewRouter()
router.NotFoundHandler = http.HandlerFunc(notFound)
for _, v := range rs.Routes() {
router.Methods(v.verb).Path(v.path).HandlerFunc(v.fn)
}
return startServerCommon(name, sockBase, port, rs, router)
}
func startServerCommon(name string, sockBase string, port uint16, rs restServer, router *mux.Router) (*http.Server, *http.Server, error) {
var (
listener net.Listener
err error
)
socket := path.Join(sockBase, name+".sock")
os.Remove(socket)
os.MkdirAll(path.Dir(socket), 0755)
logrus.Printf("Starting REST service on socket : %+v", socket)
listener, err = net.Listen("unix", socket)
if err != nil {
logrus.Warnln("Cannot listen on UNIX socket: ", err)
return nil, nil, err
}
unixServer := &http.Server{Handler: router}
go func() {
if err := unixServer.Serve(listener); err != nil {
logrus.Errorf("error starting unix server %v", err)
}
}()
if port != 0 {
logrus.Printf("Starting REST service on port : %v", port)
portServer := &http.Server{Addr: fmt.Sprintf(":%d", port), Handler: router}
go portServer.ListenAndServe()
return unixServer, portServer, nil
}
return unixServer, nil, nil
}
type restServer interface {
Routes() []*Route
SetupRoutesWithAuth(router *mux.Router, authenticators map[string]auth.Authenticator) (*mux.Router, error)
String() string
logRequest(request string, id string) *logrus.Entry
sendError(request string, id string, w http.ResponseWriter, msg string, code int)
}
type restBase struct {
restServer
version string
name string
}
func regxAnonString(inputStr string, anonRegxes []AnonRegex) string {
anonString := inputStr
if len(anonString) > 0 {
for _, anonRegx := range anonRegxes {
anonString = anonRegx.anonymizeRegx.ReplaceAllString(
anonString,
anonRegx.replaceString)
}
}
return anonString
}
func (rest *restBase) logRequest(request string, id string) *logrus.Entry {
return logrus.WithFields(map[string]interface{}{
"Driver": rest.name,
"Request": request,
"ID": regxAnonString(id, anonIDRegxes),
})
}
func (rest *restBase) sendError(request string, id string, w http.ResponseWriter, msg string, code int) {
rest.logRequest(request, id).Warnln(code, " ", msg)
http.Error(w, msg, code)
}
func notFound(w http.ResponseWriter, r *http.Request) {
logrus.Warnf("Not found: %+v ", r.URL)
http.NotFound(w, r)
}