Decoy Getting started Docs GitLab Hub

Getting started

This guide builds Decoy from source and starts it with api.stripe.com as an Intercepted Host. You answer a curl call to that host with a Rule you register, then read the call back from the Call Journal. No application code changes.

Decoy is part of SlopKit. The source is at gitlab.techtailors.net/slopkit/decoy. There is no published release yet, so you build it.

Prerequisites

Build Decoy

Clone the repository and enter the dev shell:

git clone https://gitlab.techtailors.net/slopkit/decoy.git
cd decoy
devenv shell

The shell prints:

slopkit/decoy dev shell

Run the rest of this guide in that shell. Build the decoy binary and put it on your PATH:

cargo build
export PATH="$PWD/target/debug:$PATH"

The first build compiles every dependency. It ends with a line like this, with your own time:

    Finished `dev` profile [unoptimized + debuginfo] target(s) in 21.68s

Check the binary:

decoy --version
decoy 0.1.0

Start Decoy and intercept a host

Start Decoy in the background and name the host to intercept

decoy serve --intercept api.stripe.com &

Decoy prints where it listens. The upstream resolver and your home directory will differ.

decoy: control API   http://127.0.0.1:7411
decoy: DNS           127.0.0.1:5300 (udp+tcp), upstream 127.0.0.1:53
decoy: proxy         http://127.0.0.1:7412  (HTTPS_PROXY / HTTP_PROXY)
decoy: transparent   tls://127.0.0.1:7413
decoy: CA            /home/<you>/.local/state/decoy/ca.pem

Call the host with no Rule registered

decoy run starts a command with the proxy and the Decoy CA trust set up, so curl reaches Decoy instead of Stripe.

decoy run -- curl -s -w '\nHTTP %{http_code}\n' https://api.stripe.com/v1/payment_intents

Decoy answers 502 and says why. The host's Unmatched Policy is fail and nothing matches yet.

{
  "error": "decoy: no Rule matched this request and the Unmatched Policy for this host is `fail`",
  "hint": "register a Rule with POST /rules on the control API, or change the host's `unmatched` policy",
  "host": "api.stripe.com",
  "method": "GET",
  "path": "/v1/payment_intents"
}
HTTP 502

Answer the call with a Rule

Register a Rule on the control API

It matches POST calls under /v1/payment_intents and answers with a declined card.

curl -s -X POST http://127.0.0.1:7411/rules -d '{
  "label": "first-try", "host": "api.stripe.com", "method": "POST",
  "path": {"prefix": "/v1/payment_intents"},
  "respond": {"status": 402, "json": {"error": {"code": "card_declined"}}}
}' | jq -c '{id, label, host}'
{"id":"r1","label":"first-try","host":"api.stripe.com"}

Call the host again, this time with a POST

decoy run -- curl -s -w '\nHTTP %{http_code}\n' -X POST https://api.stripe.com/v1/payment_intents -d amount=1000

The Rule answers. This call never left your machine.

{"error":{"code":"card_declined"}}
HTTP 402

Read the Call Journal

It shows both calls and which one a Rule answered.

curl -s "http://127.0.0.1:7411/calls?host=api.stripe.com" | jq -c '.calls[] | {id, method, path, outcome, rule_id, status: .response.status}'
{"id":"c1","method":"GET","path":"/v1/payment_intents","outcome":"fail","rule_id":null,"status":502}
{"id":"c2","method":"POST","path":"/v1/payment_intents","outcome":"rule","rule_id":"r1","status":402}

Stop Decoy

kill %1

Decoy keeps Rules, hosts and the journal in memory, so they are gone now. Only the Decoy CA stays on disk.

Check the files Decoy wrote

ls ~/.local/state/decoy
ca-bundle.pem
ca-key.pem
ca.pem
client-ca.pem

ca.pem is the Decoy CA. ca-bundle.pem holds the public roots plus the Decoy CA. decoy run points SSL_CERT_FILE at it.

Next steps

Open the reference for the control API, Rule fields, Handlers, Setup Helpers and the limits of what Decoy can intercept.