Files
nmon/internal/api/m6_test.go

355 lines
14 KiB
Go

package api_test
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"mime/multipart"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
"time"
"github.com/v10networks/nmon/internal/api"
"github.com/v10networks/nmon/internal/auth"
"github.com/v10networks/nmon/internal/auth/authtest"
"github.com/v10networks/nmon/internal/store"
)
// call sends a request through the auth middleware with the given credentials.
func (e *authedEnv) call(method, path, body string, c authtest.Creds) *httptest.ResponseRecorder {
req := httptest.NewRequest(method, path, strings.NewReader(body))
if body != "" {
req.Header.Set("Content-Type", "application/json")
}
if c.Token != "" {
req.Header.Set("Authorization", "Bearer "+c.Token)
}
if c.Cookie != nil {
req.AddCookie(c.Cookie)
req.Header.Set(auth.CSRFHeader, c.CSRF)
}
rec := httptest.NewRecorder()
e.top.ServeHTTP(rec, req)
return rec
}
func TestAPITokensOfEachKindMintListAndRevokeWithTheRightScope(t *testing.T) {
e := newAuthedEnv(t, nil)
adminSess, _ := authtest.Mint(t, e.auth, auth.RoleAdmin)
viewerSess, _ := authtest.Mint(t, e.auth, auth.RoleViewer)
dev := e.device(t, "core-1", "dc1", "core")
mint := func(body string) (id, secret string) {
rec := e.call(http.MethodPost, "/api/v1/tokens", body, adminSess)
if rec.Code != http.StatusCreated {
t.Fatalf("mint %s: %d %s", body, rec.Code, rec.Body)
}
var v struct{ ID, Secret string }
if err := json.Unmarshal(rec.Body.Bytes(), &v); err != nil {
t.Fatal(err)
}
return v.ID, v.Secret
}
vid, vsecret := mint(`{"name":"grafana","scope":"viewer"}`)
_, asecret := mint(`{"name":"ci","scope":"admin"}`)
_, psecret := mint(fmt.Sprintf(`{"name":"pve","scope":"push","devices":[%q]}`, dev.Slug))
if !strings.HasPrefix(vsecret, "nma_") || !strings.HasPrefix(asecret, "nma_") || !strings.HasPrefix(psecret, "nmp_") {
t.Fatalf("secret prefixes: %q %q %q", vsecret, asecret, psecret)
}
viewer, admin, push := authtest.Creds{Token: vsecret}, authtest.Creds{Token: asecret}, authtest.Creds{Token: psecret}
for _, tc := range []struct {
name string
c authtest.Creds
method string
path string
want int
}{
{"viewer token reads", viewer, http.MethodGet, "/api/v1/devices", http.StatusOK},
{"viewer token cannot PUT", viewer, http.MethodPut, "/api/v1/devices/" + dev.Slug, http.StatusForbidden},
{"viewer token cannot list tokens", viewer, http.MethodGet, "/api/v1/tokens", http.StatusForbidden},
{"admin token PUTs", admin, http.MethodPut, "/api/v1/devices/" + dev.Slug, http.StatusOK},
{"admin token lists tokens", admin, http.MethodGet, "/api/v1/tokens", http.StatusOK},
{"push token cannot read", push, http.MethodGet, "/api/v1/devices", http.StatusForbidden},
} {
body := ""
if tc.method == http.MethodPut {
body = `{"name":"core-1"}`
}
if got := e.call(tc.method, tc.path, body, tc.c).Code; got != tc.want {
t.Errorf("%s: %s %s = %d, want %d", tc.name, tc.method, tc.path, got, tc.want)
}
}
if code := e.call(http.MethodGet, "/api/v1/tokens", "", viewerSess).Code; code != http.StatusForbidden {
t.Errorf("a viewer session listing tokens: %d, want 403", code)
}
list := e.call(http.MethodGet, "/api/v1/tokens", "", admin)
for _, secret := range []string{vsecret, asecret, psecret} {
if strings.Contains(list.Body.String(), secret) || strings.Contains(list.Body.String(), "$") {
t.Fatalf("the token list leaks a secret or hash: %s", list.Body)
}
}
var items struct {
Items []struct{ ID, Name, Scope string }
}
if err := json.Unmarshal(list.Body.Bytes(), &items); err != nil || len(items.Items) != 5 { // three minted here and the two Mint made for the sessions
t.Fatalf("list: %v %s", err, list.Body)
}
if code := e.call(http.MethodDelete, "/api/v1/tokens/"+vid, "", admin).Code; code != http.StatusNoContent {
t.Fatalf("revoke the viewer token: %d", code)
}
if code := e.call(http.MethodGet, "/api/v1/devices", "", viewer).Code; code != http.StatusUnauthorized {
t.Fatalf("a revoked token must be refused at once: %d", code)
}
if code := e.call(http.MethodGet, "/api/v1/devices", "", admin).Code; code != http.StatusOK {
t.Fatalf("revoking one token must not affect another: %d", code)
}
if code := e.call(http.MethodPost, "/api/v1/tokens", `{"name":"x","scope":"admin","devices":["`+dev.Slug+`"]}`, adminSess).Code; code != http.StatusUnprocessableEntity {
t.Fatalf("an admin token bound to devices: %d, want 422", code)
}
}
func TestMeReportsTheCaller(t *testing.T) {
e := newAuthedEnv(t, nil)
viewerSess, _ := authtest.Mint(t, e.auth, auth.RoleViewer)
var me struct{ User, Role, Via, CSRF string }
rec := e.call(http.MethodGet, "/api/v1/me", "", viewerSess)
if err := json.Unmarshal(rec.Body.Bytes(), &me); err != nil || me.Role != "viewer" || me.Via != "session" || me.CSRF != viewerSess.CSRF {
t.Fatalf("me = %+v (%v) %s", me, err, rec.Body)
}
}
func TestEveryAuditedActionNamesThePrincipalNotAnonymous(t *testing.T) {
e := newAuthedEnv(t, nil)
adminSess, _ := authtest.Mint(t, e.auth, auth.RoleAdmin)
dev := e.device(t, "core-1", "dc1", "core")
if rec := e.call(http.MethodPut, "/api/v1/devices/"+dev.Slug, `{"site":"dc2"}`, adminSess); rec.Code != http.StatusOK {
t.Fatalf("PUT: %d %s", rec.Code, rec.Body)
}
rows, _, err := e.st.ListAudit(context.Background(), store.Page{Limit: 50})
if err != nil {
t.Fatal(err)
}
found := false
for _, r := range rows {
if r.Action == "device.edit" {
found = true
if r.Actor != "oidc:authtest-admin" {
t.Errorf("device.edit actor %q, want the session's subject", r.Actor)
}
}
if r.Actor == "anonymous" {
t.Errorf("audit row %s has the actor anonymous", r.Action)
}
}
if !found {
t.Fatalf("no device.edit audit row in %+v", rows)
}
}
// fakeSettings records what the API asked and refuses invalid documents.
type fakeSettings struct {
mu sync.Mutex
yaml string
last string
}
func (f *fakeSettings) Current(context.Context) (string, []string, error) {
f.mu.Lock()
defer f.mu.Unlock()
return f.yaml, []string{"retention"}, nil
}
func (f *fakeSettings) Apply(_ context.Context, actor, y string) (api.SettingsResult, error) {
f.mu.Lock()
defer f.mu.Unlock()
if strings.Contains(y, "bad") {
return api.SettingsResult{}, fmt.Errorf("%w: retention.hot must be at least 1d", api.ErrInvalidSettings)
}
f.yaml, f.last = y, actor
return api.SettingsResult{Applied: []string{"backup"}}, nil
}
func TestSettingsInvalidIs422AndUnchangedValidIsApplied(t *testing.T) {
fs := &fakeSettings{yaml: "timezone: UTC\n"}
e := newAuthedEnv(t, func(d *api.Deps) { d.Settings = fs })
adminSess, _ := authtest.Mint(t, e.auth, auth.RoleAdmin)
viewerSess, _ := authtest.Mint(t, e.auth, auth.RoleViewer)
if code := e.call(http.MethodGet, "/api/v1/settings", "", viewerSess).Code; code != http.StatusForbidden {
t.Fatalf("a viewer reading settings: %d, want 403", code)
}
rec := e.call(http.MethodPut, "/api/v1/settings", `{"yaml":"bad: 1"}`, adminSess)
if rec.Code != http.StatusUnprocessableEntity || !strings.Contains(rec.Body.String(), "retention.hot") {
t.Fatalf("invalid settings: %d %s", rec.Code, rec.Body)
}
if fs.yaml != "timezone: UTC\n" {
t.Fatalf("an invalid document changed the configuration: %q", fs.yaml)
}
rec = e.call(http.MethodPut, "/api/v1/settings", `{"yaml":"backup: {at: \"03:00\"}"}`, adminSess)
var out struct {
Applied []string `json:"applied"`
RestartRequired []string `json:"restart_required"`
}
if err := json.Unmarshal(rec.Body.Bytes(), &out); err != nil || rec.Code != http.StatusOK || len(out.Applied) != 1 || out.Applied[0] != "backup" {
t.Fatalf("valid settings: %d %s", rec.Code, rec.Body)
}
if fs.last != "oidc:authtest-admin" {
t.Fatalf("the change was attributed to %q", fs.last)
}
}
func multipartBody(t *testing.T, files map[string]string) (*bytes.Buffer, string) {
t.Helper()
var buf bytes.Buffer
w := multipart.NewWriter(&buf)
for field, content := range files {
fw, err := w.CreateFormFile(field, field+".csv")
if err != nil {
t.Fatal(err)
}
_, _ = fw.Write([]byte(content))
}
if err := w.Close(); err != nil {
t.Fatal(err)
}
return &buf, w.FormDataContentType()
}
func (e *authedEnv) importCSV(t *testing.T, c authtest.Creds, files map[string]string) *httptest.ResponseRecorder {
t.Helper()
body, ct := multipartBody(t, files)
req := httptest.NewRequest(http.MethodPost, "/api/v1/devices/import", body)
req.Header.Set("Content-Type", ct)
req.AddCookie(c.Cookie)
req.Header.Set(auth.CSRFHeader, c.CSRF)
rec := httptest.NewRecorder()
e.top.ServeHTTP(rec, req)
return rec
}
func TestImportCycleIs422WithPathAndWritesNothingThenAValidImportLands(t *testing.T) {
var imported []int64
e := newAuthedEnv(t, func(d *api.Deps) { d.OnImported = func(ids []int64) { imported = append(imported, ids...) } })
adminSess, _ := authtest.Mint(t, e.auth, auth.RoleAdmin)
cyc := "name,address,parents\na,192.0.2.1,c\nb,192.0.2.2,a\nc,192.0.2.3,b\n"
rec := e.importCSV(t, adminSess, map[string]string{"devices": cyc})
if rec.Code != http.StatusUnprocessableEntity || !strings.Contains(rec.Body.String(), "->") || !strings.Contains(rec.Body.String(), "cycle") {
t.Fatalf("cycle import: %d %s", rec.Code, rec.Body)
}
if _, n, err := e.st.ListDevices(context.Background(), store.DeviceFilter{}, store.Page{Limit: 10}); err != nil || n != 0 {
t.Fatalf("a rejected import wrote %d devices (%v)", n, err)
}
bad := "name,address\n,192.0.2.1\n"
rec = e.importCSV(t, adminSess, map[string]string{"devices": bad})
if rec.Code != http.StatusUnprocessableEntity || !strings.Contains(rec.Body.String(), "devices.csv row 2") {
t.Fatalf("bad row: %d %s", rec.Code, rec.Body)
}
ok := "name,address,site,role,parents\ncore,192.0.2.1,dc1,core,\nedge,192.0.2.2,dc1,edge,core\n"
rec = e.importCSV(t, adminSess, map[string]string{"devices": ok})
var res struct {
DevicesCreated int `json:"devices_created"`
}
if err := json.Unmarshal(rec.Body.Bytes(), &res); err != nil || rec.Code != http.StatusOK || res.DevicesCreated != 2 {
t.Fatalf("valid import: %d %s", rec.Code, rec.Body)
}
if len(imported) != 2 {
t.Fatalf("new devices were not handed to discovery: %v", imported)
}
viewerSess, _ := authtest.Mint(t, e.auth, auth.RoleViewer)
if rec := e.importCSV(t, viewerSess, map[string]string{"devices": ok}); rec.Code != http.StatusForbidden {
t.Fatalf("a viewer importing: %d", rec.Code)
}
}
func TestPortDescriptionImportSetsTheOperatorOwnedFieldAndSearchFindsIt(t *testing.T) {
e := newAuthedEnv(t, nil)
adminSess, _ := authtest.Mint(t, e.auth, auth.RoleAdmin)
d := e.device(t, "sw1", "dc1", "edge")
ifc := e.iface(t, d, 1, "Gi1/0/1", "uplink from device")
rec := e.importCSV(t, adminSess, map[string]string{"ports": "device,ifname,alias\nsw1,Gi1/0/1,Customer ACME Corp\n"})
if rec.Code != http.StatusOK {
t.Fatalf("ports import: %d %s", rec.Code, rec.Body)
}
got, err := e.st.GetInterface(context.Background(), ifc.ID)
if err != nil || got.Description != "Customer ACME Corp" || got.IfAlias != "uplink from device" {
t.Fatalf("interface = %+v (%v): the description must be set and the discovered alias left alone", got, err)
}
var list struct {
Items []struct{ Description, IfAlias string }
}
rec = e.call(http.MethodGet, "/api/v1/interfaces?q=acme", "", adminSess)
if err := json.Unmarshal(rec.Body.Bytes(), &list); err != nil || len(list.Items) != 1 || list.Items[0].Description != "Customer ACME Corp" {
t.Fatalf("search by description: %d %s", rec.Code, rec.Body)
}
var dv struct {
Interfaces []struct{ Description string }
}
rec = e.call(http.MethodGet, "/api/v1/devices/"+d.Slug, "", adminSess)
if err := json.Unmarshal(rec.Body.Bytes(), &dv); err != nil || len(dv.Interfaces) != 1 || dv.Interfaces[0].Description != "Customer ACME Corp" {
t.Fatalf("device page: %s", rec.Body)
}
}
type fakeSeries struct {
mu sync.Mutex
calls []string
err error
}
func (f *fakeSeries) DeleteSeries(_ context.Context, st string, m []string) error {
f.mu.Lock()
defer f.mu.Unlock()
f.calls = append(f.calls, st+":"+strings.Join(m, ","))
return f.err
}
func TestPurgeOnlyAfterThePeriodDeletesSeriesFromBothStoresAndAudits(t *testing.T) {
fs := &fakeSeries{}
e := newAuthedEnv(t, func(d *api.Deps) { d.Series = fs; d.PurgeAfter = func() time.Duration { return 30 * 24 * time.Hour } })
adminSess, _ := authtest.Mint(t, e.auth, auth.RoleAdmin)
active := e.device(t, "active", "dc1", "core")
old := e.device(t, "old", "dc1", "core")
e.iface(t, old, 1, "Gi1", "")
if err := e.st.RetireDevice(context.Background(), old.ID); err != nil {
t.Fatal(err)
}
if code := e.call(http.MethodPost, "/api/v1/devices/"+active.Slug+"/purge", "", adminSess).Code; code != http.StatusConflict {
t.Fatalf("purging an active device: %d, want 409", code)
}
if code := e.call(http.MethodPost, "/api/v1/devices/"+old.Slug+"/purge", "", adminSess).Code; code != http.StatusConflict || len(fs.calls) != 0 {
t.Fatalf("purging before the period: %d, deletions %v", code, fs.calls)
}
e.clk.Advance(31 * 24 * time.Hour)
adminSess, _ = authtest.Mint(t, e.auth, auth.RoleAdmin) // the first session expired meanwhile
fs.err = errors.New("store down")
if code := e.call(http.MethodPost, "/api/v1/devices/"+old.Slug+"/purge", "", adminSess).Code; code != http.StatusBadGateway {
t.Fatalf("purge with the store down: %d, want 502", code)
}
if d, _ := e.st.GetDeviceByID(context.Background(), old.ID); d.PurgedAt != nil {
t.Fatal("the device was marked purged although its series could not be deleted")
}
fs.err, fs.calls = nil, nil
if code := e.call(http.MethodPost, "/api/v1/devices/"+old.Slug+"/purge", "", adminSess).Code; code != http.StatusNoContent {
t.Fatalf("purge: %d", code)
}
if len(fs.calls) != 2 || !strings.HasPrefix(fs.calls[0], "hot:") || !strings.HasPrefix(fs.calls[1], "long:") ||
!strings.Contains(fs.calls[0], `device="`+old.Slug+`"`) || !strings.Contains(fs.calls[0], "iface=~") {
t.Fatalf("deletions: %v", fs.calls)
}
if d, _ := e.st.GetDeviceByID(context.Background(), old.ID); d.PurgedAt == nil {
t.Fatal("the device is not marked purged")
}
}