Skip to content
Draft
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
24 changes: 24 additions & 0 deletions accessaudit/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package accessaudit

import "context"

type storeKey struct{}
type identityKey struct{}

func WithStore(ctx context.Context, s *Store) context.Context {
return context.WithValue(ctx, storeKey{}, s)
}
func WithIdentity(ctx context.Context, f func(*Identity)) context.Context {
i := IdentityFrom(ctx)
f(&i)
return context.WithValue(ctx, identityKey{}, i)
}
func IdentityFrom(ctx context.Context) Identity {
i, _ := ctx.Value(identityKey{}).(Identity)
return i
}
func Record(ctx context.Context, e Event) {
s, _ := ctx.Value(storeKey{}).(*Store)
e.Identity = IdentityFrom(ctx)
s.Record(e)
}
76 changes: 76 additions & 0 deletions accessaudit/http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package accessaudit

import (
"context"
"crypto/rand"
"net/http"
"net/http/httptrace"
"net/url"
"sync"
)

type transport struct {
base http.RoundTripper
tool string
}

// Transport goes INSIDE the governor so cache hits are not network evidence.
func Transport(base http.RoundTripper, tool string) http.RoundTripper {
if base == nil {
base = http.DefaultTransport
}
return transport{base, tool}
}
func (t transport) RoundTrip(req *http.Request) (*http.Response, error) {
id := rand.Text()
e := Event{Source: "native_http", Outcome: "transport_attempt", Tool: t.tool, Method: req.Method, URL: req.URL.String(), RequestID: id}
Record(req.Context(), e)
resp, err := t.base.RoundTrip(req)
e.Outcome = "transport_error"
if err == nil && resp != nil {
e.Outcome = "response_headers"
e.Status = resp.StatusCode
}
Record(req.Context(), e)
return resp, err
}

type fetchKey struct{}
type fetchTrace struct {
mu sync.Mutex
raw, id string
}

// TraceFetch observes Norma's real network callbacks without replacing its
// client, TLS, redirect or pinned-dial policy. Tool output is never evidence.
func TraceFetch(ctx context.Context, u *url.URL) context.Context {
st := &fetchTrace{raw: u.String(), id: rand.Text()}
ctx = context.WithValue(ctx, fetchKey{}, st)
emit := func(outcome string) {
st.mu.Lock()
e := Event{Source: "webfetch_trace", Outcome: outcome, Tool: "WebFetch", Method: "GET", URL: st.raw, RequestID: st.id}
st.mu.Unlock()
Record(ctx, e)
}
Record(ctx, Event{Source: "webfetch_trace", Outcome: "tool_attempt", Tool: "WebFetch", Method: "GET", URL: u.String()})
return httptrace.WithClientTrace(ctx, &httptrace.ClientTrace{
GetConn: func(string) { st.mu.Lock(); st.id = rand.Text(); st.mu.Unlock() },
WroteRequest: func(i httptrace.WroteRequestInfo) {
if i.Err != nil {
emit("write_error")
} else {
emit("request_written")
}
},
GotFirstResponseByte: func() { emit("response_started") },
})
}

// FetchRedirect runs only after the existing redirect gate allows the hop.
func FetchRedirect(req *http.Request) {
if st, ok := req.Context().Value(fetchKey{}).(*fetchTrace); ok {
st.mu.Lock()
st.raw = req.URL.String()
st.mu.Unlock()
}
}
50 changes: 50 additions & 0 deletions accessaudit/http_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package accessaudit

import (
"errors"
"net/http"
"net/http/httptest"
"testing"
)

type failTransport struct{}

func (failTransport) RoundTrip(*http.Request) (*http.Response, error) {
return nil, errors.New("secret URL must not be recorded")
}
func TestHTTPRedirectAttributionAndFailure(t *testing.T) {
s := testStore(t)
target := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/first" {
http.Redirect(w, r, "/last?token=hidden", 302)
return
}
w.WriteHeader(204)
}))
defer target.Close()
ctx := WithIdentity(WithStore(t.Context(), s), func(i *Identity) { i.TaskID = "7"; i.RunID = "run-1" })
req, _ := http.NewRequestWithContext(ctx, "GET", target.URL+"/first", nil)
resp, err := (&http.Client{Transport: Transport(nil, "test")}).Do(req)
if err != nil {
t.Fatal(err)
}
resp.Body.Close()
_, err = Transport(failTransport{}, "failed").RoundTrip(req)
if err == nil {
t.Fatal("error swallowed")
}
p, err := s.Search(t.Context(), window())
if err != nil || len(p.Events) != 6 {
t.Fatal(p, err)
}
counts := map[string]int{}
for _, e := range p.Events {
counts[e.Outcome]++
if e.TaskID != "7" || e.RunID != "run-1" {
t.Fatal(e)
}
}
if counts["transport_attempt"] != 3 || counts["response_headers"] != 2 || counts["transport_error"] != 1 {
t.Fatal(counts)
}
}
18 changes: 18 additions & 0 deletions accessaudit/proxy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package accessaudit

import "context"

// ProxyFactory issues an opaque, revocable capability for this execution. The
// proxy resolves it server-side; client-supplied task headers are never trusted.
type ProxyFactory func(Identity) (string, string, func())
type proxyFactoryKey struct{}

func WithProxyFactory(ctx context.Context, f ProxyFactory) context.Context {
return context.WithValue(ctx, proxyFactoryKey{}, f)
}
func ExecutionProxy(ctx context.Context, fallback, caCert string) (string, string, func()) {
if f, ok := ctx.Value(proxyFactoryKey{}).(ProxyFactory); ok && f != nil {
return f(IdentityFrom(ctx))
}
return fallback, caCert, func() {}
}
Loading
Loading