s3api_acl_helper_test.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. package s3api
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "github.com/aws/aws-sdk-go/aws"
  6. "github.com/aws/aws-sdk-go/service/s3"
  7. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  8. "github.com/seaweedfs/seaweedfs/weed/pb/iam_pb"
  9. "github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
  10. "github.com/seaweedfs/seaweedfs/weed/s3api/s3err"
  11. "io"
  12. "net/http"
  13. "testing"
  14. )
  15. var accountManager *IdentityAccessManagement
  16. func init() {
  17. accountManager = &IdentityAccessManagement{}
  18. _ = accountManager.loadS3ApiConfiguration(&iam_pb.S3ApiConfiguration{
  19. Accounts: []*iam_pb.Account{
  20. {
  21. Id: "accountA",
  22. DisplayName: "accountAName",
  23. EmailAddress: "accountA@example.com",
  24. },
  25. {
  26. Id: "accountB",
  27. DisplayName: "accountBName",
  28. EmailAddress: "accountB@example.com",
  29. },
  30. },
  31. })
  32. }
  33. func TestGetAccountId(t *testing.T) {
  34. req := &http.Request{
  35. Header: make(map[string][]string),
  36. }
  37. //case1
  38. //accountId: "admin"
  39. req.Header.Set(s3_constants.AmzAccountId, s3_constants.AccountAdminId)
  40. if GetAccountId(req) != s3_constants.AccountAdminId {
  41. t.Fatal("expect accountId: admin")
  42. }
  43. //case2
  44. //accountId: "anoymous"
  45. req.Header.Set(s3_constants.AmzAccountId, s3_constants.AccountAnonymousId)
  46. if GetAccountId(req) != s3_constants.AccountAnonymousId {
  47. t.Fatal("expect accountId: anonymous")
  48. }
  49. //case3
  50. //accountId is nil => "anonymous"
  51. req.Header.Del(s3_constants.AmzAccountId)
  52. if GetAccountId(req) != s3_constants.AccountAnonymousId {
  53. t.Fatal("expect accountId: anonymous")
  54. }
  55. }
  56. func TestExtractAcl(t *testing.T) {
  57. type Case struct {
  58. id int
  59. resultErrCode, expectErrCode s3err.ErrorCode
  60. resultGrants, expectGrants []*s3.Grant
  61. }
  62. testCases := make([]*Case, 0)
  63. accountAdminId := "admin"
  64. {
  65. //case1 (good case)
  66. //parse acp from request body
  67. req := &http.Request{
  68. Header: make(map[string][]string),
  69. }
  70. req.Body = io.NopCloser(bytes.NewReader([]byte(`
  71. <AccessControlPolicy xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
  72. <Owner>
  73. <ID>admin</ID>
  74. <DisplayName>admin</DisplayName>
  75. </Owner>
  76. <AccessControlList>
  77. <Grant>
  78. <Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="CanonicalUser">
  79. <ID>admin</ID>
  80. </Grantee>
  81. <Permission>FULL_CONTROL</Permission>
  82. </Grant>
  83. <Grant>
  84. <Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Group">
  85. <URI>http://acs.amazonaws.com/groups/global/AllUsers</URI>
  86. </Grantee>
  87. <Permission>FULL_CONTROL</Permission>
  88. </Grant>
  89. </AccessControlList>
  90. </AccessControlPolicy>
  91. `)))
  92. objectWriter := "accountA"
  93. grants, errCode := ExtractAcl(req, accountManager, s3_constants.OwnershipObjectWriter, accountAdminId, accountAdminId, objectWriter)
  94. testCases = append(testCases, &Case{
  95. 1,
  96. errCode, s3err.ErrNone,
  97. grants, []*s3.Grant{
  98. {
  99. Grantee: &s3.Grantee{
  100. Type: &s3_constants.GrantTypeCanonicalUser,
  101. ID: &accountAdminId,
  102. },
  103. Permission: &s3_constants.PermissionFullControl,
  104. },
  105. {
  106. Grantee: &s3.Grantee{
  107. Type: &s3_constants.GrantTypeGroup,
  108. URI: &s3_constants.GranteeGroupAllUsers,
  109. },
  110. Permission: &s3_constants.PermissionFullControl,
  111. },
  112. },
  113. })
  114. }
  115. {
  116. //case2 (good case)
  117. //parse acp from header (cannedAcl)
  118. req := &http.Request{
  119. Header: make(map[string][]string),
  120. }
  121. req.Body = nil
  122. req.Header.Set(s3_constants.AmzCannedAcl, s3_constants.CannedAclPrivate)
  123. objectWriter := "accountA"
  124. grants, errCode := ExtractAcl(req, accountManager, s3_constants.OwnershipObjectWriter, accountAdminId, accountAdminId, objectWriter)
  125. testCases = append(testCases, &Case{
  126. 2,
  127. errCode, s3err.ErrNone,
  128. grants, []*s3.Grant{
  129. {
  130. Grantee: &s3.Grantee{
  131. Type: &s3_constants.GrantTypeCanonicalUser,
  132. ID: &objectWriter,
  133. },
  134. Permission: &s3_constants.PermissionFullControl,
  135. },
  136. },
  137. })
  138. }
  139. {
  140. //case3 (bad case)
  141. //parse acp from request body (content is invalid)
  142. req := &http.Request{
  143. Header: make(map[string][]string),
  144. }
  145. req.Body = io.NopCloser(bytes.NewReader([]byte("zdfsaf")))
  146. req.Header.Set(s3_constants.AmzCannedAcl, s3_constants.CannedAclPrivate)
  147. objectWriter := "accountA"
  148. _, errCode := ExtractAcl(req, accountManager, s3_constants.OwnershipObjectWriter, accountAdminId, accountAdminId, objectWriter)
  149. testCases = append(testCases, &Case{
  150. id: 3,
  151. resultErrCode: errCode, expectErrCode: s3err.ErrInvalidRequest,
  152. })
  153. }
  154. //case4 (bad case)
  155. //parse acp from header (cannedAcl is invalid)
  156. req := &http.Request{
  157. Header: make(map[string][]string),
  158. }
  159. req.Body = nil
  160. req.Header.Set(s3_constants.AmzCannedAcl, "dfaksjfk")
  161. objectWriter := "accountA"
  162. _, errCode := ExtractAcl(req, accountManager, s3_constants.OwnershipObjectWriter, accountAdminId, "", objectWriter)
  163. testCases = append(testCases, &Case{
  164. id: 4,
  165. resultErrCode: errCode, expectErrCode: s3err.ErrInvalidRequest,
  166. })
  167. {
  168. //case5 (bad case)
  169. //parse acp from request body: owner is inconsistent
  170. req.Body = io.NopCloser(bytes.NewReader([]byte(`
  171. <AccessControlPolicy xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
  172. <Owner>
  173. <ID>admin</ID>
  174. <DisplayName>admin</DisplayName>
  175. </Owner>
  176. <AccessControlList>
  177. <Grant>
  178. <Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="CanonicalUser">
  179. <ID>admin</ID>
  180. </Grantee>
  181. <Permission>FULL_CONTROL</Permission>
  182. </Grant>
  183. <Grant>
  184. <Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Group">
  185. <URI>http://acs.amazonaws.com/groups/global/AllUsers</URI>
  186. </Grantee>
  187. <Permission>FULL_CONTROL</Permission>
  188. </Grant>
  189. </AccessControlList>
  190. </AccessControlPolicy>
  191. `)))
  192. objectWriter = "accountA"
  193. _, errCode := ExtractAcl(req, accountManager, s3_constants.OwnershipObjectWriter, accountAdminId, objectWriter, objectWriter)
  194. testCases = append(testCases, &Case{
  195. id: 5,
  196. resultErrCode: errCode, expectErrCode: s3err.ErrAccessDenied,
  197. })
  198. }
  199. for _, tc := range testCases {
  200. if tc.resultErrCode != tc.expectErrCode {
  201. t.Fatalf("case[%d]: errorCode not expect", tc.id)
  202. }
  203. if !grantsEquals(tc.resultGrants, tc.expectGrants) {
  204. t.Fatalf("case[%d]: grants not expect", tc.id)
  205. }
  206. }
  207. }
  208. func TestParseAndValidateAclHeaders(t *testing.T) {
  209. type Case struct {
  210. id int
  211. resultOwner, expectOwner string
  212. resultErrCode, expectErrCode s3err.ErrorCode
  213. resultGrants, expectGrants []*s3.Grant
  214. }
  215. testCases := make([]*Case, 0)
  216. bucketOwner := "admin"
  217. {
  218. //case1 (good case)
  219. //parse custom acl
  220. req := &http.Request{
  221. Header: make(map[string][]string),
  222. }
  223. objectWriter := "accountA"
  224. req.Header.Set(s3_constants.AmzAclFullControl, `uri="http://acs.amazonaws.com/groups/global/AllUsers", id="anonymous", emailAddress="admin@example.com"`)
  225. ownerId, grants, errCode := ParseAndValidateAclHeaders(req, accountManager, s3_constants.OwnershipObjectWriter, bucketOwner, objectWriter, false)
  226. testCases = append(testCases, &Case{
  227. 1,
  228. ownerId, objectWriter,
  229. errCode, s3err.ErrNone,
  230. grants, []*s3.Grant{
  231. {
  232. Grantee: &s3.Grantee{
  233. Type: &s3_constants.GrantTypeGroup,
  234. URI: &s3_constants.GranteeGroupAllUsers,
  235. },
  236. Permission: &s3_constants.PermissionFullControl,
  237. },
  238. {
  239. Grantee: &s3.Grantee{
  240. Type: &s3_constants.GrantTypeCanonicalUser,
  241. ID: aws.String(s3_constants.AccountAnonymousId),
  242. },
  243. Permission: &s3_constants.PermissionFullControl,
  244. },
  245. {
  246. Grantee: &s3.Grantee{
  247. Type: &s3_constants.GrantTypeCanonicalUser,
  248. ID: aws.String(s3_constants.AccountAdminId),
  249. },
  250. Permission: &s3_constants.PermissionFullControl,
  251. },
  252. },
  253. })
  254. }
  255. {
  256. //case2 (good case)
  257. //parse canned acl (ownership=ObjectWriter)
  258. req := &http.Request{
  259. Header: make(map[string][]string),
  260. }
  261. objectWriter := "accountA"
  262. req.Header.Set(s3_constants.AmzCannedAcl, s3_constants.CannedAclBucketOwnerFullControl)
  263. ownerId, grants, errCode := ParseAndValidateAclHeaders(req, accountManager, s3_constants.OwnershipObjectWriter, bucketOwner, objectWriter, false)
  264. testCases = append(testCases, &Case{
  265. 2,
  266. ownerId, objectWriter,
  267. errCode, s3err.ErrNone,
  268. grants, []*s3.Grant{
  269. {
  270. Grantee: &s3.Grantee{
  271. Type: &s3_constants.GrantTypeCanonicalUser,
  272. ID: &objectWriter,
  273. },
  274. Permission: &s3_constants.PermissionFullControl,
  275. },
  276. {
  277. Grantee: &s3.Grantee{
  278. Type: &s3_constants.GrantTypeCanonicalUser,
  279. ID: &bucketOwner,
  280. },
  281. Permission: &s3_constants.PermissionFullControl,
  282. },
  283. },
  284. })
  285. }
  286. {
  287. //case3 (good case)
  288. //parse canned acl (ownership=OwnershipBucketOwnerPreferred)
  289. req := &http.Request{
  290. Header: make(map[string][]string),
  291. }
  292. objectWriter := "accountA"
  293. req.Header.Set(s3_constants.AmzCannedAcl, s3_constants.CannedAclBucketOwnerFullControl)
  294. ownerId, grants, errCode := ParseAndValidateAclHeaders(req, accountManager, s3_constants.OwnershipBucketOwnerPreferred, bucketOwner, objectWriter, false)
  295. testCases = append(testCases, &Case{
  296. 3,
  297. ownerId, bucketOwner,
  298. errCode, s3err.ErrNone,
  299. grants, []*s3.Grant{
  300. {
  301. Grantee: &s3.Grantee{
  302. Type: &s3_constants.GrantTypeCanonicalUser,
  303. ID: &bucketOwner,
  304. },
  305. Permission: &s3_constants.PermissionFullControl,
  306. },
  307. },
  308. })
  309. }
  310. {
  311. //case4 (bad case)
  312. //parse custom acl (grantee id not exists)
  313. req := &http.Request{
  314. Header: make(map[string][]string),
  315. }
  316. objectWriter := "accountA"
  317. req.Header.Set(s3_constants.AmzAclFullControl, `uri="http://acs.amazonaws.com/groups/global/AllUsers", id="notExistsAccount", emailAddress="admin@example.com"`)
  318. _, _, errCode := ParseAndValidateAclHeaders(req, accountManager, s3_constants.OwnershipObjectWriter, bucketOwner, objectWriter, false)
  319. testCases = append(testCases, &Case{
  320. id: 4,
  321. resultErrCode: errCode, expectErrCode: s3err.ErrInvalidRequest,
  322. })
  323. }
  324. {
  325. //case5 (bad case)
  326. //parse custom acl (invalid format)
  327. req := &http.Request{
  328. Header: make(map[string][]string),
  329. }
  330. objectWriter := "accountA"
  331. req.Header.Set(s3_constants.AmzAclFullControl, `uri="http:sfasf"`)
  332. _, _, errCode := ParseAndValidateAclHeaders(req, accountManager, s3_constants.OwnershipObjectWriter, bucketOwner, objectWriter, false)
  333. testCases = append(testCases, &Case{
  334. id: 5,
  335. resultErrCode: errCode, expectErrCode: s3err.ErrInvalidRequest,
  336. })
  337. }
  338. {
  339. //case6 (bad case)
  340. //parse canned acl (invalid value)
  341. req := &http.Request{
  342. Header: make(map[string][]string),
  343. }
  344. objectWriter := "accountA"
  345. req.Header.Set(s3_constants.AmzCannedAcl, `uri="http:sfasf"`)
  346. _, _, errCode := ParseAndValidateAclHeaders(req, accountManager, s3_constants.OwnershipObjectWriter, bucketOwner, objectWriter, false)
  347. testCases = append(testCases, &Case{
  348. id: 5,
  349. resultErrCode: errCode, expectErrCode: s3err.ErrInvalidRequest,
  350. })
  351. }
  352. for _, tc := range testCases {
  353. if tc.expectErrCode != tc.resultErrCode {
  354. t.Errorf("case[%d]: errCode unexpect", tc.id)
  355. }
  356. if tc.resultOwner != tc.expectOwner {
  357. t.Errorf("case[%d]: ownerId unexpect", tc.id)
  358. }
  359. if !grantsEquals(tc.resultGrants, tc.expectGrants) {
  360. t.Fatalf("case[%d]: grants not expect", tc.id)
  361. }
  362. }
  363. }
  364. func grantsEquals(a, b []*s3.Grant) bool {
  365. if len(a) != len(b) {
  366. return false
  367. }
  368. for i, grant := range a {
  369. if !GrantEquals(grant, b[i]) {
  370. return false
  371. }
  372. }
  373. return true
  374. }
  375. func TestDetermineReqGrants(t *testing.T) {
  376. {
  377. //case1: request account is anonymous
  378. accountId := s3_constants.AccountAnonymousId
  379. reqPermission := s3_constants.PermissionRead
  380. resultGrants := DetermineReqGrants(accountId, reqPermission)
  381. expectGrants := []*s3.Grant{
  382. {
  383. Grantee: &s3.Grantee{
  384. Type: &s3_constants.GrantTypeGroup,
  385. URI: &s3_constants.GranteeGroupAllUsers,
  386. },
  387. Permission: &reqPermission,
  388. },
  389. {
  390. Grantee: &s3.Grantee{
  391. Type: &s3_constants.GrantTypeGroup,
  392. URI: &s3_constants.GranteeGroupAllUsers,
  393. },
  394. Permission: &s3_constants.PermissionFullControl,
  395. },
  396. {
  397. Grantee: &s3.Grantee{
  398. Type: &s3_constants.GrantTypeCanonicalUser,
  399. ID: &accountId,
  400. },
  401. Permission: &reqPermission,
  402. },
  403. {
  404. Grantee: &s3.Grantee{
  405. Type: &s3_constants.GrantTypeCanonicalUser,
  406. ID: &accountId,
  407. },
  408. Permission: &s3_constants.PermissionFullControl,
  409. },
  410. }
  411. if !grantsEquals(resultGrants, expectGrants) {
  412. t.Fatalf("grants not expect")
  413. }
  414. }
  415. {
  416. //case2: request account is not anonymous (Iam authed)
  417. accountId := "accountX"
  418. reqPermission := s3_constants.PermissionRead
  419. resultGrants := DetermineReqGrants(accountId, reqPermission)
  420. expectGrants := []*s3.Grant{
  421. {
  422. Grantee: &s3.Grantee{
  423. Type: &s3_constants.GrantTypeGroup,
  424. URI: &s3_constants.GranteeGroupAllUsers,
  425. },
  426. Permission: &reqPermission,
  427. },
  428. {
  429. Grantee: &s3.Grantee{
  430. Type: &s3_constants.GrantTypeGroup,
  431. URI: &s3_constants.GranteeGroupAllUsers,
  432. },
  433. Permission: &s3_constants.PermissionFullControl,
  434. },
  435. {
  436. Grantee: &s3.Grantee{
  437. Type: &s3_constants.GrantTypeCanonicalUser,
  438. ID: &accountId,
  439. },
  440. Permission: &reqPermission,
  441. },
  442. {
  443. Grantee: &s3.Grantee{
  444. Type: &s3_constants.GrantTypeCanonicalUser,
  445. ID: &accountId,
  446. },
  447. Permission: &s3_constants.PermissionFullControl,
  448. },
  449. {
  450. Grantee: &s3.Grantee{
  451. Type: &s3_constants.GrantTypeGroup,
  452. URI: &s3_constants.GranteeGroupAuthenticatedUsers,
  453. },
  454. Permission: &reqPermission,
  455. },
  456. {
  457. Grantee: &s3.Grantee{
  458. Type: &s3_constants.GrantTypeGroup,
  459. URI: &s3_constants.GranteeGroupAuthenticatedUsers,
  460. },
  461. Permission: &s3_constants.PermissionFullControl,
  462. },
  463. }
  464. if !grantsEquals(resultGrants, expectGrants) {
  465. t.Fatalf("grants not expect")
  466. }
  467. }
  468. }
  469. func TestAssembleEntryWithAcp(t *testing.T) {
  470. defaultOwner := "admin"
  471. //case1
  472. //assemble with non-empty grants
  473. expectOwner := "accountS"
  474. expectGrants := []*s3.Grant{
  475. {
  476. Permission: &s3_constants.PermissionRead,
  477. Grantee: &s3.Grantee{
  478. Type: &s3_constants.GrantTypeGroup,
  479. ID: aws.String(s3_constants.AccountAdminId),
  480. URI: &s3_constants.GranteeGroupAllUsers,
  481. },
  482. },
  483. }
  484. entry := &filer_pb.Entry{}
  485. AssembleEntryWithAcp(entry, expectOwner, expectGrants)
  486. resultOwner := GetAcpOwner(entry.Extended, defaultOwner)
  487. if resultOwner != expectOwner {
  488. t.Fatalf("owner not expect")
  489. }
  490. resultGrants := GetAcpGrants(entry.Extended)
  491. if !grantsEquals(resultGrants, expectGrants) {
  492. t.Fatal("grants not expect")
  493. }
  494. //case2
  495. //assemble with empty grants (override)
  496. AssembleEntryWithAcp(entry, "", nil)
  497. resultOwner = GetAcpOwner(entry.Extended, defaultOwner)
  498. if resultOwner != defaultOwner {
  499. t.Fatalf("owner not expect")
  500. }
  501. resultGrants = GetAcpGrants(entry.Extended)
  502. if len(resultGrants) != 0 {
  503. t.Fatal("grants not expect")
  504. }
  505. }
  506. func TestGrantEquals(t *testing.T) {
  507. testCases := map[bool]bool{
  508. GrantEquals(nil, nil): true,
  509. GrantEquals(&s3.Grant{}, nil): false,
  510. GrantEquals(&s3.Grant{}, &s3.Grant{}): true,
  511. GrantEquals(&s3.Grant{
  512. Permission: &s3_constants.PermissionRead,
  513. }, &s3.Grant{}): false,
  514. GrantEquals(&s3.Grant{
  515. Permission: &s3_constants.PermissionRead,
  516. }, &s3.Grant{
  517. Permission: &s3_constants.PermissionRead,
  518. }): true,
  519. GrantEquals(&s3.Grant{
  520. Permission: &s3_constants.PermissionRead,
  521. Grantee: &s3.Grantee{},
  522. }, &s3.Grant{
  523. Permission: &s3_constants.PermissionRead,
  524. Grantee: &s3.Grantee{},
  525. }): true,
  526. GrantEquals(&s3.Grant{
  527. Permission: &s3_constants.PermissionRead,
  528. Grantee: &s3.Grantee{
  529. Type: &s3_constants.GrantTypeGroup,
  530. },
  531. }, &s3.Grant{
  532. Permission: &s3_constants.PermissionRead,
  533. Grantee: &s3.Grantee{},
  534. }): false,
  535. //type not present, compare other fields of grant is meaningless
  536. GrantEquals(&s3.Grant{
  537. Permission: &s3_constants.PermissionRead,
  538. Grantee: &s3.Grantee{
  539. ID: aws.String(s3_constants.AccountAdminId),
  540. //EmailAddress: &s3account.AccountAdmin.EmailAddress,
  541. },
  542. }, &s3.Grant{
  543. Permission: &s3_constants.PermissionRead,
  544. Grantee: &s3.Grantee{
  545. ID: aws.String(s3_constants.AccountAdminId),
  546. },
  547. }): true,
  548. GrantEquals(&s3.Grant{
  549. Permission: &s3_constants.PermissionRead,
  550. Grantee: &s3.Grantee{
  551. Type: &s3_constants.GrantTypeGroup,
  552. },
  553. }, &s3.Grant{
  554. Permission: &s3_constants.PermissionRead,
  555. Grantee: &s3.Grantee{
  556. Type: &s3_constants.GrantTypeGroup,
  557. },
  558. }): true,
  559. GrantEquals(&s3.Grant{
  560. Permission: &s3_constants.PermissionRead,
  561. Grantee: &s3.Grantee{
  562. Type: &s3_constants.GrantTypeGroup,
  563. URI: &s3_constants.GranteeGroupAllUsers,
  564. },
  565. }, &s3.Grant{
  566. Permission: &s3_constants.PermissionRead,
  567. Grantee: &s3.Grantee{
  568. Type: &s3_constants.GrantTypeGroup,
  569. URI: &s3_constants.GranteeGroupAllUsers,
  570. },
  571. }): true,
  572. GrantEquals(&s3.Grant{
  573. Permission: &s3_constants.PermissionWrite,
  574. Grantee: &s3.Grantee{
  575. Type: &s3_constants.GrantTypeGroup,
  576. URI: &s3_constants.GranteeGroupAllUsers,
  577. },
  578. }, &s3.Grant{
  579. Permission: &s3_constants.PermissionRead,
  580. Grantee: &s3.Grantee{
  581. Type: &s3_constants.GrantTypeGroup,
  582. URI: &s3_constants.GranteeGroupAllUsers,
  583. },
  584. }): false,
  585. GrantEquals(&s3.Grant{
  586. Permission: &s3_constants.PermissionRead,
  587. Grantee: &s3.Grantee{
  588. Type: &s3_constants.GrantTypeGroup,
  589. ID: aws.String(s3_constants.AccountAdminId),
  590. },
  591. }, &s3.Grant{
  592. Permission: &s3_constants.PermissionRead,
  593. Grantee: &s3.Grantee{
  594. Type: &s3_constants.GrantTypeGroup,
  595. ID: aws.String(s3_constants.AccountAdminId),
  596. },
  597. }): true,
  598. GrantEquals(&s3.Grant{
  599. Permission: &s3_constants.PermissionRead,
  600. Grantee: &s3.Grantee{
  601. Type: &s3_constants.GrantTypeGroup,
  602. ID: aws.String(s3_constants.AccountAdminId),
  603. URI: &s3_constants.GranteeGroupAllUsers,
  604. },
  605. }, &s3.Grant{
  606. Permission: &s3_constants.PermissionRead,
  607. Grantee: &s3.Grantee{
  608. Type: &s3_constants.GrantTypeGroup,
  609. ID: aws.String(s3_constants.AccountAdminId),
  610. },
  611. }): false,
  612. GrantEquals(&s3.Grant{
  613. Permission: &s3_constants.PermissionRead,
  614. Grantee: &s3.Grantee{
  615. Type: &s3_constants.GrantTypeGroup,
  616. ID: aws.String(s3_constants.AccountAdminId),
  617. URI: &s3_constants.GranteeGroupAllUsers,
  618. },
  619. }, &s3.Grant{
  620. Permission: &s3_constants.PermissionRead,
  621. Grantee: &s3.Grantee{
  622. Type: &s3_constants.GrantTypeGroup,
  623. URI: &s3_constants.GranteeGroupAllUsers,
  624. },
  625. }): true,
  626. }
  627. for tc, expect := range testCases {
  628. if tc != expect {
  629. t.Fatal("TestGrantEquals not expect!")
  630. }
  631. }
  632. }
  633. func TestSetAcpOwnerHeader(t *testing.T) {
  634. ownerId := "accountZ"
  635. req := &http.Request{
  636. Header: make(map[string][]string),
  637. }
  638. SetAcpOwnerHeader(req, ownerId)
  639. if req.Header.Get(s3_constants.ExtAmzOwnerKey) != ownerId {
  640. t.Fatalf("owner unexpect")
  641. }
  642. }
  643. func TestSetAcpGrantsHeader(t *testing.T) {
  644. req := &http.Request{
  645. Header: make(map[string][]string),
  646. }
  647. grants := []*s3.Grant{
  648. {
  649. Permission: &s3_constants.PermissionRead,
  650. Grantee: &s3.Grantee{
  651. Type: &s3_constants.GrantTypeGroup,
  652. ID: aws.String(s3_constants.AccountAdminId),
  653. URI: &s3_constants.GranteeGroupAllUsers,
  654. },
  655. },
  656. }
  657. SetAcpGrantsHeader(req, grants)
  658. grantsJson, _ := json.Marshal(grants)
  659. if req.Header.Get(s3_constants.ExtAmzAclKey) != string(grantsJson) {
  660. t.Fatalf("owner unexpect")
  661. }
  662. }