/
tempurl.go
207 lines (188 loc) · 6.22 KB
/
tempurl.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
// Copyright (c) 2017 Rackspace
//
// 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
// limitations under the License.
package middleware
import (
"crypto/hmac"
"crypto/sha1"
"encoding/hex"
"fmt"
"net/http"
"path/filepath"
"strings"
"time"
"github.com/troubling/hummingbird/common"
"github.com/troubling/hummingbird/common/conf"
"github.com/troubling/hummingbird/common/srv"
"github.com/uber-go/tally"
)
const (
SCOPE_INVALID = iota
SCOPE_ACCOUNT
SCOPE_CONTAINER
)
type tuWriter struct {
http.ResponseWriter
method string
filename string
obj string
expires string
inline bool
}
func dispositionFormat(dtype string, filename string) string {
return fmt.Sprintf("%s; filename=\"%s\"; filename*=UTF-8''%s",
dtype, common.Urlencode(filename), common.Urlencode(filename))
}
func (w *tuWriter) WriteHeader(status int) {
if (w.method == "GET" || w.method == "HEAD") && status/100 == 2 {
for k := range w.Header() {
if strings.HasPrefix(k, "X-Object-Meta") && !strings.HasPrefix(k, "X-Object-Meta-Public-") {
w.Header().Del(k)
}
}
if w.inline {
if w.filename == "" {
w.Header().Set("Content-Disposition", "inline")
} else {
w.Header().Set("Content-Disposition", dispositionFormat("inline", w.filename))
}
} else if w.filename != "" {
w.Header().Set("Content-Disposition", dispositionFormat("attachment", w.filename))
} else if w.Header().Get("Content-Disposition") == "" {
w.Header().Set("Content-Disposition", dispositionFormat("attachment", filepath.Base(w.obj)))
}
w.Header().Set("Expires", w.expires)
}
w.ResponseWriter.WriteHeader(status)
}
func checkhmac(key, sig []byte, method, path string, expires time.Time) bool {
if method == "HEAD" {
for _, meth := range []string{"HEAD", "GET", "POST", "PUT"} {
mac := hmac.New(sha1.New, key)
fmt.Fprintf(mac, "%s\n%d\n%s", meth, expires.Unix(), path)
if hmac.Equal(sig, mac.Sum(nil)) {
return true
}
}
return false
} else {
mac := hmac.New(sha1.New, key)
fmt.Fprintf(mac, "%s\n%d\n%s", method, expires.Unix(), path)
return hmac.Equal(sig, mac.Sum(nil))
}
}
func tempurl(requestsMetric tally.Counter) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
if request.Method == "OPTIONS" {
next.ServeHTTP(writer, request)
return
}
ctx := GetProxyContext(request)
if ctx.Authorize != nil {
next.ServeHTTP(writer, request)
return
}
q := request.URL.Query()
sig := q.Get("temp_url_sig")
exps := q.Get("temp_url_expires")
_, inline := q["inline"]
if sig == "" && exps == "" {
next.ServeHTTP(writer, request)
return
} else if sig == "" || exps == "" {
srv.StandardResponse(writer, 401)
return
}
requestsMetric.Inc(1)
expires, err := common.ParseDate(exps)
if err != nil || time.Now().After(expires) {
srv.StandardResponse(writer, 401)
return
}
sigb, err := hex.DecodeString(sig)
if err != nil {
srv.StandardResponse(writer, 401)
return
}
apiReq, account, container, obj := getPathParts(request)
if !apiReq || account == "" || container == "" {
srv.StandardResponse(writer, 401)
return
}
if bh := request.Header.Get("X-Object-Manifest"); bh != "" && (request.Method == "PUT" || request.Method == "POST") {
srv.StandardResponse(writer, 400)
return
}
path := ""
if _, hasPrefix := q["temp_url_prefix"]; hasPrefix {
prefix := q.Get("temp_url_prefix")
if !strings.HasPrefix(obj, prefix) {
srv.StandardResponse(writer, 401)
return
}
path = fmt.Sprintf("prefix:/v1/%s/%s/%s", account, container, prefix)
} else {
path = fmt.Sprintf("/v1/%s/%s/%s", account, container, obj)
}
scope := SCOPE_INVALID
if ai, err := ctx.GetAccountInfo(request.Context(), account); err == nil {
if key, ok := ai.Metadata["Temp-Url-Key"]; ok && checkhmac([]byte(key), sigb, request.Method, path, expires) {
scope = SCOPE_ACCOUNT
} else if key, ok := ai.Metadata["Temp-Url-Key-2"]; ok && checkhmac([]byte(key), sigb, request.Method, path, expires) {
scope = SCOPE_ACCOUNT
} else if ci, err := ctx.C.GetContainerInfo(request.Context(), account, container); err == nil {
if key, ok := ci.Metadata["Temp-Url-Key"]; ok && checkhmac([]byte(key), sigb, request.Method, path, expires) {
scope = SCOPE_CONTAINER
} else if key, ok := ci.Metadata["Temp-Url-Key-2"]; ok && checkhmac([]byte(key), sigb, request.Method, path, expires) {
scope = SCOPE_CONTAINER
}
}
}
if scope == SCOPE_INVALID {
srv.StandardResponse(writer, 401)
return
}
ctx.RemoteUsers = []string{".tempurl"}
ctx.Authorize = func(r *http.Request) (bool, int) {
ar, a, c, _ := getPathParts(r)
if ar && ((scope == SCOPE_ACCOUNT && a == account) || (scope == SCOPE_CONTAINER && c == container)) {
return true, http.StatusOK
}
return false, http.StatusUnauthorized
}
next.ServeHTTP(
&tuWriter{
ResponseWriter: writer,
method: request.Method,
obj: obj,
filename: q.Get("filename"),
expires: expires.Format(time.RFC1123),
inline: inline,
},
request,
)
})
}
}
func NewTempURL(config conf.Section, metricsScope tally.Scope) (func(http.Handler) http.Handler, error) {
RegisterInfo("tempurl", map[string]interface{}{
"methods": []string{"GET", "HEAD", "PUT", "POST", "DELETE"},
"incoming_remove_headers": []string{"x-timestamp"},
"incoming_allow_headers": []string{},
"outgoing_remove_headers": []string{"x-object-meta-*"}, "outgoing_allow_headers": []string{"x-object-meta-public-*"},
})
requestsMetric := metricsScope.Counter("tempurl_requests")
return tempurl(requestsMetric), nil
}