Documentation
¶
Overview ¶
Package tailapi provides shared access to Tailscale's policy-file API.
Index ¶
- Constants
- func IsForbidden(err error) bool
- func NewClient(config Config) (*tailscale.Client, error)
- func RedactSecret(err error, clientSecret string) error
- func WrapOAuthError(err error, requiredScope, clientID, clientSecret string) error
- type Config
- type FetchedPolicy
- type OAuthScopeError
- type Plan
- type ServiceLookup
- type UploadedPolicy
- type ValidationDetail
- type ValidationError
- type ValidationIndeterminateError
- type ValidationOutcome
- type ValidationResult
- type ValidationUnavailableError
Constants ¶
const ( ScopePolicyFileRead = "policy_file:read" ScopePolicyFile = "policy_file" )
Tailscale's OAuth scopes for the policy-file API: the read scope covers GET and validation, the write scope additionally replaces the policy.
The endpoint path is /acl but the scopes are not named after it — these are the literal strings from Tailscale's trust-credentials reference, and an invented name like "acl:read" is rejected at the token endpoint rather than downscoped, so the failure lands on every request instead of just the privileged ones.
const DefaultBaseURL = "https://api.tailscale.com"
Variables ¶
This section is empty.
Functions ¶
func IsForbidden ¶
IsForbidden reports whether the control plane answered with HTTP 403.
func RedactSecret ¶
RedactSecret removes the client secret from an error without attributing a scope. It is the right wrapper for an endpoint no documented scope governs, where naming one would send the operator after the wrong credential.
func WrapOAuthError ¶
WrapOAuthError removes the client secret from every error string that can reach a caller and, when an OAuth client issued the request, names the scope an authorization failure most likely lacked. The token endpoint reports invalid_scope as a normal OAuth error rather than a Tailscale APIError, so its text is checked as well as the typed status.
clientID gates the scope attribution, and that gate is the point: a 403 is not scope-specific. A non-admin API key, a tailnet the credential cannot reach, and a GitOps-managed policy file all return one, so blaming a missing OAuth scope is only ever correct when an OAuth client is what authenticated. Without the gate every API-key 403 in the tool reports a scope the operator does not have and cannot grant.
Types ¶
type Config ¶
type Config struct {
APIKey string
OAuthClientID string
OAuthClientSecret string
OAuthScopes []string
Tailnet string
BaseURL string
HTTP *http.Client
}
Config configures the official Tailscale v2 API client.
type FetchedPolicy ¶
FetchedPolicy is the deployed policy body and the version that supplied it.
func FetchPolicy ¶
FetchPolicy reads the deployed policy without normalizing its HuJSON. The caller can therefore persist comments, whitespace, and key order verbatim.
type OAuthScopeError ¶
OAuthScopeError reports an OAuth client that was accepted but is not authorized for the operation's required scope. It unwraps the original error so callers can still classify the HTTP response, and reports that error's text too: the secret is already redacted by the time one is constructed, and a 403 body carries the only remediation the control plane offered. Naming a scope while discarding "policy file is managed by a GitOps integration" sends the operator to fix a credential that was never the problem.
func (*OAuthScopeError) Error ¶
func (e *OAuthScopeError) Error() string
func (*OAuthScopeError) Unwrap ¶
func (e *OAuthScopeError) Unwrap() error
type Plan ¶
type Plan struct {
Diff string
ETag string
Changed bool
Validation ValidationResult
}
Plan describes the validated difference between the deployed policy and a local candidate. ETag identifies the deployed version used for the diff.
func PlanFile ¶
PlanFile fetches the deployed policy, validates path without applying it, and returns a formatted unified diff from deployed to candidate policy.
Once validation has run, every failure returns a non-nil *Plan carrying only Validation — Diff, ETag, and Changed stay zero — so a caller can still report the per-user detail and any warnings the control plane did supply rather than discarding diagnostics it already collected. A caller must therefore check the error before reading Changed, which otherwise reads as a confident "no changes" for a candidate that was never diffed. Validation verdicts wrap ValidationError or ValidationIndeterminateError; every failure before validation returns a nil *Plan.
type ServiceLookup ¶
ServiceLookup records whether the control plane found a service and the exact name spelling it accepted. Name is empty when Exists is false.
func LookupService ¶
func LookupService( ctx context.Context, client *tailscale.Client, name string, ) (ServiceLookup, error)
LookupService asks the control plane about one service rather than assuming how names returned by the collection endpoint correspond to svc: selectors. It sends the policy selector verbatim: the pinned client's service contract uses the full svc:<name> spelling for Get paths.
A readable 404 is a clean Exists=false verdict. Any other failure is wrapped as a ValidationUnavailableError, because an unreadable response cannot prove that the service is absent.
type UploadedPolicy ¶
UploadedPolicy is the exact HuJSON sent to the control plane and the new version returned by a successful upload. HuJSON is retained so a caller can safely verify a read-back when an API response omits its ETag.
func UploadFile ¶
func UploadFile( ctx context.Context, client *tailscale.Client, path, ifMatch string, ) (UploadedPolicy, error)
UploadFile validates path as HuJSON and sets it as the tailnet policy. The v2 client's raw-HuJSON Set method intentionally returns only an error, so a request-local transport wrapper captures the successful response ETag without a racy second fetch or any mutation of the caller's HTTP client.
type ValidationDetail ¶
type ValidationDetail struct {
User string `json:"user,omitempty"`
Errors []string `json:"errors,omitempty"`
Warnings []string `json:"warnings,omitempty"`
}
ValidationDetail preserves the structured diagnostics returned by /acl/validate. Warnings is owned here because the pinned v2 client's APIErrorData drops that documented field.
type ValidationError ¶
type ValidationError struct {
Status int
Message string
Data []ValidationDetail
}
ValidationError is the typed control-plane rejection. Data retains the server's per-user errors and warnings so errors.As callers do not have to parse a formatted message.
func (*ValidationError) Error ¶
func (err *ValidationError) Error() string
type ValidationIndeterminateError ¶
type ValidationIndeterminateError struct {
Message string
Unreadable []ValidationDetail
}
ValidationIndeterminateError reports a response this client cannot prove clean. Two shapes reach it, and neither has been observed in a capture:
- Every detail carried warnings and no errors, yet the server also sent a message. Reporting it valid would risk a false green on a rejection whose reason lived only in the message; reporting it invalid would fail every warning-bearing policy if the server routinely narrates warnings there.
- A detail carried neither errors nor warnings, so this client cannot read what the server was reporting. The pinned v2 client already dropped one documented per-detail field, so an unreadable entry is evidence the response shape moved — not evidence the policy is bad.
Callers report both as indeterminate rather than guessing which.
func (*ValidationIndeterminateError) Error ¶
func (err *ValidationIndeterminateError) Error() string
type ValidationOutcome ¶
type ValidationOutcome int
ValidationOutcome names what a validation error proves about the policy. It is the single place the three typed errors are triaged: callers that need an exit code, an envelope status, or a "did the server judge this at all" answer all switch on it, so adding a fourth outcome cannot silently keep the old answer at one of those sites.
const ( // ValidationNotAttempted means err is not about validation at all — a // missing file, a usage mistake, or a failure before the request was built. ValidationNotAttempted ValidationOutcome = iota // ValidationClean means the control plane judged the policy and found // nothing that blocks it. Warnings may still have been reported. ValidationClean // ValidationRejected means the control plane judged the policy and refused // it, with detail to back the refusal. ValidationRejected // ValidationIndeterminate means a response arrived that cannot be read as // either verdict, so reporting one would assert what was never observed. ValidationIndeterminate ValidationUnavailable )
func ClassifyValidation ¶
func ClassifyValidation(err error) ValidationOutcome
ClassifyValidation triages a validation error into the outcome it proves.
func (ValidationOutcome) Judged ¶
func (outcome ValidationOutcome) Judged() bool
Judged reports whether the control plane actually reached this outcome, as opposed to a request that never produced one.
type ValidationResult ¶
type ValidationResult struct {
Message string `json:"message,omitempty"`
Failures []ValidationDetail `json:"failures"`
Warnings []ValidationDetail `json:"warnings"`
}
ValidationResult is the additive CLI-facing validation summary. Its slices are always initialized so JSON output distinguishes a completed clean validation from a command that never reached the validation endpoint.
func ValidateFile ¶
func ValidateFile(ctx context.Context, client *tailscale.Client, path string) (ValidationResult, error)
ValidateFile asks Tailscale to validate the policy and run its policy tests.
func ValidatePolicy ¶
func ValidatePolicy( ctx context.Context, client *tailscale.Client, raw []byte, ) (ValidationResult, error)
ValidatePolicy calls /acl/validate directly because PolicyFile().Validate cannot represent valid-with-warnings and discards warning text and typed data on HTTP 200. This implementation follows the documented protocol rather than copying Tailscale source; cmd/gitops-pusher's testNewACLs is corroborating evidence for checking both message and data.
type ValidationUnavailableError ¶
type ValidationUnavailableError struct {
}
ValidationUnavailableError reports that the control plane never judged the policy: authentication, rate limiting, a server fault, a redirect away from the endpoint, or a transport failure answered instead of a verdict. It is deliberately a distinct type from ValidationError so no caller reports a policy as invalid on the strength of a request that never read it. Status is zero when no HTTP response arrived at all.
func (*ValidationUnavailableError) Error ¶
func (err *ValidationUnavailableError) Error() string
func (*ValidationUnavailableError) Unwrap ¶
func (err *ValidationUnavailableError) Unwrap() error