auth_credentials_test.go 5.6 KB

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