Repository navigation
-home: one directory, chosen deliberately, and an error when there is nowhere - #55
Merged
Merged
Conversation
The directory holding the keypair, the link table, the device table and share state was computed in three places, each spelling `~/.bitbang` itself. One of them fell back to "." when there was no home directory, so a service started without HOME wrote its private key relative to whatever directory it started in -- `/` for an init script, which is how an OpenWRT listener ended up with its identity in /.bitbang. internal/bbhome resolves it once, in an order a README can state: the -home flag (next commit), BITBANG_HOME, systemd's STATE_DIRECTORY, XDG_STATE_HOME/bitbang, ~/.bitbang, and then an error. The first three name the directory; the last two name a parent it hangs off. The error is the point. Writing a private key somewhere nobody chose is worse than refusing to write it, so identity.Dir returns an error now and its five callers handle it. `link` exits, since every one of its subcommands needs the path and bbhome's message already says what to do. Resolved on every call rather than cached. The first draft cached it, which survives a test pointing HOME at a temp directory: the first test to resolve decided for all the others, and one of them went looking in the real device table. Groundwork for #41.
Not just `serve`. The directory holds the device table `connect` and `cp` read and the link table `link` edits, so a flag on one command would leave the others needing the environment variable anyway, and forgetting it once on `bitbang link` would quietly edit a different table. Registered with flag.Func so it applies during Parse, before any command body can ask where to look. Nothing to thread through, no order to get wrong. The share worker takes it too, and gets it passed explicitly. It runs in a tmux pane, so its environment is the tmux server's rather than ours: BITBANG_HOME would not reach it, and even HOME can differ, which means the worker could already have loaded a different identity than the process that spawned it. Verified against the built binary: -home and BITBANG_HOME both land in the named directory, STATE_DIRECTORY is used as given, the flag beats the environment, and with none of them and no HOME it exits 1 saying what to pass rather than writing to the working directory. For #41.
Six ways to choose the directory means an operator whose files are not
where they expected has six things to check. The listener names it:
Home: /home/you/.bitbang
Home: /var/lib/bitbang (STATE_DIRECTORY)
Always, including the default. Somebody running this for the first time has
the same question as somebody who configured it, and #41 began with an
operator not knowing where his files had gone. The source is named only
when it is not the plain home directory, because "(HOME)" after a path
under ~ says nothing the path did not.
This does not undo #34: it is printed once at startup and deliberately not
from ready(), which reprints on every reconnect. One line per process, not
one per reconnect.
The README section is the order itself, as a table, plus the systemd unit
that needs no BitBang-specific configuration because StateDirectory= is
already one of the rungs.
Closes the written part of #41.
Two Windows assumptions in the new tests, neither in the code they test. filepath.Clean turns a forward slash into a backslash there, so a `want` written as "/flag" could never match. The composed expectations were already fine, since filepath.Join does the same thing on both sides; it is the literals that needed FromSlash. And os.UserHomeDir reads USERPROFILE on Windows, not HOME, so every case expecting the home rung errored instead -- the ladder fell through to "no home directory". Set both, which is what devices_test.go already does. Found by CI, which is the only thing that can run these: on linux FromSlash is the identity function and USERPROFILE is never read, so neither mistake is reproducible locally.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #41.
-homeandBITBANG_HOMEset the directory BitBang keeps its own files in:the identity keypair, the link table, the device table
connectandcpread, and share state.
Three commits: the resolver, the flag, the startup line and README.
The order, first match wins
-home <dir>$BITBANG_HOME$STATE_DIRECTORYStateDirectory=$XDG_STATE_HOME$XDG_STATE_HOME/bitbang$HOME$HOME/.bitbangThe first three name the directory; the last two name a parent it hangs off.
internal/bbhomeis the authority and says so in its package comment, so theasymmetry does not get "fixed" later.
The bug
identityDirfell back tohome = "."whenos.UserHomeDir()failed, so aservice started without HOME wrote its private key relative to whatever
directory it started in --
/for an init script. That is how @lars18th'sOpenWRT listener ended up with its identity in
/.bitbang, which took himtime to work out was a fallback rather than a choice.
It refuses now.
identity.Dirreturns an error and its five callers handleit;
linkexits, since every one of its subcommands needs the path andbbhome's message already says what to pass. Writing a keypair somewhere
nobody chose is worse than not writing it.
Notes on three decisions
The flag is on every command, not just
serve. The directory is notserve's, so a flag on one command leaves the rest needing the env var anyway,
and forgetting it once on
bitbang linkwould quietly edit a different table.Registered with
flag.Funcso it applies duringParse, before any commandbody can ask bbhome where to look.
The share worker gets it in its argv. It runs in a tmux pane, so its
environment is the tmux server's:
BITBANG_HOMEwould not reach it and evenHOMEcan differ, which means the worker could already have loaded adifferent identity than the process that spawned it.
The startup line is printed once, not from
ready().ready()reprintson every reconnect, and the directory cannot move under a running listener, so
this is one line per process rather than one per reconnect -- it does not undo
#34. It always names the directory, default included, and names the source
only when that is not the plain home directory.
Verified against the built binary
Not only unit tests:
-homeandBITBANG_HOMEeach land in the nameddirectory,
STATE_DIRECTORYis used as given, the flag beats the environment,-home directoryappears in help on every command, and with none of them andno
HOMEit exits 1 saying what to pass instead of writing to the workingdirectory.
Twelve table cases in
internal/bbhomecover each rung and its source, therefusal, and that
Rootre-reads the environment rather than caching -- thefirst draft cached it, which let one test read the developer's real device
table instead of its own temp directory.
Not in this
The URL file from #54. That needs a directory that does not move, which is
what this provides, but it is its own change.