-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
299 lines (266 loc) · 9.12 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
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
// Copyright 2020 Booking.com
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
package http
import (
"bytes"
"context"
"crypto/tls"
"fmt"
"io"
"net"
"net/http"
"time"
"github.com/rs/zerolog"
"github.com/bookingcom/yubistack/pkg/ykauth"
"github.com/bookingcom/yubistack/pkg/ykksm"
"github.com/bookingcom/yubistack/pkg/ykval"
)
const (
defaultAddr = ":8080"
defaultReadTimeout = 5 * time.Second
defaultWriteTimeout = 10 * time.Second
defaultIdleTimeout = 120 * time.Second
// HeaderContent is constant extracted value of Content-Type header
HeaderContent = "Content-Type"
// HeaderAccept is constant extracted value of Accept header
HeaderAccept = "Accept"
// MimeText is constant extracted value of mimetype for text
MimeText = "text/plain"
// MimeJSON is constant extracted value of mimetype for text
MimeJSON = "application/json"
// FmtErr is yubico format for text error response
FmtErr = "ERR %s"
)
var (
// ErrSkip is the error used to notify that error has been handled
ErrSkip = fmt.Errorf("error already handled")
//nolint:golint,gochecknoglobals
// TLS preferences
preferredCiphers = []uint16{
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
tls.TLS_AES_256_GCM_SHA384,
tls.TLS_AES_128_GCM_SHA256,
}
//nolint:golint,gochecknoglobals
preferredCurves = []tls.CurveID{
tls.X25519, // Go 1.8 only
tls.CurveP256,
}
)
type (
// HandlerFunc is a wrapper for a function to implement the Handler interface
HandlerFunc func(*Context) error
// Handler is an interface we will use to handle HTTP requests
Handler interface {
Handle(*Context) error
}
)
// Handle wraps a function to implement Handler interface
func (hf HandlerFunc) Handle(ctx *Context) error { return hf(ctx) }
// Context wraps most of the structures we want to pass down to the Handler
// It contains the stdlib ServeHTTP arguments: Request, Response and is
// enhanced with other services, for the moment only the Logger, but some
// may be added later (like DB or so).
type Context struct {
*Response
*http.Request
zerolog.Logger
}
// Error function allow to quickly define an error response
func (c *Context) Error(msg string, code int) error {
c.error(msg, code)
return ErrSkip
}
func (c *Context) error(msg string, code int) {
c.Code = code
c.Writef(FmtErr, msg)
}
// RemoteHost get the value of the host without the port
func (c *Context) RemoteHost() string {
host, _, err := net.SplitHostPort(c.RemoteAddr)
if err != nil {
return c.RemoteAddr
}
return host
}
// Writef creates a text response and write it to the buffer with format
func (c *Context) Writef(format string, a ...interface{}) {
// according to documentation https://golang.org/pkg/bytes/?m=all#Buffer.Write
// writing never fails with buffer
_, _ = fmt.Fprintf(c.Buffer, format, a...)
}
func (c *Context) write() {
c.Debug().Str("buffer", c.Buffer.String()).Msgf("encoding response")
if c.Response.w.Header().Get(HeaderContent) == "" {
c.Response.w.Header().Set(HeaderContent, MimeText+"; charset=utf-8")
}
c.Response.w.Header().Set("X-Content-Type-Options", "nosniff")
c.Response.w.WriteHeader(c.Code)
if _, err := io.Copy(c.Response.w, c.Buffer); err != nil {
c.Code = http.StatusInternalServerError
c.Logger.Error().Err(err).Msg("failed encoding response")
}
}
// ErrorHandler is the function type we will use to customize error handling
type ErrorHandler func(*Context, error)
// DefaultErrorHandler is the default way of dealing with error in Yubistack.
// It maps error returned by the caller in Yubico text equivalent responses
func DefaultErrorHandler(ctx *Context, err error) {
switch err {
case nil, ErrSkip:
return
case ykval.ErrNoID, ykval.ErrInvalidID, ykksm.ErrNoOTP, ykval.ErrNoNonce, ykval.ErrInvalidNonce:
ctx.error(ykval.MissingParameter.String(), http.StatusBadRequest)
case ykksm.ErrInvalidOTP, ykksm.ErrCorruptOTP, ykval.ErrDelayedOTP, ykauth.ErrInvalidPassword:
ctx.error(ykval.BadOTP.String(), http.StatusBadRequest)
case ykval.ErrNoClient:
ctx.error(ykval.NoSuchClient.String(), http.StatusBadRequest)
case ykval.ErrInvalidSignature:
ctx.error(ykval.BadSignature.String(), http.StatusBadRequest)
case ykval.ErrReplayedOTP:
ctx.error(ykval.ReplayedOTP.String(), http.StatusBadRequest)
case ykval.ErrReplayedRequest:
ctx.error(ykval.ReplayedRequest.String(), http.StatusBadRequest)
case ykval.ErrNotEnoughAnswers, context.DeadlineExceeded:
ctx.error(ykval.NotEnoughAnswers.String(), http.StatusInternalServerError)
case ykauth.ErrNoUser:
// TODO: decide if we extend Yubico specification or loose information notifying the user.
// this case is not handled through Yubico specification. A decision has
// to be made regarding this and proper documentation has to be written.
// In the meantime relying on this hardcoded string will do the trick.
ctx.error("NO_USER", http.StatusBadRequest)
default:
ctx.error(ykval.BackendError.String(), http.StatusInternalServerError)
}
ctx.Logger.Error().Msgf("failed serving %s", ctx.Request.URL.Path)
}
type (
// Server wraps the serving logic
Server struct {
*http.Server
zerolog.Logger
ErrorHandler
middlewares []Middleware
router *http.ServeMux
tlsCertFile string
tlsKeyFile string
}
// ServerOption is the function type used to customize the Server fields at creation
ServerOption func(*Server)
// Middleware is the function type we will use to chain middleware in the application
Middleware func(HandlerFunc) HandlerFunc
)
// NewServer creates an instance of a server using option parameters to configure.
func NewServer(options ...ServerOption) *Server {
mux := http.NewServeMux()
server := &Server{
Server: &http.Server{
Addr: defaultAddr,
Handler: mux,
ReadTimeout: defaultReadTimeout,
WriteTimeout: defaultWriteTimeout,
IdleTimeout: defaultIdleTimeout,
},
Logger: zerolog.Nop(),
ErrorHandler: DefaultErrorHandler,
middlewares: []Middleware{},
router: mux,
}
for _, option := range options {
option(server)
}
return server
}
// WithTLS creates a ServerOption setting up TLS options
func WithTLS(certFile, keyFile string) ServerOption {
return func(server *Server) {
server.tlsCertFile = certFile
server.tlsKeyFile = keyFile
}
}
// Use plugs a middleware in the request handling chain
func (s *Server) Use(middlewares ...Middleware) {
s.middlewares = append(s.middlewares, middlewares...)
}
// HandleFunc add a handler function to a specific path
func (s *Server) HandleFunc(path string, handle HandlerFunc) {
wrapped := HandlerFunc(func(ctx *Context) error {
if err := handle(ctx); err != nil {
s.ErrorHandler(ctx, err)
}
return nil
})
for i := len(s.middlewares) - 1; i >= 0; i-- {
wrapped = s.middlewares[i](wrapped)
}
s.router.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
ctx := &Context{NewResponse(w), r, s.Logger}
if err := wrapped(ctx); err != nil {
s.Error().Err(err).Msg("middleware error")
}
ctx.write()
})
}
// Handle add a handler to a specific path
func (s *Server) Handle(path string, handler Handler) { s.HandleFunc(path, handler.Handle) }
// Serve starts a server on the given network and address and route request to handlers
func (s *Server) Serve(network, address string) error {
if s.tlsCertFile == "" {
s.Info().Str("addr", address).Msg("serving http")
} else {
s.Info().Str("addr", address).Msg("serving https")
}
var (
conn net.Listener
err error
)
if conn, err = net.Listen(network, address); err != nil {
return err
}
if s.tlsCertFile != "" {
cert, err := tls.LoadX509KeyPair(s.tlsCertFile, s.tlsKeyFile)
if err != nil {
_ = conn.Close()
return err
}
conn = tls.NewListener(conn, &tls.Config{
Certificates: []tls.Certificate{cert},
CipherSuites: preferredCiphers,
PreferServerCipherSuites: true,
SessionTicketsDisabled: true,
CurvePreferences: preferredCurves,
MinVersion: tls.VersionTLS12,
})
}
return s.Server.Serve(conn)
}
// ListenAndServe starts a server on the configured addressed over TCP
func (s *Server) ListenAndServe() error { return s.Serve("tcp", s.Server.Addr) }
// Response wraps the stdlib ResponseWriter interface. It allows to keep
// some states before writing them to the underlying ResponseWriter where
// this information can't be retrieved once pushed. The state is pushed by
// the Context.write() function which is not accessible through the public API.
type Response struct {
Code int
Header http.Header
*bytes.Buffer
w http.ResponseWriter
}
// NewResponse creates a new instance of a response with default OK state
func NewResponse(w http.ResponseWriter) *Response {
return &Response{
http.StatusOK, w.Header(),
bytes.NewBuffer(nil), w,
}
}