1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
29 "git.arvados.org/arvados.git/sdk/go/arvados"
30 "git.arvados.org/arvados.git/sdk/go/ctxlog"
31 "github.com/AdRoll/goamz/s3"
36 s3SignAlgorithm = "AWS4-HMAC-SHA256"
37 s3MaxClockSkew = 5 * time.Minute
40 type commonPrefix struct {
44 type listV1Resp struct {
45 XMLName string `xml:"http://s3.amazonaws.com/doc/2006-03-01/ ListBucketResult"`
47 // s3.ListResp marshals an empty tag when
48 // CommonPrefixes is nil, which confuses some clients.
49 // Fix by using this nested struct instead.
50 CommonPrefixes []commonPrefix
51 // Similarly, we need omitempty here, because an empty
52 // tag confuses some clients (e.g.,
53 // github.com/aws/aws-sdk-net never terminates its
55 NextMarker string `xml:"NextMarker,omitempty"`
56 // ListObjectsV2 has a KeyCount response field.
60 type listV2Resp struct {
61 XMLName string `xml:"http://s3.amazonaws.com/doc/2006-03-01/ ListBucketResult"`
68 CommonPrefixes []commonPrefix
69 EncodingType string `xml:",omitempty"`
71 ContinuationToken string `xml:",omitempty"`
72 NextContinuationToken string `xml:",omitempty"`
73 StartAfter string `xml:",omitempty"`
76 func hmacstring(msg string, key []byte) []byte {
77 h := hmac.New(sha256.New, key)
78 io.WriteString(h, msg)
82 func hashdigest(h hash.Hash, payload string) string {
83 io.WriteString(h, payload)
84 return fmt.Sprintf("%x", h.Sum(nil))
87 // Signing key for given secret key and request attrs.
88 func s3signatureKey(key, datestamp, regionName, serviceName string) []byte {
89 return hmacstring("aws4_request",
90 hmacstring(serviceName,
91 hmacstring(regionName,
92 hmacstring(datestamp, []byte("AWS4"+key)))))
95 // Canonical query string for S3 V4 signature: sorted keys, spaces
96 // escaped as %20 instead of +, keyvalues joined with &.
97 func s3querystring(u *url.URL) string {
98 keys := make([]string, 0, len(u.Query()))
99 values := make(map[string]string, len(u.Query()))
100 for k, vs := range u.Query() {
101 k = strings.Replace(url.QueryEscape(k), "+", "%20", -1)
102 keys = append(keys, k)
103 for _, v := range vs {
104 v = strings.Replace(url.QueryEscape(v), "+", "%20", -1)
108 values[k] += k + "=" + v
112 for i, k := range keys {
115 return strings.Join(keys, "&")
118 var reMultipleSlashChars = regexp.MustCompile(`//+`)
120 func s3stringToSign(alg, scope, signedHeaders string, r *http.Request) (string, error) {
121 timefmt, timestr := "20060102T150405Z", r.Header.Get("X-Amz-Date")
123 timefmt, timestr = time.RFC1123, r.Header.Get("Date")
125 t, err := time.Parse(timefmt, timestr)
127 return "", fmt.Errorf("invalid timestamp %q: %s", timestr, err)
129 if skew := time.Now().Sub(t); skew < -s3MaxClockSkew || skew > s3MaxClockSkew {
130 return "", errors.New("exceeded max clock skew")
133 var canonicalHeaders string
134 for _, h := range strings.Split(signedHeaders, ";") {
136 canonicalHeaders += h + ":" + r.Host + "\n"
138 canonicalHeaders += h + ":" + r.Header.Get(h) + "\n"
142 normalizedPath := normalizePath(r.URL.Path)
143 ctxlog.FromContext(r.Context()).Debugf("normalizedPath %q", normalizedPath)
144 canonicalRequest := fmt.Sprintf("%s\n%s\n%s\n%s\n%s\n%s", r.Method, normalizedPath, s3querystring(r.URL), canonicalHeaders, signedHeaders, r.Header.Get("X-Amz-Content-Sha256"))
145 ctxlog.FromContext(r.Context()).Debugf("s3stringToSign: canonicalRequest %s", canonicalRequest)
146 return fmt.Sprintf("%s\n%s\n%s\n%s", alg, r.Header.Get("X-Amz-Date"), scope, hashdigest(sha256.New(), canonicalRequest)), nil
149 func normalizePath(s string) string {
150 // (url.URL).EscapedPath() would be incorrect here. AWS
151 // documentation specifies the URL path should be normalized
152 // according to RFC 3986, i.e., unescaping ALPHA / DIGIT / "-"
153 // / "." / "_" / "~". The implication is that everything other
154 // than those chars (and "/") _must_ be percent-encoded --
155 // even chars like ";" and "," that are not normally
156 // percent-encoded in paths.
158 for _, c := range []byte(reMultipleSlashChars.ReplaceAllString(s, "/")) {
159 if (c >= 'a' && c <= 'z') ||
160 (c >= 'A' && c <= 'Z') ||
161 (c >= '0' && c <= '9') ||
169 out += fmt.Sprintf("%%%02X", c)
175 func s3signature(secretKey, scope, signedHeaders, stringToSign string) (string, error) {
176 // scope is {datestamp}/{region}/{service}/aws4_request
177 drs := strings.Split(scope, "/")
179 return "", fmt.Errorf("invalid scope %q", scope)
181 key := s3signatureKey(secretKey, drs[0], drs[1], drs[2])
182 return hashdigest(hmac.New(sha256.New, key), stringToSign), nil
185 var v2tokenUnderscore = regexp.MustCompile(`^v2_[a-z0-9]{5}-gj3su-[a-z0-9]{15}_`)
187 func unescapeKey(key string) string {
188 if v2tokenUnderscore.MatchString(key) {
189 // Entire Arvados token, with "/" replaced by "_" to
190 // avoid colliding with the Authorization header
192 return strings.Replace(key, "_", "/", -1)
193 } else if s, err := url.PathUnescape(key); err == nil {
200 // checks3signature verifies the given S3 V4 signature and returns the
201 // Arvados token that corresponds to the given accessKey. An error is
202 // returned if accessKey is not a valid token UUID or the signature
204 func (h *handler) checks3signature(r *http.Request) (string, error) {
205 var key, scope, signedHeaders, signature string
206 authstring := strings.TrimPrefix(r.Header.Get("Authorization"), s3SignAlgorithm+" ")
207 for _, cmpt := range strings.Split(authstring, ",") {
208 cmpt = strings.TrimSpace(cmpt)
209 split := strings.SplitN(cmpt, "=", 2)
211 case len(split) != 2:
213 case split[0] == "Credential":
214 keyandscope := strings.SplitN(split[1], "/", 2)
215 if len(keyandscope) == 2 {
216 key, scope = keyandscope[0], keyandscope[1]
218 case split[0] == "SignedHeaders":
219 signedHeaders = split[1]
220 case split[0] == "Signature":
225 client := (&arvados.Client{
226 APIHost: h.Cluster.Services.Controller.ExternalURL.Host,
227 Insecure: h.Cluster.TLS.Insecure,
228 }).WithRequestID(r.Header.Get("X-Request-Id"))
229 var aca arvados.APIClientAuthorization
232 if len(key) == 27 && key[5:12] == "-gj3su-" {
233 // Access key is the UUID of an Arvados token, secret
234 // key is the secret part.
235 ctx := arvados.ContextWithAuthorization(r.Context(), "Bearer "+h.Cluster.SystemRootToken)
236 err = client.RequestAndDecodeContext(ctx, &aca, "GET", "arvados/v1/api_client_authorizations/"+key, nil, nil)
237 secret = aca.APIToken
239 // Access key and secret key are both an entire
240 // Arvados token or OIDC access token.
241 ctx := arvados.ContextWithAuthorization(r.Context(), "Bearer "+unescapeKey(key))
242 err = client.RequestAndDecodeContext(ctx, &aca, "GET", "arvados/v1/api_client_authorizations/current", nil, nil)
246 ctxlog.FromContext(r.Context()).WithError(err).WithField("UUID", key).Info("token lookup failed")
247 return "", errors.New("invalid access key")
249 stringToSign, err := s3stringToSign(s3SignAlgorithm, scope, signedHeaders, r)
253 expect, err := s3signature(secret, scope, signedHeaders, stringToSign)
256 } else if expect != signature {
257 return "", fmt.Errorf("signature does not match (scope %q signedHeaders %q stringToSign %q)", scope, signedHeaders, stringToSign)
259 return aca.TokenV2(), nil
262 func s3ErrorResponse(w http.ResponseWriter, s3code string, message string, resource string, code int) {
263 w.Header().Set("Content-Type", "application/xml")
264 w.Header().Set("X-Content-Type-Options", "nosniff")
266 var errstruct struct {
272 errstruct.Code = s3code
273 errstruct.Message = message
274 errstruct.Resource = resource
275 errstruct.RequestId = ""
276 enc := xml.NewEncoder(w)
277 fmt.Fprint(w, xml.Header)
278 enc.EncodeElement(errstruct, xml.StartElement{Name: xml.Name{Local: "Error"}})
281 var NoSuchKey = "NoSuchKey"
282 var NoSuchBucket = "NoSuchBucket"
283 var InvalidArgument = "InvalidArgument"
284 var InternalError = "InternalError"
285 var UnauthorizedAccess = "UnauthorizedAccess"
286 var InvalidRequest = "InvalidRequest"
287 var SignatureDoesNotMatch = "SignatureDoesNotMatch"
289 var reRawQueryIndicatesAPI = regexp.MustCompile(`^[a-z]+(&|$)`)
291 // serveS3 handles r and returns true if r is a request from an S3
292 // client, otherwise it returns false.
293 func (h *handler) serveS3(w http.ResponseWriter, r *http.Request) bool {
295 if auth := r.Header.Get("Authorization"); strings.HasPrefix(auth, "AWS ") {
296 split := strings.SplitN(auth[4:], ":", 2)
298 s3ErrorResponse(w, InvalidRequest, "malformed Authorization header", r.URL.Path, http.StatusUnauthorized)
301 token = unescapeKey(split[0])
302 } else if strings.HasPrefix(auth, s3SignAlgorithm+" ") {
303 t, err := h.checks3signature(r)
305 s3ErrorResponse(w, SignatureDoesNotMatch, "signature verification failed: "+err.Error(), r.URL.Path, http.StatusForbidden)
313 fs, sess, tokenUser, err := h.Cache.GetSession(token)
315 s3ErrorResponse(w, InternalError, err.Error(), r.URL.Path, http.StatusInternalServerError)
319 if writeMethod[r.Method] {
320 // Create a FileSystem for this request, to avoid
321 // exposing incomplete write operations to concurrent
323 client := sess.client.WithRequestID(r.Header.Get("X-Request-Id"))
324 fs = client.SiteFileSystem(sess.keepclient)
325 fs.ForwardSlashNameSubstitution(h.Cluster.Collections.ForwardSlashNameSubstitution)
328 var objectNameGiven bool
329 var bucketName string
331 if id := arvados.CollectionIDFromDNSName(r.Host); id != "" {
334 objectNameGiven = strings.Count(strings.TrimSuffix(r.URL.Path, "/"), "/") > 0
336 bucketName = strings.SplitN(strings.TrimPrefix(r.URL.Path, "/"), "/", 2)[0]
337 objectNameGiven = strings.Count(strings.TrimSuffix(r.URL.Path, "/"), "/") > 1
339 fspath += reMultipleSlashChars.ReplaceAllString(r.URL.Path, "/")
342 case r.Method == http.MethodGet && !objectNameGiven:
343 // Path is "/{uuid}" or "/{uuid}/", has no object name
344 if _, ok := r.URL.Query()["versioning"]; ok {
345 // GetBucketVersioning
346 w.Header().Set("Content-Type", "application/xml")
347 io.WriteString(w, xml.Header)
348 fmt.Fprintln(w, `<VersioningConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/"/>`)
349 } else if _, ok = r.URL.Query()["location"]; ok {
351 w.Header().Set("Content-Type", "application/xml")
352 io.WriteString(w, xml.Header)
353 fmt.Fprintln(w, `<LocationConstraint><LocationConstraint xmlns="http://s3.amazonaws.com/doc/2006-03-01/">`+
355 `</LocationConstraint></LocationConstraint>`)
356 } else if reRawQueryIndicatesAPI.MatchString(r.URL.RawQuery) {
357 // GetBucketWebsite ("GET /bucketid/?website"), GetBucketTagging, etc.
358 s3ErrorResponse(w, InvalidRequest, "API not supported", r.URL.Path+"?"+r.URL.RawQuery, http.StatusBadRequest)
361 h.s3list(bucketName, w, r, fs)
364 case r.Method == http.MethodGet || r.Method == http.MethodHead:
365 if reRawQueryIndicatesAPI.MatchString(r.URL.RawQuery) {
366 // GetObjectRetention ("GET /bucketid/objectid?retention&versionID=..."), etc.
367 s3ErrorResponse(w, InvalidRequest, "API not supported", r.URL.Path+"?"+r.URL.RawQuery, http.StatusBadRequest)
370 fi, err := fs.Stat(fspath)
371 if r.Method == "HEAD" && !objectNameGiven {
373 if err == nil && fi.IsDir() {
374 err = setFileInfoHeaders(w.Header(), fs, fspath)
376 s3ErrorResponse(w, InternalError, err.Error(), r.URL.Path, http.StatusBadGateway)
379 w.WriteHeader(http.StatusOK)
380 } else if os.IsNotExist(err) {
381 s3ErrorResponse(w, NoSuchBucket, "The specified bucket does not exist.", r.URL.Path, http.StatusNotFound)
383 s3ErrorResponse(w, InternalError, err.Error(), r.URL.Path, http.StatusBadGateway)
387 if err == nil && fi.IsDir() && objectNameGiven && strings.HasSuffix(fspath, "/") && h.Cluster.Collections.S3FolderObjects {
388 err = setFileInfoHeaders(w.Header(), fs, fspath)
390 s3ErrorResponse(w, InternalError, err.Error(), r.URL.Path, http.StatusBadGateway)
393 w.Header().Set("Content-Type", "application/x-directory")
394 w.WriteHeader(http.StatusOK)
397 if os.IsNotExist(err) ||
398 (err != nil && err.Error() == "not a directory") ||
399 (fi != nil && fi.IsDir()) {
400 s3ErrorResponse(w, NoSuchKey, "The specified key does not exist.", r.URL.Path, http.StatusNotFound)
404 if !h.userPermittedToUploadOrDownload(r.Method, tokenUser) {
405 http.Error(w, "Not permitted", http.StatusForbidden)
408 h.logUploadOrDownload(r, sess.arvadosclient, fs, fspath, nil, tokenUser)
410 // shallow copy r, and change URL path
413 err = setFileInfoHeaders(w.Header(), fs, fspath)
415 s3ErrorResponse(w, InternalError, err.Error(), r.URL.Path, http.StatusBadGateway)
418 http.FileServer(fs).ServeHTTP(w, &r)
420 case r.Method == http.MethodPut:
421 if reRawQueryIndicatesAPI.MatchString(r.URL.RawQuery) {
422 // PutObjectAcl ("PUT /bucketid/objectid?acl&versionID=..."), etc.
423 s3ErrorResponse(w, InvalidRequest, "API not supported", r.URL.Path+"?"+r.URL.RawQuery, http.StatusBadRequest)
426 if !objectNameGiven {
427 s3ErrorResponse(w, InvalidArgument, "Missing object name in PUT request.", r.URL.Path, http.StatusBadRequest)
431 if strings.HasSuffix(fspath, "/") {
432 if !h.Cluster.Collections.S3FolderObjects {
433 s3ErrorResponse(w, InvalidArgument, "invalid object name: trailing slash", r.URL.Path, http.StatusBadRequest)
436 n, err := r.Body.Read(make([]byte, 1))
437 if err != nil && err != io.EOF {
438 s3ErrorResponse(w, InternalError, fmt.Sprintf("error reading request body: %s", err), r.URL.Path, http.StatusInternalServerError)
441 s3ErrorResponse(w, InvalidArgument, "cannot create object with trailing '/' char unless content is empty", r.URL.Path, http.StatusBadRequest)
443 } else if strings.SplitN(r.Header.Get("Content-Type"), ";", 2)[0] != "application/x-directory" {
444 s3ErrorResponse(w, InvalidArgument, "cannot create object with trailing '/' char unless Content-Type is 'application/x-directory'", r.URL.Path, http.StatusBadRequest)
447 // Given PUT "foo/bar/", we'll use "foo/bar/."
448 // in the "ensure parents exist" block below,
449 // and then we'll be done.
453 fi, err := fs.Stat(fspath)
454 if err != nil && err.Error() == "not a directory" {
455 // requested foo/bar, but foo is a file
456 s3ErrorResponse(w, InvalidArgument, "object name conflicts with existing object", r.URL.Path, http.StatusBadRequest)
459 if strings.HasSuffix(r.URL.Path, "/") && err == nil && !fi.IsDir() {
460 // requested foo/bar/, but foo/bar is a file
461 s3ErrorResponse(w, InvalidArgument, "object name conflicts with existing object", r.URL.Path, http.StatusBadRequest)
464 // create missing parent/intermediate directories, if any
465 for i, c := range fspath {
466 if i > 0 && c == '/' {
468 if strings.HasSuffix(dir, "/") {
469 err = errors.New("invalid object name (consecutive '/' chars)")
470 s3ErrorResponse(w, InvalidArgument, err.Error(), r.URL.Path, http.StatusBadRequest)
473 err = fs.Mkdir(dir, 0755)
474 if errors.Is(err, arvados.ErrInvalidArgument) || errors.Is(err, arvados.ErrInvalidOperation) {
475 // Cannot create a directory
477 err = fmt.Errorf("mkdir %q failed: %w", dir, err)
478 s3ErrorResponse(w, InvalidArgument, err.Error(), r.URL.Path, http.StatusBadRequest)
480 } else if err != nil && !os.IsExist(err) {
481 err = fmt.Errorf("mkdir %q failed: %w", dir, err)
482 s3ErrorResponse(w, InternalError, err.Error(), r.URL.Path, http.StatusInternalServerError)
488 f, err := fs.OpenFile(fspath, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644)
489 if os.IsNotExist(err) {
490 f, err = fs.OpenFile(fspath, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644)
493 err = fmt.Errorf("open %q failed: %w", r.URL.Path, err)
494 s3ErrorResponse(w, InvalidArgument, err.Error(), r.URL.Path, http.StatusBadRequest)
499 if !h.userPermittedToUploadOrDownload(r.Method, tokenUser) {
500 http.Error(w, "Not permitted", http.StatusForbidden)
503 h.logUploadOrDownload(r, sess.arvadosclient, fs, fspath, nil, tokenUser)
505 _, err = io.Copy(f, r.Body)
507 err = fmt.Errorf("write to %q failed: %w", r.URL.Path, err)
508 s3ErrorResponse(w, InternalError, err.Error(), r.URL.Path, http.StatusBadGateway)
513 err = fmt.Errorf("write to %q failed: close: %w", r.URL.Path, err)
514 s3ErrorResponse(w, InternalError, err.Error(), r.URL.Path, http.StatusBadGateway)
518 err = h.syncCollection(fs, readfs, fspath)
520 err = fmt.Errorf("sync failed: %w", err)
521 s3ErrorResponse(w, InternalError, err.Error(), r.URL.Path, http.StatusInternalServerError)
524 w.WriteHeader(http.StatusOK)
526 case r.Method == http.MethodDelete:
527 if reRawQueryIndicatesAPI.MatchString(r.URL.RawQuery) {
528 // DeleteObjectTagging ("DELETE /bucketid/objectid?tagging&versionID=..."), etc.
529 s3ErrorResponse(w, InvalidRequest, "API not supported", r.URL.Path+"?"+r.URL.RawQuery, http.StatusBadRequest)
532 if !objectNameGiven || r.URL.Path == "/" {
533 s3ErrorResponse(w, InvalidArgument, "missing object name in DELETE request", r.URL.Path, http.StatusBadRequest)
536 if strings.HasSuffix(fspath, "/") {
537 fspath = strings.TrimSuffix(fspath, "/")
538 fi, err := fs.Stat(fspath)
539 if os.IsNotExist(err) {
540 w.WriteHeader(http.StatusNoContent)
542 } else if err != nil {
543 s3ErrorResponse(w, InternalError, err.Error(), r.URL.Path, http.StatusInternalServerError)
545 } else if !fi.IsDir() {
546 // if "foo" exists and is a file, then
547 // "foo/" doesn't exist, so we say
548 // delete was successful.
549 w.WriteHeader(http.StatusNoContent)
552 } else if fi, err := fs.Stat(fspath); err == nil && fi.IsDir() {
553 // if "foo" is a dir, it is visible via S3
554 // only as "foo/", not "foo" -- so we leave
555 // the dir alone and return 204 to indicate
556 // that "foo" does not exist.
557 w.WriteHeader(http.StatusNoContent)
560 err = fs.Remove(fspath)
561 if os.IsNotExist(err) {
562 w.WriteHeader(http.StatusNoContent)
566 err = fmt.Errorf("rm failed: %w", err)
567 s3ErrorResponse(w, InvalidArgument, err.Error(), r.URL.Path, http.StatusBadRequest)
570 err = h.syncCollection(fs, readfs, fspath)
572 err = fmt.Errorf("sync failed: %w", err)
573 s3ErrorResponse(w, InternalError, err.Error(), r.URL.Path, http.StatusInternalServerError)
576 w.WriteHeader(http.StatusNoContent)
579 s3ErrorResponse(w, InvalidRequest, "method not allowed", r.URL.Path, http.StatusMethodNotAllowed)
584 // Save modifications to the indicated collection in srcfs, then (if
585 // successful) ensure they are also reflected in dstfs.
586 func (h *handler) syncCollection(srcfs, dstfs arvados.CustomFileSystem, path string) error {
587 coll, _ := h.determineCollection(srcfs, path)
588 if coll == nil || coll.UUID == "" {
589 return errors.New("could not determine collection to sync")
591 d, err := srcfs.OpenFile("by_id/"+coll.UUID, os.O_RDWR, 0777)
600 snap, err := d.Snapshot()
604 dstd, err := dstfs.OpenFile("by_id/"+coll.UUID, os.O_RDWR, 0777)
609 return dstd.Splice(snap)
612 func setFileInfoHeaders(header http.Header, fs arvados.CustomFileSystem, path string) error {
613 maybeEncode := func(s string) string {
614 for _, c := range s {
615 if c > '\u007f' || c < ' ' {
616 return mime.BEncoding.Encode("UTF-8", s)
621 path = strings.TrimSuffix(path, "/")
622 var props map[string]interface{}
624 fi, err := fs.Stat(path)
628 switch src := fi.Sys().(type) {
629 case *arvados.Collection:
630 props = src.Properties
632 props = src.Properties
634 if err, ok := src.(error); ok {
638 cut := strings.LastIndexByte(path, '/')
647 for k, v := range props {
648 if !validMIMEHeaderKey(k) {
651 k = "x-amz-meta-" + k
652 if s, ok := v.(string); ok {
653 header.Set(k, maybeEncode(s))
654 } else if j, err := json.Marshal(v); err == nil {
655 header.Set(k, maybeEncode(string(j)))
661 func validMIMEHeaderKey(k string) bool {
663 return check != textproto.CanonicalMIMEHeaderKey(check)
666 // Call fn on the given path (directory) and its contents, in
667 // lexicographic order.
669 // If isRoot==true and path is not a directory, return nil.
671 // If fn returns filepath.SkipDir when called on a directory, don't
672 // descend into that directory.
673 func walkFS(fs arvados.CustomFileSystem, path string, isRoot bool, fn func(path string, fi os.FileInfo) error) error {
675 fi, err := fs.Stat(path)
676 if os.IsNotExist(err) || (err == nil && !fi.IsDir()) {
678 } else if err != nil {
682 if err == filepath.SkipDir {
684 } else if err != nil {
688 f, err := fs.Open(path)
689 if os.IsNotExist(err) && isRoot {
691 } else if err != nil {
692 return fmt.Errorf("open %q: %w", path, err)
698 fis, err := f.Readdir(-1)
702 sort.Slice(fis, func(i, j int) bool { return fis[i].Name() < fis[j].Name() })
703 for _, fi := range fis {
704 err = fn(path+"/"+fi.Name(), fi)
705 if err == filepath.SkipDir {
707 } else if err != nil {
711 err = walkFS(fs, path+"/"+fi.Name(), false, fn)
720 var errDone = errors.New("done")
722 func (h *handler) s3list(bucket string, w http.ResponseWriter, r *http.Request, fs arvados.CustomFileSystem) {
728 marker string // decoded continuationToken (v2) or provided by client (v1)
729 startAfter string // v2
730 continuationToken string // v2
731 encodingTypeURL bool // v2
733 params.delimiter = r.FormValue("delimiter")
734 if mk, _ := strconv.ParseInt(r.FormValue("max-keys"), 10, 64); mk > 0 && mk < s3MaxKeys {
735 params.maxKeys = int(mk)
737 params.maxKeys = s3MaxKeys
739 params.prefix = r.FormValue("prefix")
740 switch r.FormValue("list-type") {
745 http.Error(w, "invalid list-type parameter", http.StatusBadRequest)
749 params.continuationToken = r.FormValue("continuation-token")
750 marker, err := base64.StdEncoding.DecodeString(params.continuationToken)
752 http.Error(w, "invalid continuation token", http.StatusBadRequest)
755 params.marker = string(marker)
756 params.startAfter = r.FormValue("start-after")
757 switch r.FormValue("encoding-type") {
760 params.encodingTypeURL = true
762 http.Error(w, "invalid encoding-type parameter", http.StatusBadRequest)
766 params.marker = r.FormValue("marker")
769 bucketdir := "by_id/" + bucket
770 // walkpath is the directory (relative to bucketdir) we need
771 // to walk: the innermost directory that is guaranteed to
772 // contain all paths that have the requested prefix. Examples:
773 // prefix "foo/bar" => walkpath "foo"
774 // prefix "foo/bar/" => walkpath "foo/bar"
775 // prefix "foo" => walkpath ""
776 // prefix "" => walkpath ""
777 walkpath := params.prefix
778 if cut := strings.LastIndex(walkpath, "/"); cut >= 0 {
779 walkpath = walkpath[:cut]
786 Prefix: params.prefix,
787 Delimiter: params.delimiter,
788 MaxKeys: params.maxKeys,
789 ContinuationToken: r.FormValue("continuation-token"),
790 StartAfter: params.startAfter,
794 commonPrefixes := map[string]bool{}
795 err := walkFS(fs, strings.TrimSuffix(bucketdir+"/"+walkpath, "/"), true, func(path string, fi os.FileInfo) error {
796 if path == bucketdir {
799 path = path[len(bucketdir)+1:]
800 filesize := fi.Size()
805 if len(path) <= len(params.prefix) {
806 if path > params.prefix[:len(path)] {
807 // with prefix "foobar", walking "fooz" means we're done
810 if path < params.prefix[:len(path)] {
811 // with prefix "foobar", walking "foobag" is pointless
812 return filepath.SkipDir
814 if fi.IsDir() && !strings.HasPrefix(params.prefix+"/", path) {
815 // with prefix "foo/bar", walking "fo"
816 // is pointless (but walking "foo" or
817 // "foo/bar" is necessary)
818 return filepath.SkipDir
820 if len(path) < len(params.prefix) {
821 // can't skip anything, and this entry
822 // isn't in the results, so just
827 if path[:len(params.prefix)] > params.prefix {
828 // with prefix "foobar", nothing we
829 // see after "foozzz" is relevant
833 if path < params.marker || path < params.prefix || path <= params.startAfter {
836 if fi.IsDir() && !h.Cluster.Collections.S3FolderObjects {
837 // Note we don't add anything to
838 // commonPrefixes here even if delimiter is
839 // "/". We descend into the directory, and
840 // return a commonPrefix only if we end up
841 // finding a regular file inside it.
844 if len(resp.Contents)+len(commonPrefixes) >= params.maxKeys {
845 resp.IsTruncated = true
846 if params.delimiter != "" || params.v2 {
851 if params.delimiter != "" {
852 idx := strings.Index(path[len(params.prefix):], params.delimiter)
854 // with prefix "foobar" and delimiter
855 // "z", when we hit "foobar/baz", we
856 // add "/baz" to commonPrefixes and
858 commonPrefixes[path[:len(params.prefix)+idx+1]] = true
859 return filepath.SkipDir
862 resp.Contents = append(resp.Contents, s3.Key{
864 LastModified: fi.ModTime().UTC().Format("2006-01-02T15:04:05.999") + "Z",
869 if err != nil && err != errDone {
870 http.Error(w, err.Error(), http.StatusInternalServerError)
873 if params.delimiter != "" {
874 resp.CommonPrefixes = make([]commonPrefix, 0, len(commonPrefixes))
875 for prefix := range commonPrefixes {
876 resp.CommonPrefixes = append(resp.CommonPrefixes, commonPrefix{prefix})
878 sort.Slice(resp.CommonPrefixes, func(i, j int) bool { return resp.CommonPrefixes[i].Prefix < resp.CommonPrefixes[j].Prefix })
880 resp.KeyCount = len(resp.Contents)
881 var respV1orV2 interface{}
883 if params.encodingTypeURL {
884 // https://docs.aws.amazon.com/AmazonS3/latest/API/API_ListObjectsV2.html
885 // "If you specify the encoding-type request
886 // parameter, Amazon S3 includes this element in the
887 // response, and returns encoded key name values in
888 // the following response elements:
890 // Delimiter, Prefix, Key, and StartAfter.
894 // Valid Values: url"
896 // This is somewhat vague but in practice it appears
897 // to mean x-www-form-urlencoded as in RFC1866 8.2.1
898 // para 1 (encode space as "+") rather than straight
899 // percent-encoding as in RFC1738 2.2. Presumably,
900 // the intent is to allow the client to decode XML and
901 // then paste the strings directly into another URI
902 // query or POST form like "https://host/path?foo=" +
903 // foo + "&bar=" + bar.
904 resp.EncodingType = "url"
905 resp.Delimiter = url.QueryEscape(resp.Delimiter)
906 resp.Prefix = url.QueryEscape(resp.Prefix)
907 resp.StartAfter = url.QueryEscape(resp.StartAfter)
908 for i, ent := range resp.Contents {
909 ent.Key = url.QueryEscape(ent.Key)
910 resp.Contents[i] = ent
912 for i, ent := range resp.CommonPrefixes {
913 ent.Prefix = url.QueryEscape(ent.Prefix)
914 resp.CommonPrefixes[i] = ent
919 resp.NextContinuationToken = base64.StdEncoding.EncodeToString([]byte(nextMarker))
922 respV1orV2 = listV1Resp{
923 CommonPrefixes: resp.CommonPrefixes,
924 NextMarker: nextMarker,
925 KeyCount: resp.KeyCount,
926 ListResp: s3.ListResp{
927 IsTruncated: resp.IsTruncated,
929 Prefix: params.prefix,
930 Delimiter: params.delimiter,
931 Marker: params.marker,
932 MaxKeys: params.maxKeys,
933 Contents: resp.Contents,
938 w.Header().Set("Content-Type", "application/xml")
939 io.WriteString(w, xml.Header)
940 if err := xml.NewEncoder(w).Encode(respV1orV2); err != nil {
941 ctxlog.FromContext(r.Context()).WithError(err).Error("error writing xml response")