engine_test.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. package policy_engine
  2. import (
  3. "net/http"
  4. "net/url"
  5. "testing"
  6. "github.com/seaweedfs/seaweedfs/weed/s3api/s3err"
  7. )
  8. func TestPolicyEngine(t *testing.T) {
  9. engine := NewPolicyEngine()
  10. // Test policy JSON
  11. policyJSON := `{
  12. "Version": "2012-10-17",
  13. "Statement": [
  14. {
  15. "Effect": "Allow",
  16. "Action": ["s3:GetObject", "s3:PutObject"],
  17. "Resource": ["arn:aws:s3:::test-bucket/*"]
  18. },
  19. {
  20. "Effect": "Deny",
  21. "Action": ["s3:DeleteObject"],
  22. "Resource": ["arn:aws:s3:::test-bucket/*"],
  23. "Condition": {
  24. "StringEquals": {
  25. "s3:RequestMethod": ["DELETE"]
  26. }
  27. }
  28. }
  29. ]
  30. }`
  31. // Set bucket policy
  32. err := engine.SetBucketPolicy("test-bucket", policyJSON)
  33. if err != nil {
  34. t.Fatalf("Failed to set bucket policy: %v", err)
  35. }
  36. // Test Allow case
  37. args := &PolicyEvaluationArgs{
  38. Action: "s3:GetObject",
  39. Resource: "arn:aws:s3:::test-bucket/test-object",
  40. Principal: "user1",
  41. Conditions: map[string][]string{},
  42. }
  43. result := engine.EvaluatePolicy("test-bucket", args)
  44. if result != PolicyResultAllow {
  45. t.Errorf("Expected Allow, got %v", result)
  46. }
  47. // Test Deny case
  48. args = &PolicyEvaluationArgs{
  49. Action: "s3:DeleteObject",
  50. Resource: "arn:aws:s3:::test-bucket/test-object",
  51. Principal: "user1",
  52. Conditions: map[string][]string{
  53. "s3:RequestMethod": {"DELETE"},
  54. },
  55. }
  56. result = engine.EvaluatePolicy("test-bucket", args)
  57. if result != PolicyResultDeny {
  58. t.Errorf("Expected Deny, got %v", result)
  59. }
  60. // Test non-matching action
  61. args = &PolicyEvaluationArgs{
  62. Action: "s3:ListBucket",
  63. Resource: "arn:aws:s3:::test-bucket",
  64. Principal: "user1",
  65. Conditions: map[string][]string{},
  66. }
  67. result = engine.EvaluatePolicy("test-bucket", args)
  68. if result != PolicyResultDeny {
  69. t.Errorf("Expected Deny for non-matching action, got %v", result)
  70. }
  71. // Test GetBucketPolicy
  72. policy, err := engine.GetBucketPolicy("test-bucket")
  73. if err != nil {
  74. t.Fatalf("Failed to get bucket policy: %v", err)
  75. }
  76. if policy.Version != "2012-10-17" {
  77. t.Errorf("Expected version 2012-10-17, got %s", policy.Version)
  78. }
  79. // Test DeleteBucketPolicy
  80. err = engine.DeleteBucketPolicy("test-bucket")
  81. if err != nil {
  82. t.Fatalf("Failed to delete bucket policy: %v", err)
  83. }
  84. // Test policy is gone
  85. result = engine.EvaluatePolicy("test-bucket", args)
  86. if result != PolicyResultIndeterminate {
  87. t.Errorf("Expected Indeterminate after policy deletion, got %v", result)
  88. }
  89. }
  90. func TestConditionEvaluators(t *testing.T) {
  91. tests := []struct {
  92. name string
  93. operator string
  94. conditionValue interface{}
  95. contextValues []string
  96. expected bool
  97. }{
  98. {
  99. name: "StringEquals - match",
  100. operator: "StringEquals",
  101. conditionValue: "test-value",
  102. contextValues: []string{"test-value"},
  103. expected: true,
  104. },
  105. {
  106. name: "StringEquals - no match",
  107. operator: "StringEquals",
  108. conditionValue: "test-value",
  109. contextValues: []string{"other-value"},
  110. expected: false,
  111. },
  112. {
  113. name: "StringLike - wildcard match",
  114. operator: "StringLike",
  115. conditionValue: "test-*",
  116. contextValues: []string{"test-value"},
  117. expected: true,
  118. },
  119. {
  120. name: "StringLike - wildcard no match",
  121. operator: "StringLike",
  122. conditionValue: "test-*",
  123. contextValues: []string{"other-value"},
  124. expected: false,
  125. },
  126. {
  127. name: "NumericEquals - match",
  128. operator: "NumericEquals",
  129. conditionValue: "42",
  130. contextValues: []string{"42"},
  131. expected: true,
  132. },
  133. {
  134. name: "NumericLessThan - match",
  135. operator: "NumericLessThan",
  136. conditionValue: "100",
  137. contextValues: []string{"50"},
  138. expected: true,
  139. },
  140. {
  141. name: "NumericLessThan - no match",
  142. operator: "NumericLessThan",
  143. conditionValue: "100",
  144. contextValues: []string{"150"},
  145. expected: false,
  146. },
  147. {
  148. name: "IpAddress - CIDR match",
  149. operator: "IpAddress",
  150. conditionValue: "192.168.1.0/24",
  151. contextValues: []string{"192.168.1.100"},
  152. expected: true,
  153. },
  154. {
  155. name: "IpAddress - CIDR no match",
  156. operator: "IpAddress",
  157. conditionValue: "192.168.1.0/24",
  158. contextValues: []string{"10.0.0.1"},
  159. expected: false,
  160. },
  161. {
  162. name: "Bool - true match",
  163. operator: "Bool",
  164. conditionValue: "true",
  165. contextValues: []string{"true"},
  166. expected: true,
  167. },
  168. {
  169. name: "Bool - false match",
  170. operator: "Bool",
  171. conditionValue: "false",
  172. contextValues: []string{"false"},
  173. expected: true,
  174. },
  175. {
  176. name: "Bool - no match",
  177. operator: "Bool",
  178. conditionValue: "true",
  179. contextValues: []string{"false"},
  180. expected: false,
  181. },
  182. }
  183. for _, tt := range tests {
  184. t.Run(tt.name, func(t *testing.T) {
  185. evaluator, err := GetConditionEvaluator(tt.operator)
  186. if err != nil {
  187. t.Fatalf("Failed to get condition evaluator: %v", err)
  188. }
  189. result := evaluator.Evaluate(tt.conditionValue, tt.contextValues)
  190. if result != tt.expected {
  191. t.Errorf("Expected %v, got %v", tt.expected, result)
  192. }
  193. })
  194. }
  195. }
  196. func TestConvertIdentityToPolicy(t *testing.T) {
  197. identityActions := []string{
  198. "Read:bucket1/*",
  199. "Write:bucket1/*",
  200. "Admin:bucket2",
  201. }
  202. policy, err := ConvertIdentityToPolicy(identityActions, "bucket1")
  203. if err != nil {
  204. t.Fatalf("Failed to convert identity to policy: %v", err)
  205. }
  206. if policy.Version != "2012-10-17" {
  207. t.Errorf("Expected version 2012-10-17, got %s", policy.Version)
  208. }
  209. if len(policy.Statement) != 3 {
  210. t.Errorf("Expected 3 statements, got %d", len(policy.Statement))
  211. }
  212. // Check first statement (Read)
  213. stmt := policy.Statement[0]
  214. if stmt.Effect != PolicyEffectAllow {
  215. t.Errorf("Expected Allow effect, got %s", stmt.Effect)
  216. }
  217. actions := normalizeToStringSlice(stmt.Action)
  218. if len(actions) != 3 {
  219. t.Errorf("Expected 3 read actions, got %d", len(actions))
  220. }
  221. resources := normalizeToStringSlice(stmt.Resource)
  222. if len(resources) != 2 {
  223. t.Errorf("Expected 2 resources, got %d", len(resources))
  224. }
  225. }
  226. func TestPolicyValidation(t *testing.T) {
  227. tests := []struct {
  228. name string
  229. policyJSON string
  230. expectError bool
  231. }{
  232. {
  233. name: "Valid policy",
  234. policyJSON: `{
  235. "Version": "2012-10-17",
  236. "Statement": [
  237. {
  238. "Effect": "Allow",
  239. "Action": "s3:GetObject",
  240. "Resource": "arn:aws:s3:::test-bucket/*"
  241. }
  242. ]
  243. }`,
  244. expectError: false,
  245. },
  246. {
  247. name: "Invalid version",
  248. policyJSON: `{
  249. "Version": "2008-10-17",
  250. "Statement": [
  251. {
  252. "Effect": "Allow",
  253. "Action": "s3:GetObject",
  254. "Resource": "arn:aws:s3:::test-bucket/*"
  255. }
  256. ]
  257. }`,
  258. expectError: true,
  259. },
  260. {
  261. name: "Missing action",
  262. policyJSON: `{
  263. "Version": "2012-10-17",
  264. "Statement": [
  265. {
  266. "Effect": "Allow",
  267. "Resource": "arn:aws:s3:::test-bucket/*"
  268. }
  269. ]
  270. }`,
  271. expectError: true,
  272. },
  273. {
  274. name: "Invalid JSON",
  275. policyJSON: `{
  276. "Version": "2012-10-17",
  277. "Statement": [
  278. {
  279. "Effect": "Allow",
  280. "Action": "s3:GetObject",
  281. "Resource": "arn:aws:s3:::test-bucket/*"
  282. }
  283. ]
  284. }extra`,
  285. expectError: true,
  286. },
  287. }
  288. for _, tt := range tests {
  289. t.Run(tt.name, func(t *testing.T) {
  290. _, err := ParsePolicy(tt.policyJSON)
  291. if (err != nil) != tt.expectError {
  292. t.Errorf("Expected error: %v, got error: %v", tt.expectError, err)
  293. }
  294. })
  295. }
  296. }
  297. func TestPatternMatching(t *testing.T) {
  298. tests := []struct {
  299. name string
  300. pattern string
  301. value string
  302. expected bool
  303. }{
  304. {
  305. name: "Exact match",
  306. pattern: "s3:GetObject",
  307. value: "s3:GetObject",
  308. expected: true,
  309. },
  310. {
  311. name: "Wildcard match",
  312. pattern: "s3:Get*",
  313. value: "s3:GetObject",
  314. expected: true,
  315. },
  316. {
  317. name: "Wildcard no match",
  318. pattern: "s3:Put*",
  319. value: "s3:GetObject",
  320. expected: false,
  321. },
  322. {
  323. name: "Full wildcard",
  324. pattern: "*",
  325. value: "anything",
  326. expected: true,
  327. },
  328. {
  329. name: "Question mark wildcard",
  330. pattern: "s3:GetObjec?",
  331. value: "s3:GetObject",
  332. expected: true,
  333. },
  334. }
  335. for _, tt := range tests {
  336. t.Run(tt.name, func(t *testing.T) {
  337. compiled, err := compilePattern(tt.pattern)
  338. if err != nil {
  339. t.Fatalf("Failed to compile pattern %s: %v", tt.pattern, err)
  340. }
  341. result := compiled.MatchString(tt.value)
  342. if result != tt.expected {
  343. t.Errorf("Pattern %s against %s: expected %v, got %v", tt.pattern, tt.value, tt.expected, result)
  344. }
  345. })
  346. }
  347. }
  348. func TestExtractConditionValuesFromRequest(t *testing.T) {
  349. // Create a test request
  350. req := &http.Request{
  351. Method: "GET",
  352. URL: &url.URL{
  353. Path: "/test-bucket/test-object",
  354. RawQuery: "prefix=test&delimiter=/",
  355. },
  356. Header: map[string][]string{
  357. "User-Agent": {"test-agent"},
  358. "X-Amz-Copy-Source": {"source-bucket/source-object"},
  359. },
  360. RemoteAddr: "192.168.1.100:12345",
  361. }
  362. values := ExtractConditionValuesFromRequest(req)
  363. // Check extracted values
  364. if len(values["aws:SourceIp"]) != 1 || values["aws:SourceIp"][0] != "192.168.1.100" {
  365. t.Errorf("Expected SourceIp to be 192.168.1.100, got %v", values["aws:SourceIp"])
  366. }
  367. if len(values["aws:UserAgent"]) != 1 || values["aws:UserAgent"][0] != "test-agent" {
  368. t.Errorf("Expected UserAgent to be test-agent, got %v", values["aws:UserAgent"])
  369. }
  370. if len(values["s3:prefix"]) != 1 || values["s3:prefix"][0] != "test" {
  371. t.Errorf("Expected prefix to be test, got %v", values["s3:prefix"])
  372. }
  373. if len(values["s3:delimiter"]) != 1 || values["s3:delimiter"][0] != "/" {
  374. t.Errorf("Expected delimiter to be /, got %v", values["s3:delimiter"])
  375. }
  376. if len(values["s3:RequestMethod"]) != 1 || values["s3:RequestMethod"][0] != "GET" {
  377. t.Errorf("Expected RequestMethod to be GET, got %v", values["s3:RequestMethod"])
  378. }
  379. if len(values["x-amz-copy-source"]) != 1 || values["x-amz-copy-source"][0] != "source-bucket/source-object" {
  380. t.Errorf("Expected X-Amz-Copy-Source header to be extracted, got %v", values["x-amz-copy-source"])
  381. }
  382. // Check that aws:CurrentTime is properly set
  383. if len(values["aws:CurrentTime"]) != 1 {
  384. t.Errorf("Expected aws:CurrentTime to be set, got %v", values["aws:CurrentTime"])
  385. }
  386. // Check that aws:RequestTime is still available for backward compatibility
  387. if len(values["aws:RequestTime"]) != 1 {
  388. t.Errorf("Expected aws:RequestTime to be set for backward compatibility, got %v", values["aws:RequestTime"])
  389. }
  390. }
  391. func TestPolicyEvaluationWithConditions(t *testing.T) {
  392. engine := NewPolicyEngine()
  393. // Policy with IP condition
  394. policyJSON := `{
  395. "Version": "2012-10-17",
  396. "Statement": [
  397. {
  398. "Effect": "Allow",
  399. "Action": "s3:GetObject",
  400. "Resource": "arn:aws:s3:::test-bucket/*",
  401. "Condition": {
  402. "IpAddress": {
  403. "aws:SourceIp": "192.168.1.0/24"
  404. }
  405. }
  406. }
  407. ]
  408. }`
  409. err := engine.SetBucketPolicy("test-bucket", policyJSON)
  410. if err != nil {
  411. t.Fatalf("Failed to set bucket policy: %v", err)
  412. }
  413. // Test matching IP
  414. args := &PolicyEvaluationArgs{
  415. Action: "s3:GetObject",
  416. Resource: "arn:aws:s3:::test-bucket/test-object",
  417. Principal: "user1",
  418. Conditions: map[string][]string{
  419. "aws:SourceIp": {"192.168.1.100"},
  420. },
  421. }
  422. result := engine.EvaluatePolicy("test-bucket", args)
  423. if result != PolicyResultAllow {
  424. t.Errorf("Expected Allow for matching IP, got %v", result)
  425. }
  426. // Test non-matching IP
  427. args.Conditions["aws:SourceIp"] = []string{"10.0.0.1"}
  428. result = engine.EvaluatePolicy("test-bucket", args)
  429. if result != PolicyResultDeny {
  430. t.Errorf("Expected Deny for non-matching IP, got %v", result)
  431. }
  432. }
  433. func TestResourceArn(t *testing.T) {
  434. tests := []struct {
  435. name string
  436. bucketName string
  437. objectName string
  438. expected string
  439. }{
  440. {
  441. name: "Bucket only",
  442. bucketName: "test-bucket",
  443. objectName: "",
  444. expected: "arn:aws:s3:::test-bucket",
  445. },
  446. {
  447. name: "Bucket and object",
  448. bucketName: "test-bucket",
  449. objectName: "test-object",
  450. expected: "arn:aws:s3:::test-bucket/test-object",
  451. },
  452. {
  453. name: "Bucket and nested object",
  454. bucketName: "test-bucket",
  455. objectName: "folder/subfolder/test-object",
  456. expected: "arn:aws:s3:::test-bucket/folder/subfolder/test-object",
  457. },
  458. }
  459. for _, tt := range tests {
  460. t.Run(tt.name, func(t *testing.T) {
  461. result := BuildResourceArn(tt.bucketName, tt.objectName)
  462. if result != tt.expected {
  463. t.Errorf("Expected %s, got %s", tt.expected, result)
  464. }
  465. })
  466. }
  467. }
  468. func TestActionConversion(t *testing.T) {
  469. tests := []struct {
  470. name string
  471. action string
  472. expected string
  473. }{
  474. {
  475. name: "Already has s3 prefix",
  476. action: "s3:GetObject",
  477. expected: "s3:GetObject",
  478. },
  479. {
  480. name: "Add s3 prefix",
  481. action: "GetObject",
  482. expected: "s3:GetObject",
  483. },
  484. }
  485. for _, tt := range tests {
  486. t.Run(tt.name, func(t *testing.T) {
  487. result := BuildActionName(tt.action)
  488. if result != tt.expected {
  489. t.Errorf("Expected %s, got %s", tt.expected, result)
  490. }
  491. })
  492. }
  493. }
  494. func TestPolicyEngineForRequest(t *testing.T) {
  495. engine := NewPolicyEngine()
  496. // Set up a policy
  497. policyJSON := `{
  498. "Version": "2012-10-17",
  499. "Statement": [
  500. {
  501. "Effect": "Allow",
  502. "Action": "s3:GetObject",
  503. "Resource": "arn:aws:s3:::test-bucket/*",
  504. "Condition": {
  505. "StringEquals": {
  506. "s3:RequestMethod": "GET"
  507. }
  508. }
  509. }
  510. ]
  511. }`
  512. err := engine.SetBucketPolicy("test-bucket", policyJSON)
  513. if err != nil {
  514. t.Fatalf("Failed to set bucket policy: %v", err)
  515. }
  516. // Create test request
  517. req := &http.Request{
  518. Method: "GET",
  519. URL: &url.URL{
  520. Path: "/test-bucket/test-object",
  521. },
  522. Header: make(map[string][]string),
  523. RemoteAddr: "192.168.1.100:12345",
  524. }
  525. // Test the request
  526. result := engine.EvaluatePolicyForRequest("test-bucket", "test-object", "GetObject", "user1", req)
  527. if result != PolicyResultAllow {
  528. t.Errorf("Expected Allow for matching request, got %v", result)
  529. }
  530. }
  531. func TestWildcardMatching(t *testing.T) {
  532. tests := []struct {
  533. name string
  534. pattern string
  535. str string
  536. expected bool
  537. }{
  538. {
  539. name: "Exact match",
  540. pattern: "test",
  541. str: "test",
  542. expected: true,
  543. },
  544. {
  545. name: "Single wildcard",
  546. pattern: "*",
  547. str: "anything",
  548. expected: true,
  549. },
  550. {
  551. name: "Prefix wildcard",
  552. pattern: "test*",
  553. str: "test123",
  554. expected: true,
  555. },
  556. {
  557. name: "Suffix wildcard",
  558. pattern: "*test",
  559. str: "123test",
  560. expected: true,
  561. },
  562. {
  563. name: "Middle wildcard",
  564. pattern: "test*123",
  565. str: "testABC123",
  566. expected: true,
  567. },
  568. {
  569. name: "No match",
  570. pattern: "test*",
  571. str: "other",
  572. expected: false,
  573. },
  574. {
  575. name: "Multiple wildcards",
  576. pattern: "test*abc*123",
  577. str: "testXYZabcDEF123",
  578. expected: true,
  579. },
  580. }
  581. for _, tt := range tests {
  582. t.Run(tt.name, func(t *testing.T) {
  583. result := MatchesWildcard(tt.pattern, tt.str)
  584. if result != tt.expected {
  585. t.Errorf("Pattern %s against %s: expected %v, got %v", tt.pattern, tt.str, tt.expected, result)
  586. }
  587. })
  588. }
  589. }
  590. func TestCompilePolicy(t *testing.T) {
  591. policyJSON := `{
  592. "Version": "2012-10-17",
  593. "Statement": [
  594. {
  595. "Effect": "Allow",
  596. "Action": ["s3:GetObject", "s3:PutObject"],
  597. "Resource": "arn:aws:s3:::test-bucket/*"
  598. }
  599. ]
  600. }`
  601. policy, err := ParsePolicy(policyJSON)
  602. if err != nil {
  603. t.Fatalf("Failed to parse policy: %v", err)
  604. }
  605. compiled, err := CompilePolicy(policy)
  606. if err != nil {
  607. t.Fatalf("Failed to compile policy: %v", err)
  608. }
  609. if len(compiled.Statements) != 1 {
  610. t.Errorf("Expected 1 compiled statement, got %d", len(compiled.Statements))
  611. }
  612. stmt := compiled.Statements[0]
  613. if len(stmt.ActionPatterns) != 2 {
  614. t.Errorf("Expected 2 action patterns, got %d", len(stmt.ActionPatterns))
  615. }
  616. if len(stmt.ResourcePatterns) != 1 {
  617. t.Errorf("Expected 1 resource pattern, got %d", len(stmt.ResourcePatterns))
  618. }
  619. }
  620. // TestNewPolicyBackedIAMWithLegacy tests the constructor overload
  621. func TestNewPolicyBackedIAMWithLegacy(t *testing.T) {
  622. // Mock legacy IAM
  623. mockLegacyIAM := &MockLegacyIAM{}
  624. // Test the new constructor
  625. policyBackedIAM := NewPolicyBackedIAMWithLegacy(mockLegacyIAM)
  626. // Verify that the legacy IAM is set
  627. if policyBackedIAM.legacyIAM != mockLegacyIAM {
  628. t.Errorf("Expected legacy IAM to be set, but it wasn't")
  629. }
  630. // Verify that the policy engine is initialized
  631. if policyBackedIAM.policyEngine == nil {
  632. t.Errorf("Expected policy engine to be initialized, but it wasn't")
  633. }
  634. // Compare with the traditional approach
  635. traditionalIAM := NewPolicyBackedIAM()
  636. traditionalIAM.SetLegacyIAM(mockLegacyIAM)
  637. // Both should behave the same
  638. if policyBackedIAM.legacyIAM != traditionalIAM.legacyIAM {
  639. t.Errorf("Expected both approaches to result in the same legacy IAM")
  640. }
  641. }
  642. // MockLegacyIAM implements the LegacyIAM interface for testing
  643. type MockLegacyIAM struct{}
  644. func (m *MockLegacyIAM) authRequest(r *http.Request, action Action) (Identity, s3err.ErrorCode) {
  645. return nil, s3err.ErrNone
  646. }