-
Notifications
You must be signed in to change notification settings - Fork 1
/
ossobject.go
221 lines (194 loc) · 5.76 KB
/
ossobject.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
package ossobject
import (
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"fmt"
"hash"
"io"
"mime"
"net/http"
"path/filepath"
"time"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"go.uber.org/zap"
)
// Interface guards
var (
// _ caddy.Provisioner = (*OSSObject)(nil)
// _ caddy.Validator = (*OSSObject)(nil)
_ caddyhttp.MiddlewareHandler = (*OSSObject)(nil)
_ caddyfile.Unmarshaler = (*OSSObject)(nil)
)
func init() {
caddy.RegisterModule(OSSObject{})
httpcaddyfile.RegisterHandlerDirective("oss_object", parseCaddyfile)
}
func copyHeader(dst, src http.Header) {
for k, vv := range src {
for _, v := range vv {
dst.Add(k, v)
}
}
}
// OSSObject implements an HTTP handler that
// provide content from aliyun oss
type OSSObject struct {
Endpoint string `json:"endpoint,omitempty"`
AccessKeyID string `json:"access_key_id,omitempty"`
AccessKeySecret string `json:"access_key_secret,omitempty"`
Bucket string `json:"bucket,omitempty"`
ObjectKey string `json:"object_key,omitempty"`
logger *zap.Logger
}
// CaddyModule returns the Caddy module information.
func (OSSObject) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: "http.handlers.oss_object",
New: func() caddy.Module { return new(OSSObject) },
}
}
// Provision implements caddy.Provisioner.
func (m *OSSObject) Provision(ctx caddy.Context) error {
m.logger = ctx.Logger(m)
return nil
}
// Validate implements caddy.Validator.
// func (m OSSObject) Validate() error {
// if !hashAlgorithm(m.Algorithm).valid() {
// return fmt.Errorf("unsupported hash type '%s'", m.Algorithm)
// }
// if m.hasher == nil {
// // this will never happen
// return fmt.Errorf("hasher is null")
// }
// return nil
// }
// ServeHTTP implements caddyhttp.MiddlewareHandler.
func (m OSSObject) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
// TODO: support more method
if r.Method != http.MethodGet {
return next.ServeHTTP(w, r)
}
repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
if m.AccessKeyID == "" {
m.AccessKeyID = "{env.ACCESS_KEY_ID}"
}
if m.AccessKeySecret == "" {
m.AccessKeySecret = "{env.ACCESS_KEY_SECRET}"
}
endpoint := repl.ReplaceAll(m.Endpoint, "")
accessKeyID := repl.ReplaceAll(m.AccessKeyID, "")
accessKeySecret := repl.ReplaceAll(m.AccessKeySecret, "")
bucket := repl.ReplaceAll(m.Bucket, "")
objectKey := repl.ReplaceAll(m.ObjectKey, "")
canonicalizedResource := fmt.Sprintf("/%s/%s", bucket, objectKey)
date := time.Now().UTC().Format(http.TimeFormat)
// TODO: support x-oss-* headers
signStr := "GET" + "\n\n\n" + date + "\n" + canonicalizedResource
h := hmac.New(func() hash.Hash { return sha1.New() }, []byte(accessKeySecret))
io.WriteString(h, signStr)
signedStr := base64.StdEncoding.EncodeToString(h.Sum(nil))
req, _ := http.NewRequest(http.MethodGet, fmt.Sprintf("https://%s.%s/%s", bucket, endpoint, objectKey), nil)
copyHeader(req.Header, r.Header)
req.Header.Set("Authorization", fmt.Sprintf("OSS %s:%s", accessKeyID, signedStr))
req.Header.Set("Date", date)
res, err := http.DefaultClient.Do(req)
if err != nil {
return next.ServeHTTP(w, r)
}
defer res.Body.Close()
copyHeader(w.Header(), res.Header)
mtyp := mime.TypeByExtension(filepath.Ext(objectKey))
if mtyp == "" {
// do not allow Go to sniff the content-type; see
// https://www.youtube.com/watch?v=8t8JYpt0egE
// TODO: If we want a Content-Type, consider writing a default of application/octet-stream - this is secure but violates spec
w.Header()["Content-Type"] = nil
} else {
w.Header().Set("Content-Type", mtyp)
}
if res.StatusCode >= 400 && res.StatusCode <= 600 {
// TODO: better handle 4XX 5XX
// w.Header().Set("Content-Length", "0")
w.WriteHeader(res.StatusCode)
io.Copy(w, res.Body)
} else if res.StatusCode >= 300 && res.StatusCode < 400 {
// TODO: better handle 3XX
// w.Header().Set("Content-Length", "0")
w.WriteHeader(res.StatusCode)
io.Copy(w, res.Body)
} else {
w.WriteHeader(res.StatusCode)
io.Copy(w, res.Body)
}
return nil
}
// UnmarshalCaddyfile implements caddyfile.Unmarshaler.
// oss_object {
// endpoint oss-cn-shanghai.aliyuncs.com
// access_key_id IWILLNOTTELLU
// access_key_secret IWILLNOTTELLU
// bucket test
// object_key test/index.html
// }
// TODO: add validator
func (m *OSSObject) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
for d.Next() {
args := d.RemainingArgs()
if len(args) != 0 {
return d.ArgErr()
}
for d.NextBlock(0) {
switch d.Val() {
case "endpoint":
endpoint := d.RemainingArgs()
if len(endpoint) == 1 {
m.Endpoint = endpoint[0]
} else {
return d.ArgErr()
}
case "access_key_id":
accessKeyID := d.RemainingArgs()
if len(accessKeyID) == 1 {
m.AccessKeyID = accessKeyID[0]
} else {
return d.ArgErr()
}
case "access_key_secret":
accessKeySecret := d.RemainingArgs()
if len(accessKeySecret) == 1 {
m.AccessKeySecret = accessKeySecret[0]
} else {
return d.ArgErr()
}
case "bucket":
bucket := d.RemainingArgs()
if len(bucket) == 1 {
m.Bucket = bucket[0]
} else {
return d.ArgErr()
}
case "object_key":
objectKey := d.RemainingArgs()
if len(objectKey) == 1 {
m.ObjectKey = objectKey[0]
} else {
return d.ArgErr()
}
default:
return d.Errf("unknown subdirective '%s'", d.Val())
}
}
}
return nil
}
// parseCaddyfile unmarshals tokens from h into a new Middleware.
func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
var m OSSObject
err := m.UnmarshalCaddyfile(h.Dispenser)
return m, err
}