|
| 1 | + |
| 2 | +# A Note On The Various "Contexts" and "LoginState" |
| 3 | + |
| 4 | +We are in the midst of wide and slow-moving code reorgnization with two major goals: |
| 5 | + |
| 6 | +1. To standardize as much code as possible to take one of three "context" objects, always |
| 7 | +as a first argument: (a) Go's standard `context.Context`; (b) our rollup `libkb.MetaContext`; |
| 8 | +or (c) our chat-specific context `chat.ChatContext`. We want to eliminate all cases |
| 9 | +of functions taking multiple contexts, or contexts not in the first parameter slot, etc. |
| 10 | +2. To retire `libkb.LoginState`, `libkb.LoginContext`, and `libkb.Session`, and to migrate |
| 11 | +their roles into `libkb.ActiveDevice`. |
| 12 | + |
| 13 | +The goal of `libkb.MetaContext` is to provide both thread-local (see `.Ctx` and `.activeDevice`) and global context (see `.g`). During signup, login or provisioning, we can store a thread local |
| 14 | +version of the `ActiveDevice` in the `MetaContext` so that the various login and provisioning |
| 15 | +routines can act upon it before exposing it to the rest of the program, since it's still provisional |
| 16 | +until login completes. Once the `ActiveDevice` becomes official, then all threads can access it |
| 17 | +via `GlobalContext`. |
| 18 | + |
| 19 | +There's a ton of changes we'd have to make in the code to achieve these goals, and |
| 20 | +we'd like to proceed in small piecemeal steps so that we can shake out any bugs |
| 21 | +as we go. |
| 22 | + |
| 23 | +# History |
| 24 | + |
| 25 | +We have a long and sordid history here, and it might be worth explaining a little bit |
| 26 | +of what happened before we describe the strategy for going forward. When we first |
| 27 | +started this project, the Go standard `context.Context` hadn't fully formed yet, |
| 28 | +so we didn't incorporate it. Instead, we had a notion of `GlobalContext` which applies to |
| 29 | +all threads. At first, all threads accessed this global context via a global variable `G`, |
| 30 | +but that strategy was terrible for many reasons, and made testing multiple instances of |
| 31 | +Keybase in the same address space near-impossible. Thus, we embarked upon a lengthy crusade |
| 32 | +to retire to `G` variable and use a combination of dependency-injection and just passing `G` |
| 33 | +wherever we could. |
| 34 | + |
| 35 | +Around the same time, we started to adopt the `context.Context` standard, especially |
| 36 | +for logging with request-specific tags (useful for debugging). These attempts were sometimes |
| 37 | +at odds, so we would up with an inconsistent ordering and placement of these contexts |
| 38 | +when passed to functions. Also, though were finally able to retire `G`, we did not succeed |
| 39 | +in fully threading `context.Context`s through the code; nor did we finish the project to always |
| 40 | +use `context.Context`-aware versions of logging functions. |
| 41 | + |
| 42 | +In addition, we've long had the LoginState/Account/LoginContext/Session family of objects |
| 43 | +to manage the user's logged-in state, and to shepherd the user through signup and device |
| 44 | +provisioning. We've experienced growing pains and bugs around the current configuration |
| 45 | +and long for a simplification. In particular, we're not happy with the Go-channel-based |
| 46 | +synchronization primitives at the heart of the state maintenance here, since it's easy |
| 47 | +to hit deadlocks hidden behind layers of abstraction. Instead, we want a simple lock-based |
| 48 | +model, where those locks are only held briefly, never during a network request (let's say). |
| 49 | + |
| 50 | +# Migration Strategy |
| 51 | + |
| 52 | +## Step 1: Use NIST Tokens for Session Establishment |
| 53 | + |
| 54 | +Status: **completed** |
| 55 | + |
| 56 | +It used to be the case we needed the exclusive lock over Account/LoginContext |
| 57 | +to make an API call, since it needed the user's session cookie (and CSRF |
| 58 | +token), and it was stored there. This setup made it very easy for API calls to |
| 59 | +fight over this locked resource and to stall, especially on application |
| 60 | +foregrounding or resumption from a long sleep. So the solution here is to |
| 61 | +authenticate a client to the server just based on a signature that the user |
| 62 | +can cook up with just her/his public key. Now, API calls are no longer |
| 63 | +dependent on Account/LoginContext, and instead depend on `ActiveDevice`, with |
| 64 | +the exception of provisioning and signup (i.e., before proper device keys are |
| 65 | +established). |
| 66 | + |
| 67 | +## Step 2: Propagate MetaContext from libkb outward |
| 68 | + |
| 69 | +Status: **ongoing** |
| 70 | + |
| 71 | +### Step 2a: Replace LoginContext with a wrapper MetaContext (Part 1) |
| 72 | + |
| 73 | +- Start with `LoginState`-related functions and propagate outwards. Cover `ActiveDevice`, |
| 74 | +`PerUserKey`, and bubble up into `engine/` too, but only as necessary. |
| 75 | + |
| 76 | +### Step 2b: Replace LoginContext with a wrapper MetaContext (Part 2) |
| 77 | + |
| 78 | +- Continue with `stellar/` and `ephemeral/` to replace those functions that take |
| 79 | +both `context.Context` and `*GlobalContext` to take only `libkb.MetaContext`. |
| 80 | + |
| 81 | +### Step 2c: Move `engine.Context` into `libkb.MetaContext` |
| 82 | + |
| 83 | +- And then change all `engine/` code to take only the `libkb.MetaContext` |
| 84 | + |
| 85 | +## Step 3: Retire LoginState |
| 86 | + |
| 87 | +Once we get to this point, things are a little less clear. The advantage of having |
| 88 | +done step 2 first is that a lot of times, we check for a thread-local `LoginContext` |
| 89 | +and then fallback to one that we grab from the global state. A lot of code is |
| 90 | +duplicated to handle these two cases, since the access pattern is different. |
| 91 | +One strategy here might be to move to a `LoginContext`-like object that can be safely |
| 92 | +copied, so it's no longer necessary to operate on it from outside of a closure. |
| 93 | + |
| 94 | +### Step 3a: Move LoginSession into ActiveDevice |
| 95 | + |
| 96 | +### Step 3b: Move PassphraseStream into ActiveDevice |
| 97 | + |
| 98 | +### Step 3c: Kill LoginContext |
| 99 | + |
| 100 | +### Step 3d: Kill LoginState |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | + |
0 commit comments