feat: publish standalone worker
Separate worker packaging and service lifecycle from the control plane.
Этот коммит содержится в:
11
checks/crkn/result.go
Обычный файл
11
checks/crkn/result.go
Обычный файл
@@ -0,0 +1,11 @@
|
||||
// Package crkn provides functionality.
|
||||
package crkn
|
||||
|
||||
import (
|
||||
"rsgit.ru/rsmon/rsmon/internal/checkresult"
|
||||
)
|
||||
|
||||
// Result is a result of a check
|
||||
type Result struct {
|
||||
checkresult.CheckResult
|
||||
}
|
||||
61
checks/crkn/rkn_init.go
Обычный файл
61
checks/crkn/rkn_init.go
Обычный файл
@@ -0,0 +1,61 @@
|
||||
package crkn
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"rsgit.ru/rsmon/rsmon/app/models"
|
||||
)
|
||||
|
||||
// Perform checks if a domain or IP is in the RKN registry
|
||||
func Perform(m *models.Monitor) *Result {
|
||||
result := &Result{}
|
||||
start := time.Now()
|
||||
|
||||
blocked, err := models.IsRknDomainBlocked(m.Host)
|
||||
if err != nil {
|
||||
failResult(err, result, start)
|
||||
return result
|
||||
}
|
||||
if blocked {
|
||||
testResult(true, result, "Domain is contained in RKN registry", start)
|
||||
return result
|
||||
}
|
||||
|
||||
for _, dnsRecord := range m.DNSRecords {
|
||||
if dnsRecord.Kind != "A" && dnsRecord.Kind != "AAAA" {
|
||||
continue
|
||||
}
|
||||
findIP, err := models.IsRknIPBlocked(dnsRecord.Value.Inet.String())
|
||||
if err != nil {
|
||||
failResult(err, result, start)
|
||||
return result
|
||||
}
|
||||
if findIP {
|
||||
testResult(true, result, "IP is contained in RKN registry", start)
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
testResult(false, result, "", start)
|
||||
return result
|
||||
}
|
||||
|
||||
func failResult(err error, result *Result, start time.Time) {
|
||||
result.State = "FAIL"
|
||||
result.Error = err
|
||||
result.Duration = time.Since(start)
|
||||
}
|
||||
|
||||
func testResult(find bool, result *Result, msg string, start time.Time) {
|
||||
if find {
|
||||
result.State = "ERR"
|
||||
result.Error = errors.New(msg)
|
||||
result.Duration = time.Since(start)
|
||||
result.Warnings = append(result.Warnings, "Resource is blocked in Russia according to RKN registry")
|
||||
} else {
|
||||
result.State = "OK"
|
||||
result.Duration = time.Since(start)
|
||||
result.Infos = append(result.Infos, "Resource is not in RKN registry")
|
||||
}
|
||||
}
|
||||
134
checks/crkn/rkn_init_test.go
Обычный файл
134
checks/crkn/rkn_init_test.go
Обычный файл
@@ -0,0 +1,134 @@
|
||||
package crkn_test
|
||||
|
||||
import (
|
||||
"net"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"rsgit.ru/rsmon/rsmon/app/models"
|
||||
"rsgit.ru/rsmon/rsmon/checks/crkn"
|
||||
"rsgit.ru/rsmon/rsmon/config/database"
|
||||
"rsgit.ru/rsmon/rsmon/internal/netaddr"
|
||||
)
|
||||
|
||||
func init() {
|
||||
database.Init()
|
||||
}
|
||||
|
||||
func TestPerform_DomainBlocked(t *testing.T) {
|
||||
models.Drop()
|
||||
models.Migrate()
|
||||
|
||||
require.NoError(t, models.ReplaceRknDomains([]string{"blocked.example.com"}))
|
||||
|
||||
monitor := &models.Monitor{Host: "blocked.example.com"}
|
||||
result := crkn.Perform(monitor)
|
||||
require.NotNil(t, result)
|
||||
assert.Equal(t, "ERR", result.State)
|
||||
assert.NotNil(t, result.Error)
|
||||
assert.Contains(t, result.Error.Error(), "RKN registry")
|
||||
assert.Contains(t, result.Warnings, "Resource is blocked in Russia according to RKN registry")
|
||||
}
|
||||
|
||||
func TestPerform_DomainClean(t *testing.T) {
|
||||
models.Drop()
|
||||
models.Migrate()
|
||||
|
||||
require.NoError(t, models.ReplaceRknDomains([]string{"blocked.example.com"}))
|
||||
|
||||
monitor := &models.Monitor{Host: "clean.example.org"}
|
||||
result := crkn.Perform(monitor)
|
||||
require.NotNil(t, result)
|
||||
assert.Equal(t, "OK", result.State)
|
||||
assert.Nil(t, result.Error)
|
||||
assert.Contains(t, result.Infos, "Resource is not in RKN registry")
|
||||
}
|
||||
|
||||
func TestPerform_DomainSuffixMatch(t *testing.T) {
|
||||
models.Drop()
|
||||
models.Migrate()
|
||||
|
||||
require.NoError(t, models.ReplaceRknDomains([]string{"example.com"}))
|
||||
|
||||
monitor := &models.Monitor{Host: "sub.example.com"}
|
||||
result := crkn.Perform(monitor)
|
||||
require.NotNil(t, result)
|
||||
assert.Equal(t, "ERR", result.State, "subdomain must match apex via IsRknDomainBlocked")
|
||||
}
|
||||
|
||||
func TestPerform_IPBlocked(t *testing.T) {
|
||||
models.Drop()
|
||||
models.Migrate()
|
||||
|
||||
_, network, err := net.ParseCIDR("10.5.5.0/24")
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, models.ReplaceRknIPs([]*net.IPNet{network}))
|
||||
|
||||
monitor := &models.Monitor{
|
||||
Host: "clean.example.org",
|
||||
DNSRecords: []models.DNSRecord{
|
||||
{Kind: "A", Name: "clean.example.org", Value: netaddr.Inet{Inet: net.ParseIP("10.5.5.7")}},
|
||||
},
|
||||
}
|
||||
result := crkn.Perform(monitor)
|
||||
require.NotNil(t, result)
|
||||
assert.Equal(t, "ERR", result.State)
|
||||
assert.NotNil(t, result.Error)
|
||||
assert.Contains(t, result.Error.Error(), "IP is contained in RKN registry")
|
||||
}
|
||||
|
||||
func TestPerform_IPClean(t *testing.T) {
|
||||
models.Drop()
|
||||
models.Migrate()
|
||||
|
||||
_, network, err := net.ParseCIDR("10.0.0.0/8")
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, models.ReplaceRknIPs([]*net.IPNet{network}))
|
||||
|
||||
monitor := &models.Monitor{
|
||||
Host: "clean.example.org",
|
||||
DNSRecords: []models.DNSRecord{
|
||||
{Kind: "A", Name: "clean.example.org", Value: netaddr.Inet{Inet: net.ParseIP("8.8.8.8")}},
|
||||
},
|
||||
}
|
||||
result := crkn.Perform(monitor)
|
||||
require.NotNil(t, result)
|
||||
assert.Equal(t, "OK", result.State)
|
||||
}
|
||||
|
||||
func TestPerform_SkipsNonADNSRecords(t *testing.T) {
|
||||
models.Drop()
|
||||
models.Migrate()
|
||||
|
||||
_, network, err := net.ParseCIDR("10.0.0.0/8")
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, models.ReplaceRknIPs([]*net.IPNet{network}))
|
||||
|
||||
monitor := &models.Monitor{
|
||||
Host: "clean.example.org",
|
||||
DNSRecords: []models.DNSRecord{
|
||||
{Kind: "CNAME", Name: "clean.example.org", Value: netaddr.Inet{Inet: net.ParseIP("10.5.5.7")}},
|
||||
{Kind: "TXT", Name: "clean.example.org", Value: netaddr.Inet{Inet: net.ParseIP("10.5.5.7")}},
|
||||
},
|
||||
}
|
||||
result := crkn.Perform(monitor)
|
||||
require.NotNil(t, result)
|
||||
assert.Equal(t, "OK", result.State, "non-A/AAAA records must be skipped")
|
||||
}
|
||||
|
||||
// TestPerform_FastReadOnly verifies the refactor removed the background
|
||||
// goroutine that previously triggered a remote data refresh on every
|
||||
// check — the call must return quickly and only depend on the local DB.
|
||||
func TestPerform_FastReadOnly(t *testing.T) {
|
||||
models.Drop()
|
||||
models.Migrate()
|
||||
require.NoError(t, models.ReplaceRknDomains([]string{"blocked.example.com"}))
|
||||
|
||||
monitor := &models.Monitor{Host: "blocked.example.com"}
|
||||
|
||||
result := crkn.Perform(monitor)
|
||||
require.NotNil(t, result)
|
||||
assert.Equal(t, "ERR", result.State)
|
||||
}
|
||||
Ссылка в новой задаче
Block a user