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.

671 lines
20 KiB

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