feat(api): organization-owned regions, checkers and status page servers
Organizations create regions and agents for themselves under Regions & checkers; an org checker only receives its org's monitors. Platform admins manage shared regions and agents in the admin panel or with the CLI, which gains 'api admin grant|revoke'. The first user of a self-hosted installation becomes a platform admin. Region lists show the org's own regions first. Closes #52 Refs #29 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
1 parent
607798d742
commit
22f0740056
19 files changed
+1932
-301
No files matched your search
@@ -13,31 +13,35 @@ import (
|
||||
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/agentapi"
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/api"
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/auth"
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/checker"
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/store"
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/tenant"
|
||||
)
|
||||
|
||||
// agentToken creates a checker agent for region directly in the database,
|
||||
// as "api agent create" would.
|
||||
func (e *env) agentToken(t *testing.T, region string) string {
|
||||
// platformChecker creates a platform region with a checker agent directly
|
||||
// in the database, as "api agent create" would, and returns the agent token
|
||||
// and the region ID.
|
||||
func (e *env) platformChecker(t *testing.T, key string) (string, string) {
|
||||
t.Helper()
|
||||
tok := api.AgentTokenPrefix + auth.RandomToken()
|
||||
ctx := context.Background()
|
||||
var (
|
||||
tok string
|
||||
region store.Region
|
||||
)
|
||||
err := tenant.New(e.dbPool).Tx(ctx, tenant.System(), func(q *store.Queries) error {
|
||||
if _, err := q.UpsertRegion(ctx, store.UpsertRegionParams{Key: region}); err != nil {
|
||||
var err error
|
||||
if region, err = q.EnsurePlatformRegion(ctx, store.EnsurePlatformRegionParams{Key: key}); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err := q.CreateAgent(ctx, store.CreateAgentParams{
|
||||
Kind: "checker", Name: "test-" + region, Region: ®ion, TokenHash: api.HashToken(tok), Prefix: tok[:10],
|
||||
})
|
||||
var params store.CreateAgentParams
|
||||
tok, params = api.NewAgentToken(nil, "checker", "test-"+key, ®ion.ID)
|
||||
_, err = q.CreateAgent(ctx, params)
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("creating agent: %v", err)
|
||||
}
|
||||
return tok
|
||||
return tok, region.ID.String()
|
||||
}
|
||||
|
||||
// startChecker runs a checker instance until the returned stop is called.
|
||||
@@ -77,8 +81,8 @@ func TestCheckersPerRegion(t *testing.T) {
|
||||
}))
|
||||
defer target.Close()
|
||||
|
||||
eu, us := uniq("eu-"), uniq("us-")
|
||||
euTok, usTok := e.agentToken(t, eu), e.agentToken(t, us)
|
||||
euTok, eu := e.platformChecker(t, uniq("eu-"))
|
||||
usTok, us := e.platformChecker(t, uniq("us-"))
|
||||
euA, stopA := e.startChecker(t, euTok)
|
||||
euB, _ := e.startChecker(t, euTok)
|
||||
usC, _ := e.startChecker(t, usTok)
|
||||
@@ -89,10 +93,10 @@ func TestCheckersPerRegion(t *testing.T) {
|
||||
|
||||
// Instances register on their first poll.
|
||||
waitFor(t, 5*time.Second, "both eu instances registered", func() bool {
|
||||
_, body := owner.do("GET", "/api/v1/regions", nil)
|
||||
_, body := owner.do("GET", "/api/v1/org/regions", nil)
|
||||
for _, r := range body["regions"].([]any) {
|
||||
r := r.(map[string]any)
|
||||
if r["key"] == eu && r["instances"] == float64(2) {
|
||||
if r["id"] == eu && r["instances"] == float64(2) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
@@ -169,7 +173,7 @@ func TestCheckersPerRegion(t *testing.T) {
|
||||
|
||||
func TestAgentAuth(t *testing.T) {
|
||||
e := newEnv(t)
|
||||
tok := e.agentToken(t, uniq("x-"))
|
||||
tok, _ := e.platformChecker(t, uniq("x-"))
|
||||
ctx := context.Background()
|
||||
if _, err := agentapi.NewClient(e.srv.URL, "usa_wrong", uuid.New(), "t").WithHTTPClient(e.srv.Client()).Hello(ctx); !errors.Is(err, agentapi.ErrUnauthorized) {
|
||||
t.Errorf("Hello(wrong token) = %v, want %v", err, agentapi.ErrUnauthorized)
|
||||
|
||||
Reference in new issue
Block a user