Help us shape the future of Go! Take the 2026 Go Developer Survey

api

package
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 16, 2026 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Overview

Package api is tailkeeper's Tailscale API client for reads (spec §4, §20).

Responses are decoded into tailkeeper's own allowlisted structs: any field a struct does not declare is dropped, so a credential the API returns in a field nobody listed can never reach an output. Tailscale's Go client library is used only for workload identity federation.

Index

Constants

View Source
const DefaultBaseURL = "https://api.tailscale.com"

DefaultBaseURL is the only API host tailkeeper talks to by default.

Variables

This section is empty.

Functions

func Path

func Path(format string, segments ...string) string

Path builds /api/v2/<format> with each %s replaced by a path-escaped segment.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client performs authenticated reads against one tailnet.

func New

func New(ctx context.Context, tailnet string, cred creds.Read, opt Options) (*Client, error)

New builds a Client for tailnet using cred. It makes no network call. ctx must outlive the Client: token requests made on its behalf stop when ctx ends, which is how a signal reaches a hung token exchange.

func (*Client) Get

func (c *Client) Get(ctx context.Context, path string, q url.Values, out any) error

Get performs a GET of path (already escaped, see TailnetPath and Path) and decodes the JSON body into out. Reads are retried on 429, 500, 502, 503, 504 and transport errors (client timeouts included) with bounded backoff; Retry-After is honoured when present (up to 30s). The caller's cancellation is never retried.

func (*Client) GetRaw

func (c *Client) GetRaw(ctx context.Context, path, accept string) (Raw, error)

GetRaw performs a GET of path with the given Accept type and returns the body unparsed, for formats that are not JSON (the policy file's HuJSON). It is retried and bounded like Get.

func (*Client) GrantedScopes

func (c *Client) GrantedScopes() (scopes []string, known bool)

GrantedScopes returns the scopes the token endpoint reported, once a token has been fetched. known is false for API keys and federated identities.

func (*Client) Tailnet

func (c *Client) Tailnet() string

Tailnet returns the tailnet ID this client is bound to.

func (*Client) TailnetPath

func (c *Client) TailnetPath(suffix string) string

TailnetPath returns /api/v2/tailnet/<tailnet><suffix>.

func (*Client) ValidateTests

func (c *Client) ValidateTests(ctx context.Context, tests []PolicyTest) (Validation, error)

ValidateTests runs tests against the live policy without changing it: POST .../acl/validate with a JSON array of tests (tests mode), the one request other than GET this client sends. The endpoint answers HTTP 200 for pass, fail and warnings alike, so a pass is read only from a recognised shape: an empty body, or an object whose entries carry no errors. Anything else is an error, never a pass. It is retried like a read: the endpoint changes nothing.

func (*Client) WithMaxBody

func (c *Client) WithMaxBody(n int64) *Client

WithMaxBody returns a copy of c that refuses response bodies over n bytes, or over c's own limit if that is lower. The copy shares c's credential, token cache and observed scopes.

type Options

type Options struct {
	BaseURL   string                                     // default DefaultBaseURL
	Transport http.RoundTripper                          // default http.DefaultTransport
	Timeout   time.Duration                              // per request; default 60s
	Retries   int                                        // read retries; default 3; negative disables
	MaxBody   int64                                      // default 64 MiB
	Sleep     func(context.Context, time.Duration) error // default: timer honouring ctx
}

Options tune a Client. Zero values are production defaults.

type PolicyTest

type PolicyTest struct {
	Src    string   `json:"src"`
	Proto  string   `json:"proto,omitempty"`
	Accept []string `json:"accept,omitempty"`
	Deny   []string `json:"deny,omitempty"`
}

PolicyTest is one test of the live policy, in the shape Tailscale's validate endpoint takes in tests mode.

type Raw

type Raw struct {
	Body []byte
	ETag string
}

Raw is a response body as the API sent it, and its ETag (verbatim, quotes included; "" when the API sent none).

type TestResult

type TestResult struct {
	User     string   `json:"user"`
	Errors   []string `json:"errors"`
	Warnings []string `json:"warnings"`
}

TestResult is the validate endpoint's answer for one source.

type Validation

type Validation struct {
	Passed  bool         // no test failed
	Results []TestResult // sources with errors or warnings
}

Validation is how the live policy answered a set of tests.

Directories

Path Synopsis
Package apitest is a fake Tailscale API for tests.
Package apitest is a fake Tailscale API for tests.
Package openapi pins the operations of Tailscale's OpenAPI document (spec §20).
Package openapi pins the operations of Tailscale's OpenAPI document (spec §20).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL