|
| 1 | +package auth |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "path/filepath" |
| 7 | + "slices" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/entireio/cli/internal/entireclient/contexts" |
| 12 | + "github.com/entireio/cli/internal/entireclient/tokenstore" |
| 13 | +) |
| 14 | + |
| 15 | +// testCoreURL is the login server every context in this file is recorded |
| 16 | +// against; seedLoginWithJurisdictionTokens keys its keyring slots off it. |
| 17 | +const testCoreURL = "https://core.example.com" |
| 18 | + |
| 19 | +// seedAccountWithJurisdictionTokens records a login context for `handle` |
| 20 | +// against testCoreURL, notes `audiences` on it, and files a jurisdiction access |
| 21 | +// token in each of those keyring slots under that handle — the state |
| 22 | +// git-remote-entire leaves behind after a few git operations. Returns the |
| 23 | +// context name and the core keyring service its login tokens live in. |
| 24 | +func seedAccountWithJurisdictionTokens(t *testing.T, handle string, audiences ...string) (name, coreService string) { |
| 25 | + t.Helper() |
| 26 | + |
| 27 | + exp := time.Now().Add(time.Hour).Unix() |
| 28 | + token := makeJWT(t, fmt.Sprintf(`{"iss":%q,"handle":%q,"exp":%d}`, testCoreURL, handle, exp)) |
| 29 | + name, err := RecordLoginContext(token, testRefreshToken, true) |
| 30 | + if err != nil { |
| 31 | + t.Fatalf("RecordLoginContext(%s): %v", handle, err) |
| 32 | + } |
| 33 | + |
| 34 | + for _, audience := range audiences { |
| 35 | + if err := RememberJurisdictionAudience(name, audience); err != nil { |
| 36 | + t.Fatalf("RememberJurisdictionAudience(%q): %v", audience, err) |
| 37 | + } |
| 38 | + if err := tokenstore.Set(tokenstore.JurisdictionService(audience), handle, "juri-jwt"); err != nil { |
| 39 | + t.Fatalf("seed jurisdiction token for %q: %v", audience, err) |
| 40 | + } |
| 41 | + } |
| 42 | + return name, tokenstore.CoreKeyringService(testCoreURL) |
| 43 | +} |
| 44 | + |
| 45 | +// seedLoginWithJurisdictionTokens is seedAccountWithJurisdictionTokens for the |
| 46 | +// single-account tests, which all use handle "alice". |
| 47 | +func seedLoginWithJurisdictionTokens(t *testing.T, audiences ...string) (name, coreService string) { |
| 48 | + t.Helper() |
| 49 | + return seedAccountWithJurisdictionTokens(t, "alice", audiences...) |
| 50 | +} |
| 51 | + |
| 52 | +// TestRemoveContext_DeletesJurisdictionTokens pins the logout contract for |
| 53 | +// data-plane credentials: the jurisdiction access tokens git-remote-entire |
| 54 | +// filed are bearers for every repo the account can reach, with an 8h |
| 55 | +// server-side TTL, so logout must delete them alongside the login slots rather |
| 56 | +// than leave them usable on the machine. |
| 57 | +func TestRemoveContext_DeletesJurisdictionTokens(t *testing.T) { |
| 58 | + cfgDir := t.TempDir() |
| 59 | + t.Setenv("ENTIRE_CONFIG_DIR", cfgDir) |
| 60 | + t.Cleanup(tokenstore.UseFileBackendForTesting(filepath.Join(t.TempDir(), "tokens.json"))) |
| 61 | + |
| 62 | + name, coreService := seedLoginWithJurisdictionTokens(t, |
| 63 | + "https://eu.example.io", "https://au.example.io/") |
| 64 | + |
| 65 | + if err := RemoveContext(name); err != nil { |
| 66 | + t.Fatalf("RemoveContext: %v", err) |
| 67 | + } |
| 68 | + |
| 69 | + for _, audience := range []string{"https://eu.example.io", "https://au.example.io/"} { |
| 70 | + svc := tokenstore.JurisdictionService(audience) |
| 71 | + if v, err := tokenstore.Get(svc, "alice"); !errors.Is(err, tokenstore.ErrNotFound) { |
| 72 | + t.Fatalf("jurisdiction token for %q survived logout: value=%q err=%v", audience, v, err) |
| 73 | + } |
| 74 | + } |
| 75 | + if v, err := tokenstore.Get(coreService, "alice"); !errors.Is(err, tokenstore.ErrNotFound) { |
| 76 | + t.Fatalf("access slot survived logout: value=%q err=%v", v, err) |
| 77 | + } |
| 78 | + if v, err := tokenstore.Get(tokenstore.RefreshService(coreService), "alice"); !errors.Is(err, tokenstore.ErrNotFound) { |
| 79 | + t.Fatalf("refresh slot survived logout: value=%q err=%v", v, err) |
| 80 | + } |
| 81 | + f, err := contexts.Load(cfgDir) |
| 82 | + if err != nil { |
| 83 | + t.Fatalf("load contexts: %v", err) |
| 84 | + } |
| 85 | + if f.Find(name) != nil { |
| 86 | + t.Fatalf("context %q should have been removed", name) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +// TestRemoveContext_LeavesOtherAccountsJurisdictionTokens pins the scope of the |
| 91 | +// sweep: jurisdiction slots are keyed by (audience, handle), and logout only |
| 92 | +// deletes the audiences recorded on the context it is removing, under that |
| 93 | +// context's own handle. Two accounts sharing a jurisdiction have separate slots, |
| 94 | +// so logging one out must leave the other's data-plane token — and its |
| 95 | +// bookkeeping — intact. |
| 96 | +func TestRemoveContext_LeavesOtherAccountsJurisdictionTokens(t *testing.T) { |
| 97 | + cfgDir := t.TempDir() |
| 98 | + t.Setenv("ENTIRE_CONFIG_DIR", cfgDir) |
| 99 | + t.Cleanup(tokenstore.UseFileBackendForTesting(filepath.Join(t.TempDir(), "tokens.json"))) |
| 100 | + |
| 101 | + // Both accounts have a token for the same jurisdiction, plus one audience |
| 102 | + // only bob ever reached. |
| 103 | + const shared = "https://eu.example.io" |
| 104 | + const bobOnly = "https://au.example.io" |
| 105 | + aliceName, _ := seedAccountWithJurisdictionTokens(t, "alice", shared) |
| 106 | + bobName, _ := seedAccountWithJurisdictionTokens(t, "bob", shared, bobOnly) |
| 107 | + |
| 108 | + if err := RemoveContext(aliceName); err != nil { |
| 109 | + t.Fatalf("RemoveContext(%s): %v", aliceName, err) |
| 110 | + } |
| 111 | + |
| 112 | + if v, err := tokenstore.Get(tokenstore.JurisdictionService(shared), "alice"); !errors.Is(err, tokenstore.ErrNotFound) { |
| 113 | + t.Fatalf("alice's jurisdiction token survived her logout: value=%q err=%v", v, err) |
| 114 | + } |
| 115 | + for _, audience := range []string{shared, bobOnly} { |
| 116 | + if _, err := tokenstore.Get(tokenstore.JurisdictionService(audience), "bob"); err != nil { |
| 117 | + t.Fatalf("bob's jurisdiction token for %q was deleted by alice's logout: %v", audience, err) |
| 118 | + } |
| 119 | + } |
| 120 | + f, err := contexts.Load(cfgDir) |
| 121 | + if err != nil { |
| 122 | + t.Fatalf("load contexts: %v", err) |
| 123 | + } |
| 124 | + bob := f.Find(bobName) |
| 125 | + if bob == nil { |
| 126 | + t.Fatalf("context %q was removed by alice's logout", bobName) |
| 127 | + } |
| 128 | + if !slices.Equal(bob.JurisdictionAudiences, []string{shared, bobOnly}) { |
| 129 | + t.Fatalf("bob's recorded audiences = %v, want [%s %s]", bob.JurisdictionAudiences, shared, bobOnly) |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +// TestRemoveCurrentContext_DeletesJurisdictionTokens covers the default |
| 134 | +// `entire logout` path (active context, not selected by name). |
| 135 | +func TestRemoveCurrentContext_DeletesJurisdictionTokens(t *testing.T) { |
| 136 | + t.Setenv("ENTIRE_CONFIG_DIR", t.TempDir()) |
| 137 | + t.Cleanup(tokenstore.UseFileBackendForTesting(filepath.Join(t.TempDir(), "tokens.json"))) |
| 138 | + |
| 139 | + const audience = "https://eu.example.io" |
| 140 | + seedLoginWithJurisdictionTokens(t, audience) |
| 141 | + |
| 142 | + if err := RemoveCurrentContext(); err != nil { |
| 143 | + t.Fatalf("RemoveCurrentContext: %v", err) |
| 144 | + } |
| 145 | + if v, err := tokenstore.Get(tokenstore.JurisdictionService(audience), "alice"); !errors.Is(err, tokenstore.ErrNotFound) { |
| 146 | + t.Fatalf("jurisdiction token survived logout: value=%q err=%v", v, err) |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +// TestRemoveContext_JurisdictionDeleteFailureAbortsLogout extends the existing |
| 151 | +// keychain-delete contract to the jurisdiction slots: a failed delete must |
| 152 | +// surface and leave the context entry in place for a retry, never report |
| 153 | +// success over a surviving data-plane bearer. |
| 154 | +func TestRemoveContext_JurisdictionDeleteFailureAbortsLogout(t *testing.T) { |
| 155 | + cfgDir := t.TempDir() |
| 156 | + t.Setenv("ENTIRE_CONFIG_DIR", cfgDir) |
| 157 | + path := filepath.Join(t.TempDir(), "tokens.json") |
| 158 | + seedRestore := tokenstore.UseFileBackendForTesting(path) |
| 159 | + |
| 160 | + const audience = "https://eu.example.io" |
| 161 | + name, coreService := seedLoginWithJurisdictionTokens(t, audience) |
| 162 | + seedRestore() |
| 163 | + |
| 164 | + jurisdictionSvc := tokenstore.JurisdictionService(audience) |
| 165 | + failJurisdictionDelete := func(service, _ string) bool { return service == jurisdictionSvc } |
| 166 | + t.Cleanup(tokenstore.UseFailingDeleteBackendForTesting(path, failJurisdictionDelete)) |
| 167 | + |
| 168 | + if err := RemoveContext(name); err == nil { |
| 169 | + t.Fatal("RemoveContext: want error when the jurisdiction-slot delete fails") |
| 170 | + } |
| 171 | + f, err := contexts.Load(cfgDir) |
| 172 | + if err != nil { |
| 173 | + t.Fatalf("load contexts: %v", err) |
| 174 | + } |
| 175 | + c := f.Find(name) |
| 176 | + if c == nil { |
| 177 | + t.Fatal("context entry was removed despite the failed credential delete") |
| 178 | + } |
| 179 | + if !slices.Contains(c.JurisdictionAudiences, audience) { |
| 180 | + t.Fatalf("recorded audiences = %v, want %q retained for the retry", c.JurisdictionAudiences, audience) |
| 181 | + } |
| 182 | + // The access slot is deleted after the jurisdiction slots, so the abort |
| 183 | + // must have left it alone. |
| 184 | + if _, err := tokenstore.Get(coreService, "alice"); err != nil { |
| 185 | + t.Fatalf("access slot should be untouched by the aborted logout: %v", err) |
| 186 | + } |
| 187 | +} |
| 188 | + |
| 189 | +func TestRememberJurisdictionAudience(t *testing.T) { |
| 190 | + cfgDir := t.TempDir() |
| 191 | + t.Setenv("ENTIRE_CONFIG_DIR", cfgDir) |
| 192 | + t.Cleanup(tokenstore.UseFileBackendForTesting(filepath.Join(t.TempDir(), "tokens.json"))) |
| 193 | + |
| 194 | + exp := time.Now().Add(time.Hour).Unix() |
| 195 | + name, err := RecordLoginContext(makeJWT(t, fmt.Sprintf(`{"iss":%q,"handle":"alice","exp":%d}`, testCoreURL, exp)), testRefreshToken, true) |
| 196 | + if err != nil { |
| 197 | + t.Fatalf("RecordLoginContext: %v", err) |
| 198 | + } |
| 199 | + |
| 200 | + // Recorded once, trailing slash trimmed so the audience matches the |
| 201 | + // keyring service name the writer and logout both derive. |
| 202 | + if err := RememberJurisdictionAudience(name, "https://eu.example.io/"); err != nil { |
| 203 | + t.Fatalf("first record: %v", err) |
| 204 | + } |
| 205 | + // Idempotent: the same audience (in either spelling) doesn't duplicate. |
| 206 | + if err := RememberJurisdictionAudience(name, "https://eu.example.io"); err != nil { |
| 207 | + t.Fatalf("duplicate record: %v", err) |
| 208 | + } |
| 209 | + if err := RememberJurisdictionAudience(name, "https://au.example.io"); err != nil { |
| 210 | + t.Fatalf("second audience: %v", err) |
| 211 | + } |
| 212 | + |
| 213 | + f, err := contexts.Load(cfgDir) |
| 214 | + if err != nil { |
| 215 | + t.Fatalf("load contexts: %v", err) |
| 216 | + } |
| 217 | + got := f.Find(name).JurisdictionAudiences |
| 218 | + want := []string{"https://eu.example.io", "https://au.example.io"} |
| 219 | + if !slices.Equal(got, want) { |
| 220 | + t.Fatalf("recorded audiences = %v, want %v", got, want) |
| 221 | + } |
| 222 | + |
| 223 | + // A context that isn't there can't be recorded against — the caller must |
| 224 | + // not then persist a token no logout could find. |
| 225 | + if err := RememberJurisdictionAudience("nope", "https://eu.example.io"); err == nil { |
| 226 | + t.Fatal("want error for an unknown context") |
| 227 | + } |
| 228 | + if err := RememberJurisdictionAudience(name, " "); err == nil { |
| 229 | + t.Fatal("want error for a blank audience") |
| 230 | + } |
| 231 | +} |
| 232 | + |
| 233 | +// TestRecordLoginContext_ReloginKeepsJurisdictionAudiences guards the upsert: |
| 234 | +// re-logging in replaces the context entry, but the jurisdiction tokens in the |
| 235 | +// keychain (keyed by audience + handle, not by login session) survive it — so |
| 236 | +// dropping the list would strand them beyond any future logout. |
| 237 | +func TestRecordLoginContext_ReloginKeepsJurisdictionAudiences(t *testing.T) { |
| 238 | + cfgDir := t.TempDir() |
| 239 | + t.Setenv("ENTIRE_CONFIG_DIR", cfgDir) |
| 240 | + t.Cleanup(tokenstore.UseFileBackendForTesting(filepath.Join(t.TempDir(), "tokens.json"))) |
| 241 | + |
| 242 | + const audience = "https://eu.example.io" |
| 243 | + name, _ := seedLoginWithJurisdictionTokens(t, audience) |
| 244 | + |
| 245 | + exp := time.Now().Add(2 * time.Hour).Unix() |
| 246 | + again, err := RecordLoginContext(makeJWT(t, fmt.Sprintf(`{"iss":%q,"handle":"alice","exp":%d}`, testCoreURL, exp)), testRefreshToken, true) |
| 247 | + if err != nil { |
| 248 | + t.Fatalf("re-login: %v", err) |
| 249 | + } |
| 250 | + if again != name { |
| 251 | + t.Fatalf("re-login produced context %q, want %q", again, name) |
| 252 | + } |
| 253 | + |
| 254 | + f, err := contexts.Load(cfgDir) |
| 255 | + if err != nil { |
| 256 | + t.Fatalf("load contexts: %v", err) |
| 257 | + } |
| 258 | + if got := f.Find(name).JurisdictionAudiences; !slices.Equal(got, []string{audience}) { |
| 259 | + t.Fatalf("recorded audiences after re-login = %v, want [%s]", got, audience) |
| 260 | + } |
| 261 | +} |
0 commit comments