// subjectMatches returns true if specified user and group properties in the policy match the attributes func subjectMatches(p api.Policy, a authorizer.Attributes) bool { matched := false // If the policy specified a user, ensure it matches if len(p.Spec.User) > 0 { if p.Spec.User == "*" { matched = true } else { matched = p.Spec.User == a.GetUserName() if !matched { return false } } } // If the policy specified a group, ensure it matches if len(p.Spec.Group) > 0 { if p.Spec.Group == "*" { matched = true } else { matched = false for _, group := range a.GetGroups() { if p.Spec.Group == group { matched = true } } if !matched { return false } } } return matched }
func verbMatches(p api.Policy, a authorizer.Attributes) bool { // TODO: match on verb // All policies allow read only requests if a.IsReadOnly() { return true } // Allow if policy is not readonly if !p.Spec.Readonly { return true } return false }
func resourceMatches(p api.Policy, a authorizer.Attributes) bool { // A resource policy cannot match a non-resource request if a.IsResourceRequest() { if p.Spec.Namespace == "*" || p.Spec.Namespace == a.GetNamespace() { if p.Spec.Resource == "*" || p.Spec.Resource == a.GetResource() { if p.Spec.APIGroup == "*" || p.Spec.APIGroup == a.GetAPIGroup() { return true } } } } return false }
func nonResourceMatches(p api.Policy, a authorizer.Attributes) bool { // A non-resource policy cannot match a resource request if !a.IsResourceRequest() { // Allow wildcard match if p.Spec.NonResourcePath == "*" { return true } // Allow exact match if p.Spec.NonResourcePath == a.GetPath() { return true } // Allow a trailing * subpath match if strings.HasSuffix(p.Spec.NonResourcePath, "*") && strings.HasPrefix(a.GetPath(), strings.TrimRight(p.Spec.NonResourcePath, "*")) { return true } } return false }