Commit graph jw-pkg/test/unit/python/jw/pkg/lib/ec
Author SHA1 Message Date
dd67dbf544
lib.ec.ssh.AsyncSSH: Forward SIGINT to remote
All checks were successful
CI / Packaging - Kali Linux (pull_request) Successful in 5m15s
CI / Packaging - OpenSUSE Tumbleweed (pull_request) Successful in 6m1s
CI / Packaging test (pull_request) Successful in 0s
The first Ctrl-C cancels the running task while the SSH connection is still
open, but _run_ssh() has no cleanup: the connection is abandoned and the
remote process is never signalled. A long-running command such as zypper
dup therefore keeps running after the client dies, taking its children with
it and leaving the system in a half-baked state, e.g. a mid-air package
transaction.

Catch the cancellation inside the run, forward SIGINT to the remote
process, and wait for it to exit, draining the captured output, before
propagating the cancellation.

Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v1.5.2
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-11-20 22:27:52 +01:00
f717fe2174
lib.ec.secret_store.KvFile: Use lib.pf.PkgSecrets
All checks were successful
CI / Packaging - Kali Linux (pull_request) Successful in 5m12s
CI / Packaging - OpenSUSE Tumbleweed (pull_request) Successful in 6m36s
CI / Packaging test (pull_request) Successful in 0s
Do the filtering outside of KvFile, in a standard way.

KvFile currently instantiates its backend distro with a self-instantiated
backend filter, and doesn't propagate the filter passed to its ctor up to
its base class. This arrangement makes it hard to ensure the class behaves
in a standardish way. Allowing the base class filter to do its work, and
not pass any filter to the backend context, is the more straightforward
architecture, this is what this commit does.

The base class still has some freedom to create a filter. Override the base
class's _create_path_filter() to make sure that filter is a PkgSecrets /
pkg-secrets: filter, anything else is not thought through at this point.

Signed-off-by: Jan Lindemann <jan@janware.com>
2026-11-18 19:13:01 +01:00
48ec9fdb95
lib.ec.secret_store: "content|config" -> "target"
Rename lib.ec.secret_store.layout.content_name() to target_name() and
KvFile's config_path to target_path(). "Target" is a better name to settle
on instead of "config" or "content". It's used in other contexts, too,
referring to secret compilation output.

Signed-off-by: Jan Lindemann <jan@janware.com>
2026-11-18 19:13:01 +01:00
5ac36ec271
lib.Distro.pkg_files() -> .pkg_paths()
"files" is a term that can be used for many aspects of a file, its path,
its content, a handle, so better use "pkg_paths" instead of "pkg_files" for
Distro's method to inform about which files a package holds. That also
aligns better with the recent move from PackageFilter toward PathFilter.

Signed-off-by: Jan Lindemann <jan@janware.com>
2026-11-18 19:13:01 +01:00
0373ae3134
lib.ExecApp: Rename --pkg-filter to --path-filter
All checks were successful
CI / Packaging - Kali Linux (pull_request) Successful in 5m17s
CI / Packaging - OpenSUSE Tumbleweed (pull_request) Successful in 5m27s
CI / Packaging test (pull_request) Successful in 0s
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:

- ExecApp: --pkg-filter becomes --path-filter, and the fallback environment
  variable JW_DEFAULT_PKG_FILTER becomes JW_DEFAULT_PATH_FILTER
- Distro: the default_pkg_filter constructor argument and property become
  default_path_filter, matching FileContext, ExecContext, and CopyContext;
  App and KvFile pass the renamed argument
- Adapt the help test's expected output and the unit tests

Signed-off-by: Jan Lindemann <jan@janware.com>
Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v1.5.0
2026-11-07 19:37:27 +01:00
bc54e4f3b2
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.

Signed-off-by: Jan Lindemann <jan@janware.com>
Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v1.5.0
2026-11-07 19:32:34 +01:00
f74dca5a45
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_FILTER values accordingly.

PackageFilter's match() is renamed match_pkg(), to make clear that it
matches packages, and Distro._select() calls the new name.

ExecApp.path_filter builds the filter through PathFilter.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.

Signed-off-by: Jan Lindemann <jan@janware.com>
Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v1.5.0
2026-11-07 19:22:12 +01:00
3f29084790
lib.ec.secret_store.Sops: Don't encrypt annotations
All checks were successful
CI / Packaging - Kali Linux (pull_request) Successful in 5m28s
CI / Packaging - OpenSUSE Tumbleweed (pull_request) Successful in 5m7s
CI / Packaging test (pull_request) Successful in 0s
SERVICE_SECRET[n] keys of an entry are used as rotation metadata by
downstream software, and their values don't need to be kept secret: they
name the macro and the account that owns it. Encrypting them buys nothing,
and it forces every annotation tool to decrypt and re-encrypt the whole
entry for a one-line read or edit.

Run sops encrypt with --unencrypted-regex SERVICE_SECRET, so the annotation
keys stay plain text in the repository file and the store stays greppable
without the age key, and with --mac-only-encrypted, so the MAC covers the
encrypted values only and the annotation lines can be edited as plain text
without invalidating the file.

Signed-off-by: Jan Lindemann <jan@janware.com>
Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v1.5.0
2026-11-07 14:13:26 +01:00
fae3390a50
lib.ec.secret_store.KvFile: Fail get() on directories
All checks were successful
CI / Packaging - Kali Linux (pull_request) Successful in 5m16s
CI / Packaging - OpenSUSE Tumbleweed (pull_request) Successful in 5m13s
CI / Packaging test (pull_request) Successful in 0s
A directory node of the store, the root included, stats as a directory, but
a get on it falls through to the key lookup below its parent and fails with
a FileNotFoundError naming a path the caller never asked for: get() on the
root fails with an empty name, get() on a branch with its parent's compound
path.

Raise IsADirectoryError from _get() when the path is a directory on the
host, after the compound and the blob lookups, so get() and stat() agree: a
directory stats as FT_DIR and refuses a get.

Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v1.5.0
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-11-07 13:54:19 +01:00
e96bfca0e1
lib.ec.secret_store.KvFile: Construct from a URI
KvFile.__init__() accepts a host FileContext plus an optional Distro, and
the two may live on different hosts. In effect, files are read from one
host based on another host's package set, which may have its use cases but
is generally undesirable. The context parameter also breaks the uniformity
of the store constructors: Sops, TarGpg, and Vault are constructed from a
URI alone, and FileContext.create() needs a special case that builds the
kvfile store's inner context before handing it to the constructor.

This commit has KvFile constructed from a URI alone like the other stores.
The URI's authority names the host, no authority means "local", and the
constructor builds the inner context through FileContext.create() with
chroot off, forwarding the store's constructor parameters to it. The inner
context is always an exec context, which the distro derivation needs: it is
lazy, Distro.instantiate() runs on the first file listing, with the
JW_DEFAULT_PKG_FILTER environment variable as the default package filter,
as before.

Adapt all call sites:

- FileContext.create(): the kvfile case returns KvFile(uri) directly.
- FilesContext: the distro parameter goes; the store URI is derived from
  the exec context's scheme, authority, and path, so a local context with a
  hostname in its URI stays local.
- DistroContext.install(): the kvfile source wrap goes; the compounds read
  directly through the source context with parse_kv().
- The store tests monkey-patch Distro.instantiate() for the stores that
  list something

Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v1.5.0
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-11-07 13:54:19 +01:00
8f87982490
lib.ec.secret_store.Vault: Add the store class
All checks were successful
CI / Packaging - Kali Linux (pull_request) Successful in 4m37s
CI / Packaging - OpenSUSE Tumbleweed (pull_request) Successful in 4m56s
CI / Packaging test (pull_request) Successful in 0s
CI / Packaging - Kali Linux (push) Successful in 5m8s
CI / Packaging - OpenSUSE Tumbleweed (push) Successful in 5m0s
CI / Packaging test (push) Successful in 0s
This commit adds the Vault store and the vault:// dispatch in FileContext.

A Vault KV v2 server maps nicely into jw-pkg's secret handling abstraction
as just another secret store backend. Environment variables identify the
storage details, analogous to the way it's handled in the SOPS backend.

- JW_SECRETS_VAULT_ADDR names the server, a unix socket address or an http
  URL; the standard VAULT_ADDR is never read; JW_SECRETS_VAULT_MOUNT the
  mount path, default secret; the token is the standard VAULT_TOKEN
- The transport is one curl per API call, through the Local context: a
  unix:// server is reached through its socket, an http:// server through
  its port
- A node's type is derived at stat time: an entry, a blob, a key below an
  entry, a branch if the path has children, otherwise a miss
- A blob's content is base64 under a single key of its KV secret, the
  marker being a path component, so a KV secret that is not a blob is
  always an entry

The test drives a dev server fixture from the workspace: stat/readdir/get
on the fixture, the default fallback, the storage marker, and the copy
forms - single key, whole entry, recursive entry, deploy to the on-host
layout, and a host subtree.

Signed-off-by: Jan Lindemann <jan@janware.com>
Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v1.2.0
2026-10-17 14:16:02 +02:00
60a91c0692
lib.ec.secret_store.Sops: Add store class
All checks were successful
CI / Packaging - Kali Linux (pull_request) Successful in 5m0s
CI / Packaging - OpenSUSE Tumbleweed (pull_request) Successful in 5m16s
CI / Packaging test (pull_request) Successful in 0s
CI / Packaging - Kali Linux (push) Successful in 4m37s
CI / Packaging - OpenSUSE Tumbleweed (push) Successful in 4m57s
CI / Packaging test (push) Successful in 0s
This commit adds a new class inheriting from secret_store.Base: Sops. The
currently available classes allow to store secrets offline in a
GPG-encrypted tarball, but those are not versioned. This commit allows to
use a SOPS / AGE-encrypted Git repository as storage backend instead.

The additions in detail:

- Sops: the store implementation; the repository location comes from
  JW_SECRETS_SOPS_REPO, get() on an entry returns the decrypted serialized
  content, and a typed put (FT_SECRET) re-encrypts the entry wholesale
- The age key comes from JW_SECRETS_SOPS_KEY_PATH only: the house variable
  is the sole input to the store, and SOPS_AGE_KEY_FILE is set on the sops
  subprocess per invocation, never read from the process environment;
  SopsKeyError is raised when the variable is not set
- FileContext.create(): the sops:// scheme
- The store's unit tests, covering the node types, the deploy and fetch
  round trip, the whole-entry get and put, the key resolution, and the
  default fallback

Signed-off-by: Jan Lindemann <jan@janware.com>
Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v1.1.0
Signed-off-by: Jan Lindemann <jan@janware.com>
Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v1.1.0
2026-10-17 13:58:00 +02:00
f70cc48eb6
lib.ec.secret_store.TarGpg: Add the write path
All checks were successful
CI / Packaging - Kali Linux (pull_request) Successful in 4m32s
CI / Packaging - OpenSUSE Tumbleweed (pull_request) Successful in 4m52s
CI / Packaging test (pull_request) Successful in 0s
CI / Packaging - Kali Linux (push) Successful in 4m35s
CI / Packaging - OpenSUSE Tumbleweed (push) Successful in 5m17s
CI / Packaging test (push) Successful in 0s
The TarGpg store is read-only: put() and unlink() raise, so the store can
be copied from, but not into, and a copy that lands entries and blobs below
a host prefix has no tarball target.

This matches the way jw-pkg secrets has worked for a long time. Since
Stores are now FileContext-backed, a write path can be cleanly implemented,
and that's what this commit does.

A put rebuilds the tarball: the existing members minus the removed, plus
the written one; the result is signed with the recipient from
JW_SECRETS_TARGPG_RECIPIENT and written back to the tarball location, which
must be a local path. A missing tarball is an empty store, so a put creates
it.

The put dispatch follows the other stores: a typed entry put replaces the
entry with the given serialized content, a key put merges into its entry,
creating it if it does not exist yet, and an untyped put below an entry
lands a key there, otherwise a blob. unlink() removes the node's member.

Signed-off-by: Jan Lindemann <jan@janware.com>
Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v1.1.0
2026-10-17 13:09:45 +02:00
f08f658d33
lib.ec.secret_store: Add and use framework
All checks were successful
CI / Packaging - Kali Linux (pull_request) Successful in 4m48s
CI / Packaging - OpenSUSE Tumbleweed (pull_request) Successful in 5m12s
CI / Packaging test (pull_request) Successful in 0s
CI / Packaging - Kali Linux (push) Successful in 4m35s
CI / Packaging - OpenSUSE Tumbleweed (push) Successful in 4m49s
CI / Packaging test (push) Successful in 0s
"jw-pkg secrets install" copies secret material from a GPG-encrypted tar
archive to dotenv .jw-secret files on a target host. This commit refactors
the underlying code and adds hooks for the inverse copy direction, and for
more secret sources and targets.

The peers between which secrets are copied are represented by the new
abstract secret_store.Base class, and the existing tar blob and host
storages, inheriting from that class, are TarGpg and KvFile. Moreover,
secret_store.Base is inheriting from FileContext, so all of jw-pkg's
file-like operations are available as hooks.

Secrets inside instances of two file contexts are addressable as
(targpg|kvfile)://target.host.com/path/to/file.conf.jw-secret. The on-host
KvFile will look either locally or via SSH for that file, and TarGpg will
look inside a tar archive specified in the JW_SECRETS_TARGPG_TAR
environment variable.

Uris with these schemas can be passed to FileContext.create(), which will
return a chrooted file context of the correct type.

KvFile.stat("/path/to/file.conf.jw-secret").ftype is FT_SECRET which
inherits from FT_COMPOUND - which means that readdir() works on such
animals. Not entirely sure if that's a good idea, but it feels pretty
generic. Let's see where that takes us.

DistroContext.install() after this refactor reads its source through the
store interface and writes the landing through the KvFile store.

Both secret stores are exercised through the existing unit test of jw-pkg
secrets install, and unit tests for both stores are also added by this
commit.

Signed-off-by: Jan Lindemann <jan@janware.com>
2026-10-17 12:52:20 +02:00
0c732672ed
lib.FileContext: Add readdir() method
FileContext offers get(), put(), unlink(), mkdir(), and stat(), but no way
to list the entries of a directory, so consumers that need the names of the
entries under a path in a context have no portable means to obtain them.

Add readdir(), returning Iterable[str], following the _readdir() ->
readdir() pattern used by the other operations: the public method chroots
the path via _chroot() and delegates to the _readdir() hook, which raises
NotImplementedError by default.

Implement _readdir() in the context families:

- Local._readdir(): os.listdir()
- ExecContext._readdir(): runs ls -1A and splits the output into one entry
  per line; inherited by SSHClient and the other command-based contexts
- Curl._readdir(): raises OSError with errno.ENOSYS

Add unit tests covering Local (listing, hidden entries, missing directory)
and Curl (ENOSYS).

Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v0.87.1
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-09-29 15:29:19 +02:00
dc101a5a7f
lib.ec.ssh.Exec: Fix askpass script
All checks were successful
CI / Packaging - Kali Linux (pull_request) Successful in 4m10s
CI / Packaging - OpenSUSE Tumbleweed (pull_request) Successful in 4m21s
CI / Packaging test (pull_request) Successful in 0s
CI / Packaging - Kali Linux (push) Successful in 4m3s
CI / Packaging - OpenSUSE Tumbleweed (push) Successful in 4m32s
CI / Packaging test (push) Successful in 0s
__init_askpass() embeds the password verbatim in a generated bash script,
with a newline included inside the double quotes of the echo -n. The
askpass program therefore always appends a newline to the password, and
any password containing quotes or shell metacharacters either breaks the
script or injects commands into it.

Embed the password as base64 and decode it with printf piped into base64
-d, so the script is safe for any password and prints the password exactly,
byte for byte.

Make __del__() idempotent: it deletes the environment variables and the
script file, so a second call, e.g. an explicit one followed by garbage
collection, raises KeyError and FileNotFoundError.

Add a unit test that executes the generated script and compares its output
with the password byte for byte.

Signed-off-by: Jan Lindemann <jan@janware.com>
Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v0.85.1
2026-09-14 20:32:14 +02:00