| 1 |
# Repo-managed JJ configuration |
| 2 |
|
| 3 |
Author: [Matt Stark](mailto:[email protected]) |
| 4 |
|
| 5 |
Status: Pending implementation |
| 6 |
|
| 7 |
## Background |
| 8 |
|
| 9 |
The design doc [Secure JJ Config](secure-config.md) introduces a mechanism, |
| 10 |
`metadata.binpb`, through which information about a repository / workspace can |
| 11 |
be stored. It also discusses how allowing an external user to have control over |
| 12 |
your config is a security risk. |
| 13 |
|
| 14 |
## Overview |
| 15 |
|
| 16 |
There is a need for a repository to impose requirements upon users. Examples of |
| 17 |
these things include, but are not limited to: |
| 18 |
|
| 19 |
* Formatter configuration (eg. [Chromium Formatter Config]) |
| 20 |
* Pre-upload checks (eg. [Chromium Pre-upload Checks]) |
| 21 |
* Syncing scripts (eg. [Chromium Syncing Scripts]) |
| 22 |
* Custom aliases / revsets documented by some kind of “getting started with |
| 23 |
<project>” document |
| 24 |
|
| 25 |
It should be fairly obvious that there is a strong benefit to doing so. However, |
| 26 |
controlling a user’s config is sufficient to get root access to their machine, |
| 27 |
so we require a mechanism more complex than just blindly loading the config |
| 28 |
file. |
| 29 |
|
| 30 |
This is currently achieved by projects such as chromium by instructing the user |
| 31 |
to symlink `.jj/repo/config.toml` to a file in the repository. This has several |
| 32 |
drawbacks: |
| 33 |
|
| 34 |
* It doesn’t work out-of-the-box. I need to manually symlink it |
| 35 |
* It has no security guarantees. If I update the file in the repo, the user has no |
| 36 |
opportunity to review it. |
| 37 |
* This prevents a user from having their own repo configuration on top of it. |
| 38 |
|
| 39 |
## Objective |
| 40 |
|
| 41 |
* Create a new layer of configuration between user configuration and repo |
| 42 |
configuration. |
| 43 |
* This configuration will be stored in version control and henceforth be |
| 44 |
referred to as “managed” configuration. |
| 45 |
* Implement it in a secure manner so that an attacker cannot take control over |
| 46 |
the managed config. |
| 47 |
|
| 48 |
## Detailed Design |
| 49 |
|
| 50 |
The managed configuration will be read from `$REPO/.config/jj/config.toml`. This |
| 51 |
is intentionally designed to be very similar to `$HOME/.config/jj/config.toml` |
| 52 |
for the user configuration. |
| 53 |
|
| 54 |
Any data stored here will be added to the `metadata.binpb` that was created |
| 55 |
in [secure config](secure-config.md) (that is, we will add additional fields to |
| 56 |
the `Metadata` struct). This will ensure that we don't suffer from the "zip file |
| 57 |
problem". |
| 58 |
|
| 59 |
### Security |
| 60 |
|
| 61 |
#### Trust levels |
| 62 |
|
| 63 |
We will add the following fields to metadata: |
| 64 |
``` |
| 65 |
enum TrustLevel { |
| 66 |
// There's no "optional" in protos. It just returns the zero value. |
| 67 |
UNSET = 0; |
| 68 |
// The user wishes to completely ignore the managed config file. |
| 69 |
IGNORED = 1; |
| 70 |
// In trusted mode, we directly read from the managed config file. |
| 71 |
// This presents a security risk, so the user is expected to only do this |
| 72 |
// for a repo they trust. |
| 73 |
TRUSTED = 2; |
| 74 |
// In notify mode, the user is expected to manually replicate any changes |
| 75 |
// they want from the managed config to the repo config. |
| 76 |
NOTIFY = 3; |
| 77 |
// In review mode, the user has to review every config individually. |
| 78 |
REVIEW = 4; |
| 79 |
} |
| 80 |
|
| 81 |
message Metadata { |
| 82 |
// previous fields |
| 83 |
|
| 84 |
// The trust level associated with this repo. |
| 85 |
TrustLevel trust_level = 2; |
| 86 |
|
| 87 |
// When trust_level is review, the following fields are used. |
| 88 |
set<Hash> approved_configs = 3; |
| 89 |
set<Hash> rejected_configs = 4; |
| 90 |
// This is a can of worms and thus out of scope of the MVP. |
| 91 |
// There's no one correct way to choose the last approved. |
| 92 |
// Sometimes, the user wants be the last appproved config in the workspace. |
| 93 |
// At other times, the user wants be the last appproved config in the repo. |
| 94 |
string last_approved_config = 5; |
| 95 |
} |
| 96 |
``` |
| 97 |
|
| 98 |
Note that although the `Metadata` struct is stored for both the repo and the |
| 99 |
workspace, this will only be used for the repository, not the workspace. |
| 100 |
|
| 101 |
This is because trust is associated with repositories, not workspaces. If I |
| 102 |
were to add an approved config in one workspace, for example, there is no |
| 103 |
reason I wouldn't trust it in another. |
| 104 |
|
| 105 |
#### User interface |
| 106 |
|
| 107 |
Everything here only becomes relevant when a repo has a managed config. If it |
| 108 |
does not have a managed config, we skip everything here. |
| 109 |
|
| 110 |
##### Unset trust |
| 111 |
|
| 112 |
If a repository has a managed config and unset trust, we first use |
| 113 |
`ui.can_prompt()` to determine whether we are in a TUI. |
| 114 |
|
| 115 |
* If we are in a TUI, we ask the user what trust level they would like. |
| 116 |
* If we are not in a TUI, we warn them that we are ignoring the repo config (we |
| 117 |
return `IGNORE`), and that they can run `jj config managed --ignore/notify/trust` in a terminal to configure this. |
| 118 |
|
| 119 |
##### Ignored |
| 120 |
|
| 121 |
Nice and easy. We just completely ignore the managed config file. |
| 122 |
|
| 123 |
##### Trusted |
| 124 |
|
| 125 |
Nice and easy. We just read the managed config file. |
| 126 |
|
| 127 |
##### Notify |
| 128 |
|
| 129 |
Roughly speaking, if `mtime(repo_config_file) > mtime(managed_config)`, then |
| 130 |
the config is up to date. |
| 131 |
|
| 132 |
If it isn't up to date, we print a warning. The user is then expected to update |
| 133 |
their repo config with any options they want from the managed config, which |
| 134 |
will stop the warning from printing as the mtime has changed. |
| 135 |
|
| 136 |
This notify option has a rather painful UX when it comes to keeping it in sync |
| 137 |
(particularly for a user who constantly switches between different branches |
| 138 |
synced to different versions), but I choose it for several reasons. The first is |
| 139 |
that the vast majority of users will simply select to blindly trust the repo. |
| 140 |
The only people who will choose this option are the very security conscious, and |
| 141 |
this is by far the most secure mechanism. Secondly, it is so much simpler than |
| 142 |
an approval based mechanism, as we don’t need to worry about things such as |
| 143 |
workspaces being synced to different places. It provides far fewer edge cases. |
| 144 |
|
| 145 |
In the future we may consider improving on the UX, doing things such as printing |
| 146 |
a diff since the last seen config (per `last_approved_config`), but that isn't |
| 147 |
a part of the MVP. |
| 148 |
|
| 149 |
##### Review |
| 150 |
|
| 151 |
This is, by far, the most difficult option. There is no doubt that this |
| 152 |
provides value to some users, but the precise details of how this should behave |
| 153 |
is unclear (and much harder to implement than the other options). |
| 154 |
|
| 155 |
The biggest problem with this is that there is not a clear "correct" set of |
| 156 |
semantics. If a config is not approved / rejected, we have 3 options: |
| 157 |
* Fail the command. This seems like a bad idea since users have an assumption |
| 158 |
that most jj commands will not fail. |
| 159 |
* Use no config. This can potentially have quite bad effects on the repo. |
| 160 |
* Use best-effort to use an approved config (probably most recently approved). |
| 161 |
* Force the user to make a selection before running a command |
| 162 |
* This is impossible in general due to `ui.can_prompt()` returning `false` |
| 163 |
* We would need to fall back to another option if we can't prompt. |
| 164 |
* But `ui.can_prompt()` does not mean that we *should* prompt |
| 165 |
* E.g., a script that runs jj to do a bisection probably doesn't want |
| 166 |
you to prompt every time the config changes. |
| 167 |
* This probably isn't too bad if it only happens once (e.g., setting trust |
| 168 |
level), but if it happens many times, it could be very annoying. |
| 169 |
|
| 170 |
Consider the following examples |
| 171 |
|
| 172 |
###### User is working on config file. |
| 173 |
|
| 174 |
1. User runs `vim .config/jj/config.toml` |
| 175 |
2. User tries to test out the config, but needs to manually approve their own |
| 176 |
local change to the config every single time (this can at least be |
| 177 |
mitigated via setting it to trust temporarily) |
| 178 |
|
| 179 |
###### Disabling config messes with the repo |
| 180 |
|
| 181 |
1. User approves a config. |
| 182 |
2. User runs `jj git fetch`. There has been an update to the managed config. |
| 183 |
3. User runs `jj rebase -b @ -o trunk()`. The managed config is now not |
| 184 |
approved, and thus the next jj command will have the config disabled. |
| 185 |
|
| 186 |
Depending on what has changed, a variety of things could happen: |
| 187 |
* A user in a repo that defines `trunk()` could run |
| 188 |
`jj rebase -b <revset> -o trunk()` |
| 189 |
* This would result in it going to the wrong trunk |
| 190 |
* A user in a repo that defines `immutable_heads() = ...` could run something |
| 191 |
like `jj rebase -b mutable() -o trunk()` |
| 192 |
* This would rebase a bunch of immutable commits |
| 193 |
* A user in a repo that defines `pre-upload` hooks (which don't yet exist, |
| 194 |
but are being planned) runs `jj gerrit upload` and bypasses them entirely. |
| 195 |
|
| 196 |
Thanks to the magic of `jj undo`, everything except the upload is recoverable. |
| 197 |
What makes them a problem, however, is the fact that a user would see the |
| 198 |
message "please approve / reject the config" and not suspect that the command |
| 199 |
has failed. |
| 200 |
|
| 201 |
###### Best-effort messes with the user |
| 202 |
|
| 203 |
If we use best-effort instead, we are likely to run into a different set of |
| 204 |
issues. |
| 205 |
|
| 206 |
The first is that it's no longer clear which config is being used. If I: |
| 207 |
1. Have two workspaces A and B with the same state, both approved. |
| 208 |
2. Sync workspace B and approve the config |
| 209 |
3. Check out an old version of workspace A |
| 210 |
|
| 211 |
Now which config is used? |
| 212 |
* The most recently approved globally? |
| 213 |
* The most recently approved for this workspace? |
| 214 |
* Some algorithm which determines that the config for A was at a closer commit |
| 215 |
than B? |
| 216 |
|
| 217 |
The second issue is that things may be incorrect. If `foo.py` was renamed to |
| 218 |
`bar.py` and the reference was changed in `config.toml`, for example, the |
| 219 |
config is going to break. This is not as large an issue as disabling config, |
| 220 |
IMO, as changes will usually result in an obvious error. |
| 221 |
|
| 222 |
###### Conclusion |
| 223 |
|
| 224 |
The vast majority of users will not use this option, as very few users are |
| 225 |
security-conscious, working on a repo they don't trust, and working on a repo |
| 226 |
with managed config. |
| 227 |
|
| 228 |
Due to: |
| 229 |
* Significant implementation complexity |
| 230 |
* My prototype without this feature is only ~200 lines of code and ~100 lines |
| 231 |
of tests. This feature would likely double that for the most hacky simple |
| 232 |
implementation, and add significantly more if we wanted a proper nice UX. |
| 233 |
* The lack of a single "correct" solution |
| 234 |
* The fact that most users just want the "trust" option |
| 235 |
* The fact that it is no more difficult to add other options, then add the |
| 236 |
review option after the fact |
| 237 |
* The time that would be required to get consensus on the various decisions |
| 238 |
that would need to be made in terms of how this would work. |
| 239 |
* There was already a lot of |
| 240 |
[discussion](https://github.com/jj-vcs/jj/pull/7761#discussion_r2476135221) |
| 241 |
on this topic, but no clear conclusion was reached. |
| 242 |
* Technical (e.g., disable vs. best-effort) |
| 243 |
* UX |
| 244 |
* When reviewing, do we review diffs or full config files? |
| 245 |
* Do we prompt the user to review before running the command? |
| 246 |
|
| 247 |
I propose that the "review" option not be put in the MVP, and and instead be |
| 248 |
added in later. |
| 249 |
|
| 250 |
##### Manually changing |
| 251 |
|
| 252 |
These options will also be able to be manually set via `jj config managed |
| 253 |
--ignore/notify/trust`. |
| 254 |
|
| 255 |
### Where to read from |
| 256 |
|
| 257 |
This is the trickiest part of the proposal. Consider the following workflow: |
| 258 |
|
| 259 |
``` |
| 260 |
jj new main@origin |
| 261 |
jj ... |
| 262 |
jj new stable-branch@origin |
| 263 |
``` |
| 264 |
|
| 265 |
There are some edge cases we need to consider: |
| 266 |
|
| 267 |
* `stable-branch` may have existed before the config was added |
| 268 |
* `stable-branch` may have a different copy of the managed config |
| 269 |
|
| 270 |
The naive assumption would be that you want to read the config from `@`, as the |
| 271 |
config will always match the version of the code you're using. However, it turns |
| 272 |
out that some things want to refer to `@`, while others want to read the config |
| 273 |
from `trunk()`. |
| 274 |
|
| 275 |
Consider several different use cases: |
| 276 |
|
| 277 |
* My formatter was previously `clang-format --foo`, but the option `--foo` was |
| 278 |
deprecated in the latest version of `clang-format` |
| 279 |
* Here, you want to read from `trunk()` |
| 280 |
* My formatter was previously `$repo/formatter --foo`, but the option `--foo` |
| 281 |
was deprecated in the latest version of `formatter` |
| 282 |
* Here, you want to read from `@` |
| 283 |
* We decide to split long lines and add a new formatter (or pre-upload hook) |
| 284 |
config `formatter --line-length=80` |
| 285 |
* Here, you probably want to read from `@` |
| 286 |
* We decide to add a pre-upload check that validates that all commit |
| 287 |
descriptions contain a reference to a bug |
| 288 |
* This should be applied to old branches as well, so you want `trunk()` |
| 289 |
* We add a new helpful alias / revset |
| 290 |
* This should be applied to old branches as well, so you want `trunk()` |
| 291 |
* We move our formatter |
| 292 |
* If it’s external, you want to read from `trunk()` |
| 293 |
* If it’s internal, you want to read from `@` |
| 294 |
|
| 295 |
All in all, you can see a general pattern. |
| 296 |
|
| 297 |
* If something refers to an in-repo tool, you **probably** want the config to |
| 298 |
be read from `@` |
| 299 |
* Otherwise, you **probably** want to read from `trunk()` |
| 300 |
* I say probably, because the split long lines example doesn’t conform to this |
| 301 |
rule. |
| 302 |
|
| 303 |
#### Problematic examples |
| 304 |
|
| 305 |
This is problematic with `trunk()` because if you add the `--reorder-hooks` and |
| 306 |
then checkout `stable-branch` it will incorrectly attempt to reorder imports |
| 307 |
|
| 308 |
``` |
| 309 |
[fix.tools.rustfmt] |
| 310 |
command = ["rustfmt", "--reorder-imports"] |
| 311 |
``` |
| 312 |
|
| 313 |
##### Solution 1: formatter config |
| 314 |
|
| 315 |
In practice, it is highly unlikely that a formatter config would be written that |
| 316 |
way. Far more likely, you would see an entry in `config.toml` like: |
| 317 |
|
| 318 |
``` |
| 319 |
[fix.tools.rustfmt] |
| 320 |
command = ["rustfmt"] |
| 321 |
``` |
| 322 |
|
| 323 |
`.rustfmt.toml`: |
| 324 |
``` |
| 325 |
reorder_imports = true |
| 326 |
``` |
| 327 |
|
| 328 |
This simply works out of the box, since the formatter is reading the config from |
| 329 |
`@`'s `.rustfmt.toml` |
| 330 |
|
| 331 |
##### Solution 2 (more general but convoluted): Wrapper script |
| 332 |
|
| 333 |
As long as the formatter is in-repo, we can just write a wrapper script which |
| 334 |
does this for us. |
| 335 |
|
| 336 |
``` |
| 337 |
[fix.tools.rustfmt] |
| 338 |
command = ["rustfmt.py"] |
| 339 |
``` |
| 340 |
|
| 341 |
`rustfmt.py`: |
| 342 |
``` |
| 343 |
os.execv(["rustfmt", "--reorder-imports"]) |
| 344 |
``` |
| 345 |
|
| 346 |
#### Solution 3 (for scripts that need to run at trunk) |
| 347 |
|
| 348 |
If you write a script for which the API keeps changing, eg. you add / remove |
| 349 |
flags to it, you can do something like this: |
| 350 |
|
| 351 |
``` |
| 352 |
[aliases] |
| 353 |
upload = ["util", "exec", "--", "bash", "-c", "python3", "-c", "$(jj file show -r 'trunk()' upload.py)"] |
| 354 |
``` |
| 355 |
|
| 356 |
#### Decision: Trunk vs @ |
| 357 |
|
| 358 |
`@` and `trunk()` are the only two reasonable candidates as places to read from, |
| 359 |
IMO. I personally believe that if only one option is available, `trunk()` would |
| 360 |
be much more appropriate, for the reasons specified above. |
| 361 |
|
| 362 |
However, @pmetzger has pointed out that in a future world where Git isn’t the |
| 363 |
backend, this decision may come back to bite us (as build tools may be checked |
| 364 |
in to the build). |
| 365 |
|
| 366 |
For now, we will only support reading from the working copy (`@`), but in the |
| 367 |
future, we may consider supporting other revisions. |
| 368 |
|
| 369 |
To achieve this, there are several methods. I'll list some of them, but they |
| 370 |
don't need review, as they are out of scope of this proposal. |
| 371 |
|
| 372 |
The simplest that comes to mind is a `--managed-config-from-revision` flag or |
| 373 |
config option. |
| 374 |
|
| 375 |
Alternatively, a more general approach could be to create some kind of include |
| 376 |
directive in `config.toml` files, like so (this has already been discussed and |
| 377 |
rejected before, so it may not be viable). |
| 378 |
``` |
| 379 |
includes = [ |
| 380 |
{ |
| 381 |
path = "other_config.toml", |
| 382 |
revision = "trunk()", |
| 383 |
}, |
| 384 |
] |
| 385 |
``` |
| 386 |
|
| 387 |
#### Read from filesystem or from repo objects? |
| 388 |
|
| 389 |
The tradeoffs are: |
| 390 |
* If we read it from the filesystem, then it will not be read if the path is |
| 391 |
not tracked, e.g. because you have done `jj sparse --clear --add lib` or |
| 392 |
because the file matches .gitignore and is not yet tracked. |
| 393 |
* This could be mitigated by always including `.config/jj` in `jj sparse` |
| 394 |
* If we read it from the repo objects, then changes will not apply to the next |
| 395 |
command unless we load this config after snapshotting |
| 396 |
* Reading from the filesystem is faster |
| 397 |
* mtime is only available when reading from the filesystem |
| 398 |
|
| 399 |
Reading from the filesystem seems strictly superior, given the mitigation |
| 400 |
strategy available, so we will go ahead with that for now. If we ever decide to |
| 401 |
support reading from `trunk()`, however, we will need to support reading from |
| 402 |
the repo. |
| 403 |
|
| 404 |
[Chromium Formatter Config]: https://source.chromium.org/chromium/chromium/src/+/main:tools/jj/config.toml;l=47-96;drc=080c978973f87ff2a1cfa514a13285baeaf3eedc |
| 405 |
[Chromium Pre-upload Checks]: https://source.chromium.org/chromium/chromium/src/+/main:tools/jj/upload.py;drc=96f39fbbb720ca391d43cbb199a85af7d3309dd3 |
| 406 |
[Chromium Syncing Scripts]: https://source.chromium.org/chromium/chromium/src/+/main:tools/jj/sync.py;drc=6ff08dcdd1fdeb1654a4d3da81d8adaeae4bbbf7 |