zuplo 7.15.3 → 7.15.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,264 @@
1
+ ---
2
+ title: Routing a Test Hostname to a Separate Backend
3
+ sidebar_label: Hostname-Based Routing
4
+ description:
5
+ Test backend releases against production data with a second Akamai hostname.
6
+ Akamai sets a routing header, and a Zuplo policy picks the matching backend.
7
+ applies_to:
8
+ product: gateway
9
+ hosting: [managed-dedicated]
10
+ ---
11
+
12
+ Test a new backend release against production data by sending a second public
13
+ hostname through the same production Zuplo gateway. Akamai tags each request
14
+ with a header based on the hostname, and one Zuplo policy uses that header to
15
+ pick the production or test backend. Your frontend and API clients change
16
+ nothing except the hostname they call.
17
+
18
+ ## How it works
19
+
20
+ <Diagram height="h-80">
21
+ <DiagramNode id="prod-client">Production clients</DiagramNode>
22
+ <DiagramNode id="test-client">Test clients</DiagramNode>
23
+ <DiagramGroup id="akamai-edge" label="Akamai property">
24
+ <DiagramNode id="cdn" variant="blue">
25
+ api.example.com
26
+ <br />
27
+ api-test.example.com
28
+ </DiagramNode>
29
+ </DiagramGroup>
30
+ <DiagramNode id="gateway" variant="zuplo">
31
+ Production Zuplo gateway
32
+ </DiagramNode>
33
+ <DiagramGroup id="backends" label="Backends">
34
+ <DiagramNode id="prod-backend">Production backend</DiagramNode>
35
+ <DiagramNode id="test-backend">Test backend</DiagramNode>
36
+ </DiagramGroup>
37
+ <DiagramEdge from="prod-client" to="cdn" label="api.example.com" />
38
+ <DiagramEdge from="test-client" to="cdn" label="api-test.example.com" />
39
+ <DiagramEdge from="cdn" to="gateway" label="x-zuplo-route header" />
40
+ <DiagramEdge from="gateway" to="prod-backend" label="prod" />
41
+ <DiagramEdge from="gateway" to="test-backend" label="test" />
42
+ </Diagram>
43
+
44
+ Akamai turns the hostname into a header, and the gateway turns the header into a
45
+ backend. Everything between the client and the backend is the production path
46
+ for both hostnames: the same Akamai property, the same gateway deployment, and
47
+ the same routes, policies, and rate limits.
48
+
49
+ The examples on this page use these hostnames. Replace them with your own.
50
+
51
+ | Example hostname | Purpose |
52
+ | ----------------------------- | ----------------------------------------------------- |
53
+ | `api.example.com` | Public production hostname |
54
+ | `api-test.example.com` | New public test hostname, on the same Akamai property |
55
+ | `gateway.example.com` | Production Zuplo custom domain, the Akamai origin |
56
+ | `origin-api.example.com` | Production backend |
57
+ | `origin-api-test.example.com` | Test backend that runs the release candidate |
58
+
59
+ ## Prerequisites
60
+
61
+ - An Akamai property that already forwards `api.example.com` to your production
62
+ Zuplo gateway. See [Setting up Akamai CDNs](./cdn.mdx).
63
+ - A test backend that runs the release you want to validate and can reach
64
+ production data.
65
+ - An edge certificate and DNS access for the new test hostname.
66
+
67
+ ## Step 1: Set the routing header in Akamai
68
+
69
+ Both public hostnames live on the same Akamai property and use the same origin:
70
+ the production Zuplo custom domain. Akamai always sets `x-zuplo-route`, so it
71
+ overwrites any value a client sends.
72
+
73
+ In [Property Manager](https://techdocs.akamai.com/property-mgr/docs), edit the
74
+ property and make these changes:
75
+
76
+ 1. **Default rule.** Add a
77
+ [Modify Outgoing Request Header](https://techdocs.akamai.com/property-mgr/docs/modify-outgoing-req-header)
78
+ behavior. Set the action to **Add** or **Modify**, the custom header name to
79
+ `x-zuplo-route`, and the value to `prod`.
80
+ 2. **New child rule named `Test routing`.** Add a **Hostname** match for
81
+ `api-test.example.com`. Add a **Modify Outgoing Request Header** behavior
82
+ with the custom header name `x-zuplo-route` and the value `test`. Because the
83
+ child rule runs after the default rule, it wins for the test hostname.
84
+ 3. **Cache key.** Make sure test and production responses can't share a cache
85
+ entry. Set **Cache Key Hostname** to **Incoming Host Header**, or turn off
86
+ caching in the `Test routing` rule.
87
+ 4. **Hostname.** Add `api-test.example.com` to the property's hostnames with its
88
+ edge certificate, and create its CNAME record in your DNS.
89
+
90
+ Activate the property version. Clients that test the release only change the
91
+ base URL they call. They never send the header themselves.
92
+
93
+ ## Step 2: Add the backend URLs in Zuplo
94
+
95
+ In the [Zuplo Portal](https://portal.zuplo.com/+/account/project/), open your
96
+ project and select **Settings → Environment Variables**. Add these variables to
97
+ the production environment. If you already have a variable for the production
98
+ backend, keep its name and use it in place of `BASE_URL_PROD` in the following
99
+ steps.
100
+
101
+ ```bash
102
+ BASE_URL_PROD=https://origin-api.example.com
103
+ BASE_URL_TEST=https://origin-api-test.example.com
104
+ ```
105
+
106
+ You don't need a new Zuplo environment, branch, or custom domain. The production
107
+ deployment you run today serves both hostnames. For more on variables, see
108
+ [Environment variables](../../articles/environment-variables.mdx).
109
+
110
+ ## Step 3: Add the routing policy
111
+
112
+ This policy follows the pattern in
113
+ [Canary routing for employees](../../guides/canary-routing-for-employees.mdx),
114
+ adapted to one signal: the `x-zuplo-route` header that Akamai sets. Any value
115
+ other than `test` goes to production, so a missing or unexpected value never
116
+ reaches the test backend. You add the policy once; it doesn't change between
117
+ releases.
118
+
119
+ Create `modules/test-routing.ts`:
120
+
121
+ ```ts title="modules/test-routing.ts"
122
+ import {
123
+ HttpProblems,
124
+ ZuploContext,
125
+ ZuploRequest,
126
+ environment,
127
+ } from "@zuplo/runtime";
128
+
129
+ // Set by the Akamai property on every request. Never set by clients.
130
+ const ROUTE_HEADER = "x-zuplo-route";
131
+
132
+ export default async function testRouting(
133
+ request: ZuploRequest,
134
+ context: ZuploContext,
135
+ ) {
136
+ const isTest =
137
+ request.headers.get(ROUTE_HEADER)?.trim().toLowerCase() === "test";
138
+
139
+ const backendUrl = isTest
140
+ ? environment.BASE_URL_TEST
141
+ : environment.BASE_URL_PROD;
142
+
143
+ if (!backendUrl) {
144
+ context.log.error("Backend base URL is not configured", {
145
+ stage: isTest ? "test" : "prod",
146
+ });
147
+ return HttpProblems.internalServerError(request, context);
148
+ }
149
+
150
+ // The route's URL Forward handler reads this value as its baseUrl.
151
+ context.custom.backendUrl = backendUrl;
152
+
153
+ if (isTest) {
154
+ context.log.info("Routing to test backend", {
155
+ path: new URL(request.url).pathname,
156
+ });
157
+ }
158
+
159
+ // Lets testers confirm which backend answered.
160
+ const stage = isTest ? "test" : "prod";
161
+ context.addResponseSendingHook((response) => {
162
+ const tagged = new Response(response.body, response);
163
+ tagged.headers.set("x-backend-stage", stage);
164
+ return tagged;
165
+ });
166
+
167
+ return request;
168
+ }
169
+ ```
170
+
171
+ Register it in `config/policies.json`:
172
+
173
+ ```json title="config/policies.json"
174
+ {
175
+ "name": "test-routing",
176
+ "policyType": "custom-code-inbound",
177
+ "handler": {
178
+ "export": "default",
179
+ "module": "$import(./modules/test-routing)"
180
+ }
181
+ }
182
+ ```
183
+
184
+ ## Step 4: Point each route at the chosen backend
185
+
186
+ On every route that forwards to the backend, make two changes:
187
+
188
+ 1. Set the [URL Forward handler](../../handlers/url-forward.mdx) `baseUrl` to
189
+ `${context.custom.backendUrl}` instead of the environment variable.
190
+ 2. Add `test-routing` to the inbound policies. Keep your existing policies. The
191
+ routing policy only reads a header, so its position in the list doesn't
192
+ matter.
193
+
194
+ ```json title="config/routes.oas.json"
195
+ "/v1/products/{id}": {
196
+ "get": {
197
+ "x-zuplo-route": {
198
+ "handler": {
199
+ "export": "urlForwardHandler",
200
+ "module": "$import(@zuplo/runtime)",
201
+ "options": {
202
+ "baseUrl": "${context.custom.backendUrl}"
203
+ }
204
+ },
205
+ "policies": {
206
+ "inbound": ["<your-existing-policies>", "test-routing"]
207
+ }
208
+ }
209
+ }
210
+ }
211
+ ```
212
+
213
+ If the route connects to your backend with
214
+ [mTLS](../../articles/securing-backend-mtls.mdx), keep the `mtlsCertificate`
215
+ option you use today and configure the test backend to trust the same client
216
+ certificate. One setting then covers both backends.
217
+
218
+ Deploy this change to production once. From then on, every route, policy, and
219
+ rate limit is identical for both hostnames; only the backend differs.
220
+
221
+ ## Test the setup
222
+
223
+ Send these requests and check the `x-backend-stage` response header on each:
224
+
225
+ | Request | Expected `x-backend-stage` |
226
+ | ------------------------------------------------------------------------ | ------------------------------------ |
227
+ | `curl -i https://api.example.com/v1/products/1` | `prod` |
228
+ | `curl -i https://api-test.example.com/v1/products/1` | `test` |
229
+ | `curl -i https://api.example.com/v1/products/1 -H "x-zuplo-route: test"` | `prod` (Akamai overwrote the header) |
230
+
231
+ If the third request returns `test`, the default rule from Step 1 isn't setting
232
+ the header. Fix that before you share the test hostname.
233
+
234
+ ## Release flow
235
+
236
+ A backend release doesn't require a gateway change:
237
+
238
+ 1. Your CI/CD pipeline deploys the new backend version to the test backend.
239
+ 2. Testers and test builds of your frontend call `api-test.example.com`. Their
240
+ requests go through the production Akamai property and the production gateway
241
+ and its policies, to production data.
242
+ 3. When testing passes, promote the same build to the production backend.
243
+
244
+ Keep using your non-production Zuplo environments to test gateway changes.
245
+ Changing the gateway and the backend in the same test makes a failure hard to
246
+ place. Serving both hostnames from one gateway also means the test and
247
+ production paths can't drift apart, which a separate Zuplo environment for
248
+ testing would allow.
249
+
250
+ ## Guardrails
251
+
252
+ - **Only Akamai sets the header.** If `gateway.example.com` is reachable without
253
+ going through Akamai, anyone could send `x-zuplo-route: test`. Restrict the
254
+ gateway to Akamai traffic with a shared secret header or mTLS. See
255
+ [Securing Zuplo from direct access](../../articles/waf-ddos-akamai.md#securing-zuplo-from-direct-access).
256
+ - **The test hostname uses production data.** Treat it as production: keep the
257
+ same authentication and rate limits, and don't publish the hostname.
258
+
259
+ ## Related resources
260
+
261
+ - [Akamai architecture](./architecture.mdx)
262
+ - [Controlling Akamai CDN caching](./caching.mdx)
263
+ - [User-based backend routing](../../guides/user-based-backend-routing.mdx)
264
+ - [ZuploContext `addResponseSendingHook`](../../programmable-api/zuplo-context.mdx#addresponsesendinghook)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zuplo",
3
- "version": "7.15.3",
3
+ "version": "7.15.4",
4
4
  "type": "module",
5
5
  "description": "The official Zuplo CLI for local development and platform management",
6
6
  "homepage": "https://zuplo.com/docs/cli/overview",
@@ -32,9 +32,9 @@
32
32
  "zuplo": "zuplo.js"
33
33
  },
34
34
  "dependencies": {
35
- "@zuplo/cli": "7.15.3",
36
- "@zuplo/core": "7.15.3",
37
- "@zuplo/runtime": "7.15.3",
38
- "@zuplo/test": "7.15.3"
35
+ "@zuplo/cli": "7.15.4",
36
+ "@zuplo/core": "7.15.4",
37
+ "@zuplo/runtime": "7.15.4",
38
+ "@zuplo/test": "7.15.4"
39
39
  }
40
40
  }