246 lines
9.0 KiB
Go
246 lines
9.0 KiB
Go
package webui
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/samcharles93/archie-core/internal/domain/access"
|
|
"github.com/samcharles93/archie-core/internal/domain/org"
|
|
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
|
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
|
|
infraaccess "github.com/samcharles93/archie-core/internal/infrastructure/access"
|
|
)
|
|
|
|
type authorizationTasks struct{ storecontract.TaskStore }
|
|
|
|
func (authorizationTasks) TaskByID(context.Context, int64) (*task.Task, error) {
|
|
return &task.Task{ID: 7}, nil
|
|
}
|
|
|
|
// These are the policy-governed HTTP surfaces. Protocol entry points and
|
|
// private account resources have separate credential/ownership behavior tests.
|
|
func TestHTTPPolicyCannotBeBypassed(t *testing.T) {
|
|
routes := []struct{ method, path string }{
|
|
{"DELETE", "/api/access/policies"},
|
|
{"DELETE", "/api/bindings/7"},
|
|
{"DELETE", "/api/event-types/7"},
|
|
{"DELETE", "/api/extensions/sample"},
|
|
{"DELETE", "/api/mappings/7"},
|
|
{"DELETE", "/api/orgs/7/members/sample"},
|
|
{"DELETE", "/api/sources/sample"},
|
|
{"DELETE", "/api/tokens/7"},
|
|
{"GET", "/api/access/policies"},
|
|
{"GET", "/api/alerts"},
|
|
{"GET", "/api/audit"},
|
|
{"GET", "/api/audit/export"},
|
|
{"GET", "/api/authentication"},
|
|
{"GET", "/api/bindings"},
|
|
{"GET", "/api/bindings/7"},
|
|
{"GET", "/api/bindings/7/dispatches"},
|
|
{"GET", "/api/capabilities"},
|
|
{"GET", "/api/captures"},
|
|
{"GET", "/api/captures/7/dispatches"},
|
|
{"GET", "/api/channels"},
|
|
{"GET", "/api/chat/file/sample"},
|
|
{"GET", "/api/chat/sessions"},
|
|
{"GET", "/api/chat/sessions/7/messages"},
|
|
{"GET", "/api/chat/sessions/7/turns"},
|
|
{"GET", "/api/chat/update"},
|
|
{"GET", "/api/config"},
|
|
{"GET", "/api/control-plane/apply-status"},
|
|
{"GET", "/api/control-plane/catalog"},
|
|
{"GET", "/api/control-plane/resources/sample"},
|
|
{"GET", "/api/control-plane/resources/sample/history"},
|
|
{"GET", "/api/curators"},
|
|
{"GET", "/api/event-types"},
|
|
{"GET", "/api/extensions"},
|
|
{"GET", "/api/extensions/catalogue"},
|
|
{"GET", "/api/harness/bindings"},
|
|
{"GET", "/api/harness/terminal"},
|
|
{"GET", "/api/identities"},
|
|
{"GET", "/api/logs"},
|
|
{"GET", "/api/repositories"},
|
|
{"GET", "/api/mappings"},
|
|
{"GET", "/api/mappings/7"},
|
|
{"GET", "/api/orgs"},
|
|
{"GET", "/api/orgs/7"},
|
|
{"GET", "/api/orgs/7/members"},
|
|
{"GET", "/api/orgs/7/workspaces"},
|
|
{"GET", "/api/secret-engines"},
|
|
{"GET", "/api/services"},
|
|
{"GET", "/api/setup"},
|
|
{"GET", "/api/skills"},
|
|
{"GET", "/api/sources"},
|
|
{"GET", "/api/stream"},
|
|
{"GET", "/api/summary"},
|
|
{"GET", "/api/task-meta"},
|
|
{"GET", "/api/tasks"},
|
|
{"GET", "/api/tasks/7"},
|
|
{"GET", "/api/tasks/7/attempts"},
|
|
{"GET", "/api/tasks/7/changes"},
|
|
{"GET", "/api/tasks/7/debug"},
|
|
{"GET", "/api/tasks/7/dispatches"},
|
|
{"GET", "/api/tasks/7/logs"},
|
|
{"GET", "/api/tasks/7/logs/download"},
|
|
{"GET", "/api/tasks/7/sandbox"},
|
|
{"GET", "/api/tokens"},
|
|
{"GET", "/api/version"},
|
|
{"GET", "/api/workflows"},
|
|
{"PATCH", "/api/bindings/7"},
|
|
{"PATCH", "/api/mappings/7"},
|
|
{"POST", "/api/accounts"},
|
|
{"POST", "/api/alerts/7/dismiss"},
|
|
{"POST", "/api/bindings"},
|
|
{"POST", "/api/bindings/7/approve"},
|
|
{"POST", "/api/bindings/7/pause"},
|
|
{"POST", "/api/bindings/7/resume"},
|
|
{"POST", "/api/channels/7/reload"},
|
|
{"POST", "/api/chat/answer"},
|
|
{"POST", "/api/chat/cancel"},
|
|
{"POST", "/api/chat/message"},
|
|
{"POST", "/api/chat/stream"},
|
|
{"POST", "/api/chat/update/defer"},
|
|
{"POST", "/api/chat/update/install"},
|
|
{"POST", "/api/control-plane/resources/sample/commands/sample"},
|
|
{"POST", "/api/event-types"},
|
|
{"POST", "/api/extensions"},
|
|
{"POST", "/api/extensions/catalogue/sample/install"},
|
|
{"POST", "/api/extensions/sample/accept"},
|
|
{"POST", "/api/extensions/sample/approve-update"},
|
|
{"POST", "/api/extensions/sample/rollback"},
|
|
{"POST", "/api/extensions/sample/update"},
|
|
{"POST", "/api/identities"},
|
|
{"POST", "/api/identities/7/sample"},
|
|
{"POST", "/api/mappings"},
|
|
{"POST", "/api/mappings/preview"},
|
|
{"POST", "/api/orgs"},
|
|
{"POST", "/api/orgs/7/members"},
|
|
{"POST", "/api/orgs/7/workspaces"},
|
|
{"POST", "/api/secret-references/check"},
|
|
{"POST", "/api/sources"},
|
|
{"POST", "/api/sources/sample/approve-unsigned"},
|
|
{"POST", "/api/sources/sample/secret"},
|
|
{"POST", "/api/sources/sample/signing"},
|
|
{"POST", "/api/tasks/7/action"},
|
|
{"POST", "/api/tokens"},
|
|
{"POST", "/api/work-requests"},
|
|
{"POST", "/api/control-plane/resources/repository-policies/check"},
|
|
{"PUT", "/api/access/policies"},
|
|
{"PUT", "/api/authentication"},
|
|
{"PUT", "/api/event-types/7"},
|
|
{"PUT", "/api/extensions/sample/enabled"},
|
|
{"PUT", "/api/extensions/sample/update-policy"},
|
|
{"PUT", "/api/orgs/7/agents/sample"},
|
|
{"PUT", "/api/orgs/7/members/sample"},
|
|
{"PUT", "/api/sources/sample/delivery-header"},
|
|
{"PUT", "/api/sources/sample/name"},
|
|
{"PUT", "/api/workflows/7/enabled"},
|
|
}
|
|
principal := access.Principal{IdentityID: "alice", Org: "acme", Memberships: []org.Membership{{IdentityID: "alice", OrgID: "acme", Role: org.RoleAdmin}}}
|
|
policy := access.Policy{ID: "deny-all", Level: access.LevelInstance, Text: `forbid(principal,action,resource);`}
|
|
for _, route := range routes {
|
|
t.Run(route.method+" "+route.path, func(t *testing.T) {
|
|
for _, signedIn := range []bool{false, true} {
|
|
srv := &Server{Store: authorizationTasks{}, Accounts: accountBoundaryStore{}, Access: infraaccess.New(append(access.ShippedOrgPolicies("acme"), policy)), Principals: principalOf(principal)}
|
|
req := httptest.NewRequestWithContext(t.Context(), route.method, "https://archie.example"+route.path, strings.NewReader(`{}`))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.Header.Set("X-Archie-CSRF", "1")
|
|
want := http.StatusUnauthorized
|
|
if signedIn {
|
|
req.AddCookie(&http.Cookie{Name: "archie_session", Value: "valid"})
|
|
want = http.StatusForbidden
|
|
}
|
|
rec := httptest.NewRecorder()
|
|
srv.Handler().ServeHTTP(rec, req)
|
|
if rec.Code != want {
|
|
t.Fatalf("signedIn=%v status=%d want=%d body=%s", signedIn, rec.Code, want, rec.Body.String())
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestResourceHistoryNeedsPolicyManagement pins the raw-value history route:
|
|
// it serves unredacted revisions because restore re-applies the real value, so
|
|
// it needs policy management of the kind rather than the redacted feed's
|
|
// read_audit grant. A developer and a viewer are refused; an admin passes the
|
|
// gate and reaches the handler, which reports the unwired control plane.
|
|
func TestResourceHistoryNeedsPolicyManagement(t *testing.T) {
|
|
const path = "/api/control-plane/resources/audit-retention/history"
|
|
tests := []struct {
|
|
role org.Role
|
|
want int
|
|
}{
|
|
{org.RoleViewer, http.StatusForbidden},
|
|
{org.RoleDeveloper, http.StatusForbidden},
|
|
{org.RoleAdmin, http.StatusServiceUnavailable},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(string(tt.role), func(t *testing.T) {
|
|
principal := access.Principal{
|
|
IdentityID: "alice", Org: "acme",
|
|
Memberships: []org.Membership{{IdentityID: "alice", OrgID: "acme", Role: tt.role}},
|
|
}
|
|
srv := &Server{
|
|
Accounts: accountBoundaryStore{},
|
|
Access: infraaccess.New(access.ShippedOrgPolicies("acme")),
|
|
Principals: principalOf(principal),
|
|
}
|
|
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "https://archie.example"+path, nil)
|
|
req.Header.Set("X-Archie-CSRF", "1")
|
|
req.AddCookie(&http.Cookie{Name: "archie_session", Value: "valid"})
|
|
rec := httptest.NewRecorder()
|
|
srv.Handler().ServeHTTP(rec, req)
|
|
if rec.Code != tt.want {
|
|
t.Fatalf("status = %d, want %d body=%s", rec.Code, tt.want, rec.Body.String())
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestAuditRoutesFollowTheReadAuditRole pins the behavioural boundary the
|
|
// instance-forbid matrix cannot see: with the shipped org policies, a viewer
|
|
// and a developer are refused the audit feed and its export, while an admin
|
|
// is allowed through.
|
|
func TestAuditRoutesFollowTheReadAuditRole(t *testing.T) {
|
|
routes := []struct{ name, path string }{
|
|
{"feed", "/api/audit"},
|
|
{"export", "/api/audit/export?format=json&from=2026-01-01T00:00:00Z&to=2026-01-02T00:00:00Z"},
|
|
}
|
|
tests := []struct {
|
|
role org.Role
|
|
want int
|
|
}{
|
|
{org.RoleViewer, http.StatusForbidden},
|
|
{org.RoleDeveloper, http.StatusForbidden},
|
|
{org.RoleAdmin, http.StatusOK},
|
|
}
|
|
for _, tt := range tests {
|
|
for _, route := range routes {
|
|
t.Run(string(tt.role)+" "+route.name, func(t *testing.T) {
|
|
principal := access.Principal{
|
|
IdentityID: "alice", Org: "acme",
|
|
Memberships: []org.Membership{{IdentityID: "alice", OrgID: "acme", Role: tt.role}},
|
|
}
|
|
srv := &Server{
|
|
Audit: auditStub{},
|
|
Accounts: accountBoundaryStore{},
|
|
Access: infraaccess.New(access.ShippedOrgPolicies("acme")),
|
|
Principals: principalOf(principal),
|
|
}
|
|
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "https://archie.example"+route.path, nil)
|
|
req.Header.Set("X-Archie-CSRF", "1")
|
|
req.AddCookie(&http.Cookie{Name: "archie_session", Value: "valid"})
|
|
rec := httptest.NewRecorder()
|
|
srv.Handler().ServeHTTP(rec, req)
|
|
if rec.Code != tt.want {
|
|
t.Fatalf("status = %d, want %d body=%s", rec.Code, tt.want, rec.Body.String())
|
|
}
|
|
})
|
|
}
|
|
}
|
|
}
|