patterns

package
v0.0.0-...-e4d0a0c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 8, 2025 License: GPL-3.0 Imports: 10 Imported by: 2

Documentation

Overview

Package patterns provides types and functions for working with path patterns for request rules related to AppArmor Prompting.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoPatterns = errors.New("cannot establish precedence: no patterns given")

Functions

func PathPatternMatches

func PathPatternMatches(pattern string, path string) (bool, error)

PathPatternMatches returns true if the given pattern matches the given path.

Paths to directories are received with trailing slashes, but we don't want to require the user to include a trailing '/' if they want to match directories (and end their pattern with `{,/}` if they want to match both directories and non-directories). Thus, we want to ensure that patterns without trailing slashes match paths with trailing slashes. However, patterns with trailing slashes should not match paths without trailing slashes.

The doublestar package (v4.6.1) has special cases for patterns ending in `/**` and `/**/`: `/foo/**`, and `/foo/**/` both match `/foo` and `/foo/`. We want to override this behavior to make `/foo/**/` not match `/foo`. We also want to override doublestar to make `/foo` match `/foo/`.

Types

type PathPattern

type PathPattern struct {
	// contains filtered or unexported fields
}

PathPattern is an iterator which yields expanded path patterns.

func ParsePathPattern

func ParsePathPattern(pattern string) (*PathPattern, error)

ParsePathPattern validates the given pattern and parses it into a PathPattern from which expanded path patterns can be iterated, and returns it.

func (*PathPattern) MarshalJSON

func (p *PathPattern) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaller for PathPattern.

func (*PathPattern) Match

func (p *PathPattern) Match(path string) (bool, error)

Match returns true if the path pattern matches the given path.

func (*PathPattern) NumVariants

func (p *PathPattern) NumVariants() int

NumVariants returns the total number of expanded path patterns for the given path pattern.

func (*PathPattern) RenderAllVariants

func (p *PathPattern) RenderAllVariants(observe func(index int, variant PatternVariant))

RenderAllVariants enumerates every alternative for each group in the path pattern and renders each one into a PatternVariant which is then passed into the given observe closure, along with the index of the variant.

The given observe closure should perform some action with the rendered variant, such as adding it to a data structure.

func (*PathPattern) String

func (p *PathPattern) String() string

func (*PathPattern) UnmarshalJSON

func (p *PathPattern) UnmarshalJSON(b []byte) error

UnmarshalJSON implements json.Unmarshaller for PathPattern.

type PatternVariant

type PatternVariant struct {
	// contains filtered or unexported fields
}

func HighestPrecedencePattern

func HighestPrecedencePattern(patterns []PatternVariant, matchingPath string) (PatternVariant, error)

HighestPrecedencePattern determines which of the given path patterns is the most specific, and thus has the highest precedence.

Precedence is only defined between patterns which match the same path. Precedence is determined according to which pattern places the earliest and greatest restriction on the path.

func (PatternVariant) Compare

func (v PatternVariant) Compare(other PatternVariant, matchingPath string) (int, error)

Compare returns the relative precence of the receiver and the given pattern variant when considering the given matching path.

Returns one of the following, if no error occurs: -1 if v has lower precedence than other 0 if v and other have equal precedence (only possible if v == other) 1 if v has higher precedence than other.

func (PatternVariant) String

func (v PatternVariant) String() string

String returns the rendered string associated with the pattern variant.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL