-
Notifications
You must be signed in to change notification settings - Fork 351
/
middleware.go
332 lines (310 loc) · 10.5 KB
/
middleware.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package gateway
import (
"context"
"errors"
"net/http"
"regexp"
"strconv"
"strings"
"time"
"github.com/treeverse/lakefs/pkg/auth"
"github.com/treeverse/lakefs/pkg/catalog"
gatewayerrors "github.com/treeverse/lakefs/pkg/gateway/errors"
"github.com/treeverse/lakefs/pkg/gateway/operations"
"github.com/treeverse/lakefs/pkg/gateway/path"
"github.com/treeverse/lakefs/pkg/gateway/sig"
"github.com/treeverse/lakefs/pkg/graveler"
"github.com/treeverse/lakefs/pkg/httputil"
"github.com/treeverse/lakefs/pkg/logging"
"github.com/treeverse/lakefs/pkg/permissions"
"github.com/treeverse/lakefs/pkg/stats"
)
func AuthenticationHandler(authService auth.GatewayService, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
ctx := req.Context()
user, err := auth.GetUser(ctx)
if err == nil {
ctx = logging.AddFields(ctx, logging.Fields{logging.UserFieldKey: user.Username})
req = req.WithContext(auth.WithUser(ctx, user))
next.ServeHTTP(w, req)
return
}
o := ctx.Value(ContextKeyOperation).(*operations.Operation)
authenticator := sig.ChainedAuthenticator(
sig.NewV4Authenticator(req),
sig.NewV2SigAuthenticator(req, o.FQDN),
sig.NewJavaV2SigAuthenticator(req, o.FQDN),
)
authContext, err := authenticator.Parse()
if err != nil {
o.Log(req).WithError(err).Warn("failed to parse signature")
_ = o.EncodeError(w, req, err, getAPIErrOrDefault(err, gatewayerrors.ErrAccessDenied))
return
}
accessKeyID := authContext.GetAccessKeyID()
creds, err := authService.GetCredentials(ctx, accessKeyID)
logger := o.Log(req)
if err != nil {
if !errors.Is(err, auth.ErrNotFound) {
logger.WithError(err).Warn("error getting access key")
_ = o.EncodeError(w, req, err, gatewayerrors.ErrInternalError.ToAPIErr())
} else {
logger.WithError(err).Warn("could not find access key")
_ = o.EncodeError(w, req, err, gatewayerrors.ErrAccessDenied.ToAPIErr())
}
return
}
err = authenticator.Verify(creds)
if err != nil {
logger.WithError(err).Warn("error verifying credentials for key")
_ = o.EncodeError(w, req, err, getAPIErrOrDefault(err, gatewayerrors.ErrAccessDenied))
return
}
user, err = authService.GetUser(ctx, creds.Username)
if err != nil {
logger.WithError(err).Warn("could not get user for credentials key")
_ = o.EncodeError(w, req, err, gatewayerrors.ErrAccessDenied.ToAPIErr())
return
}
ctx = logging.AddFields(ctx, logging.Fields{logging.UserFieldKey: user.Username})
ctx = auth.WithUser(ctx, user)
ctx = context.WithValue(ctx, ContextKeyAuthContext, authContext)
req = req.WithContext(ctx)
next.ServeHTTP(w, req)
})
}
func EnrichWithParts(bareDomains []string, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
ctx := req.Context()
parts := ParseRequestParts(req.Host, req.URL.Path, bareDomains)
ctx = context.WithValue(ctx, ContextKeyRepositoryID, parts.Repository)
ctx = context.WithValue(ctx, ContextKeyRef, parts.Ref)
ctx = context.WithValue(ctx, ContextKeyPath, parts.Path)
ctx = context.WithValue(ctx, ContextKeyMatchedHost, parts.MatchedHost)
req = req.WithContext(ctx)
next.ServeHTTP(w, req)
})
}
func getBareDomain(hostname string, bareDomains []string) string {
for _, bd := range bareDomains {
if hostname == stripPort(bd) || strings.HasSuffix(hostname, "."+stripPort(bd)) {
return bd
}
}
// If no matching bare domain found, assume no gateways.s3.domain_name setting existing,
// and we're using path-based routing, with whichever domain our Host header specifies.
return hostname
}
var trailingPortRegexp = regexp.MustCompile(`:\d+$`)
func stripPort(host string) string {
return trailingPortRegexp.ReplaceAllString(host, "")
}
func EnrichWithOperation(sc *ServerContext, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
ctx := req.Context()
client := httputil.GetRequestLakeFSClient(req)
o := &operations.Operation{
Region: sc.region,
FQDN: getBareDomain(stripPort(req.Host), sc.bareDomains),
Catalog: sc.catalog,
MultipartTracker: sc.multipartTracker,
BlockStore: sc.blockStore,
Auth: sc.authService,
VerifyUnsupported: sc.verifyUnsupported,
Incr: func(action, userID, repository, ref string) {
logging.FromContext(ctx).
WithFields(logging.Fields{
"action": action,
"message_type": "action",
"repository": repository,
"ref": ref,
"user_id": userID,
}).
Debug("performing S3 action")
sc.stats.CollectEvent(stats.Event{
Class: "s3_gateway",
Name: action,
Repository: repository,
Ref: ref,
UserID: userID,
Client: client,
})
},
PathProvider: sc.pathProvider,
}
next.ServeHTTP(w, req.WithContext(context.WithValue(ctx, ContextKeyOperation, o)))
})
}
func DurationHandler(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
ctx := req.Context()
o := ctx.Value(ContextKeyOperation).(*operations.Operation)
start := time.Now()
mrw := httputil.NewMetricResponseWriter(w)
next.ServeHTTP(mrw, req)
requestHistograms.WithLabelValues(string(o.OperationID), strconv.Itoa(mrw.StatusCode)).Observe(time.Since(start).Seconds())
})
}
func EnrichWithRepositoryOrFallback(c *catalog.Catalog, authService auth.GatewayService, fallbackProxy http.Handler, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
ctx := req.Context()
repoID := ctx.Value(ContextKeyRepositoryID).(string)
user, _ := auth.GetUser(ctx)
username := user.Username
o := ctx.Value(ContextKeyOperation).(*operations.Operation)
if repoID == "" {
// action without a repo
next.ServeHTTP(w, req)
return
}
repo, err := c.GetRepository(ctx, repoID)
if errors.Is(err, graveler.ErrNotFound) {
authResp, authErr := authService.Authorize(ctx, &auth.AuthorizationRequest{
Username: username,
RequiredPermissions: permissions.Node{
Permission: permissions.Permission{Action: permissions.ListRepositoriesAction, Resource: "*"},
},
})
if authErr != nil || authResp.Error != nil || !authResp.Allowed {
_ = o.EncodeError(w, req, err, gatewayerrors.ErrAccessDenied.ToAPIErr())
return
}
if fallbackProxy != nil {
fallbackProxy.ServeHTTP(w, req)
return
}
_ = o.EncodeError(w, req, err, gatewayerrors.ErrNoSuchBucket.ToAPIErr())
return
}
if repo == nil {
_ = o.EncodeError(w, req, err, gatewayerrors.ErrInternalError.ToAPIErr())
return
}
req = req.WithContext(context.WithValue(ctx, ContextKeyRepository, repo))
next.ServeHTTP(w, req)
})
}
func OperationLookupHandler(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
ctx := req.Context()
o := ctx.Value(ContextKeyOperation).(*operations.Operation)
repoID := ctx.Value(ContextKeyRepositoryID).(string)
ref := ctx.Value(ContextKeyRef).(string)
pth := ctx.Value(ContextKeyPath).(string)
// based on the operation level, we can determine the operation id
switch {
case repoID == "":
o.OperationID = rootBasedOperationID(req.Method)
case ref != "" && pth != "":
o.OperationID = pathBasedOperationID(req.Method)
case ref == "" && pth == "":
o.OperationID = repositoryBasedOperationID(req.Method)
default:
o.OperationID = operations.OperationIDOperationNotFound
}
req = req.WithContext(logging.AddFields(ctx, logging.Fields{"operation_id": o.OperationID}))
next.ServeHTTP(w, req)
})
}
// memberFold returns true if 'a' is an equal case-folded to a member of bs.
func memberFold(a string, bs []string) bool {
for _, b := range bs {
if strings.EqualFold(a, b) {
return true
}
}
return false
}
type RequestParts struct {
Repository string
Ref string
Path string
MatchedHost bool
}
// ParseRequestParts returns the repo id, ref and path according to whether the request is path-style or virtual-host-style.
func ParseRequestParts(host string, urlPath string, bareDomains []string) RequestParts {
var parts RequestParts
urlPath = strings.TrimPrefix(urlPath, path.Separator)
var p []string
ourHosts := httputil.HostsOnly(bareDomains)
// we need to check using this order:
// 1. if exact hosts, path based
// 2. if suffixes, virtual host
// 3. none of the above, path based
if memberFold(httputil.HostOnly(host), ourHosts) {
// path style: extract repo from first part
p = strings.SplitN(urlPath, path.Separator, 3) //nolint: gomnd
parts.Repository = p[0]
if len(p) >= 1 {
p = p[1:]
}
parts.MatchedHost = true
} else {
// virtual host style: extract repo from subdomain
host := httputil.HostOnly(host)
for _, ourHost := range ourHosts {
if strings.HasSuffix(host, ourHost) {
parts.Repository = strings.TrimSuffix(host, "."+ourHost)
parts.MatchedHost = true
break
}
}
if parts.MatchedHost {
p = strings.SplitN(urlPath, path.Separator, 2) //nolint: gomnd
}
}
if !parts.MatchedHost {
// assume path-based for domains we don't explicitly know
p = strings.SplitN(urlPath, path.Separator, 3) //nolint: gomnd
parts.Repository = p[0]
if len(p) >= 1 {
p = p[1:]
}
}
// extract ref and path from remaining parts
if len(p) > 0 {
parts.Ref = p[0]
}
if len(p) > 1 {
parts.Path = p[1]
}
return parts
}
func rootBasedOperationID(method string) operations.OperationID {
if method == http.MethodGet {
return operations.OperationIDListBuckets
}
return operations.OperationIDOperationNotFound
}
func pathBasedOperationID(method string) operations.OperationID {
switch method {
case http.MethodDelete:
return operations.OperationIDDeleteObject
case http.MethodPost:
return operations.OperationIDPostObject
case http.MethodGet:
return operations.OperationIDGetObject
case http.MethodHead:
return operations.OperationIDHeadObject
case http.MethodPut:
return operations.OperationIDPutObject
default:
return operations.OperationIDOperationNotFound
}
}
func repositoryBasedOperationID(method string) operations.OperationID {
switch method {
case http.MethodDelete:
return operations.OperationIDUnsupportedOperation
case http.MethodPut:
return operations.OperationIDPutBucket
case http.MethodHead:
return operations.OperationIDHeadBucket
case http.MethodPost:
return operations.OperationIDDeleteObjects
case http.MethodGet:
return operations.OperationIDListObjects
default:
return operations.OperationIDOperationNotFound
}
}