|
| 1 | +// Copyright (c) 2026 Feng Ruohang |
| 2 | +// SPDX-License-Identifier: AGPL-3.0-or-later |
| 3 | + |
| 4 | +package policy |
| 5 | + |
| 6 | +import ( |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | +) |
| 10 | + |
| 11 | +func TestPasswordAndUserManagementActions(t *testing.T) { |
| 12 | + for _, tt := range []struct { |
| 13 | + name string |
| 14 | + statements string |
| 15 | + password bool |
| 16 | + createUser bool |
| 17 | + }{ |
| 18 | + {"implicit self service", `{"Effect":"Allow","Action":"s3:GetObject","Resource":"arn:aws:s3:::*"}`, true, false}, |
| 19 | + {"explicit password grant", `{"Effect":"Allow","Action":"admin:ChangeMyPassword"}`, true, false}, |
| 20 | + {"explicit user management grant", `{"Effect":"Allow","Action":"admin:CreateUser"}`, true, true}, |
| 21 | + {"legacy user management deny", `{"Effect":"Deny","Action":"admin:CreateUser","Resource":"arn:aws:s3:::*"}`, true, false}, |
| 22 | + {"password deny", `{"Effect":"Deny","Action":"admin:ChangeMyPassword"}`, false, false}, |
| 23 | + {"password deny with user management grant", `{"Effect":"Deny","Action":"admin:ChangeMyPassword"},{"Effect":"Allow","Action":"admin:CreateUser"}`, false, true}, |
| 24 | + {"deny overrides allow", `{"Effect":"Allow","Action":"admin:ChangeMyPassword"},{"Effect":"Deny","Action":"admin:ChangeMyPassword"}`, false, false}, |
| 25 | + {"wildcard deny", `{"Effect":"Deny","Action":"admin:*"}`, false, false}, |
| 26 | + {"both denies", `{"Effect":"Deny","Action":["admin:CreateUser","admin:ChangeMyPassword"]}`, false, false}, |
| 27 | + } { |
| 28 | + t.Run(tt.name, func(t *testing.T) { |
| 29 | + doc := `{"Version":"2012-10-17","Statement":[` + tt.statements + `]}` |
| 30 | + for _, mode := range []string{"read", "write", "merge"} { |
| 31 | + t.Run(mode, func(t *testing.T) { |
| 32 | + parser := ParseConfig |
| 33 | + if mode == "write" { |
| 34 | + parser = ParseConfigStrict |
| 35 | + } |
| 36 | + p, err := parser(strings.NewReader(doc)) |
| 37 | + if err != nil { |
| 38 | + t.Fatal(err) |
| 39 | + } |
| 40 | + if mode == "merge" { |
| 41 | + merged := MergePolicies(*p, Policy{}) |
| 42 | + p = &merged |
| 43 | + } |
| 44 | + actions := p.IsAllowedActions("", "", nil) |
| 45 | + for action, want := range map[Action]bool{ |
| 46 | + ChangeMyPasswordAdminAction: tt.password, |
| 47 | + CreateUserAdminAction: tt.createUser, |
| 48 | + } { |
| 49 | + if got := actions.Contains(action); got != want { |
| 50 | + t.Errorf("reported %s = %v, want %v", action, got, want) |
| 51 | + } |
| 52 | + if got := p.IsAllowed(Args{Action: action, DenyOnly: action == ChangeMyPasswordAdminAction}); got != want { |
| 53 | + t.Errorf("authorized %s = %v, want %v", action, got, want) |
| 54 | + } |
| 55 | + } |
| 56 | + }) |
| 57 | + } |
| 58 | + }) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func TestReadOnlySelfServicePolicies(t *testing.T) { |
| 63 | + for _, name := range []string{"readonly", "consolereadonly"} { |
| 64 | + t.Run(name, func(t *testing.T) { |
| 65 | + var p *Policy |
| 66 | + for _, canned := range DefaultPolicies { |
| 67 | + if canned.Name == name { |
| 68 | + p = &canned.Definition |
| 69 | + break |
| 70 | + } |
| 71 | + } |
| 72 | + if p == nil { |
| 73 | + t.Fatal("missing canned policy") |
| 74 | + } |
| 75 | + actions := p.IsAllowedActions("bucket", "object", nil) |
| 76 | + for action, want := range map[Action]bool{ |
| 77 | + GetObjectAction: true, |
| 78 | + GetBucketLocationAction: true, |
| 79 | + ListBucketAction: name == "consolereadonly", |
| 80 | + PutObjectAction: false, |
| 81 | + DeleteObjectAction: false, |
| 82 | + CreateUserAdminAction: false, |
| 83 | + ChangeMyPasswordAdminAction: true, |
| 84 | + CreateServiceAccountAdminAction: true, |
| 85 | + } { |
| 86 | + if got := actions.Contains(action); got != want { |
| 87 | + t.Errorf("%s = %v, want %v", action, got, want) |
| 88 | + } |
| 89 | + } |
| 90 | + // A read-only grant must compose with a separate user-admin policy. |
| 91 | + userAdmin := Policy{Version: DefaultVersion, Statements: []Statement{{ |
| 92 | + Effect: Allow, Actions: NewActionSet(CreateUserAdminAction), |
| 93 | + }}} |
| 94 | + merged := MergePolicies(*p, userAdmin) |
| 95 | + if !merged.IsAllowed(Args{Action: CreateUserAdminAction}) { |
| 96 | + t.Error("read-only policy overrides an independent user-management grant") |
| 97 | + } |
| 98 | + }) |
| 99 | + } |
| 100 | +} |
0 commit comments