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.

222 lines
4.9 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
2 years ago
5 years ago
2 years ago
5 years ago
2 years ago
5 years ago
  1. package s3api
  2. import (
  3. . "github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
  4. "github.com/stretchr/testify/assert"
  5. "reflect"
  6. "testing"
  7. jsonpb "google.golang.org/protobuf/encoding/protojson"
  8. "github.com/seaweedfs/seaweedfs/weed/pb/iam_pb"
  9. )
  10. func TestIdentityListFileFormat(t *testing.T) {
  11. s3ApiConfiguration := &iam_pb.S3ApiConfiguration{}
  12. identity1 := &iam_pb.Identity{
  13. Name: "some_name",
  14. Credentials: []*iam_pb.Credential{
  15. {
  16. AccessKey: "some_access_key1",
  17. SecretKey: "some_secret_key2",
  18. },
  19. },
  20. Actions: []string{
  21. ACTION_ADMIN,
  22. ACTION_READ,
  23. ACTION_WRITE,
  24. },
  25. }
  26. identity2 := &iam_pb.Identity{
  27. Name: "some_read_only_user",
  28. Credentials: []*iam_pb.Credential{
  29. {
  30. AccessKey: "some_access_key1",
  31. SecretKey: "some_secret_key1",
  32. },
  33. },
  34. Actions: []string{
  35. ACTION_READ,
  36. },
  37. }
  38. identity3 := &iam_pb.Identity{
  39. Name: "some_normal_user",
  40. Credentials: []*iam_pb.Credential{
  41. {
  42. AccessKey: "some_access_key2",
  43. SecretKey: "some_secret_key2",
  44. },
  45. },
  46. Actions: []string{
  47. ACTION_READ,
  48. ACTION_WRITE,
  49. },
  50. }
  51. s3ApiConfiguration.Identities = append(s3ApiConfiguration.Identities, identity1)
  52. s3ApiConfiguration.Identities = append(s3ApiConfiguration.Identities, identity2)
  53. s3ApiConfiguration.Identities = append(s3ApiConfiguration.Identities, identity3)
  54. m := jsonpb.MarshalOptions{
  55. EmitUnpopulated: true,
  56. Indent: " ",
  57. }
  58. text, _ := m.Marshal(s3ApiConfiguration)
  59. println(string(text))
  60. }
  61. func TestCanDo(t *testing.T) {
  62. ident1 := &Identity{
  63. Name: "anything",
  64. Actions: []Action{
  65. "Write:bucket1/a/b/c/*",
  66. "Write:bucket1/a/b/other",
  67. },
  68. }
  69. // object specific
  70. assert.Equal(t, true, ident1.canDo(ACTION_WRITE, "bucket1", "/a/b/c/d.txt"))
  71. assert.Equal(t, false, ident1.canDo(ACTION_WRITE, "bucket1", "/a/b/other/some"), "action without *")
  72. // bucket specific
  73. ident2 := &Identity{
  74. Name: "anything",
  75. Actions: []Action{
  76. "Read:bucket1",
  77. "Write:bucket1/*",
  78. },
  79. }
  80. assert.Equal(t, true, ident2.canDo(ACTION_READ, "bucket1", "/a/b/c/d.txt"))
  81. assert.Equal(t, true, ident2.canDo(ACTION_WRITE, "bucket1", "/a/b/c/d.txt"))
  82. assert.Equal(t, false, ident2.canDo(ACTION_LIST, "bucket1", "/a/b/c/d.txt"))
  83. // across buckets
  84. ident3 := &Identity{
  85. Name: "anything",
  86. Actions: []Action{
  87. "Read",
  88. "Write",
  89. },
  90. }
  91. assert.Equal(t, true, ident3.canDo(ACTION_READ, "bucket1", "/a/b/c/d.txt"))
  92. assert.Equal(t, true, ident3.canDo(ACTION_WRITE, "bucket1", "/a/b/c/d.txt"))
  93. assert.Equal(t, false, ident3.canDo(ACTION_LIST, "bucket1", "/a/b/other/some"))
  94. // partial buckets
  95. ident4 := &Identity{
  96. Name: "anything",
  97. Actions: []Action{
  98. "Read:special_*",
  99. },
  100. }
  101. assert.Equal(t, true, ident4.canDo(ACTION_READ, "special_bucket", "/a/b/c/d.txt"))
  102. assert.Equal(t, false, ident4.canDo(ACTION_READ, "bucket1", "/a/b/c/d.txt"))
  103. // admin buckets
  104. ident5 := &Identity{
  105. Name: "anything",
  106. Actions: []Action{
  107. "Admin:special_*",
  108. },
  109. }
  110. assert.Equal(t, true, ident5.canDo(ACTION_READ, "special_bucket", "/a/b/c/d.txt"))
  111. assert.Equal(t, true, ident5.canDo(ACTION_WRITE, "special_bucket", "/a/b/c/d.txt"))
  112. }
  113. type LoadS3ApiConfigurationTestCase struct {
  114. pbIdent *iam_pb.Identity
  115. expectIdent *Identity
  116. }
  117. func TestLoadS3ApiConfiguration(t *testing.T) {
  118. testCases := map[string]*LoadS3ApiConfigurationTestCase{
  119. "notSpecifyAccountId": {
  120. pbIdent: &iam_pb.Identity{
  121. Name: "notSpecifyAccountId",
  122. Actions: []string{
  123. "Read",
  124. "Write",
  125. },
  126. Credentials: []*iam_pb.Credential{
  127. {
  128. AccessKey: "some_access_key1",
  129. SecretKey: "some_secret_key2",
  130. },
  131. },
  132. },
  133. expectIdent: &Identity{
  134. Name: "notSpecifyAccountId",
  135. AccountId: AccountAdmin.Id,
  136. Actions: []Action{
  137. "Read",
  138. "Write",
  139. },
  140. Credentials: []*Credential{
  141. {
  142. AccessKey: "some_access_key1",
  143. SecretKey: "some_secret_key2",
  144. },
  145. },
  146. },
  147. },
  148. "specifiedAccountID": {
  149. pbIdent: &iam_pb.Identity{
  150. Name: "specifiedAccountID",
  151. AccountId: "specifiedAccountID",
  152. Actions: []string{
  153. "Read",
  154. "Write",
  155. },
  156. },
  157. expectIdent: &Identity{
  158. Name: "specifiedAccountID",
  159. AccountId: "specifiedAccountID",
  160. Actions: []Action{
  161. "Read",
  162. "Write",
  163. },
  164. },
  165. },
  166. "anonymous": {
  167. pbIdent: &iam_pb.Identity{
  168. Name: "anonymous",
  169. Actions: []string{
  170. "Read",
  171. "Write",
  172. },
  173. },
  174. expectIdent: &Identity{
  175. Name: "anonymous",
  176. AccountId: "anonymous",
  177. Actions: []Action{
  178. "Read",
  179. "Write",
  180. },
  181. },
  182. },
  183. }
  184. config := &iam_pb.S3ApiConfiguration{
  185. Identities: make([]*iam_pb.Identity, 0),
  186. }
  187. for _, v := range testCases {
  188. config.Identities = append(config.Identities, v.pbIdent)
  189. }
  190. iam := IdentityAccessManagement{}
  191. err := iam.loadS3ApiConfiguration(config)
  192. if err != nil {
  193. return
  194. }
  195. for _, ident := range iam.identities {
  196. tc := testCases[ident.Name]
  197. if !reflect.DeepEqual(ident, tc.expectIdent) {
  198. t.Error("not expect")
  199. }
  200. }
  201. }