api

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2026 License: Apache-2.0 Imports: 11 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package api is a small client for the Tailscale HTTP API (v2). It authenticates with OAuth client credentials and currently supports creating and revoking auth keys and reading the tailnet policy.

The package never logs, and every secret value is a Secret so that it cannot leak through formatting.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrForbidden matches HTTP 403 responses.
	ErrForbidden = errors.New("forbidden")
	// ErrNotFound matches HTTP 404 responses.
	ErrNotFound = errors.New("not found")
	// ErrUnauthorized matches HTTP 401 responses.
	ErrUnauthorized = errors.New("unauthorized")
)

Sentinel errors matched by errors.Is against an APIError.

View Source
var ErrNoCredentials = errors.New("tailscale oauth credentials missing")

ErrNoCredentials is returned when OAuth credentials are missing.

Functions

This section is empty.

Types

type ACLRule

type ACLRule struct {
	Action string   `json:"action"`
	Src    []string `json:"src"`
	Dst    []string `json:"dst"`
}

ACLRule is one entry of the policy's acls section (only the fields provr inspects).

type APIError

type APIError struct {
	Status  int
	Message string
	Op      string
}

APIError is a non-2xx response from the Tailscale API.

func (*APIError) Error

func (e *APIError) Error() string

Error implements error.

func (*APIError) Unwrap

func (e *APIError) Unwrap() error

Unwrap maps the status code to a sentinel error, if any.

type Client

type Client struct {
	// BaseURL is the API root; override in tests.
	BaseURL string
	// HTTP is the HTTP client used for all requests.
	HTTP *http.Client
	// ClientID is the OAuth client ID.
	ClientID string
	// ClientSecret is the OAuth client secret.
	ClientSecret Secret
	// contains filtered or unexported fields
}

Client talks to the Tailscale API. Create it with New or NewFromEnv.

func New

func New(clientID string, clientSecret Secret) *Client

New returns a Client with default base URL and a 15s HTTP timeout.

func NewFromEnv

func NewFromEnv(getenv func(string) string) (*Client, error)

NewFromEnv builds a Client from TS_OAUTH_CLIENT_ID and TS_OAUTH_CLIENT_SECRET. The returned error names missing variables only.

func (*Client) CreateKey

func (c *Client) CreateKey(ctx context.Context, spec KeySpec) (Key, error)

CreateKey creates an auth key in the token's tailnet.

func (*Client) DeleteDevice

func (c *Client) DeleteDevice(ctx context.Context, id string) error

DeleteDevice removes a device from the tailnet. A device that no longer exists is not an error.

func (*Client) GetPolicy

func (c *Client) GetPolicy(ctx context.Context) (Policy, error)

GetPolicy reads the tailnet policy. A 403 (missing policy_file:read scope) satisfies errors.Is(err, ErrForbidden).

func (*Client) GoString

func (c *Client) GoString() string

GoString implements fmt.GoStringer with the same redaction as String.

func (*Client) ListDevices

func (c *Client) ListDevices(ctx context.Context) ([]Device, error)

ListDevices returns the devices in the token's tailnet.

func (*Client) RevokeKey

func (c *Client) RevokeKey(ctx context.Context, id string) error

RevokeKey deletes an auth key. A key that no longer exists is not an error.

func (*Client) String

func (c *Client) String() string

String describes the client without any credential or token, because fmt cannot apply Secret's redaction to unexported fields of a struct dump.

func (*Client) Token

func (c *Client) Token(ctx context.Context) (string, error)

Token returns a bearer token, fetching a new one when the cached token is missing or about to expire. It is safe for concurrent use.

type Device

type Device struct {
	ID string `json:"id"`
	// Name is the MagicDNS FQDN, e.g. "host.tail1234.ts.net".
	Name string `json:"name"`
	// Hostname is the OS hostname.
	Hostname           string    `json:"hostname"`
	Tags               []string  `json:"tags"`
	LastSeen           time.Time `json:"lastSeen"`
	ConnectedToControl bool      `json:"connectedToControl"`
}

Device is a machine in the tailnet.

func (Device) ShortName

func (d Device) ShortName() string

ShortName returns the first DNS label of Name, falling back to Hostname when Name is empty.

type Grant

type Grant struct {
	Src []string `json:"src"`
	Dst []string `json:"dst"`
}

Grant is one entry of the policy's grants section. The ip and app capabilities are ignored: provr only needs to know who a grant reaches.

type Key

type Key struct {
	ID      string
	Secret  Secret
	Expires time.Time
}

Key is a created auth key. Secret holds the key value.

type KeySpec

type KeySpec struct {
	// Tags are the ACL tags applied to devices using the key (required).
	Tags          []string
	Reusable      bool
	Ephemeral     bool
	Preauthorized bool
	// Expiry is the key lifetime; zero means one hour.
	Expiry      time.Duration
	Description string
}

KeySpec describes an auth key to create.

type Policy

type Policy struct {
	TagOwners map[string][]string `json:"tagOwners"`
	SSH       []SSHRule           `json:"ssh"`
	ACLs      []ACLRule           `json:"acls"`
	Grants    []Grant             `json:"grants"`
}

Policy is the subset of the tailnet policy file that provr inspects.

type SSHRule

type SSHRule struct {
	Action string   `json:"action"`
	Src    []string `json:"src"`
	Dst    []string `json:"dst"`
	Users  []string `json:"users"`
}

SSHRule is one entry of the policy's ssh section.

type Secret

type Secret string

Secret is a string that must never be printed or logged. Callers must convert it explicitly with string(s) to use the value.

func (Secret) GoString

func (Secret) GoString() string

GoString returns a redacted placeholder for %#v.

func (Secret) MarshalJSON

func (Secret) MarshalJSON() ([]byte, error)

MarshalJSON encodes a redacted placeholder.

func (Secret) String

func (Secret) String() string

String returns a redacted placeholder.

Jump to

Keyboard shortcuts

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