You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

546 lines
16 KiB

7 years ago
7 years ago
3 years ago
6 years ago
3 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
3 years ago
4 years ago
3 years ago
3 years ago
6 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
7 years ago
7 years ago
3 years ago
4 years ago
4 years ago
7 years ago
7 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package s3api
  2. import (
  3. "context"
  4. "encoding/xml"
  5. "errors"
  6. "fmt"
  7. "github.com/aws/aws-sdk-go/private/protocol/xml/xmlutil"
  8. "github.com/seaweedfs/seaweedfs/weed/s3api/s3bucket"
  9. "github.com/seaweedfs/seaweedfs/weed/util"
  10. "math"
  11. "net/http"
  12. "time"
  13. "github.com/seaweedfs/seaweedfs/weed/filer"
  14. "github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
  15. "github.com/seaweedfs/seaweedfs/weed/storage/needle"
  16. "github.com/seaweedfs/seaweedfs/weed/s3api/s3err"
  17. "github.com/aws/aws-sdk-go/aws"
  18. "github.com/aws/aws-sdk-go/service/s3"
  19. "github.com/seaweedfs/seaweedfs/weed/glog"
  20. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  21. )
  22. type ListAllMyBucketsResult struct {
  23. XMLName xml.Name `xml:"http://s3.amazonaws.com/doc/2006-03-01/ ListAllMyBucketsResult"`
  24. Owner *s3.Owner
  25. Buckets []*s3.Bucket `xml:"Buckets>Bucket"`
  26. }
  27. func (s3a *S3ApiServer) ListBucketsHandler(w http.ResponseWriter, r *http.Request) {
  28. glog.V(3).Infof("ListBucketsHandler")
  29. var identity *Identity
  30. var s3Err s3err.ErrorCode
  31. if s3a.iam.isEnabled() {
  32. identity, s3Err = s3a.iam.authUser(r)
  33. if s3Err != s3err.ErrNone {
  34. s3err.WriteErrorResponse(w, r, s3Err)
  35. return
  36. }
  37. }
  38. var response ListAllMyBucketsResult
  39. entries, _, err := s3a.list(s3a.option.BucketsPath, "", "", false, math.MaxInt32)
  40. if err != nil {
  41. s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
  42. return
  43. }
  44. identityId := r.Header.Get(s3_constants.AmzIdentityId)
  45. var buckets []*s3.Bucket
  46. for _, entry := range entries {
  47. if entry.IsDirectory {
  48. if identity != nil && !identity.canDo(s3_constants.ACTION_LIST, entry.Name, "") {
  49. continue
  50. }
  51. buckets = append(buckets, &s3.Bucket{
  52. Name: aws.String(entry.Name),
  53. CreationDate: aws.Time(time.Unix(entry.Attributes.Crtime, 0).UTC()),
  54. })
  55. }
  56. }
  57. response = ListAllMyBucketsResult{
  58. Owner: &s3.Owner{
  59. ID: aws.String(identityId),
  60. DisplayName: aws.String(identityId),
  61. },
  62. Buckets: buckets,
  63. }
  64. writeSuccessResponseXML(w, r, response)
  65. }
  66. func (s3a *S3ApiServer) PutBucketHandler(w http.ResponseWriter, r *http.Request) {
  67. bucket, _ := s3_constants.GetBucketAndObject(r)
  68. glog.V(3).Infof("PutBucketHandler %s", bucket)
  69. // validate the bucket name
  70. err := s3bucket.VerifyS3BucketName(bucket)
  71. if err != nil {
  72. glog.Errorf("put invalid bucket name: %v %v", bucket, err)
  73. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidBucketName)
  74. return
  75. }
  76. // avoid duplicated buckets
  77. errCode := s3err.ErrNone
  78. if err := s3a.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  79. if resp, err := client.CollectionList(context.Background(), &filer_pb.CollectionListRequest{
  80. IncludeEcVolumes: true,
  81. IncludeNormalVolumes: true,
  82. }); err != nil {
  83. glog.Errorf("list collection: %v", err)
  84. return fmt.Errorf("list collections: %v", err)
  85. } else {
  86. for _, c := range resp.Collections {
  87. if s3a.getCollectionName(bucket) == c.Name {
  88. errCode = s3err.ErrBucketAlreadyExists
  89. break
  90. }
  91. }
  92. }
  93. return nil
  94. }); err != nil {
  95. s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
  96. return
  97. }
  98. if exist, err := s3a.exists(s3a.option.BucketsPath, bucket, true); err == nil && exist {
  99. errCode = s3err.ErrBucketAlreadyExists
  100. }
  101. if errCode != s3err.ErrNone {
  102. s3err.WriteErrorResponse(w, r, errCode)
  103. return
  104. }
  105. fn := func(entry *filer_pb.Entry) {
  106. if identityId := r.Header.Get(s3_constants.AmzIdentityId); identityId != "" {
  107. if entry.Extended == nil {
  108. entry.Extended = make(map[string][]byte)
  109. }
  110. entry.Extended[s3_constants.AmzIdentityId] = []byte(identityId)
  111. }
  112. }
  113. // create the folder for bucket, but lazily create actual collection
  114. if err := s3a.mkdir(s3a.option.BucketsPath, bucket, fn); err != nil {
  115. glog.Errorf("PutBucketHandler mkdir: %v", err)
  116. s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
  117. return
  118. }
  119. w.Header().Set("Location", "/"+bucket)
  120. writeSuccessResponseEmpty(w, r)
  121. }
  122. func (s3a *S3ApiServer) DeleteBucketHandler(w http.ResponseWriter, r *http.Request) {
  123. bucket, _ := s3_constants.GetBucketAndObject(r)
  124. glog.V(3).Infof("DeleteBucketHandler %s", bucket)
  125. if err := s3a.checkBucket(r, bucket); err != s3err.ErrNone {
  126. s3err.WriteErrorResponse(w, r, err)
  127. return
  128. }
  129. err := s3a.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  130. if !s3a.option.AllowDeleteBucketNotEmpty {
  131. entries, _, err := s3a.list(s3a.option.BucketsPath+"/"+bucket, "", "", false, 2)
  132. if err != nil {
  133. return fmt.Errorf("failed to list bucket %s: %v", bucket, err)
  134. }
  135. for _, entry := range entries {
  136. if entry.Name != s3_constants.MultipartUploadsFolder {
  137. return errors.New(s3err.GetAPIError(s3err.ErrBucketNotEmpty).Code)
  138. }
  139. }
  140. }
  141. // delete collection
  142. deleteCollectionRequest := &filer_pb.DeleteCollectionRequest{
  143. Collection: s3a.getCollectionName(bucket),
  144. }
  145. glog.V(1).Infof("delete collection: %v", deleteCollectionRequest)
  146. if _, err := client.DeleteCollection(context.Background(), deleteCollectionRequest); err != nil {
  147. return fmt.Errorf("delete collection %s: %v", bucket, err)
  148. }
  149. return nil
  150. })
  151. if err != nil {
  152. s3ErrorCode := s3err.ErrInternalError
  153. if err.Error() == s3err.GetAPIError(s3err.ErrBucketNotEmpty).Code {
  154. s3ErrorCode = s3err.ErrBucketNotEmpty
  155. }
  156. s3err.WriteErrorResponse(w, r, s3ErrorCode)
  157. return
  158. }
  159. err = s3a.rm(s3a.option.BucketsPath, bucket, false, true)
  160. if err != nil {
  161. s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
  162. return
  163. }
  164. s3err.WriteEmptyResponse(w, r, http.StatusNoContent)
  165. }
  166. func (s3a *S3ApiServer) HeadBucketHandler(w http.ResponseWriter, r *http.Request) {
  167. bucket, _ := s3_constants.GetBucketAndObject(r)
  168. glog.V(3).Infof("HeadBucketHandler %s", bucket)
  169. if entry, err := s3a.getEntry(s3a.option.BucketsPath, bucket); entry == nil || err == filer_pb.ErrNotFound {
  170. s3err.WriteErrorResponse(w, r, s3err.ErrNoSuchBucket)
  171. return
  172. }
  173. writeSuccessResponseEmpty(w, r)
  174. }
  175. func (s3a *S3ApiServer) checkBucket(r *http.Request, bucket string) s3err.ErrorCode {
  176. entry, err := s3a.getEntry(s3a.option.BucketsPath, bucket)
  177. if entry == nil || err == filer_pb.ErrNotFound {
  178. return s3err.ErrNoSuchBucket
  179. }
  180. if !s3a.hasAccess(r, entry) {
  181. return s3err.ErrAccessDenied
  182. }
  183. return s3err.ErrNone
  184. }
  185. func (s3a *S3ApiServer) hasAccess(r *http.Request, entry *filer_pb.Entry) bool {
  186. isAdmin := r.Header.Get(s3_constants.AmzIsAdmin) != ""
  187. if isAdmin {
  188. return true
  189. }
  190. if entry.Extended == nil {
  191. return true
  192. }
  193. identityId := r.Header.Get(s3_constants.AmzIdentityId)
  194. if id, ok := entry.Extended[s3_constants.AmzIdentityId]; ok {
  195. if identityId != string(id) {
  196. return false
  197. }
  198. }
  199. return true
  200. }
  201. // GetBucketAclHandler Get Bucket ACL
  202. // https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetBucketAcl.html
  203. func (s3a *S3ApiServer) GetBucketAclHandler(w http.ResponseWriter, r *http.Request) {
  204. // collect parameters
  205. bucket, _ := s3_constants.GetBucketAndObject(r)
  206. glog.V(3).Infof("GetBucketAclHandler %s", bucket)
  207. if err := s3a.checkBucket(r, bucket); err != s3err.ErrNone {
  208. s3err.WriteErrorResponse(w, r, err)
  209. return
  210. }
  211. amzAccountId := r.Header.Get(s3_constants.AmzAccountId)
  212. amzDisplayName := s3a.iam.GetAccountNameById(amzAccountId)
  213. response := AccessControlPolicy{
  214. Owner: CanonicalUser{
  215. ID: amzAccountId,
  216. DisplayName: amzDisplayName,
  217. },
  218. }
  219. response.AccessControlList.Grant = append(response.AccessControlList.Grant, Grant{
  220. Grantee: Grantee{
  221. ID: amzAccountId,
  222. DisplayName: amzDisplayName,
  223. Type: "CanonicalUser",
  224. XMLXSI: "CanonicalUser",
  225. XMLNS: "http://www.w3.org/2001/XMLSchema-instance"},
  226. Permission: s3.PermissionFullControl,
  227. })
  228. writeSuccessResponseXML(w, r, response)
  229. }
  230. // PutBucketAclHandler Put bucket ACL only responds success if the ACL is private.
  231. // https://docs.aws.amazon.com/AmazonS3/latest/API/API_PutBucketAcl.html //
  232. func (s3a *S3ApiServer) PutBucketAclHandler(w http.ResponseWriter, r *http.Request) {
  233. // collect parameters
  234. bucket, _ := s3_constants.GetBucketAndObject(r)
  235. glog.V(3).Infof("PutBucketAclHandler %s", bucket)
  236. if err := s3a.checkBucket(r, bucket); err != s3err.ErrNone {
  237. s3err.WriteErrorResponse(w, r, err)
  238. return
  239. }
  240. cannedAcl := r.Header.Get(s3_constants.AmzCannedAcl)
  241. switch {
  242. case cannedAcl == "":
  243. acl := &s3.AccessControlPolicy{}
  244. if err := xmlDecoder(r.Body, acl, r.ContentLength); err != nil {
  245. glog.Errorf("PutBucketAclHandler: %s", err)
  246. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidRequest)
  247. return
  248. }
  249. if len(acl.Grants) == 1 && acl.Grants[0].Permission != nil && *acl.Grants[0].Permission == s3_constants.PermissionFullControl {
  250. writeSuccessResponseEmpty(w, r)
  251. return
  252. }
  253. case cannedAcl == s3_constants.CannedAclPrivate:
  254. writeSuccessResponseEmpty(w, r)
  255. return
  256. }
  257. s3err.WriteErrorResponse(w, r, s3err.ErrNotImplemented)
  258. }
  259. // GetBucketLifecycleConfigurationHandler Get Bucket Lifecycle configuration
  260. // https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetBucketLifecycleConfiguration.html
  261. func (s3a *S3ApiServer) GetBucketLifecycleConfigurationHandler(w http.ResponseWriter, r *http.Request) {
  262. // collect parameters
  263. bucket, _ := s3_constants.GetBucketAndObject(r)
  264. glog.V(3).Infof("GetBucketLifecycleConfigurationHandler %s", bucket)
  265. if err := s3a.checkBucket(r, bucket); err != s3err.ErrNone {
  266. s3err.WriteErrorResponse(w, r, err)
  267. return
  268. }
  269. fc, err := filer.ReadFilerConf(s3a.option.Filer, s3a.option.GrpcDialOption, nil)
  270. if err != nil {
  271. glog.Errorf("GetBucketLifecycleConfigurationHandler: %s", err)
  272. s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
  273. return
  274. }
  275. ttls := fc.GetCollectionTtls(s3a.getCollectionName(bucket))
  276. if len(ttls) == 0 {
  277. s3err.WriteErrorResponse(w, r, s3err.ErrNoSuchLifecycleConfiguration)
  278. return
  279. }
  280. response := Lifecycle{}
  281. for prefix, internalTtl := range ttls {
  282. ttl, _ := needle.ReadTTL(internalTtl)
  283. days := int(ttl.Minutes() / 60 / 24)
  284. if days == 0 {
  285. continue
  286. }
  287. response.Rules = append(response.Rules, Rule{
  288. Status: Enabled, Filter: Filter{
  289. Prefix: Prefix{string: prefix, set: true},
  290. set: true,
  291. },
  292. Expiration: Expiration{Days: days, set: true},
  293. })
  294. }
  295. writeSuccessResponseXML(w, r, response)
  296. }
  297. // PutBucketLifecycleConfigurationHandler Put Bucket Lifecycle configuration
  298. // https://docs.aws.amazon.com/AmazonS3/latest/API/API_PutBucketLifecycleConfiguration.html
  299. func (s3a *S3ApiServer) PutBucketLifecycleConfigurationHandler(w http.ResponseWriter, r *http.Request) {
  300. s3err.WriteErrorResponse(w, r, s3err.ErrNotImplemented)
  301. }
  302. // DeleteBucketMetricsConfiguration Delete Bucket Lifecycle
  303. // https://docs.aws.amazon.com/AmazonS3/latest/API/API_DeleteBucketLifecycle.html
  304. func (s3a *S3ApiServer) DeleteBucketLifecycleHandler(w http.ResponseWriter, r *http.Request) {
  305. s3err.WriteEmptyResponse(w, r, http.StatusNoContent)
  306. }
  307. // GetBucketLocationHandler Get bucket location
  308. // https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetBucketLocation.html
  309. func (s3a *S3ApiServer) GetBucketLocationHandler(w http.ResponseWriter, r *http.Request) {
  310. writeSuccessResponseXML(w, r, LocationConstraint{})
  311. }
  312. // GetBucketRequestPaymentHandler Get bucket location
  313. // https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetBucketRequestPayment.html
  314. func (s3a *S3ApiServer) GetBucketRequestPaymentHandler(w http.ResponseWriter, r *http.Request) {
  315. writeSuccessResponseXML(w, r, RequestPaymentConfiguration{Payer: "BucketOwner"})
  316. }
  317. // PutBucketOwnershipControls https://docs.aws.amazon.com/AmazonS3/latest/API/API_PutBucketOwnershipControls.html
  318. func (s3a *S3ApiServer) PutBucketOwnershipControls(w http.ResponseWriter, r *http.Request) {
  319. bucket, _ := s3_constants.GetBucketAndObject(r)
  320. glog.V(3).Infof("PutBucketOwnershipControls %s", bucket)
  321. errCode := s3a.checkAccessByOwnership(r, bucket)
  322. if errCode != s3err.ErrNone {
  323. s3err.WriteErrorResponse(w, r, errCode)
  324. return
  325. }
  326. if r.Body == nil || r.Body == http.NoBody {
  327. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidRequest)
  328. return
  329. }
  330. var v s3.OwnershipControls
  331. defer util.CloseRequest(r)
  332. err := xmlutil.UnmarshalXML(&v, xml.NewDecoder(r.Body), "")
  333. if err != nil {
  334. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidRequest)
  335. return
  336. }
  337. if len(v.Rules) != 1 {
  338. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidRequest)
  339. return
  340. }
  341. printOwnership := true
  342. ownership := *v.Rules[0].ObjectOwnership
  343. switch ownership {
  344. case s3_constants.OwnershipObjectWriter:
  345. case s3_constants.OwnershipBucketOwnerPreferred:
  346. case s3_constants.OwnershipBucketOwnerEnforced:
  347. printOwnership = false
  348. default:
  349. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidRequest)
  350. return
  351. }
  352. bucketEntry, err := s3a.getEntry(s3a.option.BucketsPath, bucket)
  353. if err != nil {
  354. if err == filer_pb.ErrNotFound {
  355. s3err.WriteErrorResponse(w, r, s3err.ErrNoSuchBucket)
  356. return
  357. }
  358. s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
  359. return
  360. }
  361. oldOwnership, ok := bucketEntry.Extended[s3_constants.ExtOwnershipKey]
  362. if !ok || string(oldOwnership) != ownership {
  363. if bucketEntry.Extended == nil {
  364. bucketEntry.Extended = make(map[string][]byte)
  365. }
  366. bucketEntry.Extended[s3_constants.ExtOwnershipKey] = []byte(ownership)
  367. err = s3a.updateEntry(s3a.option.BucketsPath, bucketEntry)
  368. if err != nil {
  369. s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
  370. return
  371. }
  372. }
  373. if printOwnership {
  374. result := &s3.PutBucketOwnershipControlsInput{
  375. OwnershipControls: &v,
  376. }
  377. s3err.WriteAwsXMLResponse(w, r, http.StatusOK, result)
  378. } else {
  379. writeSuccessResponseEmpty(w, r)
  380. }
  381. }
  382. // GetBucketOwnershipControls https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetBucketOwnershipControls.html
  383. func (s3a *S3ApiServer) GetBucketOwnershipControls(w http.ResponseWriter, r *http.Request) {
  384. bucket, _ := s3_constants.GetBucketAndObject(r)
  385. glog.V(3).Infof("GetBucketOwnershipControls %s", bucket)
  386. errCode := s3a.checkAccessByOwnership(r, bucket)
  387. if errCode != s3err.ErrNone {
  388. s3err.WriteErrorResponse(w, r, errCode)
  389. return
  390. }
  391. bucketEntry, err := s3a.getEntry(s3a.option.BucketsPath, bucket)
  392. if err != nil {
  393. if err == filer_pb.ErrNotFound {
  394. s3err.WriteErrorResponse(w, r, s3err.ErrNoSuchBucket)
  395. return
  396. }
  397. s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
  398. return
  399. }
  400. v, ok := bucketEntry.Extended[s3_constants.ExtOwnershipKey]
  401. if !ok {
  402. s3err.WriteErrorResponse(w, r, s3err.OwnershipControlsNotFoundError)
  403. return
  404. }
  405. ownership := string(v)
  406. result := &s3.PutBucketOwnershipControlsInput{
  407. OwnershipControls: &s3.OwnershipControls{
  408. Rules: []*s3.OwnershipControlsRule{
  409. {
  410. ObjectOwnership: &ownership,
  411. },
  412. },
  413. },
  414. }
  415. s3err.WriteAwsXMLResponse(w, r, http.StatusOK, result)
  416. }
  417. // DeleteBucketOwnershipControls https://docs.aws.amazon.com/AmazonS3/latest/API/API_DeleteBucketOwnershipControls.html
  418. func (s3a *S3ApiServer) DeleteBucketOwnershipControls(w http.ResponseWriter, r *http.Request) {
  419. bucket, _ := s3_constants.GetBucketAndObject(r)
  420. glog.V(3).Infof("PutBucketOwnershipControls %s", bucket)
  421. errCode := s3a.checkAccessByOwnership(r, bucket)
  422. if errCode != s3err.ErrNone {
  423. s3err.WriteErrorResponse(w, r, errCode)
  424. return
  425. }
  426. bucketEntry, err := s3a.getEntry(s3a.option.BucketsPath, bucket)
  427. if err != nil {
  428. if err == filer_pb.ErrNotFound {
  429. s3err.WriteErrorResponse(w, r, s3err.ErrNoSuchBucket)
  430. return
  431. }
  432. s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
  433. return
  434. }
  435. _, ok := bucketEntry.Extended[s3_constants.ExtOwnershipKey]
  436. if !ok {
  437. s3err.WriteErrorResponse(w, r, s3err.OwnershipControlsNotFoundError)
  438. return
  439. }
  440. delete(bucketEntry.Extended, s3_constants.ExtOwnershipKey)
  441. err = s3a.updateEntry(s3a.option.BucketsPath, bucketEntry)
  442. if err != nil {
  443. s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
  444. return
  445. }
  446. emptyOwnershipControls := &s3.OwnershipControls{
  447. Rules: []*s3.OwnershipControlsRule{},
  448. }
  449. s3err.WriteAwsXMLResponse(w, r, http.StatusOK, emptyOwnershipControls)
  450. }
  451. // GetBucketVersioningHandler Get Bucket Versioning status
  452. // https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetBucketVersioning.html
  453. func (s3a *S3ApiServer) GetBucketVersioningHandler(w http.ResponseWriter, r *http.Request) {
  454. bucket, _ := s3_constants.GetBucketAndObject(r)
  455. glog.V(3).Infof("GetBucketVersioning %s", bucket)
  456. if err := s3a.checkBucket(r, bucket); err != s3err.ErrNone {
  457. s3err.WriteErrorResponse(w, r, err)
  458. return
  459. }
  460. s3err.WriteAwsXMLResponse(w, r, http.StatusOK, &s3.PutBucketVersioningInput{
  461. VersioningConfiguration: &s3.VersioningConfiguration{
  462. Status: aws.String(s3.BucketVersioningStatusSuspended),
  463. },
  464. })
  465. }