acl_entity_test.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package acl
  2. import (
  3. "testing"
  4. authpublic "github.com/OliveTin/OliveTin/internal/auth/authpublic"
  5. config "github.com/OliveTin/OliveTin/internal/config"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func TestIsAllowedViewEntityTypeAbsentAclsUnrestricted(t *testing.T) {
  9. cfg := config.DefaultConfig()
  10. cfg.DefaultPermissions.View = false
  11. entityFile := &config.EntityFile{
  12. Name: "printers",
  13. File: "printers.yaml",
  14. }
  15. guest := &authpublic.AuthenticatedUser{Username: "guest", Provider: "system"}
  16. guest.BuildUserAcls(cfg)
  17. assert.True(t, IsAllowedViewEntityType(cfg, guest, entityFile))
  18. assert.True(t, IsAllowedViewEntityType(cfg, guest, nil))
  19. }
  20. func TestIsAllowedViewEntityTypeAllowDenyAndDefaultFallback(t *testing.T) {
  21. cfg := config.DefaultConfig()
  22. cfg.DefaultPermissions.View = false
  23. cfg.AccessControlLists = []*config.AccessControlList{
  24. {
  25. Name: "ops",
  26. MatchUsernames: []string{"admin"},
  27. Permissions: config.PermissionsList{View: true, Exec: true},
  28. },
  29. }
  30. entityFile := &config.EntityFile{
  31. Name: "servers",
  32. File: "servers.yaml",
  33. Acls: []string{"ops"},
  34. }
  35. guest := &authpublic.AuthenticatedUser{Username: "guest", Provider: "system"}
  36. guest.BuildUserAcls(cfg)
  37. admin := &authpublic.AuthenticatedUser{Username: "admin"}
  38. admin.BuildUserAcls(cfg)
  39. assert.False(t, IsAllowedViewEntityType(cfg, guest, entityFile))
  40. assert.True(t, IsAllowedViewEntityType(cfg, admin, entityFile))
  41. cfg.DefaultPermissions.View = true
  42. assert.True(t, IsAllowedViewEntityType(cfg, guest, entityFile),
  43. "when no relevant ACL matches, fall back to defaultPermissions.view")
  44. }
  45. func TestIsAllowedViewEntityTypeIgnoresAddToEveryAction(t *testing.T) {
  46. cfg := config.DefaultConfig()
  47. cfg.DefaultPermissions.View = false
  48. cfg.AccessControlLists = []*config.AccessControlList{
  49. {
  50. Name: "admins",
  51. MatchUsernames: []string{"admin"},
  52. AddToEveryAction: true,
  53. Permissions: config.PermissionsList{View: true, Exec: true},
  54. },
  55. }
  56. entityFile := &config.EntityFile{
  57. Name: "secret",
  58. File: "secret.yaml",
  59. Acls: []string{"other"},
  60. }
  61. admin := &authpublic.AuthenticatedUser{Username: "admin"}
  62. admin.BuildUserAcls(cfg)
  63. assert.False(t, IsAllowedViewEntityType(cfg, admin, entityFile),
  64. "AddToEveryAction must not grant entity view without listing the ACL on the entity")
  65. }