lib.PathFilter: Generalize the package filter into path filters #170
Loading…
Reference in a new issue
No description provided.
Delete branch "jan/feature/20261107-lib-execapp-rename-pkg-filter-to-path-filter"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
This PR generalizes the
JW_DEFAULT_PKG_FILTER/--pkg-filterframework into loadable path filters that can be attached to any FileContext. The series adds the PathFilter base class and the lib.pf module for concrete filters, teaches FileContext to check path actions against an attached filter, and threads the filter through copies and package selection. The existing package filter becomes the first loadable filter, and the CLI option and environment variable are renamed from--pkg-filterto--path-filter.lib.PathFilter / lib.pf: Add class + module
Add the abstract PathFilter class, designed to be attached to a FileContext in later commits. This is the first commit of a series that generalizes the
JW_DEFAULT_PKG_FILTER/--pkg-filterframework into loadable path filters that can be attached to any FileContext.A filter answers an access check, _access(), with a bool. run() turns the answer into a verdict: ALLOW when the check passes, otherwise ABORT or IGNORE according to the filter's deny action, LOG or ABORT. The verdict is handled by the filter's handle, a pair of per-verdict gates: with the ABORT gate set, an ABORT verdict raises PermissionError; with the LOG gate set, an IGNORE verdict is logged and returned. A verdict whose gate is not set passes through unhandled.
Also add the lib.pf module that hosts the concrete filters. A filter spec of the form foo:something is loaded by
PathFilter.create()as the Foo class from thejw.pkg.lib.pf.Foomodule. A spec without the name prefix, or a name that no module provides, is rejected with an error that names the expected class and module.lib.FileContext: Limit accessible addresses
Allow to attach a default path filter to a FileContext, given in the constructor as
default_path_filter, or in the URI query aspath-filter. Passing False asdefault_path_filterwill make the FileContext ignore any path-filter in the query.The filter is created lazily on first use through
PathFilter.create(), so a string spec is loaded from the lib.pf module like any other filter.Every path based action evaluates the filter before it is carried out. An
IGNOREverdict is logged and the action is skipped: reads and stats raise FileNotFoundError, writes, unlinks, erases, renames, and mkdirs are no-ops, andreaddir()drops the filtered entries. AnABORTverdict raises PermissionError.Every path-driven method also takes a flag parameter,
filtered, defaulting to True, so a caller that must work regardless of the context's filter can opt out of the check.Add a unit test that exercises the verdicts, the filtered bypass, and the distribution probe on a Local context.
lib.CopyContext: Add optional default_path_filter
CopyContext creates its src and dst contexts from URIs when they are not given as instances. Pass an optional
default_path_filterinto that creation, so a copy made under a filter limits the addresses it reads and writes. A context given as an instance keeps the filter it already carries.lib.util.copy(): Add optional default_path_filter
Hand the new
default_path_filterparameter to the CopyContextcopy()opens, and to the recursive calls it makes for a list of URIs, so an app can limit the addresses a copy touches the same way it limits its own contexts.lib.PackageFilter: Derive from PathFilter
This commit moves package-based filtering from the package level to the path level, using the new path filter framework. Derive PackageFilter from a new FileContextFilter base, which binds a PathFilter to the FileContext it guards, and implements
_access().The filter's space is the file list of the packages it selects, taken from the backing distro's
pkg_files(). An address is allowed only if it names one of those files.The backing distro is taken from the file context, if the context carries one, and is otherwise autodetected from the host the context executes on, so the filter also works on a bare exec context. A context that is not an exec context and carries no distro rejects path queries with an error.
The filter string now follows the loader syntax, pkg:url=~ regex, and a bare url=~regex like the old url=~jw.dev form is rejected; update
JW_DEFAULT_PKG_FILTERvalues accordingly.PackageFilter's
match()is renamedmatch_pkg(), to make clear that it matches packages, andDistro._select()calls the new name.ExecApp.
path_filterbuilds the filter throughPathFilter.create()and binds it to the exec context. CmdSelect passes its argument to the new constructor, CmdCopy hands the app's filter to the copy, and the App distro picks it up as the default package filter.Extend the unit test to cover the space membership and the distro lookup.
lib.pf.Pkg: Add module
Add the Pkg path filter to the lib.pf module, the loader-visible home of the package filter spec. A spec of the form pkg:criterion=~regex instantiates Pkg through
PathFilter.create(). The parsing duplicates the one of PackageFilterString for now; a later commit consolidates the two.lib.Distro: Widen the package filter to PathFilter
The default package filter is typed PackageFilter, but it is handed over from file contexts that carry a PathFilter. Widen the
default_pkg_filterparameter.cmds.pkg.CmdSelect: Load path filter dynamically
The select command names PackageFilterString directly, but the filter is a spec string and the class is resolved dynamically by the loader. Build the filter through
PathFilter.create()so the concrete class no longer appears at the call site.lib.ExecApp: Move .path_filter into the app's ExecContext
Create an app's exec context with the user-specified path filter, and drop the ExecApp.
path_filterproperty, now a duplicate ofexec_context.path_filter. Use the latter in the two call sites.lib.pf.Pkg: Consolidate PackageFilter, drop PackageFilterString
PackageFilterString and Pkg are duplicates. The PackageFilter and FileContextFilter base classes live in
lib/PackageFilter.py, imported by Pkg, Distro, and the tests.Move the two base classes into
lib/pf/Pkg.py(where Pkg already lives), drop PackageFilterString, deletelib/PackageFilter.py, and repoint Distro and the tests to the new location. Move the test suite with the module to test/.../pf/Pkg/.lib.ec.secret_store.KvFile: Init filter in FC-style
Drop the re-read of
JW_DEFAULT_PKG_FILTER, and derive the filter from the ctor and URI like all other FileContext instances would. Use that to filter the backend for the packages provinding secrets.lib.ExecApp: Rename --pkg-filter to --path-filter
The exec context's default filter selects paths, not just packages, so the pkg spelling of the option and environment variable that set it no longer fit. Rename both and some variables accordingly:
--pkg-filterbecomes--path-filter, and the fallback environment variableJW_DEFAULT_PKG_FILTERbecomesJW_DEFAULT_PATH_FILTERdefault_pkg_filterconstructor argument and property becomedefault_path_filter, matching FileContext, ExecContext, and CopyContext; App and KvFile pass the renamed argumentlib.ExecApp: Rename --pkg-filter to --path-filterto lib.PathFilter: Generalize the package filter into path filters