2021-03-05 04:18:31 +00:00
|
|
|
package authorization
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
|
2021-08-11 01:04:35 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/configuration/schema"
|
|
|
|
"github.com/authelia/authelia/v4/internal/utils"
|
2021-03-05 04:18:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewAccessControlRules converts a schema.AccessControlConfiguration into an AccessControlRule slice.
|
|
|
|
func NewAccessControlRules(config schema.AccessControlConfiguration) (rules []*AccessControlRule) {
|
|
|
|
networksMap, networksCacheMap := parseSchemaNetworks(config.Networks)
|
|
|
|
|
2021-04-14 10:53:23 +00:00
|
|
|
for i, schemaRule := range config.Rules {
|
|
|
|
rules = append(rules, NewAccessControlRule(i+1, schemaRule, networksMap, networksCacheMap))
|
2021-03-05 04:18:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return rules
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewAccessControlRule parses a schema ACL and generates an internal ACL.
|
2021-04-14 10:53:23 +00:00
|
|
|
func NewAccessControlRule(pos int, rule schema.ACLRule, networksMap map[string][]*net.IPNet, networksCacheMap map[string]*net.IPNet) *AccessControlRule {
|
2022-09-26 04:33:08 +00:00
|
|
|
r := &AccessControlRule{
|
|
|
|
Position: pos,
|
2022-10-19 03:09:22 +00:00
|
|
|
Query: NewAccessControlQuery(rule.Query),
|
2022-09-26 04:33:08 +00:00
|
|
|
Methods: schemaMethodsToACL(rule.Methods),
|
|
|
|
Networks: schemaNetworksToACL(rule.Networks, networksMap, networksCacheMap),
|
|
|
|
Subjects: schemaSubjectsToACL(rule.Subjects),
|
|
|
|
Policy: StringToLevel(rule.Policy),
|
2021-03-05 04:18:31 +00:00
|
|
|
}
|
2022-09-26 04:33:08 +00:00
|
|
|
|
|
|
|
if len(r.Subjects) != 0 {
|
|
|
|
r.HasSubjects = true
|
|
|
|
}
|
|
|
|
|
2022-10-18 08:14:34 +00:00
|
|
|
ruleAddDomain(rule.Domains, r)
|
2022-09-26 04:33:08 +00:00
|
|
|
ruleAddDomainRegex(rule.DomainsRegex, r)
|
|
|
|
ruleAddResources(rule.Resources, r)
|
|
|
|
|
|
|
|
return r
|
2021-03-05 04:18:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AccessControlRule controls and represents an ACL internally.
|
|
|
|
type AccessControlRule struct {
|
2022-09-26 04:33:08 +00:00
|
|
|
HasSubjects bool
|
|
|
|
|
2021-04-14 10:53:23 +00:00
|
|
|
Position int
|
2022-06-28 02:51:05 +00:00
|
|
|
Domains []AccessControlDomain
|
2021-03-05 04:18:31 +00:00
|
|
|
Resources []AccessControlResource
|
2022-10-19 03:09:22 +00:00
|
|
|
Query []AccessControlQuery
|
2021-03-05 04:18:31 +00:00
|
|
|
Methods []string
|
|
|
|
Networks []*net.IPNet
|
|
|
|
Subjects []AccessControlSubjects
|
|
|
|
Policy Level
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsMatch returns true if all elements of an AccessControlRule match the object and subject.
|
|
|
|
func (acr *AccessControlRule) IsMatch(subject Subject, object Object) (match bool) {
|
2022-10-19 03:09:22 +00:00
|
|
|
if !acr.MatchesDomains(subject, object) {
|
2021-03-05 04:18:31 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-10-19 03:09:22 +00:00
|
|
|
if !acr.MatchesResources(subject, object) {
|
2021-03-05 04:18:31 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-10-19 03:09:22 +00:00
|
|
|
if !acr.MatchesQuery(object) {
|
2021-03-05 04:18:31 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-10-19 03:09:22 +00:00
|
|
|
if !acr.MatchesMethods(object) {
|
2021-03-05 04:18:31 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-10-19 03:09:22 +00:00
|
|
|
if !acr.MatchesNetworks(subject) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if !acr.MatchesSubjects(subject) {
|
2021-03-05 04:18:31 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-10-19 03:09:22 +00:00
|
|
|
// MatchesDomains returns true if the rule matches the domains.
|
|
|
|
func (acr *AccessControlRule) MatchesDomains(subject Subject, object Object) (matches bool) {
|
2021-03-05 04:18:31 +00:00
|
|
|
// If there are no domains in this rule then the domain condition is a match.
|
2022-10-19 03:09:22 +00:00
|
|
|
if len(acr.Domains) == 0 {
|
2021-03-05 04:18:31 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate over the domains until we find a match (return true) or until we exit the loop (return false).
|
2022-10-19 03:09:22 +00:00
|
|
|
for _, domain := range acr.Domains {
|
2021-03-05 04:18:31 +00:00
|
|
|
if domain.IsMatch(subject, object) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-10-19 03:09:22 +00:00
|
|
|
// MatchesResources returns true if the rule matches the resources.
|
|
|
|
func (acr *AccessControlRule) MatchesResources(subject Subject, object Object) (matches bool) {
|
2021-03-05 04:18:31 +00:00
|
|
|
// If there are no resources in this rule then the resource condition is a match.
|
2022-10-19 03:09:22 +00:00
|
|
|
if len(acr.Resources) == 0 {
|
2021-03-05 04:18:31 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate over the resources until we find a match (return true) or until we exit the loop (return false).
|
2022-10-19 03:09:22 +00:00
|
|
|
for _, resource := range acr.Resources {
|
2022-06-28 02:51:05 +00:00
|
|
|
if resource.IsMatch(subject, object) {
|
2021-03-05 04:18:31 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-10-19 03:09:22 +00:00
|
|
|
// MatchesQuery returns true if the rule matches the query arguments.
|
|
|
|
func (acr *AccessControlRule) MatchesQuery(object Object) (match bool) {
|
|
|
|
// If there are no query rules in this rule then the query condition is a match.
|
|
|
|
if len(acr.Query) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate over the queries until we find a match (return true) or until we exit the loop (return false).
|
|
|
|
for _, query := range acr.Query {
|
|
|
|
if query.IsMatch(object) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// MatchesMethods returns true if the rule matches the method.
|
|
|
|
func (acr *AccessControlRule) MatchesMethods(object Object) (match bool) {
|
2021-03-05 04:18:31 +00:00
|
|
|
// If there are no methods in this rule then the method condition is a match.
|
2022-10-19 03:09:22 +00:00
|
|
|
if len(acr.Methods) == 0 {
|
2021-03-05 04:18:31 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-10-19 03:09:22 +00:00
|
|
|
return utils.IsStringInSlice(object.Method, acr.Methods)
|
2021-03-05 04:18:31 +00:00
|
|
|
}
|
|
|
|
|
2022-10-19 03:09:22 +00:00
|
|
|
// MatchesNetworks returns true if the rule matches the networks.
|
|
|
|
func (acr *AccessControlRule) MatchesNetworks(subject Subject) (match bool) {
|
2021-03-05 04:18:31 +00:00
|
|
|
// If there are no networks in this rule then the network condition is a match.
|
2022-10-19 03:09:22 +00:00
|
|
|
if len(acr.Networks) == 0 {
|
2021-03-05 04:18:31 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate over the networks until we find a match (return true) or until we exit the loop (return false).
|
2022-10-19 03:09:22 +00:00
|
|
|
for _, network := range acr.Networks {
|
2021-03-05 04:18:31 +00:00
|
|
|
if network.Contains(subject.IP) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-10-19 03:09:22 +00:00
|
|
|
// MatchesSubjects returns true if the rule matches the subjects.
|
|
|
|
func (acr *AccessControlRule) MatchesSubjects(subject Subject) (match bool) {
|
2022-02-28 03:15:01 +00:00
|
|
|
if subject.IsAnonymous() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-10-19 03:09:22 +00:00
|
|
|
return acr.MatchesSubjectExact(subject)
|
2022-02-28 03:15:01 +00:00
|
|
|
}
|
|
|
|
|
2022-10-19 03:09:22 +00:00
|
|
|
// MatchesSubjectExact returns true if the rule matches the subjects exactly.
|
|
|
|
func (acr *AccessControlRule) MatchesSubjectExact(subject Subject) (match bool) {
|
2021-03-05 04:18:31 +00:00
|
|
|
// If there are no subjects in this rule then the subject condition is a match.
|
2022-10-19 03:09:22 +00:00
|
|
|
if len(acr.Subjects) == 0 {
|
2021-03-05 04:18:31 +00:00
|
|
|
return true
|
2022-02-28 03:15:01 +00:00
|
|
|
} else if subject.IsAnonymous() {
|
|
|
|
return false
|
2021-03-05 04:18:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate over the subjects until we find a match (return true) or until we exit the loop (return false).
|
2022-10-19 03:09:22 +00:00
|
|
|
for _, subjectRule := range acr.Subjects {
|
2021-03-05 04:18:31 +00:00
|
|
|
if subjectRule.IsMatch(subject) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|