Reach the internet
Call outside APIs from handlers with fetch, turn the internet off for a luna type, give sandboxes and isolates a network, and know which addresses Luna always refuses.
Handlers call payment providers, model APIs and other services. A handler’s fetch works as it does in Node, and Luna checks where each request goes.
Call an API from a handler
Call fetch in any handler, including a stateless luna type’s. Keep keys for the API in secrets, and read them from ctx.env.
import { defineLuna, t } from "luna";
export const Shop = defineLuna({
name: "Shop",
key: { id: t.string() },
handlers: (h) => ({
rate: h({ args: { currency: t.string() } }, async (ctx, { currency }) => {
const r = await fetch(`https://api.example.com/rates/${currency}`, {
headers: { authorization: `Bearer ${ctx.env.RATES_KEY}` },
});
if (!r.ok) throw new Error(`rates answered ${r.status}`);
return (await r.json()) as { rate: number };
}),
}),
});
A call that succeeded outside stays done, even if your handler fails afterwards and its own writes are undone. Pass the API an idempotency key when it takes one, so a retried handler does not charge twice. Secrets are in Store secrets.
Addresses Luna always refuses
Luna refuses every address that is not on the public internet, whatever a luna type allows. This holds for handlers, isolates and sandboxes.
| Refused | Ranges |
|---|---|
| This machine and private networks | 127.0.0.0/8, 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16, 100.64.0.0/10, 0.0.0.0/8 |
| Cloud metadata and link-local addresses | 169.254.0.0/16 |
| Reserved, test and multicast ranges | 192.0.0.0/24, 192.0.2.0/24, 198.18.0.0/15, 198.51.100.0/24, 203.0.113.0/24, 224.0.0.0/3 |
| IPv6 | Every address outside global unicast, and the documentation and transition ranges within it |
A name that resolves only to refused addresses is refused too. From a handler, a refused https request throws, and a refused plain http request answers with status 403 or 502.
Turn the internet off
Set internet: false on a luna type to cut its handlers off from the internet.
import { defineLuna, t } from "luna";
export const Wallet = defineLuna({
name: "Wallet",
key: { id: t.string() },
internet: false,
handlers: (h) => ({
noop: h({}, () => null),
}),
});
The handlers of all your luna types share one way out. So internet: false on any one type stops fetch in every handler deployed with it. It also takes the network from that type’s sandboxes.
Because of that, a deploy that sets internet: false on some types and not others asks you to confirm first, and names the types it also cuts off.
Give a sandbox a network
A sandbox has no network device unless its declaration asks for one with network.
network |
Reaches |
|---|---|
"none" |
Nothing. The default |
"public" |
Any public address |
{ allow: [...] } |
Only those host names. "*.github.com" covers every name under github.com |
ci: sandbox({ image: "<IMAGE>", network: { allow: ["registry.npmjs.org", "*.github.com"] } }),
Luna sends a sandbox’s outbound HTTP and HTTPS through a proxy of its own. Every process Luna starts in the sandbox gets HTTP_PROXY, HTTPS_PROXY and ALL_PROXY set to it, so tools such as curl, npm, pip and git find their way out. A program that ignores those variables cannot reach outside.
Connect a luna’s sandboxes to each other
Sandboxes of one luna declared with privateNetwork: true reach each other at <NAME>.sandbox.internal, whatever their network says.
web: sandbox({ image: "<IMAGE>", network: "public", privateNetwork: true }),
db: sandbox({ image: "<IMAGE>", privateNetwork: true }),
Here web reaches db.sandbox.internal. An instance is named with its declaration, so instance x of web is web-x. Sandboxes of other lunas are never reachable.
Give an isolate a network
An isolate has no fetch unless its network option gives it one, with the same three choices as a sandbox. Run untrusted code covers it.
Next steps
- Connect to an outside WebSocket keeps a socket open from a luna.
- Run a sandbox covers the rest of a sandbox’s options.
- Track usage shows the data your lunas send out.