Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
feat(workspace): nginx proxy_pass routes, including envsubst templates, as gateway routes (#4543)
Raghav Chamadiya7820170f1671
diff --git a/docs/architecture/workspace.md b/docs/architecture/workspace.mdindex da2d9b6fb..4f2e88ba8 100644--- a/docs/architecture/workspace.md+++ b/docs/architecture/workspace.md@@ -413,6 +413,8 @@ add its case to `test_graph_evidence.py`.links.- Gateway routes are prefix routes only. A relative call from a frontend repo otherthan the gateway's is not routed, since nothing says which gateway serves it.+ nginx regex locations are not read, and `${VAR}` resolves as an env key only in a+ `*.template` / `*.tmpl` file (envsubst); in a plain config it is an nginx variable.- A `host_only` edge is service-level: it has no provider route, so breaking-changediffs and test impact do not follow it.- The network probe is lexical. It reads only extensions some extractor walks,diff --git a/docs/reference/WORKSPACE_CONTRACTS.md b/docs/reference/WORKSPACE_CONTRACTS.mdindex ccda333cc..7edce6690 100644--- a/docs/reference/WORKSPACE_CONTRACTS.md+++ b/docs/reference/WORKSPACE_CONTRACTS.md@@ -127,6 +127,8 @@ records `target_via` (the source) and `target_evidence` (the file).| `k8s_service` | `*.yaml` under `k8s/`, `deploy/`, `manifests/`, ... | `kind: Service` `metadata.name` | A selector or label value equal to a repo alias, else the manifest's repo when it declares no other Service || `spring_application_name` | `application` / `bootstrap` `.yml` or `.properties` | `spring.application.name` | The module the file sits in || `spring_gateway` | `application` / `bootstrap` in a module's resources, or a config-server file named `*gateway*`, `*zuul*`, `*proxy*`, `*edge*` (`.yml` or `.properties`) holding `routes` | Spring Cloud Gateway `routes[]` (`lb://name`, `http://host`) and Zuul `zuul.routes` | A gateway route (below) |+| `dockerfile_env` | `Dockerfile`, `Dockerfile.*`, `*.Dockerfile` holding `ENV` | `ENV` keys whose value is an address (`CATALOGUE_HOST=catalogue`) | The host the value names |+| `nginx` | `*.conf`, `*.conf.template`, `*.conf.tmpl` holding `proxy_pass` | Prefix `location` blocks with `proxy_pass` to a host, an `upstream` of one host, or an envsubst `${X_HOST}` in a template | A gateway route (below) |Each repo is walked once per update for these file names; test, example and build-outputdirectories are skipped, and files at the workspace root are read too. A name two files@@ -135,7 +137,8 @@ the host or base token to `service_bases`, which always wins.A gateway source states routes rather than names: a path prefix the gateway forwards to aservice, and the path it forwards (Spring `StripPrefix`, `PrefixPath` and a-`RewritePath=/x/?(?<s>.*), /y/${s}`; Zuul `stripPrefix`). The gateway is the repo holding+`RewritePath=/x/?(?<s>.*), /y/${s}`; Zuul `stripPrefix`). An nginx `proxy_pass` with a path replaces the location prefix with that path; one+without passes the request path unchanged. The nginx gateway is the repo holding the file. The gateway is the repo holdingan `application` file in its module's own resources, or for a config server's `gateway.yml`the app its name spells. Each route is a `gateway_route` edge from the gateway to theservice. A route whose forwarded path or target is not read (a Host or Method predicate,diff --git a/packages/core/src/repowise/core/workspace/service_names/__init__.py b/packages/core/src/repowise/core/workspace/service_names/__init__.pyindex 10b6ae83d..db7bf6e71 100644--- a/packages/core/src/repowise/core/workspace/service_names/__init__.py+++ b/packages/core/src/repowise/core/workspace/service_names/__init__.py@@ -35,7 +35,7 @@ from typing import Anyfrom repowise.core.fs_walk import PRUNED_DIRS, walk_repo-from . import aspire, compose, k8s_service, spring, spring_gateway+from . import aspire, compose, dockerfile, k8s_service, nginx, spring, spring_gatewayfrom .base import ENV, HOST, Claim, Route, ServiceNameSource, SourceFile_log = logging.getLogger("repowise.workspace.service_names")@@ -43,7 +43,9 @@ _log = logging.getLogger("repowise.workspace.service_names")SOURCES: tuple[ServiceNameSource, ...] = (aspire.SOURCE,compose.SOURCE,+ dockerfile.SOURCE,k8s_service.SOURCE,+ nginx.SOURCE,spring.SOURCE,spring_gateway.SOURCE,)@@ -261,7 +263,7 @@ def _settle(out.ambiguous[key] = tuple(sorted(by_repo))-def _route_row(f: _Found, named: Callable[[str], str | None]) -> GatewayRoute | None:+def _route_row(f: _Found, named: Callable[..., str | None]) -> GatewayRoute | None:"""One route claim placed: its gateway repo and target repo (``""`` when unknown or itself)."""route: Route = f.claim.route # type: ignore[assignment]if route.gateway:@@ -270,7 +272,7 @@ def _route_row(f: _Found, named: Callable[[str], str | None]) -> GatewayRoute |gateway = f.file_repoif gateway is None or not route.prefix:return None- target = named(f.claim.name)+ target = named(f.claim.name, f.claim.kind)target = "" if target is None or target == gateway else targetreturn GatewayRoute(gateway, route.prefix, route.upstream, target, f.claim.name, f.source.name, f.evidence@@ -278,7 +280,7 @@ def _route_row(f: _Found, named: Callable[[str], str | None]) -> GatewayRoute |def _settle_routes(- found: list[_Found], named: Callable[[str], str | None]+ found: list[_Found], named: Callable[..., str | None]) -> tuple[GatewayRoute, ...]:"""One route per gateway and prefix.@@ -352,9 +354,11 @@ def build_service_name_map(# Ceiling: keys and names are workspace-wide, not scoped to the stack that# declares them; two stacks reusing a name collide and are dropped. Scoping# by compose project is the upgrade if estates mix unrelated stacks.- def named(name: str) -> str | None:- hit = out.lookup(name, HOST)- return hit.repo if hit is not None else resolver.named((name.lower(),))+ def named(name: str, kind: str = HOST) -> str | None:+ hit = out.lookup(name, kind)+ if hit is not None or kind != HOST:+ return hit.repo if hit is not None else None # an env key names no alias+ return resolver.named((name.lower(),))env_claimed: dict[tuple[str, str], dict[str, tuple[str, str]]] = defaultdict(dict)for f in refs:diff --git a/packages/core/src/repowise/core/workspace/service_names/base.py b/packages/core/src/repowise/core/workspace/service_names/base.pyindex 790245db0..9883fd997 100644--- a/packages/core/src/repowise/core/workspace/service_names/base.py+++ b/packages/core/src/repowise/core/workspace/service_names/base.py@@ -59,7 +59,8 @@ class Claim:A claim with a ``route`` says instead that a gateway forwards that pathprefix to *name*; *name* resolves like a ``ref``, through the finished host- map and then the repo aliases.+ map and then the repo aliases, or with ``kind`` :data:`ENV` through the+ finished env keys (an nginx template's ``${CATALOGUE_HOST}``)."""name: strdiff --git a/packages/core/src/repowise/core/workspace/service_names/dockerfile.py b/packages/core/src/repowise/core/workspace/service_names/dockerfile.pynew file mode 100644index 000000000..b5d687bb8--- /dev/null+++ b/packages/core/src/repowise/core/workspace/service_names/dockerfile.py@@ -0,0 +1,53 @@+"""Dockerfile ``ENV``: the addresses an image's defaults give its env keys.++``ENV CATALOGUE_HOST=catalogue`` makes ``CATALOGUE_HOST`` a reference to the+host ``catalogue``, as a compose ``environment`` entry does; an nginx template+or a consumer's ``${CATALOGUE_HOST}`` resolves through it. Both the+``KEY=value ...`` and the legacy ``KEY value`` forms are read, and only values+:func:`.base.host_of` reads as addresses. A compose or deploy value that sets+the same key to another host makes the key ambiguous, so it is dropped rather+than resolved to the image default.+"""++from __future__ import annotations++import re+import shlex+from collections.abc import Iterable, Iterator++from .base import ENV, Claim, ServiceNameSource, SourceFile, host_of++_CONTINUATION_RE = re.compile(r"\\\r?\n")+_ENV_RE = re.compile(r"^\s*ENV\s+(.+)$", re.IGNORECASE | re.MULTILINE)+++def _pairs(args: str) -> Iterator[tuple[str, str]]:+ try:+ words = shlex.split(args, comments=False)+ except ValueError:+ return+ if not words:+ return+ if "=" not in words[0]:+ yield words[0], " ".join(words[1:]) # legacy ``ENV KEY value``+ return+ for word in words:+ key, sep, value = word.partition("=")+ if sep:+ yield key, value+++def read(file: SourceFile) -> Iterable[Claim]:+ for m in _ENV_RE.finditer(_CONTINUATION_RE.sub(" ", file.text)):+ for key, value in _pairs(m.group(1)):+ host = host_of(key, value)+ if host:+ yield Claim(name=key.lower(), kind=ENV, ref=host)+++SOURCE = ServiceNameSource(+ name="dockerfile_env",+ filenames=("Dockerfile", "Dockerfile.*", "*.Dockerfile"),+ marker="ENV",+ read=read,+)diff --git a/packages/core/src/repowise/core/workspace/service_names/nginx.py b/packages/core/src/repowise/core/workspace/service_names/nginx.pynew file mode 100644index 000000000..4ab28afda--- /dev/null+++ b/packages/core/src/repowise/core/workspace/service_names/nginx.py@@ -0,0 +1,191 @@+"""nginx ``proxy_pass``: which path prefix an nginx front forwards to which service.++``location /api/catalogue/ { proxy_pass http://catalogue:8080/; }`` forwards+``/api/catalogue/products`` as ``/products``: a ``proxy_pass`` with a path+replaces the matched prefix with that path, one without passes the request+path unchanged. The upstream host names the service: a literal host, an+``upstream`` block whose servers all name one host, or an ``envsubst``+placeholder (``${CATALOGUE_HOST}`` in a ``*.conf.template``) resolved through+the env keys the workspace's config declares. The gateway is the repo holding+the file.++A location the reader cannot rewrite through is still stated, with no+forwarded path, so it keeps a shorter prefix from claiming its calls: one+served locally (``root``, ``return``, ``fastcgi_pass``, ...), an exact (``=``)+location, a ``rewrite`` in it or around it, a ``proxy_pass`` under ``if``, with+a variable path, or whose trailing slash differs from the location's, or a host+only known at runtime (``$var``). Regex and named locations are skipped+(ceiling: a regex location that overrides a prefix location is not seen), as+is the catch-all ``location /``.+"""++from __future__ import annotations++import re+from collections.abc import Iterable, Iterator+from dataclasses import dataclass++from .base import ENV, EXACT, HOST, Claim, Route, ServiceNameSource, SourceFile, literal_prefix++# A word may hold ``${VAR}``, whose braces are not block delimiters, and a+# ``#`` that does not start it.+_WORD = r"(?:\$\{[^}\s]*\}|[^\s{};\"'#])(?:\$\{[^}\s]*\}|[^\s{};\"'])*"+_TOKEN_RE = re.compile(+ r"\s+|#[^\n]*|\"((?:[^\"\\]|\\.)*)\"|'((?:[^'\\]|\\.)*)'|([{};])|(" + _WORD + ")"+)+_PROXY_RE = re.compile(r"^[a-z][a-z0-9+.-]*://([^/]+)(/.*)?$", re.IGNORECASE)+_ENVSUBST_RE = re.compile(r"^\$\{(\w+)\}$")+_HOSTNAME_RE = re.compile(r"^[a-z][a-z0-9_.-]*$", re.IGNORECASE)+_PORT_RE = re.compile(r":\d+$")+_ODD_URI_RE = re.compile(r"[$?#]|/\.\.?(?:/|$)")+++@dataclass+class _Block:+ """One directive: its name, its arguments and, for a block, its children."""++ name: str+ args: list[str]+ children: list[_Block] | None = None+++def _parse(text: str) -> list[_Block]:+ """The file's directives as a tree; an unbalanced brace ends the parse quietly."""+ root: list[_Block] = []+ stack = [root]+ words: list[str] = []+ for m in _TOKEN_RE.finditer(text):+ quoted = m.group(1) if m.group(1) is not None else m.group(2) # "" is an argument+ punct, word = m.group(3), m.group(4)+ if word is not None or quoted is not None:+ words.append(word if word is not None else quoted) # type: ignore[arg-type]+ elif punct == ";" and words:+ stack[-1].append(_Block(words[0], words[1:]))+ words = []+ elif punct == "{":+ block = _Block(words[0] if words else "", words[1:], children=[])+ stack[-1].append(block)+ stack.append(block.children) # type: ignore[arg-type]+ words = []+ elif punct == "}":+ if len(stack) == 1:+ break+ stack.pop()+ words = []+ return root+++def _host(address: str) -> str:+ """*address* without its ``:port``."""+ return _PORT_RE.sub("", address)+++def _upstreams(blocks: list[_Block]) -> dict[str, set[str]]:+ """Each ``upstream`` block's name and the hosts its ``server`` lines name."""+ out: dict[str, set[str]] = {}+ for b in blocks:+ if b.children is None:+ continue+ if b.name == "upstream" and b.args:+ out[b.args[0].lower()] = {_host(s.args[0]) for s in b.children if s.name == "server" and s.args}+ else:+ out.update(_upstreams(b.children))+ return out+++def _target(address: str, upstreams: dict[str, set[str]], envsubst: bool) -> tuple[str, str]:+ """``(kind, name)`` of the service a ``proxy_pass`` address names; ``(HOST, "")`` when unknown."""+ host = _host(address)+ if address.lower() in upstreams: # an upstream group is named without a port+ servers = upstreams[address.lower()]+ if len(servers) != 1:+ return HOST, "" # a pool of several hosts, or none: no one service+ [host] = servers+ if envsubst and (m := _ENVSUBST_RE.match(host)):+ return ENV, m.group(1).lower()+ if _HOSTNAME_RE.match(host):+ return HOST, host.lower()+ return HOST, "" # an nginx variable or a unix socket: known only at runtime+++def _rewrites(blocks: list[_Block]) -> bool:+ """True when these directives rewrite the path: a ``rewrite``, or one under ``if``."""+ return any(+ d.name == "rewrite" or (d.name == "if" and _rewrites(d.children or [])) for d in blocks+ )+++def _proxy(location: _Block) -> tuple[_Block | None, bool]:+ """The location's own ``proxy_pass``, and whether a rewrite or ``if`` makes it unreadable."""+ children = location.children or []+ proxy = next((d for d in children if d.name == "proxy_pass" and d.args), None)+ unreadable = _rewrites(children) or any(+ d.name == "if" and any(c.name == "proxy_pass" for c in d.children or []) for d in children+ )+ return proxy, unreadable+++def _locations(blocks: list[_Block], rewritten: bool = False) -> Iterator[tuple[_Block, bool]]:+ """Every location, and whether a ``rewrite`` around it changes the path it sees."""+ rewritten = rewritten or _rewrites(blocks)+ for b in blocks:+ if b.children is None:+ continue+ if b.name == "location":+ yield b, rewritten+ if b.name != "upstream":+ yield from _locations(b.children, rewritten)+++def _forwarded(path: str, prefix: str, uri: str | None) -> str | None:+ """What a call under *path* is forwarded as: *uri* in place of the prefix, the path+ unchanged without one, None when nginx would splice it oddly.++ nginx appends the rest of the path to the URI as written (``/api/`` to ``/v2``+ forwards ``/v2x``), so only a URI ending as the location does is read.+ """+ if uri is None:+ return prefix+ if path.endswith("/") != uri.endswith("/") or _ODD_URI_RE.search(uri):+ return None+ return uri.rstrip("/").lower()+++def _route(+ location: _Block, upstreams: dict[str, set[str]], envsubst: bool, rewritten: bool+) -> tuple[str, str, Route] | None:+ args = location.args+ modifier, path = (args[0], args[1]) if len(args) == 2 else ("", args[0] if args else "")+ if modifier not in ("", "^~", "=") or not path.startswith("/"):+ return None # a regex or named location: which paths it takes is not read+ prefix, shape = literal_prefix(path)+ if not prefix:+ return None # a catch-all is never more specific than a real route+ proxy, unreadable = _proxy(location)+ m = _PROXY_RE.match(proxy.args[0]) if proxy is not None else None+ if m is None:+ # Served here, or proxied somewhere unreadable: only blocks shorter prefixes.+ return HOST, "", Route(prefix, None)+ kind, name = _target(m.group(1), upstreams, envsubst)+ readable = not (unreadable or rewritten) and modifier != "=" and shape == EXACT+ return kind, name, Route(prefix, _forwarded(path, prefix, m.group(2)) if readable else None)+++def read(file: SourceFile) -> Iterable[Claim]:+ # ``${VAR}`` is an envsubst placeholder only in a template; in a plain+ # config it is an nginx variable, known only at runtime.+ envsubst = file.path.name.lower().endswith((".template", ".tmpl"))+ blocks = _parse(file.text)+ upstreams = _upstreams(blocks)+ for location, rewritten in _locations(blocks):+ if (found := _route(location, upstreams, envsubst, rewritten)) is not None:+ kind, name, route = found+ yield Claim(name=name, kind=kind, route=route)+++SOURCE = ServiceNameSource(+ name="nginx",+ filenames=("*.conf", "*.conf.template", "*.conf.tmpl"),+ marker="proxy_pass",+ read=read,+)diff --git a/tests/fixtures/service_names/robotshop/web/Dockerfile b/tests/fixtures/service_names/robotshop/web/Dockerfilenew file mode 100644index 000000000..22a8df8aa--- /dev/null+++ b/tests/fixtures/service_names/robotshop/web/Dockerfile@@ -0,0 +1,43 @@+FROM alpine AS build+ARG KEY++WORKDIR /instana++RUN apk add --update --no-cache wget++ENV ARTI_PATH='artifact-public.instana.io/artifactory/shared/com/instana/nginx_tracing/'++RUN if [ -n "$KEY" ]; then \+ sensor_version=$(wget -O- "https://_:${KEY}@${ARTI_PATH}" 2>&1 | \+ egrep -o "href=\"([0-9]+\.){2}[0-9]+/" | cut -d'"' -f2 | sort -V | tail -n1 | tr -d /); \+ echo "Downloading sensor version ${sensor_version} for Nginx version 1.21.6" ; \+ $(wget -O instana.zip \+ "https://_:${KEY}@${ARTI_PATH}/${sensor_version}/linux-amd64-glibc-nginx-1.21.6.zip") && \+ unzip instana.zip && \+ mv glibc-libinstana_sensor.so libinstana_sensor.so && \+ mv glibc-nginx-1.21.6-ngx_http_ot_module.so ngx_http_opentracing_module.so; \+ else echo "KEY not provided. Not adding tracing"; \+ touch dummy.so; \+ fi+++FROM nginx:1.21.6++EXPOSE 8080++ENV CATALOGUE_HOST=catalogue \+ USER_HOST=user \+ CART_HOST=cart \+ SHIPPING_HOST=shipping \+ PAYMENT_HOST=payment \+ RATINGS_HOST=ratings \+ INSTANA_SERVICE_NAME=nginx-web++# Instana tracing+COPY --from=build /instana/*.so /tmp/++COPY entrypoint.sh /root/+ENTRYPOINT ["/root/entrypoint.sh"]++COPY default.conf.template /etc/nginx/conf.d/default.conf.template+COPY static /usr/share/nginx/htmldiff --git a/tests/fixtures/service_names/robotshop/web/default.conf.template b/tests/fixtures/service_names/robotshop/web/default.conf.templatenew file mode 100644index 000000000..ff70fe5d5--- /dev/null+++ b/tests/fixtures/service_names/robotshop/web/default.conf.template@@ -0,0 +1,88 @@+# Instana tracing+opentracing_load_tracer /usr/local/lib/libinstana_sensor.so /etc/instana-config.json;+opentracing_propagate_context;++server {+ listen 8080;+ server_name localhost;++ proxy_http_version 1.1;++ #charset koi8-r;+ #access_log /var/log/nginx/host.access.log main;+ #error_log /dev/stdout debug;+ #rewrite_log on;++ location / {+ root /usr/share/nginx/html;+ index index.html index.htm;+ ssi on;+ }++ location /images/ {+ expires 5s;+ root /usr/share/nginx/html;+ try_files $uri /images/placeholder.png;+ }++ #error_page 404 /404.html;++ # redirect server error pages to the static page /50x.html+ #+ error_page 500 502 503 504 /50x.html;+ location = /50x.html {+ root /usr/share/nginx/html;+ }++ # proxy the PHP scripts to Apache listening on 127.0.0.1:80+ #+ #location ~ \.php$ {+ # proxy_pass http://127.0.0.1;+ #}++ # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000+ #+ #location ~ \.php$ {+ # root html;+ # fastcgi_pass 127.0.0.1:9000;+ # fastcgi_index index.php;+ # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;+ # include fastcgi_params;+ #}++ # deny access to .htaccess files, if Apache's document root+ # concurs with nginx's one+ #+ #location ~ /\.ht {+ # deny all;+ #}++ location /api/catalogue/ {+ proxy_pass http://${CATALOGUE_HOST}:8080/;+ }++ location /api/user/ {+ proxy_pass http://${USER_HOST}:8080/;+ }++ location /api/cart/ {+ proxy_pass http://${CART_HOST}:8080/;+ }++ location /api/shipping/ {+ proxy_pass http://${SHIPPING_HOST}:8080/;+ }++ location /api/payment/ {+ proxy_pass http://${PAYMENT_HOST}:8080/;+ }++ location /api/ratings/ {+ proxy_pass http://${RATINGS_HOST}:80/;+ }++ location /nginx_status {+ stub_status on;+ access_log off;+ }+}diff --git a/tests/unit/workspace/test_nginx_routes.py b/tests/unit/workspace/test_nginx_routes.pynew file mode 100644index 000000000..b72154cb7--- /dev/null+++ b/tests/unit/workspace/test_nginx_routes.py@@ -0,0 +1,187 @@+"""nginx ``proxy_pass`` routes and Dockerfile ``ENV`` addresses in the service-name map."""++from __future__ import annotations++from pathlib import Path++from repowise.core.workspace.contracts import Contract+from repowise.core.workspace.matching import annotate_consumer_targets, run_matching+from repowise.core.workspace.origins import LinkStrategy+from repowise.core.workspace.service_names import (+ ENV,+ HOST,+ ServiceNameMap,+ build_service_name_map,+ dockerfile,+ nginx,+)+from repowise.core.workspace.service_names.base import Route, SourceFile++FIXTURES = Path(__file__).resolve().parents[2] / "fixtures" / "service_names"+ROBOTSHOP = ("cart", "catalogue", "user", "shipping", "payment", "ratings", "web")+++def _routes(m: ServiceNameMap) -> set[tuple[str, str, str | None, str]]:+ return {(r.gateway, r.prefix, r.upstream, r.repo) for r in m.routes}+++def _nginx(text: str, name: str = "default.conf") -> list[tuple[str, str, Route]]:+ return [(c.kind, c.name, c.route) for c in nginx.read(SourceFile(Path(name), text)) if c.route]+++def _http(repo: str, role: str, path: str) -> Contract:+ return Contract(+ repo=repo,+ contract_id=f"http::GET::{path}",+ contract_type="http",+ role=role,+ file_path=f"{role}.js",+ symbol_name=path,+ confidence=0.85 if role == "provider" else 0.75,+ meta={"method": "GET", "path": path},+ )+++def _robotshop() -> ServiceNameMap:+ root = FIXTURES / "robotshop"+ return build_service_name_map({a: root / a for a in ROBOTSHOP}, root)+++def test_robotshop_template_routes_each_prefix_through_its_env_host() -> None:+ m = _robotshop()+ # web/Dockerfile's ENV defaults say which host each ${X_HOST} placeholder names.+ assert m.lookup("catalogue_host", ENV).repo == "catalogue" # type: ignore[union-attr]+ assert m.lookup("catalogue_host", ENV).source == "dockerfile_env" # type: ignore[union-attr]+ assert _routes(m) == {+ ("web", f"/api/{svc}", "", svc)+ for svc in ("catalogue", "user", "cart", "shipping", "payment", "ratings")+ } | {+ # Served by nginx itself: blockers with no target.+ ("web", "/images", None, ""),+ ("web", "/50x.html", None, ""),+ ("web", "/nginx_status", None, ""),+ }+ [route] = [r for r in m.routes if r.repo == "ratings"]+ assert (route.name, route.source, route.file) == (+ "ratings_host", "nginx", "web/default.conf.template"+ )+++def test_calls_from_the_nginx_repo_link_on_the_stripped_path() -> None:+ m = _robotshop()+ contracts = [+ _http("catalogue", "provider", "/products"),+ _http("web", "consumer", "/api/catalogue/products"), # the nginx repo's own page+ ]+ annotate_consumer_targets(contracts, {}, m)+ assert contracts[1].meta["route_path"] == "/products"+ state = run_matching(contracts, m.routes)+ assert [(lk.consumer_repo, lk.provider_repo, lk.contract_id, lk.strategy) for lk in state.links] == [+ ("web", "catalogue", "http::GET::/products", LinkStrategy.GATEWAY_ROUTE)+ ]+ assert {lk.provider_repo for lk in state.service_links} == {+ "catalogue", "user", "cart", "shipping", "payment", "ratings"+ }+++def test_proxy_pass_shapes() -> None:+ text = """+ upstream backend { server cart:8080 weight=2; server cart:8081; }+ upstream pool { server cart:8080; server user:8080; }+ server {+ listen 80;+ location / { root /usr/share/nginx/html; }+ location /assets/ { proxy_pass http://catalogue; }+ # location /old/ { proxy_pass http://user:8080/; }+ location /api/v2/ { proxy_pass http://user:8080/v2/; }+ location ^~ /api/cart/ { proxy_pass http://backend/; }+ location /api/pool/ { proxy_pass http://pool/; }+ location = /api/exact { proxy_pass http://user:8080/; }+ location /api/rw/ { rewrite ^/api/rw/(.*)$ /$1 break; proxy_pass http://user:8080; }+ location /api/var/ { proxy_pass http://user:8080/$request_uri; }+ location /api/rt/ { proxy_pass http://$upstream_host; }+ location ~ ^/api/re/(.*)$ { proxy_pass http://user:8080/$1; }+ location /api/ext/ { proxy_pass https://api.example.com/; }+ location /api/nested/ {+ location /api/nested/deep/ { proxy_pass "http://catalogue:8080/deep/"; }+ }+ location /api/static/ { root /srv; }+ location /api/slash/ { proxy_pass http://user:8080/v2; }+ location /api/frag/ { proxy_pass http://user:8080/x#y; }+ location /api/port/ { proxy_pass http://backend:9000/; }+ }+ server {+ rewrite ^/r/(.*)$ /$1 break;+ location /r/ { proxy_pass http://user:8080; }+ }+ """+ assert _nginx(text) == [+ (HOST, "catalogue", Route("/assets", "/assets")), # no path: forwarded unchanged+ (HOST, "user", Route("/api/v2", "/v2")),+ (HOST, "cart", Route("/api/cart", "")), # one host behind the upstream block+ (HOST, "", Route("/api/pool", "")), # a pool of two services: no one target+ (HOST, "user", Route("/api/exact", None)),+ (HOST, "user", Route("/api/rw", None)),+ (HOST, "user", Route("/api/var", None)),+ (HOST, "", Route("/api/rt", "/api/rt")), # a runtime host: no target+ (HOST, "api.example.com", Route("/api/ext", "")),+ (HOST, "", Route("/api/nested", None)), # served here: a blocker+ (HOST, "catalogue", Route("/api/nested/deep", "/deep")),+ (HOST, "", Route("/api/static", None)),+ (HOST, "user", Route("/api/slash", None)), # /api/x would go out as /v2x+ (HOST, "user", Route("/api/frag", None)), # a # inside a word is not a comment+ (HOST, "backend", Route("/api/port", "")), # with a port it is a host, not the group+ (HOST, "user", Route("/r", None)), # a server-level rewrite changes the path+ ]+++def test_placeholders_are_env_keys_only_in_a_template() -> None:+ text = "server { location /api/x/ { proxy_pass http://${X_HOST}:8080/; } }"+ assert _nginx(text, "default.conf.template") == [(ENV, "x_host", Route("/api/x", ""))]+ # In a plain config ${x} is an nginx variable, known only at runtime.+ assert _nginx(text, "default.conf") == [(HOST, "", Route("/api/x", ""))]+ # An unbalanced brace ends the parse without an error.+ assert _nginx("server { location /a/ { proxy_pass http://a/; }") == [(HOST, "a", Route("/a", ""))]+++def test_dockerfile_env_forms() -> None:+ text = (+ "FROM nginx\n"+ "ENV CATALOGUE_HOST=catalogue \\\n CART_URL=\"http://cart:8080/x\" \\\n"+ " INSTANA_SERVICE_NAME=nginx-web\n"+ "ENV USER_ENDPOINT user:8080\n"+ "ENV MYSQL_USER=root DB_HOST=${DB}\n"+ )+ claims = list(dockerfile.read(SourceFile(Path("Dockerfile"), text)))+ assert {(c.name, c.ref) for c in claims} == {+ ("catalogue_host", "catalogue"),+ ("cart_url", "cart"),+ ("user_endpoint", "user"),+ }+ assert all(c.kind == ENV for c in claims)+++def test_a_key_compose_sets_to_another_host_is_dropped(tmp_path: Path) -> None:+ for name in ("web", "catalogue", "catalogue-v2"):+ (tmp_path / name).mkdir()+ (tmp_path / "web" / "Dockerfile").write_text("ENV CATALOGUE_HOST=catalogue\n")+ (tmp_path / "web" / "default.conf.template").write_text(+ "server { location /api/catalogue/ { proxy_pass http://${CATALOGUE_HOST}:8080/; } }"+ )+ (tmp_path / "docker-compose.yml").write_text(+ "services:\n web:\n environment:\n CATALOGUE_HOST: catalogue-v2\n"+ )+ m = build_service_name_map(+ {n: tmp_path / n for n in ("web", "catalogue", "catalogue-v2")}, tmp_path+ )+ assert m.lookup("catalogue_host", ENV) is None+ # The route stays as a blocker: it rewrites nothing and draws no edge.+ assert _routes(m) == {("web", "/api/catalogue", "", "")}+++def test_an_empty_quoted_argument_keeps_its_place() -> None:+ blocks = nginx._parse('location ^~ "" { }\nproxy_set_header Host "" ;')+ assert [(b.name, b.args) for b in blocks] == [+ ("location", ["^~", ""]),+ ("proxy_set_header", ["Host", ""]),+ ]