acl.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package acl
  2. import (
  3. authpublic "github.com/OliveTin/OliveTin/internal/auth/authpublic"
  4. config "github.com/OliveTin/OliveTin/internal/config"
  5. log "github.com/sirupsen/logrus"
  6. "slices"
  7. )
  8. type PermissionBits int
  9. const (
  10. View PermissionBits = 1 << iota
  11. Exec
  12. Logs
  13. Kill
  14. )
  15. func (p PermissionBits) Has(permission PermissionBits) bool {
  16. return p&permission != 0
  17. }
  18. func logAclNotMatched(cfg *config.Config, aclFunction string, user *authpublic.AuthenticatedUser, resourceTitle string, acl *config.AccessControlList) {
  19. if cfg.LogDebugOptions.AclNotMatched {
  20. log.WithFields(log.Fields{
  21. "User": user.Username,
  22. "Resource": resourceTitle,
  23. "ACL": acl.Name,
  24. }).Debugf("%v - ACL Not Matched", aclFunction)
  25. }
  26. }
  27. func logAclMatched(cfg *config.Config, aclFunction string, user *authpublic.AuthenticatedUser, resourceTitle string, acl *config.AccessControlList) {
  28. if resourceTitle == "" {
  29. resourceTitle = "N/A"
  30. }
  31. if cfg.LogDebugOptions.AclMatched {
  32. log.WithFields(log.Fields{
  33. "User": user.Username,
  34. "Resource": resourceTitle,
  35. "ACL": acl.Name,
  36. }).Debugf("%v - Matched ACL", aclFunction)
  37. }
  38. }
  39. func logAclNoneMatched(cfg *config.Config, aclFunction string, user *authpublic.AuthenticatedUser, resourceTitle string, defaultPermission bool) {
  40. if cfg.LogDebugOptions.AclNoneMatched {
  41. log.WithFields(log.Fields{
  42. "User": user.Username,
  43. "Resource": resourceTitle,
  44. "Default": defaultPermission,
  45. }).Debugf("%v - No ACLs Matched, returning default permission", aclFunction)
  46. }
  47. }
  48. func permissionsConfigToBits(permissions config.PermissionsList) PermissionBits {
  49. type permPair struct {
  50. enabled bool
  51. bit PermissionBits
  52. }
  53. permMap := []permPair{
  54. {permissions.View, View},
  55. {permissions.Exec, Exec},
  56. {permissions.Logs, Logs},
  57. {permissions.Kill, Kill},
  58. }
  59. var ret PermissionBits
  60. for _, perm := range permMap {
  61. if perm.enabled {
  62. ret |= perm.bit
  63. }
  64. }
  65. return ret
  66. }
  67. func aclCheck(requiredPermission PermissionBits, defaultValue bool, cfg *config.Config, aclFunction string, user *authpublic.AuthenticatedUser, resourceTitle string, resourceAcls []string, includeAddToEvery bool) bool {
  68. relevantAcls := getRelevantAcls(cfg, resourceAcls, user, includeAddToEvery)
  69. if cfg.LogDebugOptions.AclCheckStarted {
  70. log.WithFields(log.Fields{
  71. "resourceTitle": resourceTitle,
  72. "username": user.Username,
  73. "usergroupLine": user.UsergroupLine,
  74. "relevantAcls": len(relevantAcls),
  75. "requiredPermission": requiredPermission,
  76. }).Debugf("ACL check - %v", aclFunction)
  77. }
  78. for _, acl := range relevantAcls {
  79. permissionBits := permissionsConfigToBits(acl.Permissions)
  80. if permissionBits.Has(requiredPermission) {
  81. logAclMatched(cfg, aclFunction, user, resourceTitle, acl)
  82. return true
  83. }
  84. logAclNotMatched(cfg, aclFunction, user, resourceTitle, acl)
  85. }
  86. logAclNoneMatched(cfg, aclFunction, user, resourceTitle, defaultValue)
  87. return defaultValue
  88. }
  89. // IsAllowedLogs checks if a AuthenticatedUser is allowed to view an action's logs
  90. func IsAllowedLogs(cfg *config.Config, user *authpublic.AuthenticatedUser, action *config.Action) bool {
  91. return aclCheck(Logs, cfg.DefaultPermissions.Logs, cfg, "isAllowedLogs", user, action.Title, action.Acls, true)
  92. }
  93. // IsAllowedExec checks if a AuthenticatedUser is allowed to execute an Action
  94. func IsAllowedExec(cfg *config.Config, user *authpublic.AuthenticatedUser, action *config.Action) bool {
  95. return aclCheck(Exec, cfg.DefaultPermissions.Exec, cfg, "isAllowedExec", user, action.Title, action.Acls, true)
  96. }
  97. // IsAllowedView checks if a User is allowed to view an Action.
  98. // Action.Hidden is not a security control — it only affects dashboard listing.
  99. func IsAllowedView(cfg *config.Config, user *authpublic.AuthenticatedUser, action *config.Action) bool {
  100. return aclCheck(View, cfg.DefaultPermissions.View, cfg, "isAllowedView", user, action.Title, action.Acls, true)
  101. }
  102. func IsAllowedKill(cfg *config.Config, user *authpublic.AuthenticatedUser, action *config.Action) bool {
  103. return aclCheck(Kill, cfg.DefaultPermissions.Kill, cfg, "isAllowedKill", user, action.Title, action.Acls, true)
  104. }
  105. // IsAllowedViewDashboard checks if a user may see a root dashboard.
  106. // Dashboards with no acls are unrestricted. AddToEveryAction does not apply.
  107. func IsAllowedViewDashboard(cfg *config.Config, user *authpublic.AuthenticatedUser, dashboard *config.DashboardComponent) bool {
  108. if dashboard == nil || len(dashboard.Acls) == 0 {
  109. return true
  110. }
  111. return aclCheck(View, cfg.DefaultPermissions.View, cfg, "isAllowedViewDashboard", user, dashboard.Title, dashboard.Acls, false)
  112. }
  113. // IsAllowedViewEntityType checks if a user may see an entity type (list, details, search).
  114. // Entity types with no acls are unrestricted. AddToEveryAction does not apply.
  115. func IsAllowedViewEntityType(cfg *config.Config, user *authpublic.AuthenticatedUser, entityFile *config.EntityFile) bool {
  116. if entityFile == nil || len(entityFile.Acls) == 0 {
  117. return true
  118. }
  119. return aclCheck(View, cfg.DefaultPermissions.View, cfg, "isAllowedViewEntityType", user, entityFile.Name, entityFile.Acls, false)
  120. }
  121. func isACLRelevant(resourceAcls []string, acl *config.AccessControlList, user *authpublic.AuthenticatedUser, includeAddToEvery bool) bool {
  122. if !slices.Contains(user.Acls, acl.Name) {
  123. return false
  124. }
  125. if includeAddToEvery && acl.AddToEveryAction {
  126. return true
  127. }
  128. return slices.Contains(resourceAcls, acl.Name)
  129. }
  130. func getRelevantAcls(cfg *config.Config, resourceAcls []string, user *authpublic.AuthenticatedUser, includeAddToEvery bool) []*config.AccessControlList {
  131. var ret []*config.AccessControlList
  132. for _, acl := range cfg.AccessControlLists {
  133. if isACLRelevant(resourceAcls, acl, user, includeAddToEvery) {
  134. ret = append(ret, acl)
  135. }
  136. }
  137. return ret
  138. }