oficina / agent-access Public

Clone
git clone https://amadan.net/oficina/agent-access

Plain git — no account needed to clone.

Download this file

1package agentaccess
2
3import (
4 "context"
5 "encoding/json"
6 "net/http"
7 "net/http/httptest"
8 "sync/atomic"
9 "testing"
10)
11
12type testCredentials struct {
13 resource, team string
14 scopes []string
15 calls int
16}
17
18func (c *testCredentials) Downstream(_ context.Context, _ Principal, resource, team string, scopes []string) (string, error) {
19 c.resource, c.team, c.scopes = resource, team, scopes
20 c.calls++
21 return "downstream", nil
22}
23
24func TestGatewayRoutesAndAttenuates(t *testing.T) {
25 var count atomic.Int32
26 app := testRegistry(t, &count)
27 s := httptest.NewTLSServer(app.HTTP(VerifyFunc(func(_ context.Context, token string) (Principal, error) {
28 if token != "downstream" {
29 t.Error("parent or missing token forwarded")
30 }
31 return principal(), nil
32 })))
33 defer s.Close()
34 credentials := &testCredentials{}
35 gateway, err := Gateway([]Route{{Team: "acme", Resource: s.URL + "/api/v1", Operations: app.List(principal())}}, credentials, s.Client().Transport)
36 if err != nil {
37 t.Fatal(err)
38 }
39 out, err := gateway.Call(t.Context(), principal(), "notes_create", json.RawMessage(`{"team":"acme","title":"x"}`))
40 if err != nil || out == nil || count.Load() != 1 {
41 t.Fatal(out, err, count.Load())
42 }
43 if credentials.resource != s.URL+"/api/v1" || credentials.team != "acme" || len(credentials.scopes) != 1 || credentials.scopes[0] != "notes:write" {
44 t.Fatal("incorrect attenuation")
45 }
46 p := principal()
47 p.Teams = []string{"other"}
48 if len(gateway.List(p)) != 0 {
49 t.Fatal("advertised operations outside granted teams")
50 }
51 p = principal()
52 p.Teams = append(p.Teams, "other")
53 _, err = gateway.Call(t.Context(), p, "notes_create", json.RawMessage(`{"team":"other","title":"x"}`))
54 if err == nil || credentials.calls != 1 || count.Load() != 1 {
55 t.Fatal("unconfigured team reached app")
56 }
57 _, err = gateway.Call(t.Context(), p, "notes_create", json.RawMessage(`{"team":"acme","title":"x","resource":"https://evil.example"}`))
58 if err == nil || credentials.calls != 1 {
59 t.Fatal("caller supplied destination accepted")
60 }
61}
62
63func TestGatewayDoesNotFollowAppRedirect(t *testing.T) {
64 var followed atomic.Bool
65 target := httptest.NewTLSServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) { followed.Store(true) }))
66 defer target.Close()
67 s := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { http.Redirect(w, r, target.URL, 307) }))
68 defer s.Close()
69 var count atomic.Int32
70 app := testRegistry(t, &count)
71 gateway, err := Gateway([]Route{{Team: "acme", Resource: s.URL + "/api/v1", Operations: app.List(principal())}}, &testCredentials{}, s.Client().Transport)
72 if err != nil {
73 t.Fatal(err)
74 }
75 _, err = gateway.Call(t.Context(), principal(), "notes_create", json.RawMessage(`{"team":"acme","title":"x"}`))
76 if err == nil || followed.Load() {
77 t.Fatal("followed app redirect")
78 }
79}
80