policy_engine_test.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. package policy
  2. import (
  3. "context"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/stretchr/testify/require"
  7. )
  8. // TestPolicyEngineInitialization tests policy engine initialization
  9. func TestPolicyEngineInitialization(t *testing.T) {
  10. tests := []struct {
  11. name string
  12. config *PolicyEngineConfig
  13. wantErr bool
  14. }{
  15. {
  16. name: "valid config",
  17. config: &PolicyEngineConfig{
  18. DefaultEffect: "Deny",
  19. StoreType: "memory",
  20. },
  21. wantErr: false,
  22. },
  23. {
  24. name: "invalid default effect",
  25. config: &PolicyEngineConfig{
  26. DefaultEffect: "Invalid",
  27. StoreType: "memory",
  28. },
  29. wantErr: true,
  30. },
  31. {
  32. name: "nil config",
  33. config: nil,
  34. wantErr: true,
  35. },
  36. }
  37. for _, tt := range tests {
  38. t.Run(tt.name, func(t *testing.T) {
  39. engine := NewPolicyEngine()
  40. err := engine.Initialize(tt.config)
  41. if tt.wantErr {
  42. assert.Error(t, err)
  43. } else {
  44. assert.NoError(t, err)
  45. assert.True(t, engine.IsInitialized())
  46. }
  47. })
  48. }
  49. }
  50. // TestPolicyDocumentValidation tests policy document structure validation
  51. func TestPolicyDocumentValidation(t *testing.T) {
  52. tests := []struct {
  53. name string
  54. policy *PolicyDocument
  55. wantErr bool
  56. errorMsg string
  57. }{
  58. {
  59. name: "valid policy document",
  60. policy: &PolicyDocument{
  61. Version: "2012-10-17",
  62. Statement: []Statement{
  63. {
  64. Sid: "AllowS3Read",
  65. Effect: "Allow",
  66. Action: []string{"s3:GetObject", "s3:ListBucket"},
  67. Resource: []string{"arn:seaweed:s3:::mybucket/*"},
  68. },
  69. },
  70. },
  71. wantErr: false,
  72. },
  73. {
  74. name: "missing version",
  75. policy: &PolicyDocument{
  76. Statement: []Statement{
  77. {
  78. Effect: "Allow",
  79. Action: []string{"s3:GetObject"},
  80. Resource: []string{"arn:seaweed:s3:::mybucket/*"},
  81. },
  82. },
  83. },
  84. wantErr: true,
  85. errorMsg: "version is required",
  86. },
  87. {
  88. name: "empty statements",
  89. policy: &PolicyDocument{
  90. Version: "2012-10-17",
  91. Statement: []Statement{},
  92. },
  93. wantErr: true,
  94. errorMsg: "at least one statement is required",
  95. },
  96. {
  97. name: "invalid effect",
  98. policy: &PolicyDocument{
  99. Version: "2012-10-17",
  100. Statement: []Statement{
  101. {
  102. Effect: "Maybe",
  103. Action: []string{"s3:GetObject"},
  104. Resource: []string{"arn:seaweed:s3:::mybucket/*"},
  105. },
  106. },
  107. },
  108. wantErr: true,
  109. errorMsg: "invalid effect",
  110. },
  111. }
  112. for _, tt := range tests {
  113. t.Run(tt.name, func(t *testing.T) {
  114. err := ValidatePolicyDocument(tt.policy)
  115. if tt.wantErr {
  116. assert.Error(t, err)
  117. if tt.errorMsg != "" {
  118. assert.Contains(t, err.Error(), tt.errorMsg)
  119. }
  120. } else {
  121. assert.NoError(t, err)
  122. }
  123. })
  124. }
  125. }
  126. // TestPolicyEvaluation tests policy evaluation logic
  127. func TestPolicyEvaluation(t *testing.T) {
  128. engine := setupTestPolicyEngine(t)
  129. // Add test policies
  130. readPolicy := &PolicyDocument{
  131. Version: "2012-10-17",
  132. Statement: []Statement{
  133. {
  134. Sid: "AllowS3Read",
  135. Effect: "Allow",
  136. Action: []string{"s3:GetObject", "s3:ListBucket"},
  137. Resource: []string{
  138. "arn:seaweed:s3:::public-bucket/*", // For object operations
  139. "arn:seaweed:s3:::public-bucket", // For bucket operations
  140. },
  141. },
  142. },
  143. }
  144. err := engine.AddPolicy("", "read-policy", readPolicy)
  145. require.NoError(t, err)
  146. denyPolicy := &PolicyDocument{
  147. Version: "2012-10-17",
  148. Statement: []Statement{
  149. {
  150. Sid: "DenyS3Delete",
  151. Effect: "Deny",
  152. Action: []string{"s3:DeleteObject"},
  153. Resource: []string{"arn:seaweed:s3:::*"},
  154. },
  155. },
  156. }
  157. err = engine.AddPolicy("", "deny-policy", denyPolicy)
  158. require.NoError(t, err)
  159. tests := []struct {
  160. name string
  161. context *EvaluationContext
  162. policies []string
  163. want Effect
  164. }{
  165. {
  166. name: "allow read access",
  167. context: &EvaluationContext{
  168. Principal: "user:alice",
  169. Action: "s3:GetObject",
  170. Resource: "arn:seaweed:s3:::public-bucket/file.txt",
  171. RequestContext: map[string]interface{}{
  172. "sourceIP": "192.168.1.100",
  173. },
  174. },
  175. policies: []string{"read-policy"},
  176. want: EffectAllow,
  177. },
  178. {
  179. name: "deny delete access (explicit deny)",
  180. context: &EvaluationContext{
  181. Principal: "user:alice",
  182. Action: "s3:DeleteObject",
  183. Resource: "arn:seaweed:s3:::public-bucket/file.txt",
  184. },
  185. policies: []string{"read-policy", "deny-policy"},
  186. want: EffectDeny,
  187. },
  188. {
  189. name: "deny by default (no matching policy)",
  190. context: &EvaluationContext{
  191. Principal: "user:alice",
  192. Action: "s3:PutObject",
  193. Resource: "arn:seaweed:s3:::public-bucket/file.txt",
  194. },
  195. policies: []string{"read-policy"},
  196. want: EffectDeny,
  197. },
  198. {
  199. name: "allow with wildcard action",
  200. context: &EvaluationContext{
  201. Principal: "user:admin",
  202. Action: "s3:ListBucket",
  203. Resource: "arn:seaweed:s3:::public-bucket",
  204. },
  205. policies: []string{"read-policy"},
  206. want: EffectAllow,
  207. },
  208. }
  209. for _, tt := range tests {
  210. t.Run(tt.name, func(t *testing.T) {
  211. result, err := engine.Evaluate(context.Background(), "", tt.context, tt.policies)
  212. assert.NoError(t, err)
  213. assert.Equal(t, tt.want, result.Effect)
  214. // Verify evaluation details
  215. assert.NotNil(t, result.EvaluationDetails)
  216. assert.Equal(t, tt.context.Action, result.EvaluationDetails.Action)
  217. assert.Equal(t, tt.context.Resource, result.EvaluationDetails.Resource)
  218. })
  219. }
  220. }
  221. // TestConditionEvaluation tests policy conditions
  222. func TestConditionEvaluation(t *testing.T) {
  223. engine := setupTestPolicyEngine(t)
  224. // Policy with IP address condition
  225. conditionalPolicy := &PolicyDocument{
  226. Version: "2012-10-17",
  227. Statement: []Statement{
  228. {
  229. Sid: "AllowFromOfficeIP",
  230. Effect: "Allow",
  231. Action: []string{"s3:*"},
  232. Resource: []string{"arn:seaweed:s3:::*"},
  233. Condition: map[string]map[string]interface{}{
  234. "IpAddress": {
  235. "seaweed:SourceIP": []string{"192.168.1.0/24", "10.0.0.0/8"},
  236. },
  237. },
  238. },
  239. },
  240. }
  241. err := engine.AddPolicy("", "ip-conditional", conditionalPolicy)
  242. require.NoError(t, err)
  243. tests := []struct {
  244. name string
  245. context *EvaluationContext
  246. want Effect
  247. }{
  248. {
  249. name: "allow from office IP",
  250. context: &EvaluationContext{
  251. Principal: "user:alice",
  252. Action: "s3:GetObject",
  253. Resource: "arn:seaweed:s3:::mybucket/file.txt",
  254. RequestContext: map[string]interface{}{
  255. "sourceIP": "192.168.1.100",
  256. },
  257. },
  258. want: EffectAllow,
  259. },
  260. {
  261. name: "deny from external IP",
  262. context: &EvaluationContext{
  263. Principal: "user:alice",
  264. Action: "s3:GetObject",
  265. Resource: "arn:seaweed:s3:::mybucket/file.txt",
  266. RequestContext: map[string]interface{}{
  267. "sourceIP": "8.8.8.8",
  268. },
  269. },
  270. want: EffectDeny,
  271. },
  272. {
  273. name: "allow from internal IP",
  274. context: &EvaluationContext{
  275. Principal: "user:alice",
  276. Action: "s3:PutObject",
  277. Resource: "arn:seaweed:s3:::mybucket/newfile.txt",
  278. RequestContext: map[string]interface{}{
  279. "sourceIP": "10.1.2.3",
  280. },
  281. },
  282. want: EffectAllow,
  283. },
  284. }
  285. for _, tt := range tests {
  286. t.Run(tt.name, func(t *testing.T) {
  287. result, err := engine.Evaluate(context.Background(), "", tt.context, []string{"ip-conditional"})
  288. assert.NoError(t, err)
  289. assert.Equal(t, tt.want, result.Effect)
  290. })
  291. }
  292. }
  293. // TestResourceMatching tests resource ARN matching
  294. func TestResourceMatching(t *testing.T) {
  295. tests := []struct {
  296. name string
  297. policyResource string
  298. requestResource string
  299. want bool
  300. }{
  301. {
  302. name: "exact match",
  303. policyResource: "arn:seaweed:s3:::mybucket/file.txt",
  304. requestResource: "arn:seaweed:s3:::mybucket/file.txt",
  305. want: true,
  306. },
  307. {
  308. name: "wildcard match",
  309. policyResource: "arn:seaweed:s3:::mybucket/*",
  310. requestResource: "arn:seaweed:s3:::mybucket/folder/file.txt",
  311. want: true,
  312. },
  313. {
  314. name: "bucket wildcard",
  315. policyResource: "arn:seaweed:s3:::*",
  316. requestResource: "arn:seaweed:s3:::anybucket/file.txt",
  317. want: true,
  318. },
  319. {
  320. name: "no match different bucket",
  321. policyResource: "arn:seaweed:s3:::mybucket/*",
  322. requestResource: "arn:seaweed:s3:::otherbucket/file.txt",
  323. want: false,
  324. },
  325. {
  326. name: "prefix match",
  327. policyResource: "arn:seaweed:s3:::mybucket/documents/*",
  328. requestResource: "arn:seaweed:s3:::mybucket/documents/secret.txt",
  329. want: true,
  330. },
  331. }
  332. for _, tt := range tests {
  333. t.Run(tt.name, func(t *testing.T) {
  334. result := matchResource(tt.policyResource, tt.requestResource)
  335. assert.Equal(t, tt.want, result)
  336. })
  337. }
  338. }
  339. // TestActionMatching tests action pattern matching
  340. func TestActionMatching(t *testing.T) {
  341. tests := []struct {
  342. name string
  343. policyAction string
  344. requestAction string
  345. want bool
  346. }{
  347. {
  348. name: "exact match",
  349. policyAction: "s3:GetObject",
  350. requestAction: "s3:GetObject",
  351. want: true,
  352. },
  353. {
  354. name: "wildcard service",
  355. policyAction: "s3:*",
  356. requestAction: "s3:PutObject",
  357. want: true,
  358. },
  359. {
  360. name: "wildcard all",
  361. policyAction: "*",
  362. requestAction: "filer:CreateEntry",
  363. want: true,
  364. },
  365. {
  366. name: "prefix match",
  367. policyAction: "s3:Get*",
  368. requestAction: "s3:GetObject",
  369. want: true,
  370. },
  371. {
  372. name: "no match different service",
  373. policyAction: "s3:GetObject",
  374. requestAction: "filer:GetEntry",
  375. want: false,
  376. },
  377. }
  378. for _, tt := range tests {
  379. t.Run(tt.name, func(t *testing.T) {
  380. result := matchAction(tt.policyAction, tt.requestAction)
  381. assert.Equal(t, tt.want, result)
  382. })
  383. }
  384. }
  385. // Helper function to set up test policy engine
  386. func setupTestPolicyEngine(t *testing.T) *PolicyEngine {
  387. engine := NewPolicyEngine()
  388. config := &PolicyEngineConfig{
  389. DefaultEffect: "Deny",
  390. StoreType: "memory",
  391. }
  392. err := engine.Initialize(config)
  393. require.NoError(t, err)
  394. return engine
  395. }