Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/zakura-agent.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ jobs:
- uses: actions/download-artifact@v4
with:
name: zakura-agent-binaries
path: go/agent/dist
path: apps/agent/dist
- name: attach raw binaries
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh release upload "${GITHUB_REF_NAME}" \
go/agent/dist/zakura-agent_* \
go/agent/dist/VERSION \
go/agent/dist/checksums.txt \
apps/agent/dist/zakura-agent_* \
apps/agent/dist/VERSION \
apps/agent/dist/checksums.txt \
--clobber
33 changes: 31 additions & 2 deletions apps/server/internal/runtime/runtime_nodes_extra.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,23 @@ func runnerShellInstaller(server, token, kind, prelude string) string {
}

func shellQuote(value string) string { return "'" + strings.ReplaceAll(value, "'", "'\\''") + "'" }

var errRuntimeNodeUpdateForbidden = errors.New("platform admin required")

func (h *handler) authorizeRuntimeNodeUpdate(ctx context.Context, actor httpx.Principal, nodeID string) error {
if !actor.IsPlatformAdmin {
return errRuntimeNodeUpdateForbidden
}
var count int64
if err := h.deps.Gorm.WithContext(ctx).Table("runtime_nodes").Where("tenant_id=? AND id=?", actor.TenantID, nodeID).Count(&count).Error; err != nil {
return err
}
if count != 1 {
return ErrNotFound
}
return nil
}

func (h *handler) getRuntimeNodeMap(r *http.Request) (map[string]any, error) {
node, err := scanRuntimeNode(h.deps.Gorm.WithContext(r.Context()).Raw(`SELECT id,name,slug,kind,status,endpoint,capabilities_json,host_info_json,storage_root,agent_version,last_seen_at,labels_json,is_shared,created_at,updated_at FROM runtime_nodes WHERE (tenant_id=? OR is_shared=true) AND id=?`, principal(r).TenantID, chi.URLParam(r, "id")).Row())
if err == nil {
Expand Down Expand Up @@ -426,6 +443,14 @@ func (h *handler) runtimeNodeVersion(w http.ResponseWriter, r *http.Request) {
httpx.JSON(w, http.StatusOK, result)
}
func (h *handler) runtimeUpdateInfo(w http.ResponseWriter, r *http.Request) {
if err := h.authorizeRuntimeNodeUpdate(r.Context(), principal(r), chi.URLParam(r, "id")); err != nil {
if errors.Is(err, errRuntimeNodeUpdateForbidden) {
httpx.Error(w, http.StatusForbidden, err.Error())
} else {
statusErr(w, err)
}
return
}
id := r.URL.Query().Get("id")
if id == "" {
httpx.Error(w, http.StatusBadRequest, "id required")
Expand All @@ -441,8 +466,12 @@ func (h *handler) runtimeUpdateInfo(w http.ResponseWriter, r *http.Request) {
httpx.JSON(w, http.StatusOK, map[string]any{"update": job})
}
func (h *handler) runtimeUpdateRunner(w http.ResponseWriter, r *http.Request) {
if _, err := h.getRuntimeNodeMap(r); err != nil {
statusErr(w, err)
if err := h.authorizeRuntimeNodeUpdate(r.Context(), principal(r), chi.URLParam(r, "id")); err != nil {
if errors.Is(err, errRuntimeNodeUpdateForbidden) {
httpx.Error(w, http.StatusForbidden, err.Error())
} else {
statusErr(w, err)
}
return
}
session, err := h.hub.get(chi.URLParam(r, "id"))
Expand Down
48 changes: 48 additions & 0 deletions apps/server/internal/runtime/runtime_update_authorization_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
package runtime

import (
"context"
"errors"
"testing"

"github.com/Moonrend/Zakura/apps/server/internal/platform/db/models"
"github.com/Moonrend/Zakura/apps/server/internal/platform/httpx"
)

func TestRuntimeNodeUpdateAuthorizationRequiresPlatformAdminOwnership(t *testing.T) {
d := testDeps(t)
seedTenant(t, d, "tenant-a")
seedTenant(t, d, "tenant-b")
now := runtimeTimeString(d.Clock())
node := func(id, tenant string, shared bool) models.RuntimeNode {
return models.RuntimeNode{ID: &id, TenantID: tenant, Name: id, Slug: id, Kind: "computer", Status: "online", CapabilitiesJSON: "{}", HostInfoJSON: "{}", StorageRoot: "/var/lib/zakura", LabelsJSON: "{}", IsShared: shared, CreatedAt: now, UpdatedAt: now}
}
for _, candidate := range []models.RuntimeNode{
node("owned", "tenant-a", false),
node("foreign-shared", "tenant-b", true),
} {
if err := d.Gorm.Create(&candidate).Error; err != nil {
t.Fatal(err)
}
}
h := &handler{deps: d}
for _, tc := range []struct {
name string
actor httpx.Principal
node string
want error
}{
{name: "member cannot update owned node", actor: httpx.Principal{TenantID: "tenant-a", Role: "member"}, node: "owned", want: errRuntimeNodeUpdateForbidden},
{name: "admin cannot update foreign shared node", actor: httpx.Principal{TenantID: "tenant-a", Role: "owner", IsPlatformAdmin: true}, node: "foreign-shared", want: ErrNotFound},
{name: "admin can update owned node", actor: httpx.Principal{TenantID: "tenant-a", Role: "owner", IsPlatformAdmin: true}, node: "owned"},
{name: "owning admin can update shared node", actor: httpx.Principal{TenantID: "tenant-b", Role: "owner", IsPlatformAdmin: true}, node: "foreign-shared"},
} {
t.Run(tc.name, func(t *testing.T) {
err := h.authorizeRuntimeNodeUpdate(context.Background(), tc.actor, tc.node)
if !errors.Is(err, tc.want) {
t.Fatalf("authorizeRuntimeNodeUpdate() error = %v, want %v", err, tc.want)
}
})
}
}
Loading