-
Notifications
You must be signed in to change notification settings - Fork 342
/
base.go
232 lines (201 loc) · 6.55 KB
/
base.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
package operations
import (
"bytes"
"encoding/xml"
"fmt"
"io"
"net/http"
"github.com/treeverse/lakefs/pkg/auth"
"github.com/treeverse/lakefs/pkg/block"
"github.com/treeverse/lakefs/pkg/catalog"
"github.com/treeverse/lakefs/pkg/gateway/errors"
"github.com/treeverse/lakefs/pkg/gateway/multiparts"
"github.com/treeverse/lakefs/pkg/gateway/simulator"
"github.com/treeverse/lakefs/pkg/httputil"
"github.com/treeverse/lakefs/pkg/logging"
)
const StorageClassHeader = "x-amz-storage-class"
type OperationID string
const (
OperationIDDeleteObject OperationID = "delete_object"
OperationIDDeleteObjects OperationID = "delete_objects"
OperationIDGetObject OperationID = "get_object"
OperationIDHeadBucket OperationID = "head_bucket"
OperationIDHeadObject OperationID = "head_object"
OperationIDListBuckets OperationID = "list_buckets"
OperationIDListObjects OperationID = "list_objects"
OperationIDPostObject OperationID = "post_object"
OperationIDPutObject OperationID = "put_object"
OperationIDPutBucket OperationID = "put_bucket"
OperationIDUnsupportedOperation OperationID = "unsupported"
OperationIDOperationNotFound OperationID = "not_found"
)
type ActionIncr func(string)
type Operation struct {
OperationID OperationID
Region string
FQDN string
Catalog catalog.Interface
MultipartsTracker multiparts.Tracker
BlockStore block.Adapter
Auth simulator.GatewayAuthService
Incr ActionIncr
MatchedHost bool
}
func StorageClassFromHeader(header http.Header) *string {
storageClass := header.Get(StorageClassHeader)
if storageClass == "" {
return nil
}
return &storageClass
}
func (o *Operation) Log(req *http.Request) logging.Logger {
return logging.FromContext(req.Context())
}
func EncodeXMLBytes(w http.ResponseWriter, t []byte, statusCode int) error {
w.WriteHeader(statusCode)
var b bytes.Buffer
b.WriteString(xml.Header)
b.Write(t)
_, err := b.WriteTo(w)
return err
}
func (o *Operation) EncodeXMLBytes(w http.ResponseWriter, req *http.Request, t []byte, statusCode int) {
err := EncodeXMLBytes(w, t, statusCode)
if err != nil {
o.Log(req).WithError(err).Error("failed to encode XML to response")
}
}
func EncodeResponse(w http.ResponseWriter, entity interface{}, statusCode int) error {
// We don't indent the XML document because of Java.
// See: https://github.com/spulec/moto/issues/1870
payload, err := xml.Marshal(entity)
if err != nil {
return err
}
return EncodeXMLBytes(w, payload, statusCode)
}
func (o *Operation) EncodeResponse(w http.ResponseWriter, req *http.Request, entity interface{}, statusCode int) {
err := EncodeResponse(w, entity, statusCode)
if err != nil {
o.Log(req).WithError(err).Error("encoding response failed")
}
}
func DecodeXMLBody(reader io.Reader, entity interface{}) error {
body := reader
content, err := io.ReadAll(body)
if err != nil {
return err
}
err = xml.Unmarshal(content, entity)
if err != nil {
return err
}
return nil
}
// SetHeader sets a header on the response while preserving its case
func (o *Operation) SetHeader(w http.ResponseWriter, key, value string) {
w.Header()[key] = []string{value}
}
// DeleteHeader deletes a header from the response
func (o *Operation) DeleteHeader(w http.ResponseWriter, key string) {
w.Header().Del(key)
}
// SetHeaders sets a map of headers on the response while preserving the header's case
func (o *Operation) SetHeaders(w http.ResponseWriter, headers map[string]string) {
for k, v := range headers {
o.SetHeader(w, k, v)
}
}
func (o *Operation) EncodeError(w http.ResponseWriter, req *http.Request, e errors.APIError) *http.Request {
req, rid := httputil.RequestID(req)
err := EncodeResponse(w, errors.APIErrorResponse{
Code: e.Code,
Message: e.Description,
BucketName: "",
Key: "",
Resource: "",
Region: o.Region,
RequestID: rid,
HostID: generateHostID(), // just for compatibility, meaningless in our case
}, e.HTTPStatusCode)
if err != nil {
o.Log(req).WithError(err).Error("encoding response failed")
}
return req
}
func generateHostID() string {
const generatedHostIDLength = 8
return auth.HexStringGenerator(generatedHostIDLength)
}
type AuthorizedOperation struct {
*Operation
Principal string
}
type RepoOperation struct {
*AuthorizedOperation
Repository *catalog.Repository
MatchedHost bool
}
func (o *RepoOperation) EncodeError(w http.ResponseWriter, req *http.Request, err errors.APIError) *http.Request {
req, rid := httputil.RequestID(req)
writeErr := EncodeResponse(w, errors.APIErrorResponse{
Code: err.Code,
Message: err.Description,
BucketName: o.Repository.Name,
Key: "",
Resource: o.Repository.Name,
Region: o.Region,
RequestID: rid,
HostID: generateHostID(),
}, err.HTTPStatusCode)
if writeErr != nil {
o.Log(req).WithError(writeErr).Error("encoding response failed")
}
return req
}
type RefOperation struct {
*RepoOperation
Reference string
}
type PathOperation struct {
*RefOperation
Path string
}
func (o *PathOperation) EncodeError(w http.ResponseWriter, req *http.Request, err errors.APIError) *http.Request {
req, rid := httputil.RequestID(req)
writeErr := EncodeResponse(w, errors.APIErrorResponse{
Code: err.Code,
Message: err.Description,
BucketName: o.Repository.Name,
Key: o.Path,
Resource: fmt.Sprintf("%s@%s", o.Reference, o.Repository.Name),
Region: o.Region,
RequestID: rid,
HostID: generateHostID(),
}, err.HTTPStatusCode)
if writeErr != nil {
o.Log(req).WithError(writeErr).Error("encoding response failed")
}
return req
}
type OperationHandler interface {
RequiredPermissions(req *http.Request) (auth.PermissionNode, error)
Handle(w http.ResponseWriter, req *http.Request, op *Operation)
}
type AuthenticatedOperationHandler interface {
RequiredPermissions(req *http.Request) (auth.PermissionNode, error)
Handle(w http.ResponseWriter, req *http.Request, op *AuthorizedOperation)
}
type RepoOperationHandler interface {
RequiredPermissions(req *http.Request, repository string) (auth.PermissionNode, error)
Handle(w http.ResponseWriter, req *http.Request, op *RepoOperation)
}
type BranchOperationHandler interface {
RequiredPermissions(req *http.Request, repository, branch string) (auth.PermissionNode, error)
Handle(w http.ResponseWriter, req *http.Request, op *RefOperation)
}
type PathOperationHandler interface {
RequiredPermissions(req *http.Request, repository, branch, path string) (auth.PermissionNode, error)
Handle(w http.ResponseWriter, req *http.Request, op *PathOperation)
}