Operations register through httpapi.Register with Public, Authorized or AdminAuthorized, and the guard asks the policy chain for the declared action and resource kind before the handler runs. An undeclared operation and an undecidable chain are refused. The workload principal, its shipped policy, a self-service policy and Recording for denials are added in governance.
83 lines
2.3 KiB
Go
83 lines
2.3 KiB
Go
package dashboard
|
|
|
|
import (
|
|
"bufio"
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/samcharles93/archie-core/internal/platform/httpapi"
|
|
"github.com/samcharles93/archie-core/internal/platform/hub"
|
|
"github.com/samcharles93/archie-core/modules/chat"
|
|
"github.com/samcharles93/archie-core/modules/governance"
|
|
)
|
|
|
|
// TestStreamTellsOnlyTheConversationsOwner guards the live stream's chat
|
|
// boundary: an action settled in a conversation is announced to its owner
|
|
// and to nobody else, administrators included.
|
|
func TestStreamTellsOnlyTheConversationsOwner(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
reader string
|
|
hears bool
|
|
}{
|
|
{"alice", true},
|
|
{"bob", false},
|
|
{string(governance.SystemID), false},
|
|
} {
|
|
t.Run(tc.reader, func(t *testing.T) {
|
|
h := hub.New()
|
|
mux := http.NewServeMux()
|
|
Register(httpapi.New(mux, "test", headerAuth{}, allowAll{}), h)
|
|
srv := httptest.NewServer(mux)
|
|
defer srv.Close()
|
|
|
|
ctx, cancel := context.WithTimeout(t.Context(), 300*time.Millisecond)
|
|
defer cancel()
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, srv.URL+"/api/stream", nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
req.Header.Set("X-As", tc.reader)
|
|
res, err := srv.Client().Do(req)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer func() { _ = res.Body.Close() }()
|
|
h.Publish(hub.Note{Topic: chat.OwnerTopic("alice")})
|
|
|
|
heard := false
|
|
lines := bufio.NewScanner(res.Body)
|
|
for lines.Scan() {
|
|
if strings.Contains(lines.Text(), "chat_outcome") {
|
|
heard = true
|
|
break
|
|
}
|
|
}
|
|
if heard != tc.hears {
|
|
t.Fatalf("%s heard the update: %v, want %v", tc.reader, heard, tc.hears)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// headerAuth signs in whoever the X-As header names; the system identity is
|
|
// the instance administrator.
|
|
type headerAuth struct{}
|
|
|
|
func (headerAuth) Authenticate(ctx context.Context, r *http.Request) (context.Context, bool, error) {
|
|
id := governance.IdentityID(r.Header.Get("X-As"))
|
|
p := governance.Principal{IdentityID: id, Org: governance.DefaultOrgID}
|
|
if id == governance.SystemID {
|
|
p = governance.SharedTokenOwner()
|
|
}
|
|
return governance.WithPrincipal(ctx, p), p.InstanceAdmin(), nil
|
|
}
|
|
|
|
// allowAll leaves every decision to the handlers under test.
|
|
type allowAll struct{}
|
|
|
|
func (allowAll) Allow(context.Context, string, string) error { return nil }
|