api-get-object-acl.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Minio Go Library for Amazon S3 Compatible Cloud Storage
  3. * Copyright 2018 Minio, Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package minio
  18. import (
  19. "context"
  20. "net/http"
  21. "net/url"
  22. )
  23. type accessControlPolicy struct {
  24. Owner struct {
  25. ID string `xml:"ID"`
  26. DisplayName string `xml:"DisplayName"`
  27. } `xml:"Owner"`
  28. AccessControlList struct {
  29. Grant []struct {
  30. Grantee struct {
  31. ID string `xml:"ID"`
  32. DisplayName string `xml:"DisplayName"`
  33. URI string `xml:"URI"`
  34. } `xml:"Grantee"`
  35. Permission string `xml:"Permission"`
  36. } `xml:"Grant"`
  37. } `xml:"AccessControlList"`
  38. }
  39. //GetObjectACL get object ACLs
  40. func (c Client) GetObjectACL(bucketName, objectName string) (*ObjectInfo, error) {
  41. resp, err := c.executeMethod(context.Background(), "GET", requestMetadata{
  42. bucketName: bucketName,
  43. objectName: objectName,
  44. queryValues: url.Values{
  45. "acl": []string{""},
  46. },
  47. })
  48. if err != nil {
  49. return nil, err
  50. }
  51. defer closeResponse(resp)
  52. if resp.StatusCode != http.StatusOK {
  53. return nil, httpRespToErrorResponse(resp, bucketName, objectName)
  54. }
  55. res := &accessControlPolicy{}
  56. if err := xmlDecoder(resp.Body, res); err != nil {
  57. return nil, err
  58. }
  59. objInfo, err := c.statObject(context.Background(), bucketName, objectName, StatObjectOptions{})
  60. if err != nil {
  61. return nil, err
  62. }
  63. cannedACL := getCannedACL(res)
  64. if cannedACL != "" {
  65. objInfo.Metadata.Add("X-Amz-Acl", cannedACL)
  66. return &objInfo, nil
  67. }
  68. grantACL := getAmzGrantACL(res)
  69. for k, v := range grantACL {
  70. objInfo.Metadata[k] = v
  71. }
  72. return &objInfo, nil
  73. }
  74. func getCannedACL(aCPolicy *accessControlPolicy) string {
  75. grants := aCPolicy.AccessControlList.Grant
  76. switch {
  77. case len(grants) == 1:
  78. if grants[0].Grantee.URI == "" && grants[0].Permission == "FULL_CONTROL" {
  79. return "private"
  80. }
  81. case len(grants) == 2:
  82. for _, g := range grants {
  83. if g.Grantee.URI == "http://acs.amazonaws.com/groups/global/AuthenticatedUsers" && g.Permission == "READ" {
  84. return "authenticated-read"
  85. }
  86. if g.Grantee.URI == "http://acs.amazonaws.com/groups/global/AllUsers" && g.Permission == "READ" {
  87. return "public-read"
  88. }
  89. if g.Permission == "READ" && g.Grantee.ID == aCPolicy.Owner.ID {
  90. return "bucket-owner-read"
  91. }
  92. }
  93. case len(grants) == 3:
  94. for _, g := range grants {
  95. if g.Grantee.URI == "http://acs.amazonaws.com/groups/global/AllUsers" && g.Permission == "WRITE" {
  96. return "public-read-write"
  97. }
  98. }
  99. }
  100. return ""
  101. }
  102. func getAmzGrantACL(aCPolicy *accessControlPolicy) map[string][]string {
  103. grants := aCPolicy.AccessControlList.Grant
  104. res := map[string][]string{}
  105. for _, g := range grants {
  106. switch {
  107. case g.Permission == "READ":
  108. res["X-Amz-Grant-Read"] = append(res["X-Amz-Grant-Read"], "id="+g.Grantee.ID)
  109. case g.Permission == "WRITE":
  110. res["X-Amz-Grant-Write"] = append(res["X-Amz-Grant-Write"], "id="+g.Grantee.ID)
  111. case g.Permission == "READ_ACP":
  112. res["X-Amz-Grant-Read-Acp"] = append(res["X-Amz-Grant-Read-Acp"], "id="+g.Grantee.ID)
  113. case g.Permission == "WRITE_ACP":
  114. res["X-Amz-Grant-Write-Acp"] = append(res["X-Amz-Grant-Write-Acp"], "id="+g.Grantee.ID)
  115. case g.Permission == "FULL_CONTROL":
  116. res["X-Amz-Grant-Full-Control"] = append(res["X-Amz-Grant-Full-Control"], "id="+g.Grantee.ID)
  117. }
  118. }
  119. return res
  120. }