package edge import ( "net/http" "github.com/carlosframework/platform/internal/registry" "github.com/carlosframework/platform/internal/seeds" ) // ImportVerifyHeader carries an uncommitted import's verify token // (seeded-instances spec §5.4). The CLI's verify sends it (instances_import.go). const ImportVerifyHeader = "Carlos-Import-Verify" // importGateAdmit is the import gate (spec §5.4, codex 15). host is the // request's host and rt the route it resolved to after the door's scope // check. ServeHTTP calls it before the meter, the password gate, the cache // and the activator, so a refused request is not served, cached, billed or // woken. // // The verify header is removed from every request that gets this far, so // the token never reaches an app, gated or not. It is never logged or // written into an answer. // // While host's own row, or the row the request resolved to, carries a gate, // exactly one request passes: a GET (the CLI's verify, spec §5) whose token // hashes to the gate, compared in constant time (seeds.TokenMatches), and // resolved to host's own row. An alias resolves to its primary's row, so an // alias of a gated host is that host. A path route does not: a prefix under // a gated host that selects another row is refused, token or not, because // the verify is the import's own request and a prefix's target is another // route; and an ungated host's prefix that selects a gated row is refused // for the same reason. ImportGateClosed (a rollback) matches no token. // // Everything else gets 503 with Retry-After: 5 and Cache-Control: no-store. func (e *Edge) importGateAdmit(w http.ResponseWriter, r *http.Request, host string, rt registry.Route) bool { token := r.Header.Get(ImportVerifyHeader) r.Header.Del(ImportVerifyHeader) front := rt if up, ok := lookupIn(e.current(), host); ok { front = up.route } if rt.ImportGate == "" && front.ImportGate == "" { return true } if front.Host == rt.Host && r.Method == http.MethodGet && seeds.TokenMatches(rt.ImportGate, token) { return true } w.Header().Set("Retry-After", "5") w.Header().Set("Cache-Control", "no-store") http.Error(w, "this instance is being imported. Try again in a few seconds.", http.StatusServiceUnavailable) return false }