|
| 1 | +package handlers |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/base64" |
| 5 | + "encoding/json" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "strconv" |
| 10 | + "strings" |
| 11 | + "testing" |
| 12 | + |
| 13 | + "infix/webui/internal/restconf" |
| 14 | +) |
| 15 | + |
| 16 | +// fakeSupportRPC serves infix-system:support-collect, recording the input |
| 17 | +// it received and answering with reply. |
| 18 | +func fakeSupportRPC(t *testing.T, reply string, status int) (*httptest.Server, *map[string]any) { |
| 19 | + t.Helper() |
| 20 | + var got map[string]any |
| 21 | + srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 22 | + if r.URL.Path != "/operations/infix-system:support-collect" || r.Method != http.MethodPost { |
| 23 | + t.Errorf("unexpected request %s %s", r.Method, r.URL.Path) |
| 24 | + http.Error(w, "unexpected", http.StatusNotFound) |
| 25 | + return |
| 26 | + } |
| 27 | + if user, pass, ok := r.BasicAuth(); !ok || user != "admin" || pass != "secret" { |
| 28 | + t.Errorf("credentials not forwarded: %q %q %v", user, pass, ok) |
| 29 | + } |
| 30 | + body, _ := io.ReadAll(r.Body) |
| 31 | + got = nil |
| 32 | + if len(body) > 0 { |
| 33 | + if err := json.Unmarshal(body, &got); err != nil { |
| 34 | + t.Errorf("input is not JSON: %v", err) |
| 35 | + } |
| 36 | + } |
| 37 | + w.Header().Set("Content-Type", "application/yang-data+json") |
| 38 | + w.WriteHeader(status) |
| 39 | + io.WriteString(w, reply) //nolint:errcheck |
| 40 | + })) |
| 41 | + t.Cleanup(srv.Close) |
| 42 | + return srv, &got |
| 43 | +} |
| 44 | + |
| 45 | +func supportRequest(srv *httptest.Server, form string) *httptest.ResponseRecorder { |
| 46 | + h := &SystemHandler{RC: restconf.NewClient(srv.URL, true)} |
| 47 | + req := httptest.NewRequest(http.MethodPost, "/maintenance/support-bundle", strings.NewReader(form)) |
| 48 | + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 49 | + req = req.WithContext(restconf.ContextWithCredentials(req.Context(), |
| 50 | + restconf.Credentials{Username: "admin", Password: "secret"})) |
| 51 | + rec := httptest.NewRecorder() |
| 52 | + h.SupportBundle(rec, req) |
| 53 | + return rec |
| 54 | +} |
| 55 | + |
| 56 | +func TestSupportBundleDownloadsArchive(t *testing.T) { |
| 57 | + archive := []byte("\x1f\x8b\x08not really gzip") |
| 58 | + reply := `{"infix-system:output":{"size":` + strconv.Itoa(len(archive)) + `,"data":"` + |
| 59 | + base64.StdEncoding.EncodeToString(archive) + `"}}` |
| 60 | + srv, got := fakeSupportRPC(t, reply, http.StatusOK) |
| 61 | + |
| 62 | + rec := supportRequest(srv, "") |
| 63 | + |
| 64 | + if rec.Code != http.StatusOK { |
| 65 | + t.Fatalf("status %d: %s", rec.Code, rec.Body.String()) |
| 66 | + } |
| 67 | + if *got != nil { |
| 68 | + t.Errorf("no password given, but input sent: %v", *got) |
| 69 | + } |
| 70 | + if rec.Body.String() != string(archive) { |
| 71 | + t.Errorf("body is not the decoded archive: %q", rec.Body.String()) |
| 72 | + } |
| 73 | + if ct := rec.Header().Get("Content-Type"); ct != "application/gzip" { |
| 74 | + t.Errorf("Content-Type %q", ct) |
| 75 | + } |
| 76 | + cd := rec.Header().Get("Content-Disposition") |
| 77 | + if !strings.HasPrefix(cd, `attachment; filename="support-`) || !strings.HasSuffix(cd, `.tar.gz"`) { |
| 78 | + t.Errorf("Content-Disposition %q", cd) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +func TestSupportBundlePasswordEncrypts(t *testing.T) { |
| 83 | + reply := `{"infix-system:output":{"size":3,"data":"` + base64.StdEncoding.EncodeToString([]byte("gpg")) + `"}}` |
| 84 | + srv, got := fakeSupportRPC(t, reply, http.StatusOK) |
| 85 | + |
| 86 | + rec := supportRequest(srv, "password=hunter2") |
| 87 | + |
| 88 | + if rec.Code != http.StatusOK { |
| 89 | + t.Fatalf("status %d: %s", rec.Code, rec.Body.String()) |
| 90 | + } |
| 91 | + input, _ := (*got)["infix-system:input"].(map[string]any) |
| 92 | + if input["password"] != "hunter2" { |
| 93 | + t.Errorf("password not passed to the RPC: %v", *got) |
| 94 | + } |
| 95 | + if ct := rec.Header().Get("Content-Type"); ct != "application/pgp-encrypted" { |
| 96 | + t.Errorf("Content-Type %q", ct) |
| 97 | + } |
| 98 | + if !strings.HasSuffix(rec.Header().Get("Content-Disposition"), `.tar.gz.gpg"`) { |
| 99 | + t.Errorf("Content-Disposition %q", rec.Header().Get("Content-Disposition")) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +func TestSupportBundleTooLarge(t *testing.T) { |
| 104 | + reply := `{"infix-system:output":{"size":20000000,"filename":"/var/lib/support/support-host-x.tar.gz"}}` |
| 105 | + srv, _ := fakeSupportRPC(t, reply, http.StatusOK) |
| 106 | + |
| 107 | + rec := supportRequest(srv, "") |
| 108 | + |
| 109 | + if rec.Code != http.StatusInternalServerError { |
| 110 | + t.Fatalf("status %d", rec.Code) |
| 111 | + } |
| 112 | + if !strings.Contains(rec.Body.String(), "19 MB") || |
| 113 | + !strings.Contains(rec.Body.String(), "/var/lib/support/support-host-x.tar.gz") { |
| 114 | + t.Errorf("message does not say where the archive is: %q", rec.Body.String()) |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +func TestSupportBundleReportsRPCError(t *testing.T) { |
| 119 | + reply := `{"ietf-restconf:errors":{"error":[{"error-type":"application","error-tag":"operation-failed",` + |
| 120 | + `"error-message":"gpg is not available on this device"}]}}` |
| 121 | + srv, _ := fakeSupportRPC(t, reply, http.StatusInternalServerError) |
| 122 | + |
| 123 | + rec := supportRequest(srv, "password=x") |
| 124 | + |
| 125 | + if rec.Code != http.StatusInternalServerError { |
| 126 | + t.Fatalf("status %d", rec.Code) |
| 127 | + } |
| 128 | + if !strings.Contains(rec.Body.String(), "gpg is not available on this device") { |
| 129 | + t.Errorf("server's reason not surfaced: %q", rec.Body.String()) |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +func TestSupportBundleDeniedIsForbidden(t *testing.T) { |
| 134 | + reply := `{"ietf-restconf:errors":{"error":[{"error-type":"application","error-tag":"access-denied",` + |
| 135 | + `"error-message":"Access denied."}]}}` |
| 136 | + srv, _ := fakeSupportRPC(t, reply, http.StatusForbidden) |
| 137 | + |
| 138 | + rec := supportRequest(srv, "") |
| 139 | + |
| 140 | + if rec.Code != http.StatusForbidden { |
| 141 | + t.Fatalf("status %d", rec.Code) |
| 142 | + } |
| 143 | + if !strings.Contains(rec.Body.String(), "Access denied") { |
| 144 | + t.Errorf("reason not surfaced: %q", rec.Body.String()) |
| 145 | + } |
| 146 | +} |
0 commit comments