|
| 1 | +# Tailscale App Capabilities Plugin |
| 2 | + |
| 3 | +The `tscap` plugin is a [Goa](https://github.com/goadesign/goa/tree/v3) plugin |
| 4 | +that provides declarative authorization using Tailscale app capabilities. |
| 5 | + |
| 6 | +Inspired by the implementation of [arnz](../arnz/README.md). |
| 7 | + |
| 8 | +## Requirements |
| 9 | + |
| 10 | +- Tailscale 1.92+ (app capabilities feature) |
| 11 | +- Service must be served via `tailscale serve --accept-app-caps` |
| 12 | + |
| 13 | +## Enabling the Plugin |
| 14 | + |
| 15 | +To enable the plugin and make use of the tscap DSL simply import both the `tscap` |
| 16 | +and the `dsl` packages as follows: |
| 17 | + |
| 18 | +```go |
| 19 | +import ( |
| 20 | + . "goa.design/goa/v3/dsl" |
| 21 | + tscap "goa.design/plugins/v3/tscap/dsl" |
| 22 | +) |
| 23 | +``` |
| 24 | + |
| 25 | +### Tailscale Setup |
| 26 | + |
| 27 | +```bash |
| 28 | +tailscale serve --accept-app-caps example.com/cap/myapp https+insecure://localhost:8080 |
| 29 | +``` |
| 30 | + |
| 31 | +### ACL Grants |
| 32 | + |
| 33 | +Configure grants in your tailnet policy: |
| 34 | + |
| 35 | +```json |
| 36 | +{ |
| 37 | + "grants": [ |
| 38 | + { |
| 39 | + "src": ["group:developers"], |
| 40 | + "dst": ["tag:myapp"], |
| 41 | + "app": { |
| 42 | + "example.com/cap/myapp": [{"action": ["*"], "resources": ["*"]}] |
| 43 | + } |
| 44 | + }, |
| 45 | + { |
| 46 | + "src": ["group:finance"], |
| 47 | + "dst": ["tag:myapp"], |
| 48 | + "app": { |
| 49 | + "example.com/cap/myapp": [{"action": ["read"], "resources": ["items/*"]}] |
| 50 | + } |
| 51 | + } |
| 52 | + ] |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | +## Effects on Code Generation |
| 57 | + |
| 58 | +Enabling the plugin changes the behavior of the `gen` command of the `goa` tool. |
| 59 | + |
| 60 | +The `gen` command output is modified as follows: |
| 61 | + |
| 62 | +1. Generates middleware that extracts the `Tailscale-App-Capabilities` header |
| 63 | +2. Parses the JSON capabilities from the header |
| 64 | +3. Checks if the caller's grants satisfy the method's requirements |
| 65 | +4. Returns 401 if header is missing, 403 if permissions are insufficient |
| 66 | + |
| 67 | +## Design |
| 68 | + |
| 69 | +This plugin adds the following functions to the Goa DSL: |
| 70 | + |
| 71 | +* `Require` declares that the method requires a Tailscale app capability with the |
| 72 | + specified action and resource. |
| 73 | +* `AllowAnonymous` marks the method as not requiring any capability check. Requests |
| 74 | + without the capabilities header will be allowed through. |
| 75 | + |
| 76 | +The usage and effect of the DSL functions are described in the [Godocs](https://godoc.org/goa.design/plugins/v3/tscap/dsl) |
| 77 | + |
| 78 | +Here is an example defining capability requirements at a method level. |
| 79 | + |
| 80 | +```go |
| 81 | +var _ = Service("myservice", func() { |
| 82 | + Method("list", func() { |
| 83 | + // Requires the caller to have "read" action on "*" resource |
| 84 | + tscap.Require("example.com/cap/myapp", "read", "*") |
| 85 | + HTTP(func() { GET("/items") }) |
| 86 | + }) |
| 87 | + |
| 88 | + Method("create", func() { |
| 89 | + // Requires the caller to have "write" action on "items/*" resource |
| 90 | + tscap.Require("example.com/cap/myapp", "write", "items/*") |
| 91 | + HTTP(func() { POST("/items") }) |
| 92 | + }) |
| 93 | + |
| 94 | + Method("health", func() { |
| 95 | + // No capability check required |
| 96 | + tscap.AllowAnonymous() |
| 97 | + HTTP(func() { GET("/health") }) |
| 98 | + }) |
| 99 | +}) |
| 100 | +``` |
| 101 | + |
| 102 | +## Matching Semantics |
| 103 | + |
| 104 | +Grants in Tailscale ACLs can use wildcards (`*`). The DSL specifies exact requirements: |
| 105 | + |
| 106 | +| Grant Action | Required Action | Match? | |
| 107 | +|--------------|-----------------|--------| |
| 108 | +| `["*"]` | `"read"` | Yes | |
| 109 | +| `["read"]` | `"read"` | Yes | |
| 110 | +| `["write"]` | `"read"` | No | |
| 111 | + |
| 112 | +| Grant Resource | Required Resource | Match? | |
| 113 | +|----------------|-------------------|--------| |
| 114 | +| `["*"]` | `"items/123"` | Yes | |
| 115 | +| `["items/*"]` | `"items/*"` | Yes (exact) | |
| 116 | +| `["items/123"]` | `"items/456"` | No | |
| 117 | + |
| 118 | +## References |
| 119 | + |
| 120 | +- [Application capabilities](https://tailscale.com/docs/features/access-control/grants/grants-app-capabilities) |
0 commit comments