mirror of
https://github.com/henrygd/beszel.git
synced 2026-10-01 05:47:48 +02:00
fix(hub): pad short monitor IDs to the schema minimum (#2454)
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package hub
|
package hub
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -21,7 +22,8 @@ func generateMonitorID(systemId string, config monitor.Config) string {
|
|||||||
if config.Protocol == "dns" {
|
if config.Protocol == "dns" {
|
||||||
args = append(args, config.Server)
|
args = append(args, config.Server)
|
||||||
}
|
}
|
||||||
return systems.MakeStableHashId(args...)
|
// Meet the record ID minimum without changing existing IDs of six or more characters.
|
||||||
|
return fmt.Sprintf("%06s", systems.MakeStableHashId(args...))
|
||||||
}
|
}
|
||||||
|
|
||||||
// bindNetworkMonitorsEvents keeps monitor records and agent monitor state in sync.
|
// bindNetworkMonitorsEvents keeps monitor records and agent monitor state in sync.
|
||||||
|
|||||||
@@ -87,6 +87,18 @@ func TestGenerateMonitorID(t *testing.T) {
|
|||||||
config monitor.Config
|
config monitor.Config
|
||||||
expected string
|
expected string
|
||||||
}{
|
}{
|
||||||
|
{
|
||||||
|
name: "Short hash meets the record ID minimum",
|
||||||
|
systemID: "000000000012369",
|
||||||
|
config: monitor.Config{Protocol: "icmp", Target: "gateway.example.com"},
|
||||||
|
expected: "0b3fd2",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Existing six-character ID is unchanged",
|
||||||
|
systemID: "000000000000274",
|
||||||
|
config: monitor.Config{Protocol: "icmp", Target: "gateway.example.com"},
|
||||||
|
expected: "77a996",
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: "HTTP monitor on example.com",
|
name: "HTTP monitor on example.com",
|
||||||
systemID: "sys123",
|
systemID: "sys123",
|
||||||
@@ -217,6 +229,59 @@ func TestGenerateMonitorID(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestNetworkMonitorShortIDRequests(t *testing.T) {
|
||||||
|
for _, method := range []string{http.MethodPost, http.MethodPatch} {
|
||||||
|
t.Run(method, func(t *testing.T) {
|
||||||
|
hub, testApp, err := createTestHub(t)
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer cleanupTestHub(hub, testApp)
|
||||||
|
bindNetworkMonitorsEvents(hub)
|
||||||
|
user, err := createTestUser(hub)
|
||||||
|
require.NoError(t, err)
|
||||||
|
system, err := createTestRecord(hub, "systems", map[string]any{
|
||||||
|
"id": "000000000012369", "name": "Paused", "host": "localhost", "port": "45876",
|
||||||
|
"status": "paused", "users": []string{user.Id},
|
||||||
|
})
|
||||||
|
require.NoError(t, err)
|
||||||
|
payload := map[string]any{
|
||||||
|
"system": system.Id, "target": "gateway.example.com", "protocol": "icmp",
|
||||||
|
"interval": 60, "enabled": true,
|
||||||
|
}
|
||||||
|
url := "/api/collections/network_monitors/records"
|
||||||
|
var oldID string
|
||||||
|
if method == http.MethodPatch {
|
||||||
|
previous, err := createTestRecord(hub, "network_monitors", map[string]any{
|
||||||
|
"system": system.Id, "target": "website.example.com", "protocol": "icmp", "interval": 60,
|
||||||
|
})
|
||||||
|
require.NoError(t, err)
|
||||||
|
oldID = previous.Id
|
||||||
|
url += "/" + oldID
|
||||||
|
}
|
||||||
|
data, err := json.Marshal(payload)
|
||||||
|
require.NoError(t, err)
|
||||||
|
token, err := user.NewAuthToken()
|
||||||
|
require.NoError(t, err)
|
||||||
|
router, err := apis.NewRouter(hub)
|
||||||
|
require.NoError(t, err)
|
||||||
|
handler, err := router.BuildMux()
|
||||||
|
require.NoError(t, err)
|
||||||
|
request := httptest.NewRequest(method, url, bytes.NewReader(data))
|
||||||
|
request.Header.Set("Content-Type", "application/json")
|
||||||
|
request.Header.Set("Authorization", token)
|
||||||
|
response := httptest.NewRecorder()
|
||||||
|
handler.ServeHTTP(response, request)
|
||||||
|
require.Equal(t, http.StatusOK, response.Code, response.Body.String())
|
||||||
|
record, err := hub.FindRecordById("network_monitors", "0b3fd2")
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, "gateway.example.com", record.GetString("target"))
|
||||||
|
if oldID != "" {
|
||||||
|
_, err = hub.FindRecordById("network_monitors", oldID)
|
||||||
|
require.Error(t, err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestCopyMonitorToNewRecordDropsResultFields(t *testing.T) {
|
func TestCopyMonitorToNewRecordDropsResultFields(t *testing.T) {
|
||||||
hub, testApp, err := createTestHub(t)
|
hub, testApp, err := createTestHub(t)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|||||||
Reference in New Issue
Block a user