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.

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