s3api_object_handlers.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. package s3api
  2. import (
  3. "bytes"
  4. "crypto/md5"
  5. "encoding/json"
  6. "encoding/xml"
  7. "fmt"
  8. "io"
  9. "net/http"
  10. "net/url"
  11. "strings"
  12. "time"
  13. "github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
  14. "github.com/seaweedfs/seaweedfs/weed/security"
  15. "github.com/seaweedfs/seaweedfs/weed/util/mem"
  16. "golang.org/x/exp/slices"
  17. "github.com/pquerna/cachecontrol/cacheobject"
  18. "github.com/seaweedfs/seaweedfs/weed/filer"
  19. "github.com/seaweedfs/seaweedfs/weed/s3api/s3err"
  20. "github.com/seaweedfs/seaweedfs/weed/glog"
  21. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  22. weed_server "github.com/seaweedfs/seaweedfs/weed/server"
  23. "github.com/seaweedfs/seaweedfs/weed/util"
  24. )
  25. const (
  26. deleteMultipleObjectsLimit = 1000
  27. )
  28. func mimeDetect(r *http.Request, dataReader io.Reader) io.ReadCloser {
  29. mimeBuffer := make([]byte, 512)
  30. size, _ := dataReader.Read(mimeBuffer)
  31. if size > 0 {
  32. r.Header.Set("Content-Type", http.DetectContentType(mimeBuffer[:size]))
  33. return io.NopCloser(io.MultiReader(bytes.NewReader(mimeBuffer[:size]), dataReader))
  34. }
  35. return io.NopCloser(dataReader)
  36. }
  37. func (s3a *S3ApiServer) PutObjectHandler(w http.ResponseWriter, r *http.Request) {
  38. // http://docs.aws.amazon.com/AmazonS3/latest/dev/UploadingObjects.html
  39. bucket, object := s3_constants.GetBucketAndObject(r)
  40. glog.V(3).Infof("PutObjectHandler %s %s", bucket, object)
  41. _, err := validateContentMd5(r.Header)
  42. if err != nil {
  43. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidDigest)
  44. return
  45. }
  46. if r.Header.Get("Cache-Control") != "" {
  47. if _, err = cacheobject.ParseRequestCacheControl(r.Header.Get("Cache-Control")); err != nil {
  48. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidDigest)
  49. return
  50. }
  51. }
  52. if r.Header.Get("Expires") != "" {
  53. if _, err = time.Parse(http.TimeFormat, r.Header.Get("Expires")); err != nil {
  54. s3err.WriteErrorResponse(w, r, s3err.ErrMalformedDate)
  55. return
  56. }
  57. }
  58. dataReader := r.Body
  59. rAuthType := getRequestAuthType(r)
  60. if s3a.iam.isEnabled() {
  61. var s3ErrCode s3err.ErrorCode
  62. switch rAuthType {
  63. case authTypeStreamingSigned:
  64. dataReader, s3ErrCode = s3a.iam.newSignV4ChunkedReader(r)
  65. case authTypeSignedV2, authTypePresignedV2:
  66. _, s3ErrCode = s3a.iam.isReqAuthenticatedV2(r)
  67. case authTypePresigned, authTypeSigned:
  68. _, s3ErrCode = s3a.iam.reqSignatureV4Verify(r)
  69. }
  70. if s3ErrCode != s3err.ErrNone {
  71. s3err.WriteErrorResponse(w, r, s3ErrCode)
  72. return
  73. }
  74. } else {
  75. if authTypeStreamingSigned == rAuthType {
  76. s3err.WriteErrorResponse(w, r, s3err.ErrAuthNotSetup)
  77. return
  78. }
  79. }
  80. defer dataReader.Close()
  81. objectContentType := r.Header.Get("Content-Type")
  82. if strings.HasSuffix(object, "/") && r.ContentLength == 0 {
  83. if err := s3a.mkdir(
  84. s3a.option.BucketsPath, bucket+strings.TrimSuffix(object, "/"),
  85. func(entry *filer_pb.Entry) {
  86. if objectContentType == "" {
  87. objectContentType = "httpd/unix-directory"
  88. }
  89. entry.Attributes.Mime = objectContentType
  90. }); err != nil {
  91. s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
  92. return
  93. }
  94. } else {
  95. uploadUrl := s3a.toFilerUrl(bucket, object)
  96. if objectContentType == "" {
  97. dataReader = mimeDetect(r, dataReader)
  98. }
  99. etag, errCode := s3a.putToFiler(r, uploadUrl, dataReader, "")
  100. if errCode != s3err.ErrNone {
  101. s3err.WriteErrorResponse(w, r, errCode)
  102. return
  103. }
  104. setEtag(w, etag)
  105. }
  106. writeSuccessResponseEmpty(w, r)
  107. }
  108. func urlEscapeObject(object string) string {
  109. t := urlPathEscape(removeDuplicateSlashes(object))
  110. if strings.HasPrefix(t, "/") {
  111. return t
  112. }
  113. return "/" + t
  114. }
  115. func urlPathEscape(object string) string {
  116. var escapedParts []string
  117. for _, part := range strings.Split(object, "/") {
  118. escapedParts = append(escapedParts, url.PathEscape(part))
  119. }
  120. return strings.Join(escapedParts, "/")
  121. }
  122. func removeDuplicateSlashes(object string) string {
  123. result := strings.Builder{}
  124. result.Grow(len(object))
  125. isLastSlash := false
  126. for _, r := range object {
  127. switch r {
  128. case '/':
  129. if !isLastSlash {
  130. result.WriteRune(r)
  131. }
  132. isLastSlash = true
  133. default:
  134. result.WriteRune(r)
  135. isLastSlash = false
  136. }
  137. }
  138. return result.String()
  139. }
  140. func (s3a *S3ApiServer) toFilerUrl(bucket, object string) string {
  141. object = urlPathEscape(removeDuplicateSlashes(object))
  142. destUrl := fmt.Sprintf("http://%s%s/%s%s",
  143. s3a.option.Filer.ToHttpAddress(), s3a.option.BucketsPath, bucket, object)
  144. return destUrl
  145. }
  146. func (s3a *S3ApiServer) GetObjectHandler(w http.ResponseWriter, r *http.Request) {
  147. bucket, object := s3_constants.GetBucketAndObject(r)
  148. glog.V(3).Infof("GetObjectHandler %s %s", bucket, object)
  149. if strings.HasSuffix(r.URL.Path, "/") {
  150. s3err.WriteErrorResponse(w, r, s3err.ErrNotImplemented)
  151. return
  152. }
  153. destUrl := s3a.toFilerUrl(bucket, object)
  154. s3a.proxyToFiler(w, r, destUrl, false, passThroughResponse)
  155. }
  156. func (s3a *S3ApiServer) HeadObjectHandler(w http.ResponseWriter, r *http.Request) {
  157. bucket, object := s3_constants.GetBucketAndObject(r)
  158. glog.V(3).Infof("HeadObjectHandler %s %s", bucket, object)
  159. destUrl := s3a.toFilerUrl(bucket, object)
  160. s3a.proxyToFiler(w, r, destUrl, false, passThroughResponse)
  161. }
  162. func (s3a *S3ApiServer) DeleteObjectHandler(w http.ResponseWriter, r *http.Request) {
  163. bucket, object := s3_constants.GetBucketAndObject(r)
  164. glog.V(3).Infof("DeleteObjectHandler %s %s", bucket, object)
  165. destUrl := s3a.toFilerUrl(bucket, object)
  166. s3a.proxyToFiler(w, r, destUrl, true, func(proxyResponse *http.Response, w http.ResponseWriter) (statusCode int) {
  167. statusCode = http.StatusNoContent
  168. for k, v := range proxyResponse.Header {
  169. w.Header()[k] = v
  170. }
  171. w.WriteHeader(statusCode)
  172. return statusCode
  173. })
  174. }
  175. // / ObjectIdentifier carries key name for the object to delete.
  176. type ObjectIdentifier struct {
  177. ObjectName string `xml:"Key"`
  178. }
  179. // DeleteObjectsRequest - xml carrying the object key names which needs to be deleted.
  180. type DeleteObjectsRequest struct {
  181. // Element to enable quiet mode for the request
  182. Quiet bool
  183. // List of objects to be deleted
  184. Objects []ObjectIdentifier `xml:"Object"`
  185. }
  186. // DeleteError structure.
  187. type DeleteError struct {
  188. Code string
  189. Message string
  190. Key string
  191. }
  192. // DeleteObjectsResponse container for multiple object deletes.
  193. type DeleteObjectsResponse struct {
  194. XMLName xml.Name `xml:"http://s3.amazonaws.com/doc/2006-03-01/ DeleteResult" json:"-"`
  195. // Collection of all deleted objects
  196. DeletedObjects []ObjectIdentifier `xml:"Deleted,omitempty"`
  197. // Collection of errors deleting certain objects.
  198. Errors []DeleteError `xml:"Error,omitempty"`
  199. }
  200. // DeleteMultipleObjectsHandler - Delete multiple objects
  201. func (s3a *S3ApiServer) DeleteMultipleObjectsHandler(w http.ResponseWriter, r *http.Request) {
  202. bucket, _ := s3_constants.GetBucketAndObject(r)
  203. glog.V(3).Infof("DeleteMultipleObjectsHandler %s", bucket)
  204. deleteXMLBytes, err := io.ReadAll(r.Body)
  205. if err != nil {
  206. s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
  207. return
  208. }
  209. deleteObjects := &DeleteObjectsRequest{}
  210. if err := xml.Unmarshal(deleteXMLBytes, deleteObjects); err != nil {
  211. s3err.WriteErrorResponse(w, r, s3err.ErrMalformedXML)
  212. return
  213. }
  214. if len(deleteObjects.Objects) > deleteMultipleObjectsLimit {
  215. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidMaxDeleteObjects)
  216. return
  217. }
  218. var deletedObjects []ObjectIdentifier
  219. var deleteErrors []DeleteError
  220. var auditLog *s3err.AccessLog
  221. directoriesWithDeletion := make(map[string]int)
  222. if s3err.Logger != nil {
  223. auditLog = s3err.GetAccessLog(r, http.StatusNoContent, s3err.ErrNone)
  224. }
  225. s3a.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  226. // delete file entries
  227. for _, object := range deleteObjects.Objects {
  228. if object.ObjectName == "" {
  229. continue
  230. }
  231. lastSeparator := strings.LastIndex(object.ObjectName, "/")
  232. parentDirectoryPath, entryName, isDeleteData, isRecursive := "", object.ObjectName, true, false
  233. if lastSeparator > 0 && lastSeparator+1 < len(object.ObjectName) {
  234. entryName = object.ObjectName[lastSeparator+1:]
  235. parentDirectoryPath = "/" + object.ObjectName[:lastSeparator]
  236. }
  237. parentDirectoryPath = fmt.Sprintf("%s/%s%s", s3a.option.BucketsPath, bucket, parentDirectoryPath)
  238. err := doDeleteEntry(client, parentDirectoryPath, entryName, isDeleteData, isRecursive)
  239. if err == nil {
  240. directoriesWithDeletion[parentDirectoryPath]++
  241. deletedObjects = append(deletedObjects, object)
  242. } else if strings.Contains(err.Error(), filer.MsgFailDelNonEmptyFolder) {
  243. deletedObjects = append(deletedObjects, object)
  244. } else {
  245. delete(directoriesWithDeletion, parentDirectoryPath)
  246. deleteErrors = append(deleteErrors, DeleteError{
  247. Code: "",
  248. Message: err.Error(),
  249. Key: object.ObjectName,
  250. })
  251. }
  252. if auditLog != nil {
  253. auditLog.Key = entryName
  254. s3err.PostAccessLog(*auditLog)
  255. }
  256. }
  257. // purge empty folders, only checking folders with deletions
  258. for len(directoriesWithDeletion) > 0 {
  259. directoriesWithDeletion = s3a.doDeleteEmptyDirectories(client, directoriesWithDeletion)
  260. }
  261. return nil
  262. })
  263. deleteResp := DeleteObjectsResponse{}
  264. if !deleteObjects.Quiet {
  265. deleteResp.DeletedObjects = deletedObjects
  266. }
  267. deleteResp.Errors = deleteErrors
  268. writeSuccessResponseXML(w, r, deleteResp)
  269. }
  270. func (s3a *S3ApiServer) doDeleteEmptyDirectories(client filer_pb.SeaweedFilerClient, directoriesWithDeletion map[string]int) (newDirectoriesWithDeletion map[string]int) {
  271. var allDirs []string
  272. for dir := range directoriesWithDeletion {
  273. allDirs = append(allDirs, dir)
  274. }
  275. slices.SortFunc(allDirs, func(a, b string) bool {
  276. return len(a) > len(b)
  277. })
  278. newDirectoriesWithDeletion = make(map[string]int)
  279. for _, dir := range allDirs {
  280. parentDir, dirName := util.FullPath(dir).DirAndName()
  281. if parentDir == s3a.option.BucketsPath {
  282. continue
  283. }
  284. if err := doDeleteEntry(client, parentDir, dirName, false, false); err != nil {
  285. glog.V(4).Infof("directory %s has %d deletion but still not empty: %v", dir, directoriesWithDeletion[dir], err)
  286. } else {
  287. newDirectoriesWithDeletion[parentDir]++
  288. }
  289. }
  290. return
  291. }
  292. func (s3a *S3ApiServer) proxyToFiler(w http.ResponseWriter, r *http.Request, destUrl string, isWrite bool, responseFn func(proxyResponse *http.Response, w http.ResponseWriter) (statusCode int)) {
  293. glog.V(3).Infof("s3 proxying %s to %s", r.Method, destUrl)
  294. proxyReq, err := http.NewRequest(r.Method, destUrl, r.Body)
  295. if err != nil {
  296. glog.Errorf("NewRequest %s: %v", destUrl, err)
  297. s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
  298. return
  299. }
  300. proxyReq.Header.Set("X-Forwarded-For", r.RemoteAddr)
  301. for k, v := range r.URL.Query() {
  302. if _, ok := s3_constants.PassThroughHeaders[strings.ToLower(k)]; ok {
  303. proxyReq.Header[k] = v
  304. }
  305. }
  306. for header, values := range r.Header {
  307. proxyReq.Header[header] = values
  308. }
  309. // ensure that the Authorization header is overriding any previous
  310. // Authorization header which might be already present in proxyReq
  311. s3a.maybeAddFilerJwtAuthorization(proxyReq, isWrite)
  312. resp, postErr := s3a.client.Do(proxyReq)
  313. if postErr != nil {
  314. glog.Errorf("post to filer: %v", postErr)
  315. s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
  316. return
  317. }
  318. defer util.CloseResponse(resp)
  319. if resp.StatusCode == http.StatusPreconditionFailed {
  320. s3err.WriteErrorResponse(w, r, s3err.ErrPreconditionFailed)
  321. return
  322. }
  323. if resp.StatusCode == http.StatusRequestedRangeNotSatisfiable {
  324. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidRange)
  325. return
  326. }
  327. if r.Method == "DELETE" {
  328. if resp.StatusCode == http.StatusNotFound {
  329. // this is normal
  330. responseStatusCode := responseFn(resp, w)
  331. s3err.PostLog(r, responseStatusCode, s3err.ErrNone)
  332. return
  333. }
  334. }
  335. if resp.StatusCode == http.StatusNotFound {
  336. s3err.WriteErrorResponse(w, r, s3err.ErrNoSuchKey)
  337. return
  338. }
  339. if resp.Header.Get(s3_constants.X_SeaweedFS_Header_Directory_Key) == "true" {
  340. responseStatusCode := responseFn(resp, w)
  341. s3err.PostLog(r, responseStatusCode, s3err.ErrNone)
  342. return
  343. }
  344. // when HEAD a directory, it should be reported as no such key
  345. // https://github.com/seaweedfs/seaweedfs/issues/3457
  346. if resp.ContentLength == -1 && resp.StatusCode != http.StatusNotModified {
  347. s3err.WriteErrorResponse(w, r, s3err.ErrNoSuchKey)
  348. return
  349. }
  350. setUserMetadataKeyToLowercase(resp)
  351. responseStatusCode := responseFn(resp, w)
  352. s3err.PostLog(r, responseStatusCode, s3err.ErrNone)
  353. }
  354. func setUserMetadataKeyToLowercase(resp *http.Response) {
  355. for key, value := range resp.Header {
  356. if strings.HasPrefix(key, s3_constants.AmzUserMetaPrefix) {
  357. resp.Header[strings.ToLower(key)] = value
  358. delete(resp.Header, key)
  359. }
  360. }
  361. }
  362. func passThroughResponse(proxyResponse *http.Response, w http.ResponseWriter) (statusCode int) {
  363. for k, v := range proxyResponse.Header {
  364. w.Header()[k] = v
  365. }
  366. if proxyResponse.Header.Get("Content-Range") != "" && proxyResponse.StatusCode == 200 {
  367. w.WriteHeader(http.StatusPartialContent)
  368. statusCode = http.StatusPartialContent
  369. } else {
  370. statusCode = proxyResponse.StatusCode
  371. }
  372. w.WriteHeader(statusCode)
  373. buf := mem.Allocate(128 * 1024)
  374. defer mem.Free(buf)
  375. if n, err := io.CopyBuffer(w, proxyResponse.Body, buf); err != nil {
  376. glog.V(1).Infof("passthrough response read %d bytes: %v", n, err)
  377. }
  378. return statusCode
  379. }
  380. func (s3a *S3ApiServer) putToFiler(r *http.Request, uploadUrl string, dataReader io.Reader, destination string) (etag string, code s3err.ErrorCode) {
  381. hash := md5.New()
  382. var body = io.TeeReader(dataReader, hash)
  383. proxyReq, err := http.NewRequest("PUT", uploadUrl, body)
  384. if err != nil {
  385. glog.Errorf("NewRequest %s: %v", uploadUrl, err)
  386. return "", s3err.ErrInternalError
  387. }
  388. proxyReq.Header.Set("X-Forwarded-For", r.RemoteAddr)
  389. if destination != "" {
  390. proxyReq.Header.Set(s3_constants.SeaweedStorageDestinationHeader, destination)
  391. }
  392. for header, values := range r.Header {
  393. for _, value := range values {
  394. proxyReq.Header.Add(header, value)
  395. }
  396. }
  397. // ensure that the Authorization header is overriding any previous
  398. // Authorization header which might be already present in proxyReq
  399. s3a.maybeAddFilerJwtAuthorization(proxyReq, true)
  400. resp, postErr := s3a.client.Do(proxyReq)
  401. if postErr != nil {
  402. glog.Errorf("post to filer: %v", postErr)
  403. return "", s3err.ErrInternalError
  404. }
  405. defer resp.Body.Close()
  406. etag = fmt.Sprintf("%x", hash.Sum(nil))
  407. resp_body, ra_err := io.ReadAll(resp.Body)
  408. if ra_err != nil {
  409. glog.Errorf("upload to filer response read %d: %v", resp.StatusCode, ra_err)
  410. return etag, s3err.ErrInternalError
  411. }
  412. var ret weed_server.FilerPostResult
  413. unmarshal_err := json.Unmarshal(resp_body, &ret)
  414. if unmarshal_err != nil {
  415. glog.Errorf("failing to read upload to %s : %v", uploadUrl, string(resp_body))
  416. return "", s3err.ErrInternalError
  417. }
  418. if ret.Error != "" {
  419. glog.Errorf("upload to filer error: %v", ret.Error)
  420. return "", filerErrorToS3Error(ret.Error)
  421. }
  422. return etag, s3err.ErrNone
  423. }
  424. func setEtag(w http.ResponseWriter, etag string) {
  425. if etag != "" {
  426. if strings.HasPrefix(etag, "\"") {
  427. w.Header()["ETag"] = []string{etag}
  428. } else {
  429. w.Header()["ETag"] = []string{"\"" + etag + "\""}
  430. }
  431. }
  432. }
  433. func filerErrorToS3Error(errString string) s3err.ErrorCode {
  434. switch {
  435. case strings.HasPrefix(errString, "existing ") && strings.HasSuffix(errString, "is a directory"):
  436. return s3err.ErrExistingObjectIsDirectory
  437. case strings.HasSuffix(errString, "is a file"):
  438. return s3err.ErrExistingObjectIsFile
  439. default:
  440. return s3err.ErrInternalError
  441. }
  442. }
  443. func (s3a *S3ApiServer) maybeAddFilerJwtAuthorization(r *http.Request, isWrite bool) {
  444. encodedJwt := s3a.maybeGetFilerJwtAuthorizationToken(isWrite)
  445. if encodedJwt == "" {
  446. return
  447. }
  448. r.Header.Set("Authorization", "BEARER "+string(encodedJwt))
  449. }
  450. func (s3a *S3ApiServer) maybeGetFilerJwtAuthorizationToken(isWrite bool) string {
  451. var encodedJwt security.EncodedJwt
  452. if isWrite {
  453. encodedJwt = security.GenJwtForFilerServer(s3a.filerGuard.SigningKey, s3a.filerGuard.ExpiresAfterSec)
  454. } else {
  455. encodedJwt = security.GenJwtForFilerServer(s3a.filerGuard.ReadSigningKey, s3a.filerGuard.ReadExpiresAfterSec)
  456. }
  457. return string(encodedJwt)
  458. }