gui: the management API leaves a key let use Magpie Memory alone
4e24994a6 let a gateway key the user picks use Magpie Memory from another
computer (access.Key.Memory). memory-key is left out of the management
API, but rotate-key is not: an admin key, which may manage magpie and
nothing more, could rotate a Memory key through POST
/v1/magpie/admin/gateway-keys/rotate-key and get its new secret back (or
set a secret of its own). With it, it read and wrote memory, whose digest
goes into every agent's instructions on this computer.
adminMayChangeKey now refuses any action on a key with Memory, as it
already did for a key that may manage magpie; the caller's own key stays
its to change. TestAdminAPILeavesMemoryKeys covers rotate-key, with and
without a chosen secret, and memory-key.
Run on mica at this tree:
- go test ./internal/gui ./internal/access: access ok; gui red once on
the first run, and the log of that run wasn't kept, so the failing test
isn't named. Two more runs of ./internal/gui, logged whole: ok, ok.
- go vet ./internal/gui ./internal/access on darwin, GOOS=windows on
mica, and -tags gtk3 on Linux (wsl-run, magpie-desktop): clean.
- gofmt -l: clean.
Broken by hand: with the new check off, the test is red. rotate-key
answers 200 with the Memory key's secret, and the caller's chosen secret
opens "Remote box".
Not tried: a running gateway with a real remote admin key.
This commit is contained in:
+10
-2
@@ -167,8 +167,12 @@ func adminAPI() http.Handler {
|
||||
// computer only would become one allowed from anywhere, or take over a
|
||||
// peer's; and turning one off or removing it locks its owner out. A key
|
||||
// that manages magpie is changed by the command line and the app, as its
|
||||
// Admin is set there (admin-key is left out above). The caller's own key
|
||||
// stays its to change. The body is read and put back for the handler.
|
||||
// Admin is set there (admin-key is left out above). So is a key let use
|
||||
// Magpie Memory from another computer (access.Key.Memory): its secret
|
||||
// reads and writes what goes into every agent's instructions here, which
|
||||
// managing magpie doesn't give, and memory-key is left out above too. The
|
||||
// caller's own key stays its to change. The body is read and put back for
|
||||
// the handler.
|
||||
func adminMayChangeKey(w http.ResponseWriter, r *http.Request) bool {
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
@@ -196,6 +200,10 @@ func adminMayChangeKey(w http.ResponseWriter, r *http.Request) bool {
|
||||
adminError(w, http.StatusForbidden, "this key may manage magpie, so the management API doesn't change it: use `magpie gateway-key` or the app")
|
||||
return false
|
||||
}
|
||||
if k.ID == in.Key && k.Memory {
|
||||
adminError(w, http.StatusForbidden, "this key may use Magpie Memory from another computer, so the management API doesn't change it: use `magpie gateway-key` or the app")
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -189,3 +189,58 @@ func TestAdminAPILeavesOtherManagingKeys(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// A key let use Magpie Memory from another computer (access.Key.Memory)
|
||||
// is left to the command line and the app as a managing key is: rotating
|
||||
// it through the API would hand a managing key its secret, and with it
|
||||
// what goes into every agent's instructions here, which managing magpie
|
||||
// doesn't give.
|
||||
func TestAdminAPILeavesMemoryKeys(t *testing.T) {
|
||||
sandboxHome(t)
|
||||
add := func(name string) string {
|
||||
t.Helper()
|
||||
if _, err := access.Update("add-key", access.Change{Name: name}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
keys, _ := access.List()
|
||||
return keys[len(keys)-1].ID
|
||||
}
|
||||
admin, mem := add("Admin"), add("Remote box")
|
||||
if _, err := access.Update("admin-key", access.Change{Key: admin, Admin: access.AdminAny}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := access.Update("memory-key", access.Change{Key: mem, Memory: true}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
as := func(action, body string) *httptest.ResponseRecorder {
|
||||
t.Helper()
|
||||
r := httptest.NewRequest("POST", "/gateway-keys/"+action, strings.NewReader(body))
|
||||
r = r.WithContext(access.WithIdentity(r.Context(), access.Identity{KeyID: admin, Admin: access.AdminAny}))
|
||||
w := httptest.NewRecorder()
|
||||
serveAdmin(w, r)
|
||||
return w
|
||||
}
|
||||
for _, c := range []struct{ action, body string }{
|
||||
{"rotate-key", `{"key":"` + mem + `"}`},
|
||||
{"rotate-key", `{"key":"` + mem + `","secret":"chosen-by-the-caller-5678"}`},
|
||||
{"memory-key", `{"key":"` + admin + `","memory":true}`},
|
||||
{"memory-key", `{"key":"` + mem + `"}`},
|
||||
} {
|
||||
w := as(c.action, c.body)
|
||||
if w.Code != http.StatusForbidden && w.Code != http.StatusNotFound {
|
||||
t.Errorf("%s %s: %d, want 403 or 404 (%s)", c.action, c.body, w.Code, w.Body)
|
||||
}
|
||||
if strings.Contains(w.Body.String(), `"secret":`) {
|
||||
t.Errorf("%s told a secret: %s", c.action, w.Body)
|
||||
}
|
||||
}
|
||||
if who, ok := access.Authenticate("chosen-by-the-caller-5678"); ok {
|
||||
t.Errorf("the caller's chosen secret opens %q", who.KeyName)
|
||||
}
|
||||
keys, _ := access.List()
|
||||
for _, k := range keys {
|
||||
if k.ID == mem && !k.Memory || k.ID == admin && k.Memory {
|
||||
t.Errorf("memory changed through the API: %+v", k)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user