Manage external site-prober actions va API and Dashboard
This commit is contained in:
1 parent
f22ad569b6
commit
42f584dd3c
28 files changed
+717
-32
No files matched your search
@@ -213,3 +213,16 @@ func (c *client) PutOrchestratorSettings(ctx context.Context, fipSettleSeconds i
|
||||
orchestratorSettingsDTO{FIPSettleSeconds: fipSettleSeconds}, &out)
|
||||
return out, err
|
||||
}
|
||||
|
||||
func (c *client) GetInboundChecks(ctx context.Context) (inboundChecksDTO, error) {
|
||||
var out inboundChecksDTO
|
||||
err := c.do(ctx, http.MethodGet, "/api/v1/admin/config/inbound-checks", nil, &out)
|
||||
return out, err
|
||||
}
|
||||
|
||||
func (c *client) PutInboundChecks(ctx context.Context, ports []int, icmp bool) (inboundChecksDTO, error) {
|
||||
var out inboundChecksDTO
|
||||
err := c.do(ctx, http.MethodPut, "/api/v1/admin/config/inbound-checks",
|
||||
inboundChecksDTO{Ports: ports, ICMP: icmp}, &out)
|
||||
return out, err
|
||||
}
|
||||
@@ -29,6 +29,8 @@ type fakeControlAPI struct {
|
||||
groups map[string][]string
|
||||
checkTypes map[string]checkTypeDTO
|
||||
fipSettleSeconds int
|
||||
inboundPorts []int
|
||||
inboundICMP bool
|
||||
}
|
||||
|
||||
func newFakeControlAPI(t *testing.T) (*fakeControlAPI, string) {
|
||||
@@ -206,6 +208,27 @@ func (f *fakeControlAPI) handler() http.Handler {
|
||||
writeJSON(w, http.StatusOK, orchestratorSettingsDTO{FIPSettleSeconds: f.fipSettleSeconds})
|
||||
})
|
||||
|
||||
mux.HandleFunc("GET /api/v1/admin/config/inbound-checks", func(w http.ResponseWriter, r *http.Request) {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
writeJSON(w, http.StatusOK, inboundChecksDTO{Ports: f.inboundPorts, ICMP: f.inboundICMP})
|
||||
})
|
||||
mux.HandleFunc("PUT /api/v1/admin/config/inbound-checks", func(w http.ResponseWriter, r *http.Request) {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
var req inboundChecksDTO
|
||||
_ = json.NewDecoder(r.Body).Decode(&req)
|
||||
for _, p := range req.Ports {
|
||||
if p < 1 || p > 65535 {
|
||||
writeAPIErr(w, http.StatusBadRequest, "port out of range")
|
||||
return
|
||||
}
|
||||
}
|
||||
f.inboundPorts = req.Ports
|
||||
f.inboundICMP = req.ICMP
|
||||
writeJSON(w, http.StatusOK, inboundChecksDTO{Ports: f.inboundPorts, ICMP: f.inboundICMP})
|
||||
})
|
||||
|
||||
mux.HandleFunc("GET /api/v1/admin/config/validators", func(w http.ResponseWriter, r *http.Request) {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
|
||||
@@ -128,3 +128,8 @@ type errorResponse struct {
|
||||
type orchestratorSettingsDTO struct {
|
||||
FIPSettleSeconds int `json:"fip_settle_seconds"`
|
||||
}
|
||||
|
||||
type inboundChecksDTO struct {
|
||||
Ports []int `json:"ports"`
|
||||
ICMP bool `json:"icmp"`
|
||||
}
|
||||
@@ -4,16 +4,22 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type settingsPageData struct {
|
||||
PageData
|
||||
Settings orchestratorSettingsDTO
|
||||
Inbound inboundChecksDTO
|
||||
}
|
||||
|
||||
func (s *Server) handleSettingsPage(w http.ResponseWriter, r *http.Request) {
|
||||
settings, err := s.CA.GetOrchestratorSettings(r.Context())
|
||||
data := settingsPageData{Settings: settings}
|
||||
inbound, inboundErr := s.CA.GetInboundChecks(r.Context())
|
||||
if err == nil {
|
||||
err = inboundErr
|
||||
}
|
||||
data := settingsPageData{Settings: settings, Inbound: inbound}
|
||||
data.ActiveNav = "settings"
|
||||
data.Banner = bannerFor(err)
|
||||
s.renderPage(w, "settings_page", data)
|
||||
@@ -28,7 +34,11 @@ func (s *Server) renderSettingsForm(w http.ResponseWriter, r *http.Request, acti
|
||||
if actionErr == nil {
|
||||
actionErr = getErr
|
||||
}
|
||||
s.renderFragment(w, "settings_form", settingsPageData{Settings: settings}, actionErr)
|
||||
inbound, inboundErr := s.CA.GetInboundChecks(r.Context())
|
||||
if actionErr == nil {
|
||||
actionErr = inboundErr
|
||||
}
|
||||
s.renderFragment(w, "settings_form", settingsPageData{Settings: settings, Inbound: inbound}, actionErr)
|
||||
}
|
||||
|
||||
func (s *Server) handleSettingsPut(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -44,3 +54,34 @@ func (s *Server) handleSettingsPut(w http.ResponseWriter, r *http.Request) {
|
||||
_, err = s.CA.PutOrchestratorSettings(r.Context(), seconds)
|
||||
s.renderSettingsForm(w, r, err)
|
||||
}
|
||||
|
||||
// handleInboundChecksPut parses the comma-separated ports field of the
|
||||
// second form on /settings and saves it via PUT
|
||||
// /api/v1/admin/config/inbound-checks. A separate form/handler from
|
||||
// fip_settle_seconds above — the two settings are unrelated and shouldn't
|
||||
// share one submit.
|
||||
func (s *Server) handleInboundChecksPut(w http.ResponseWriter, r *http.Request) {
|
||||
if err := r.ParseForm(); err != nil {
|
||||
s.renderSettingsForm(w, r, fmt.Errorf("invalid form: %w", err))
|
||||
return
|
||||
}
|
||||
raw := strings.TrimSpace(r.PostFormValue("ports"))
|
||||
var ports []int
|
||||
if raw != "" {
|
||||
for _, part := range strings.Split(raw, ",") {
|
||||
part = strings.TrimSpace(part)
|
||||
if part == "" {
|
||||
continue
|
||||
}
|
||||
p, err := strconv.Atoi(part)
|
||||
if err != nil {
|
||||
s.renderSettingsForm(w, r, &apiErr{Status: http.StatusBadRequest, Message: "порты должны быть целыми числами через запятую"})
|
||||
return
|
||||
}
|
||||
ports = append(ports, p)
|
||||
}
|
||||
}
|
||||
icmp := r.PostFormValue("icmp") == "on"
|
||||
_, err := s.CA.PutInboundChecks(r.Context(), ports, icmp)
|
||||
s.renderSettingsForm(w, r, err)
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package dashboard
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -207,6 +208,56 @@ func TestSettingsGetAndPut(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSettingsPageShowsInboundChecks(t *testing.T) {
|
||||
fake, caURL := newFakeControlAPI(t)
|
||||
fake.inboundPorts = []int{22, 443}
|
||||
fake.inboundICMP = true
|
||||
ts := newTestServer(t, caURL)
|
||||
|
||||
page := get(t, ts, "/settings")
|
||||
if !strings.Contains(page, `value="22, 443"`) {
|
||||
t.Fatalf("expected seeded ports in the form, got:\n%s", page)
|
||||
}
|
||||
if !strings.Contains(page, "checked") {
|
||||
t.Fatalf("expected icmp checkbox checked, got:\n%s", page)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInboundChecksPutUpdatesForm(t *testing.T) {
|
||||
fake, caURL := newFakeControlAPI(t)
|
||||
ts := newTestServer(t, caURL)
|
||||
|
||||
body := postForm(t, ts, "PUT", "/settings/inbound-checks", map[string][]string{"ports": {"8080, 8443"}})
|
||||
if !strings.Contains(body, `value="8080, 8443"`) {
|
||||
t.Fatalf("expected updated ports in re-rendered form, got:\n%s", body)
|
||||
}
|
||||
if !reflect.DeepEqual(fake.inboundPorts, []int{8080, 8443}) || fake.inboundICMP {
|
||||
t.Fatalf("expected fake control-api updated to {[8080 8443] false}, got ports=%v icmp=%v", fake.inboundPorts, fake.inboundICMP)
|
||||
}
|
||||
|
||||
// icmp checkbox checked, empty ports field.
|
||||
body = postForm(t, ts, "PUT", "/settings/inbound-checks", map[string][]string{"ports": {""}, "icmp": {"on"}})
|
||||
if len(fake.inboundPorts) != 0 || !fake.inboundICMP {
|
||||
t.Fatalf("expected fake control-api updated to {[] true}, got ports=%v icmp=%v", fake.inboundPorts, fake.inboundICMP)
|
||||
}
|
||||
if !strings.Contains(body, "checked") {
|
||||
t.Fatalf("expected icmp checkbox checked in re-rendered form, got:\n%s", body)
|
||||
}
|
||||
|
||||
// A malformed ports field is caught by the dashboard itself.
|
||||
body = postForm(t, ts, "PUT", "/settings/inbound-checks", map[string][]string{"ports": {"22, not-a-port"}})
|
||||
if !strings.Contains(body, "alert-warning") {
|
||||
t.Fatalf("expected client error banner for malformed ports, got:\n%s", body)
|
||||
}
|
||||
|
||||
// A control-api validation error (port out of range) surfaces via the
|
||||
// banner too.
|
||||
body = postForm(t, ts, "PUT", "/settings/inbound-checks", map[string][]string{"ports": {"70000"}})
|
||||
if !strings.Contains(body, "alert-warning") {
|
||||
t.Fatalf("expected client error banner for out-of-range port, got:\n%s", body)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIPsPageShowsSettleBadge(t *testing.T) {
|
||||
fake, caURL := newFakeControlAPI(t)
|
||||
now := time.Now()
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"errors"
|
||||
"html/template"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
@@ -87,12 +88,23 @@ func derefStr(s *string) string {
|
||||
return *s
|
||||
}
|
||||
|
||||
// joinInts renders a []int as a comma-separated string, for pre-filling the
|
||||
// inbound-checks ports form field on /settings.
|
||||
func joinInts(ints []int, sep string) string {
|
||||
parts := make([]string, len(ints))
|
||||
for i, n := range ints {
|
||||
parts[i] = strconv.Itoa(n)
|
||||
}
|
||||
return strings.Join(parts, sep)
|
||||
}
|
||||
|
||||
var funcMap = template.FuncMap{
|
||||
"ipBadge": ipBadge,
|
||||
"validatorBadge": validatorBadge,
|
||||
"fmtTime": fmtTime,
|
||||
"deref": derefStr,
|
||||
"join": strings.Join,
|
||||
"joinInts": joinInts,
|
||||
}
|
||||
|
||||
func parseTemplates() (*template.Template, error) {
|
||||
|
||||
@@ -38,6 +38,7 @@ func (s *Server) routes(mux *http.ServeMux) {
|
||||
|
||||
mux.HandleFunc("GET /settings", s.handleSettingsPage)
|
||||
mux.HandleFunc("PUT /settings", s.handleSettingsPut)
|
||||
mux.HandleFunc("PUT /settings/inbound-checks", s.handleInboundChecksPut)
|
||||
|
||||
mux.Handle("GET /static/", http.StripPrefix("/static/", http.FileServerFS(staticSubFS())))
|
||||
}
|
||||
|
||||
@@ -45,4 +45,26 @@
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="panel" style="margin-top:16px">
|
||||
<div class="panel-body">
|
||||
<p class="muted" style="margin-bottom:16px">Типы проверок, которые прогоняет <code>prober</code> на каждой
|
||||
настроенной площадке для каждого проверяемого адреса — глобальный набор, общий для всех площадок. Отдельно от
|
||||
списка площадок (<a href="/sites">/sites</a>), который решает, <em>сколько</em> площадок опрашивать, а не
|
||||
<em>что</em> именно они проверяют. Пустой список портов и выключенный ICMP — штатный способ временно свести
|
||||
inbound-проверки к нулю, не трогая список площадок.</p>
|
||||
<form hx-put="/settings/inbound-checks" hx-target="#settings-form-wrap" hx-swap="innerHTML">
|
||||
<div class="field-row">
|
||||
<div class="field">
|
||||
<label for="inbound_ports">TCP-порты (через запятую)</label>
|
||||
<input type="text" id="inbound_ports" name="ports" placeholder="22, 80, 443, 8080" value="{{joinInts .Inbound.Ports ", "}}">
|
||||
</div>
|
||||
<div class="field">
|
||||
<label for="inbound_icmp"><input type="checkbox" id="inbound_icmp" name="icmp" {{if .Inbound.ICMP}}checked{{end}}> ICMP</label>
|
||||
</div>
|
||||
<button type="submit" class="btn btn-primary">Сохранить</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
{{end}}
|
||||
Reference in new issue
Block a user