assay-lua 0.3.2

Lightweight Lua runtime for Kubernetes. Verification, scripting, and web services.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
# Assay

Lightweight Lua runtime for Kubernetes. Verification, scripting, and web services.

[![CI](https://github.com/developerinlondon/assay/actions/workflows/ci.yml/badge.svg)](https://github.com/developerinlondon/assay/actions/workflows/ci.yml)
[![Crates.io](https://img.shields.io/crates/v/assay-lua.svg)](https://crates.io/crates/assay-lua)
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)

## What is Assay?

Assay is a single ~9 MB binary that replaces 50-250 MB Python/Node/kubectl containers in Kubernetes
Jobs. It provides a full-featured Lua runtime with built-in HTTP client/server, database access,
WebSocket, JWT signing, templates, and 19 embedded Kubernetes-native libraries.

One binary, auto-detected behavior:

```bash
assay checks.yaml    # YAML → check orchestration (retry, backoff, structured output)
assay script.lua     # Lua → run it (all builtins, script decides what to do)
```

Scripts that call `http.serve()` become web services. Scripts that call `http.get()` and exit are
jobs. Same binary, same builtins.

## Why Assay?

Container image size comparison (compressed pull):

```
+------------------------------------------------------------------+
| Docker image size comparison (compressed pull)                   |
|                                                                  |
| Assay Full       ## 6 MB                                         |
| Python alpine    ########## 17 MB                                |
| bitnami/kubectl  #################### 35 MB                      |
| Python slim      ########################## 43 MB                |
| Node.js alpine   ################################## 57 MB        |
| alpine/k8s       ######################################## 60 MB  |
| Deno             ############################################ 75 |
| Node.js slim     ############################################### |
| Bun              ############################################### |
| postman/newman   ############################################### |
+------------------------------------------------------------------+
```

| Runtime         | Compressed |   On-disk | vs Assay | Sandbox | K8s-native |
| --------------- | ---------: | --------: | :------: | :-----: | :--------: |
| **Assay**       |   **6 MB** | **13 MB** |  **1x**  | **Yes** |  **Yes**   |
| Python alpine   |      17 MB |     50 MB |    3x    |   No    |     No     |
| bitnami/kubectl |      35 MB |     90 MB |    6x    |   No    |  Partial   |
| Python slim     |      43 MB |    130 MB |    9x    |   No    |     No     |
| Node.js alpine  |      57 MB |    180 MB |   12x    |   No    |     No     |
| alpine/k8s      |      60 MB |    150 MB |   10x    |   No    |  Partial   |

## Installation

### Pre-built Binary (fastest)

Download from [GitHub Releases](https://github.com/developerinlondon/assay/releases/latest):

```bash
# Linux (x86_64, static — runs on any distro, no dependencies)
curl -L -o assay https://github.com/developerinlondon/assay/releases/latest/download/assay-linux-x86_64
chmod +x assay
sudo mv assay /usr/local/bin/

# macOS (Apple Silicon)
curl -L -o assay https://github.com/developerinlondon/assay/releases/latest/download/assay-darwin-aarch64
chmod +x assay
sudo mv assay /usr/local/bin/
```

### Docker

```bash
docker pull ghcr.io/developerinlondon/assay:latest
docker run --rm ghcr.io/developerinlondon/assay:latest --version
```

### Cargo

```bash
cargo install assay-lua
```

### From Source

```bash
git clone https://github.com/developerinlondon/assay.git
cd assay
cargo build --release
./target/release/assay --version
```

## Usage

### Two Modes

Assay auto-detects behavior by file extension:

#### 1. YAML Check Mode (Orchestration)

Run structured verification checks with retry, backoff, and JSON output:

```yaml
# checks.yaml
timeout: 120s
retries: 3
backoff: 5s
parallel: false

checks:
  - name: grafana-healthy
    type: http
    url: http://grafana.monitoring:80/api/health
    expect:
      status: 200
      json: ".database == \"ok\""

  - name: prometheus-targets
    type: prometheus
    url: http://prometheus.monitoring:9090
    query: "count(up)"
    expect:
      min: 1

  - name: custom-check
    type: script
    file: verify.lua
```

```bash
assay checks.yaml
```

#### 2. Lua Script Mode (Direct Execution)

Run Lua scripts with all builtins available:

```lua
#!/usr/bin/assay
-- HTTP health check with JWT auth
local token = crypto.jwt_sign({
  iss = "assay",
  sub = "health-check",
  exp = time() + 300
}, env.get("JWT_SECRET"), "HS256")

local resp = http.get("https://api.example.com/health", {
  headers = { Authorization = "Bearer " .. token }
})

assert.eq(resp.status, 200, "API health check failed")
log.info("API healthy: " .. resp.body)
```

```bash
chmod +x script.lua
./script.lua
```

### Shebang Support

Assay supports shebang for executable Lua scripts:

```lua
#!/usr/bin/assay
log.info("Hello from Assay!")
```

```bash
chmod +x hello.lua
./hello.lua
```

## Built-in API Reference

All builtins are available to `.lua` scripts. YAML check mode uses a sandboxed subset (http, json,
yaml, assert, log, env, sleep, time, base64).

### HTTP Client

| Function                                 | Description                                |
| ---------------------------------------- | ------------------------------------------ |
| `http.get(url, opts?)`                   | GET request, returns `{status, body, ...}` |
| `http.post(url, body, opts?)`            | POST request (auto-JSON if table)          |
| `http.put(url, body, opts?)`             | PUT request                                |
| `http.patch(url, body, opts?)`           | PATCH request                              |
| `http.delete(url, opts?)`                | DELETE request                             |
| `http.serve(port, routes)`               | Start HTTP server (blocking)               |
| `opts.headers = {["X-Key"] = "value"}`   | Custom headers                             |
| `routes = {GET = {["/path"] = handler}}` | Route table for server                     |

### Serialization

| Function             | Description                     |
| -------------------- | ------------------------------- |
| `json.parse(str)`    | Parse JSON string to Lua table  |
| `json.encode(table)` | Encode Lua table to JSON string |
| `yaml.parse(str)`    | Parse YAML string to Lua table  |
| `yaml.encode(table)` | Encode Lua table to YAML string |
| `toml.parse(str)`    | Parse TOML string to Lua table  |
| `toml.encode(table)` | Encode Lua table to TOML string |
| `base64.encode(str)` | Base64 encode                   |
| `base64.decode(str)` | Base64 decode                   |

### Filesystem

| Function            | Description          |
| ------------------- | -------------------- |
| `fs.read(path)`     | Read file to string  |
| `fs.write(path, s)` | Write string to file |

### Cryptography

| Function                            | Description                                |
| ----------------------------------- | ------------------------------------------ |
| `crypto.jwt_sign(claims, key, alg, opts?)` | Sign JWT (RS256/384/512), opts: `{kid="..."}` |
| `crypto.hash(str, alg)`             | Hash string (sha256, sha384, sha512, etc.) |
| `crypto.random(len)`                | Secure random string (hex)                 |

### Regular Expressions

| Function                         | Description             |
| -------------------------------- | ----------------------- |
| `regex.match(pattern, str)`      | Test if pattern matches |
| `regex.find(pattern, str)`       | Find first match        |
| `regex.find_all(pattern, str)`   | Find all matches        |
| `regex.replace(pattern, str, r)` | Replace matches         |

### Database (SQL)

| Function                         | Description                                 |
| -------------------------------- | ------------------------------------------- |
| `db.connect(url)`                | Connect to database (Postgres/MySQL/SQLite) |
| `db.query(conn, sql, params?)`   | Execute query, return rows                  |
| `db.execute(conn, sql, params?)` | Execute statement, return affected count    |
| `db.close(conn)`                 | Close connection                            |

Supported URLs:

- `postgres://user:pass@host:5432/dbname`
- `mysql://user:pass@host:3306/dbname`
- `sqlite:///path/to/file.db`

### WebSocket

| Function             | Description                 |
| -------------------- | --------------------------- |
| `ws.connect(url)`    | Connect to WebSocket server |
| `ws.send(conn, msg)` | Send message                |
| `ws.recv(conn)`      | Receive message (blocking)  |
| `ws.close(conn)`     | Close connection            |

### Templates (Jinja2-compatible)

| Function                          | Description            |
| --------------------------------- | ---------------------- |
| `template.render(path, vars)`     | Render template file   |
| `template.render_string(tmpl, v)` | Render template string |

### Async

| Function                       | Description                          |
| ------------------------------ | ------------------------------------ |
| `async.spawn(fn)`              | Spawn async task, returns handle     |
| `async.spawn_interval(fn, ms)` | Spawn recurring task, returns handle |
| `handle:await()`               | Wait for task completion             |
| `handle:cancel()`              | Cancel recurring task                |

### Assertions

| Function                          | Description         |
| --------------------------------- | ------------------- |
| `assert.eq(a, b, msg?)`           | Assert equal        |
| `assert.gt(a, b, msg?)`           | Assert greater than |
| `assert.lt(a, b, msg?)`           | Assert less than    |
| `assert.contains(str, sub, msg?)` | Assert substring    |
| `assert.not_nil(val, msg?)`       | Assert not nil      |
| `assert.matches(str, pat, msg?)`  | Assert regex match  |

### Logging

| Function         | Description |
| ---------------- | ----------- |
| `log.info(msg)`  | Info log    |
| `log.warn(msg)`  | Warning log |
| `log.error(msg)` | Error log   |

### Utilities

| Function       | Description              |
| -------------- | ------------------------ |
| `env.get(key)` | Get environment variable |
| `sleep(secs)`  | Sleep for seconds        |
| `time()`       | Unix timestamp (seconds) |

## Stdlib Modules

Assay embeds 19 Lua modules for Kubernetes-native operations. Use `require("assay.<module>")`:

| Module               | Description                                                 |
| -------------------- | ----------------------------------------------------------- |
| `assay.prometheus`   | Query metrics, alerts, targets, rules, label values, series |
| `assay.alertmanager` | Manage alerts, silences, receivers, config                  |
| `assay.loki`         | Push logs, query, labels, series                            |
| `assay.grafana`      | Health checks, dashboards, datasources                      |
| `assay.k8s`          | 30+ resource types, CRDs, readiness checks                  |
| `assay.argocd`       | Apps, sync, health, projects, repositories                  |
| `assay.kargo`        | Stages, freight, promotions, verification                   |
| `assay.flux`         | GitRepositories, Kustomizations, HelmReleases               |
| `assay.traefik`      | Routers, services, middlewares, entrypoints                 |
| `assay.vault`        | KV secrets, policies, auth, transit, PKI                    |
| `assay.openbao`      | Alias for vault (OpenBao API-compatible)                    |
| `assay.certmanager`  | Certificates, issuers, ACME challenges                      |
| `assay.eso`          | ExternalSecrets, SecretStores, ClusterSecretStores          |
| `assay.dex`          | OIDC discovery, JWKS, health                                |
| `assay.crossplane`   | Providers, XRDs, compositions, managed resources            |
| `assay.velero`       | Backups, restores, schedules, storage locations             |
| `assay.temporal`     | Workflows, task queues, schedules                           |
| `assay.harbor`       | Projects, repositories, artifacts, vulnerability scanning   |
| `assay.healthcheck`  | HTTP checks, JSON path, body matching, latency, multi-check |

Example:

```lua
local prom = require("assay.prometheus")
local result = prom.query("http://prometheus:9090", "up")
log.info("Targets up: " .. tostring(result))
```

## Examples

### HTTP Health Check

```lua
#!/usr/bin/assay
local resp = http.get("http://grafana.monitoring:80/api/health")
assert.eq(resp.status, 200, "Grafana not responding")

local data = json.parse(resp.body)
assert.eq(data.database, "ok", "Grafana database unhealthy")
log.info("Grafana healthy: version=" .. data.version)
```

### JWT Authentication to API

```lua
#!/usr/bin/assay
-- Read RSA private key from file
local key = fs.read("/secrets/jwt-key.pem")

-- Sign JWT with RS256
local token = crypto.jwt_sign({
  iss = "assay-client",
  sub = "admin@example.com",
  exp = time() + 3600
}, key, "RS256")

-- Call API with JWT
local resp = http.get("https://api.example.com/users", {
  headers = { Authorization = "Bearer " .. token }
})

assert.eq(resp.status, 200, "API call failed")
local users = json.parse(resp.body)
log.info("Found " .. #users .. " users")
```

### Database Query

```lua
#!/usr/bin/assay
local pg = db.connect("postgres://user:pass@postgres:5432/mydb")

-- Parameterized query (safe from SQL injection)
local rows = db.query(pg, "SELECT id, name FROM users WHERE active = $1", {true})

for _, row in ipairs(rows) do
  log.info("User: " .. row.name .. " (ID: " .. row.id .. ")")
end

db.close(pg)
```

### Web Server

```lua
#!/usr/bin/assay
-- Simple API server
http.serve(8080, {
  GET = {
    ["/health"] = function(req)
      return { status = 200, body = "ok" }
    end,
    ["/api/time"] = function(req)
      return {
        status = 200,
        json = { timestamp = time(), zone = "UTC" }
      }
    end
  },
  POST = {
    ["/api/echo"] = function(req)
      local data = json.parse(req.body)
      return { status = 200, json = data }
    end
  }
})
```

### Prometheus Verification

```lua
#!/usr/bin/assay
local prom = require("assay.prometheus")

-- Check Prometheus is up
local targets = prom.targets("http://prometheus.monitoring:9090")
local up_count = 0
for _, target in ipairs(targets.activeTargets) do
  if target.health == "up" then
    up_count = up_count + 1
  end
end

assert.gt(up_count, 0, "No Prometheus targets are up")
log.info("Prometheus targets up: " .. up_count)

-- Query metrics
local result = prom.query("http://prometheus.monitoring:9090", "up")
log.info("Query result: " .. tostring(result))
```

## YAML Check Mode

YAML check mode provides structured orchestration with retry, backoff, and parallel execution:

```yaml
# Global config
timeout: 120s # Max time for all checks
retries: 3 # Retry failed checks
backoff: 5s # Wait between retries
parallel: false # Run checks sequentially (true = parallel)

checks:
  # HTTP check with JSON path assertion
  - name: api-health
    type: http
    url: https://api.example.com/health
    expect:
      status: 200
      json: ".status == \"healthy\""

  # Prometheus query check
  - name: high-cpu
    type: prometheus
    url: http://prometheus:9090
    query: "avg(rate(cpu_usage[5m]))"
    expect:
      max: 0.8 # Alert if CPU > 80%

  # Custom Lua script check
  - name: database-check
    type: script
    file: verify-db.lua
    env:
      DB_URL: postgres://user:pass@postgres:5432/mydb
```

Check types:

- `type: http` — HTTP request with status/body/JSON assertions
- `type: prometheus` — PromQL query with min/max assertions
- `type: script` — Custom Lua script (sandboxed builtins)

Output is structured JSON:

```json
{
  "passed": 2,
  "failed": 1,
  "total": 3,
  "results": [
    {
      "name": "api-health",
      "status": "passed",
      "duration_ms": 45
    },
    {
      "name": "high-cpu",
      "status": "failed",
      "error": "expected max 0.8, got 0.92",
      "duration_ms": 120
    }
  ]
}
```

Exit code: 0 if all checks pass, 1 if any fail.

## Development

### Build

```bash
cargo build --release
```

### Test

```bash
cargo test
```

### Lint

```bash
cargo clippy -- -D warnings
```

### Format

```bash
dprint fmt
```

### Run Examples

Self-contained scripts (no external services needed):

```bash
cargo run -- tests/e2e/check_json.lua
cargo run -- tests/e2e/check_yaml.lua
cargo run -- tests/e2e/check_toml.lua
cargo run -- tests/e2e/check_base64.lua
cargo run -- tests/e2e/check_crypto.lua
cargo run -- tests/e2e/check_regex.lua
cargo run -- tests/e2e/check_fs.lua
cargo run -- tests/e2e/check_template.lua
```

Kubernetes examples (require services running in-cluster):

```bash
cargo run -- examples/checks.yaml
cargo run -- examples/grafana-health.lua
cargo run -- examples/prometheus-scrape.lua
cargo run -- examples/loki-test.lua
```

## Architecture

```
+------------------------------------------------------------------+
| Assay v0.3.0 (~9 MB static MUSL binary, Alpine container)       |
|                                                                  |
| CLI (auto-detected by file extension):                           |
|   assay config.yaml           (.yaml -> check orchestration)     |
|   assay script.lua            (.lua  -> run script)              |
|                                                                  |
| Shebang support:                                                 |
|   #!/usr/bin/assay            (works like #!/usr/bin/python3)    |
|                                                                  |
| Rust Core:                                                       |
|   Config parser (YAML) -> Runner (retry/backoff/timeout)         |
|   -> Structured JSON output -> Exit code (0/1)                   |
|                                                                  |
| Lua Runtime (mlua + Lua 5.5):                                    |
|   - 64 MB memory limit per VM                                    |
|   - Fresh VM per check (YAML mode)                               |
|   - Single VM per script (Lua mode)                              |
|   - Async support via tokio LocalSet                             |
|                                                                  |
| Rust Builtins (all available to .lua scripts):                   |
|   http.{get,post,put,patch,delete,serve}                         |
|   ws.{connect,send,recv,close}                                   |
|   json.{parse,encode}  yaml.{parse,encode}  toml.{parse,encode}  |
|   fs.{read,write}  base64.{encode,decode}                        |
|   crypto.{jwt_sign,hash,random}  regex.{match,find,replace}      |
|   db.{connect,query,execute,close}  (postgres, mysql, sqlite)    |
|   template.{render,render_string}                                |
|   assert.{eq,gt,lt,contains,not_nil,matches}                     |
|   log.{info,warn,error}  env.get  sleep  time                    |
|   async.{spawn,spawn_interval}                                   |
|                                                                  |
| Lua Stdlib (embedded .lua files via include_dir!):               |
|   Monitoring: prometheus, alertmanager, loki, grafana             |
|   K8s/GitOps: k8s, argocd, kargo, flux, traefik                 |
|   Security:   vault, openbao, certmanager, eso, dex              |
|   Infra:      crossplane, velero, temporal, harbor               |
|   Utilities:  healthcheck                                        |
+------------------------------------------------------------------+
```

## Use Cases

- **ArgoCD/Kargo Hooks**: PostSync verification, PreSync validation, health checks
- **Kubernetes Jobs**: Database migrations, API configuration, secret rotation
- **Lightweight Web Services**: Webhook receivers, API proxies, mock servers, dashboards
- **Platform Automation**: Operational tasks, cross-service connectivity checks, report generation
- **Verification**: E2E tests, smoke tests, integration tests

## Why Lua 5.5?

Assay uses Lua 5.5 (released Dec 2025) over LuaJIT for:

- **Global declarations**: Catches accidental globals (reduces bugs)
- **Named vararg tables**: Cleaner function signatures
- **Incremental major GC**: Smoother latency for long-running servers
- **Native int64**: Better for timestamps, IDs
- **MUSL static linking**: No assembler issues

Our scripts are I/O bound (HTTP calls, database queries). LuaJIT's 5-10x CPU speedup provides
negligible benefit (<1% of total job time).

## License

MIT

## Contributing

Contributions welcome! Please open an issue or PR on GitHub.

## Links

- **Repository**: https://github.com/developerinlondon/assay
- **Crates.io**: https://crates.io/crates/assay-lua
- **Docker**: https://github.com/developerinlondon/assay/pkgs/container/assay
- **Issues**: https://github.com/developerinlondon/assay/issues