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
17 changes: 17 additions & 0 deletions conformance/battery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,17 @@ func sendRepeated(w *world, e exchange, l literal, requests *[]sentRequest) []st
}
mark = w.logMark()
}
asksBefore := 0
var asksDifferences []string
if l.want.asked != nil {
if e.overlap {
panic(fmt.Sprintf("exchange %q counts the origin check's questions, which a row beside it could ask too, so it must not overlap", e.name))
}
var err error
if asksBefore, err = w.originAsks(); err != nil {
asksDifferences = []string{fmt.Sprintf("reading the origin check's questions before the row: %v", err)}
}
}
for i := 1; i <= count; i++ {
r := e.send
if e.alternate != nil && i%2 == 0 {
Expand Down Expand Up @@ -504,6 +515,12 @@ func sendRepeated(w *world, e exchange, l literal, requests *[]sentRequest) []st
if p := l.want.lineAfter; p != nil && !w.lineAfter(mark, p) {
differences = append(differences, fmt.Sprintf("logged: no line the proxy wrote after the row began matches %s", p))
}
if want := l.want.asked; want != nil {
if asksDifferences == nil {
asksDifferences = w.askedSince(asksBefore, *want)
}
differences = append(differences, asksDifferences...)
}
differences = append(differences, bothSeen(l.also, sawWant, sawAlso)...)
if len(instances) > 1 || instances["no service"] > 0 {
var from []string
Expand Down
5 changes: 5 additions & 0 deletions conformance/scenario_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,11 @@ type answer struct {
// expression matches: the request log line of a target whose line is its own and no JSON object, which logged
// cannot read (sendRepeated reads it, as it reads counted).
lineAfter *regexp.Regexp
// asked, on a row sent in sequence, is how many questions the stub's origin check, the check
// conformance/testdata/unlisted-origins.yaml's ask_unlisted names, must receive while the row's requests are sent
// and answered (sendRepeated reads it); nil is not compared. The answer cannot show it for an origin RBAC admits
// whether or not the check was asked.
asked *int
// receivedForms holds headers the stub must have received with a value the regular expression matches, which the
// answer need not carry, unlike forms: a value the proxy makes anew for the service alone, such as a traceparent.
receivedForms map[string]*regexp.Regexp
Expand Down
118 changes: 86 additions & 32 deletions conformance/scenarios_test.go

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions conformance/stub/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"path/filepath"
"strconv"
"strings"
"sync/atomic"
"time"
"unicode/utf8"
)
Expand All @@ -22,6 +23,10 @@ import (
type backend struct {
service, instance, state string
hang time.Duration
// originAsks counts the questions the origin check at /auth/origin has received, which the battery reads at
// /stub/origin-asks (world.originAsks): an origin RBAC admits with or without the check's decision is answered the
// same either way, so only the count shows whether the proxy put it to the check.
originAsks atomic.Int64
}

func (b *backend) ServeHTTP(w http.ResponseWriter, r *http.Request) {
Expand All @@ -34,7 +39,12 @@ func (b *backend) ServeHTTP(w http.ResponseWriter, r *http.Request) {
case b.service == "accounts" && r.URL.Path == "/auth/check":
b.check(w, r)
case b.service == "accounts" && r.URL.Path == "/auth/origin":
// Counted as it arrives, before the answer, so once the proxy has the check's answer the count holds the
// question, and a question the proxy stopped waiting for is counted too.
b.originAsks.Add(1)
originCheck(w, r, b.hang)
case b.service == "accounts" && r.URL.Path == "/stub/origin-asks":
io.WriteString(w, strconv.FormatInt(b.originAsks.Load(), 10))
default:
b.reply(w, r)
}
Expand Down
11 changes: 8 additions & 3 deletions conformance/stub/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,11 @@ func (b *backend) check(w http.ResponseWriter, r *http.Request) {
}

// originCheck answers the check an environment puts a browser origin its lists do not hold to
// (origins.<environment name>.ask_unlisted), at /auth/origin: 200 for the one origin it admits,
// https://files.acme.example, asked the question browser-origin with no authorization header, and for any other origin
// (origins.<environment name>.ask_unlisted), at /auth/origin: 200 for the three origins it admits,
// https://files.acme.example, the same host over http, http://files.acme.example, so a row shows that an admitted
// origin of either scheme gets the proxy's answer to an admitted origin, and https://api.example.com:8443, the battery
// file's own host on another port, which the proxy must put to the check as it puts an origin no list holds, each asked
// the question browser-origin with no authorization header, and for any other origin
// or question, or a request that carries the client's authorization, 401 with a JSON body and an x-origin-check header
// of its own. So a row that wants the proxy's own 403 for a refused origin fails when the proxy passes the check's
// denial on, a row that wants an origin never put to the check, a listed one or none, fails when the proxy asks it, as
Expand All @@ -131,7 +134,9 @@ func originCheck(w http.ResponseWriter, r *http.Request, hang time.Duration) {
time.Sleep(hang)
w.Header().Set("x-origin-check", "late")
w.WriteHeader(http.StatusOK)
case r.Header.Get("x-gateway-question") == "browser-origin" && origin == "https://files.acme.example" &&
case r.Header.Get("x-gateway-question") == "browser-origin" &&
(origin == "https://files.acme.example" || origin == "http://files.acme.example" ||
origin == "https://api.example.com:8443") &&
r.Header.Values("authorization") == nil:
w.WriteHeader(http.StatusOK)
default:
Expand Down
3 changes: 2 additions & 1 deletion conformance/testdata/unlisted-origins.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
# (origins.<environment name>.ask_unlisted): the live environment of examples/full, api.example.com admitting
# https://www.example.com by its list and https://*.partner.example by a pattern, asks accounts at /auth/origin about
# any other origin, with the question browser-origin, and the stub's origin check admits https://files.acme.example
# alone, and only when it is not sent an authorization header; it answers https://down.example 503 and
# and http://files.acme.example, and https://api.example.com:8443, live's own host on another port, alone, and only
# when it is not sent an authorization header; it answers https://down.example 503 and
# https://late.example too late. The pattern covers no origin a row puts to the check, nor the environment's own host. The auth check runs after it, under deny, asked of a request with an authorization
# header. The local environment asks no check, so an origin its hosts do not serve is refused there as without the
# key. It runs the scenario of an origin no list holds alone, and only Envoy's entry runs it: the nginx and HAProxy
Expand Down
37 changes: 37 additions & 0 deletions conformance/world_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,43 @@ func (w *world) spans() ([]span, error) {
return kept, nil
}

// originAsks returns how many questions the stub's origin check has received since accounts started
// (conformance/stub/backend.go), read inside accounts, whose port only the proxy's network namespace reaches.
func (w *world) originAsks() (int, error) {
out, err := dockerStdout("exec", accounts, "/stub", "fetch", "http://127.0.0.1:7070/stub/origin-asks")
if err != nil {
return 0, err
}
text, ok := strings.CutPrefix(out, "200 ")
count, convErr := strconv.Atoi(text)
if !ok || convErr != nil {
return 0, fmt.Errorf("the origin check's count answered %q, not 200 and a number", out)
}
return count, nil
}

// askedSince returns how the questions the origin check received after it had received before differ from want, or
// nothing when they are want. A proxy answers a request it put to the check only after the check's answer, which the
// stub gives after counting the question, so the count holds every such question once the row has its answers; a
// question the proxy stopped waiting for is counted as it arrives, which may be after the proxy's own answer, so a
// count below want is read again every 100 ms, for 5 s at most.
func (w *world) askedSince(before, want int) []string {
deadline := time.Now().Add(5 * time.Second)
for {
now, err := w.originAsks()
if err != nil {
return []string{fmt.Sprintf("reading the origin check's questions: %v", err)}
}
if now-before >= want || time.Now().After(deadline) {
if now-before != want {
return []string{fmt.Sprintf("the origin check was asked %d times, want %d", now-before, want)}
}
return nil
}
time.Sleep(100 * time.Millisecond)
}
}

// waitSpans fetches the collector's spans every 100 ms until it answers, for 10 s at most, so the proxy starts with
// its collector listening.
func waitSpans(t testing.TB) {
Expand Down
10 changes: 6 additions & 4 deletions docs/auth.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,16 +140,18 @@ one path of a service that holds more is matched with `path`, not `prefix`.

## The check of an origin no list holds

An environment of `origins` may put a browser origin its lists and hosts do not admit to a check of its own,
An environment of `origins` may put a browser origin its lists and its own origin do not admit to a check of its own,
`ask_unlisted`, written as `ask` is, with the question `question`
([An origin no list holds](origins.md#an-origin-no-list-holds)). That check is asked before CORS and before this one,
in HTTP mode as this one is, at its own path, with the `Origin` and the question in `x-gateway-question`; it is never
sent the client's `Authorization`, and `send`, `accept`, `send_request` and `when_unavailable` do not apply to it. Its
`200` admits the origin, and the request then goes through this check as a request from a listed origin does: a
denial of this check reaches the client with the origin's `Access-Control-Allow-Origin`. Any other answer, a `5xx`
denial of this check reaches the client with the origin's `Access-Control-Allow-Origin`, but for an admitted `null`,
which gets none. Any other answer, a `5xx`
among them, and no answer within `1s`, refuse the origin with the `403` a foreign origin gets, before this check is
asked. A route with `auth: off` turns this check off, not the origin's. The envoy target writes it; the nginx and
haproxy targets refuse the key ([Portability](portability.md)).
asked. A route with `auth: off` turns this check off, not the origin's. The envoy target writes it once for the
environment, whatever its number of routes; the nginx and haproxy targets refuse the key
([Portability](portability.md)).

## Recipes

Expand Down
22 changes: 14 additions & 8 deletions docs/conformance.md

Large diffs are not rendered by default.

10 changes: 7 additions & 3 deletions docs/cors.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
`cors` holds the CORS settings every environment shares. [origins](origins.md) says how the gateway answers a browser
with them, and when `origins` names a file, that file holds the settings instead. `Access-Control-Allow-Origin`, and
`Access-Control-Expose-Headers` from `expose_headers`, go only to an origin an environment's `origins` or `patterns`
hold. An origin on the environment's own host that neither holds is admitted without them, also when it is the
request's own host and port; its preflight still carries `Access-Control-Allow-Methods`, `Access-Control-Allow-Headers`
and `Access-Control-Max-Age` from `allow_methods`, `allow_headers` and `max_age` (measured on Envoy 1.40.2).
hold, or that its check admits when it writes `ask_unlisted`, but `null`, and in such an environment to its own origin,
a host on `https` at the default port. Without `ask_unlisted`, an origin on the environment's own host that neither
list holds is admitted without them, also when it is the request's own host and port; its preflight still carries
`Access-Control-Allow-Methods`, `Access-Control-Allow-Headers` and `Access-Control-Max-Age` from `allow_methods`,
`allow_headers` and `max_age` (measured on Envoy 1.40.2). With `ask_unlisted`, the host on another scheme or port is
put to the check, and gets them only when the check admits it
([An origin no list holds](origins.md#an-origin-no-list-holds)).

<!-- keys: cors -->
| Key | Meaning | Rule | Class |
Expand Down
6 changes: 3 additions & 3 deletions docs/defaults.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ runtime value, for example, looks it up by its key.
| `rate_limit` | The rate limit filter's `stat_prefix`, written with a `rate_limit`. |
| `otel_collector` | The cluster of the tracing endpoint, written with the `otel` provider. |
| `jwks` | The cluster of the keys URL's host, written when `auth.jwt.keys` is an https URL. |
| `browser-origins` | The one RBAC policy of each virtual host. |
| `ask_unlisted:` | Followed by the environment's name, the name of the check of an environment that writes `ask_unlisted`, a filter off on every route but the environment's copies of the routes for an origin no list holds, which turn it on by this name. It keeps its decision in the filter state under this name and `.shadow`. |
| `admitted-by-the-check` | The RBAC policy of each such copy of a route, which admits a request only when the environment's check answered `200`. |
| `browser-origins` | The RBAC policy of each virtual host that admits a request with no `Origin`, and one from an origin the environment's hosts, `origins` or `patterns` admit. |
| `ask_unlisted:` | Followed by the environment's name, the name of the check of an environment that writes `ask_unlisted`, a filter off by default, which the environment's virtual host turns on by this name and its refused route turns off again. It keeps its decision in the filter state under this name and `.shadow`. |
| `admitted-by-the-check` | The second RBAC policy of the virtual host of such an environment, which admits a request only when the environment's check answered `200`. |
| `x-gateway-action`, `x-gateway-reports` | The question headers of an RPC route, read by the auth check. |
| `x-gateway-question` | The question `origins.<environment name>.question` sends the environment's check. |
| `x-gateway-timeout-ms` | The check's deadline in milliseconds. |
Expand Down
Loading