Documentation
¶
Overview ¶
Package policycore resolves Tailscale policy selectors against concrete values. It is shared by tail-acl and tailnet-audit so both tools answer "does this selector name this thing?" identically; two tools disagreeing about one policy is worse than the duplication this replaces.
Scope is deliberately narrow: entity resolution only. Port, protocol, and via evaluation stay in tail-acl; posture expression handling lives in the posture subpackage.
Index ¶
- func AddressContains(selector, query string) bool
- func AutogroupResolvable(selector, query string) bool
- func ContainsLiteralWildcard(selectors []string) bool
- func EffectiveSrcPosture(own, fallback []string) ([]string, bool)
- func ExpandAddressSelector(selector string, policy *tailscale.ACL) []string
- func IsCrossTailnetSelector(selector string) bool
- func IsPublicAddress(value string, policy *tailscale.ACL) bool
- func IsRoleAutogroup(selector string) bool
- func KnownAutogroup(selector string) bool
- func LooksLikeUser(selector string) bool
- func ParseRoster(data []byte) (map[string]tailscale.UserRole, error)
- func ResolveIPSet(name string, policy *tailscale.ACL) ([]netip.Prefix, error)
- func SelectorMatches(selector, query string, policy *tailscale.ACL, source string) bool
- func ValidateIPSetOperations(members []string) error
- type IPSetImplicitMemberError
- type IPSetMember
- type IPSetMemberKind
- type IPSetOperation
- type Match
- type Matcher
- func (m Matcher) AnyMatches(selectors []string, query, source string) bool
- func (m Matcher) AnyMatchesTri(selectors []string, query, source string) (Match, string)
- func (m Matcher) KnownExternalTailnetAlias(alias string) bool
- func (m Matcher) Matches(selector, query, source string) bool
- func (m Matcher) MatchesTri(selector, query, source string) Match
- func (m Matcher) Recognized(selector string) bool
- type Policy
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddressContains ¶
AddressContains reports whether selector covers query, where either may be an address or a prefix.
func AutogroupResolvable ¶
AutogroupResolvable is the roster-blind view of whether this selector's membership can be decided against this query without control-plane state. Decidability is a property of the pair, not of the selector alone: autogroup:tagged is settled against "tag:batch" and against a user identity, but against "100.64.0.9" it needs the device-to-tag map, which lives in the control plane. Answering such a query "no match" would be exactly the confident wrong answer Match exists to prevent — and reporting a settleable one Unresolvable trains callers to ignore the state, so both directions matter. This function has no policy argument, so it cannot inspect group members; use Matcher.MatchesTri or SelectorMatchesTri when resolving a policy selector that may name a group.
Having no policy document, it also holds no external-tailnet declarations, so it reports every cross-tailnet selector undecidable. A Matcher whose policy declares them settles the undeclared-alias case, exactly as a roster settles the role-autogroup case.
func ContainsLiteralWildcard ¶
ContainsLiteralWildcard reports whether selectors contains the literal "*" selector. Callers whose policy surface attaches ports to destinations must strip those ports first, so "*:443" and "*:*" become the same literal host selector the tail-acl overbroad-wildcard advisory owns.
This intentionally does not treat autogroups as wildcards. In particular, autogroup:tagged can name most of a live fleet, but only control-plane state can answer whether it does; tailnet-audit owns that dynamic question.
func EffectiveSrcPosture ¶
EffectiveSrcPosture resolves the source-posture list that gates a rule: the rule's own srcPosture when that list is non-empty, otherwise the policy's defaultSrcPosture. Tailscale documents the default as replaced by a rule's own posture, not added to it, so a rule with its own srcPosture is gated by exactly that list.
fromDefault reports whether the returned list came from the fallback, so a caller can name the origin of a gate in its decision text.
func ExpandAddressSelector ¶
ExpandAddressSelector resolves a selector through Hosts and IPSets to the concrete addresses or prefixes it names. A Hosts value is returned as written: a literal address or prefix is that literal and is not looked up again as a host name, and any other value is returned unchanged rather than followed. The control plane rejects a hosts value that is not a literal, so following an alias chain would agree with a policy that cannot deploy. An IPSets entry is materialized through ResolveIPSet, so operations, ranges, and host: references inside it resolve, and a set that cannot be materialized expands to nothing. A value that names neither is returned unchanged, as is every value when policy is nil: a document that does not exist declares no aliases, so the selector already is the concrete value it names.
func IsCrossTailnetSelector ¶
IsCrossTailnetSelector reports whether selector uses the cross-tailnet group://<alias>/<name> or tag://<alias>/<name> grammar, which names members of another tailnet rather than of this one.
It reports true for a malformed cross-tailnet selector as well. A mistyped cross-tailnet reference is still a cross-tailnet reference, and treating it as an ordinary local selector would report an authoring typo as "this rule names nobody" instead of "this cannot be decided from this policy".
func IsPublicAddress ¶
IsPublicAddress reports whether value resolves to any publicly routable address, which is what autogroup:internet selects.
func IsRoleAutogroup ¶
IsRoleAutogroup reports whether selector is one of the role-based autogroups resolved from a tailnet's user roster rather than from the policy document.
func KnownAutogroup ¶
KnownAutogroup reports whether selector is an autogroup whose membership this package can ever confirm, independent of any particular query. Callers that grade a whole policy rather than one query use it to flag autogroups whose presence makes a verdict provisional, so the list lives here rather than being restated per tool.
The role autogroups are deliberately excluded: a tag refutes them, but no offline fact confirms them. Use AutogroupResolvable when a query is in hand — a known autogroup can still be undecidable against a given query.
func LooksLikeUser ¶
LooksLikeUser reports whether a selector names a user rather than a group.
func ParseRoster ¶
ParseRoster decodes an operator-supplied login-to-role roster.
It accepts two shapes so an operator with API access needs no transformation step: the Tailscale users endpoint's envelope, {"users":[{"loginName":…, "role":…}]}, and a hand-authored flat object mapping login to role. Fields beyond loginName and role are ignored, so `curl …/users > roster.json` works directly.
Parsing lives here, beside Matcher.roleMatches, because the two must agree on normalisation. roleMatches looks up m.Roles[strings.ToLower(query)]; a loader elsewhere that forgot to lowercase would make that equality silently false, and with HaveRoles true a missed lookup is a confident wrong NoMatch rather than a missing one. Keeping both halves in one package makes that class of drift impossible instead of merely tested-for.
A successful parse always returns a non-nil map, including for an empty roster. Callers use nil to mean "no roster was supplied" and set Matcher.HaveRoles from that distinction, so returning nil for {} would silently downgrade a complete-but-empty roster to an unread one.
func ResolveIPSet ¶
ResolveIPSet materializes one IPSets entry into the address space its members cover, as the minimal sorted prefix covering. Members are applied in list order through the ipset grammar: an implicit member and an add contribute their target's address space, a remove withdraws it from whatever earlier members contributed, so a later add re-contributes space an earlier remove withdrew. A host: reference is one Hosts lookup whose value must be a literal address or prefix, a range contributes every address between its ends, and an ipset: reference recurses with a path-scoped cycle guard, a per-call memo, and the same depth and size limits tail-ipam applies.
A set that cannot be materialized — a member that does not parse, an implicit member beside a remove (ValidateIPSetOperations), a host: alias that is undefined or names something other than an address or prefix, an undefined or cyclic ipset: reference, or an expansion past the limits — is an error naming the set and the member, never a partial set: dropping a member the resolver could not read would either hide address space or, for a failed remove, silently widen the set.
func SelectorMatches ¶
SelectorMatches reports whether one selector names query using only what the policy document supplies. It is the roster-blind entry point the three-state resolvers in this package are built on, and the form callers with no tailnet access use; role autogroups therefore never match here, which is exactly what AutogroupResolvable reports as undecidable. A caller holding a user roster builds a Matcher instead.
func ValidateIPSetOperations ¶
ValidateIPSetOperations enforces the control plane's explicit-operation rule: once any member is a remove operation, every member must be an explicit add or remove, because an implicit member's position relative to the remove would otherwise be ambiguous. It classifies by the operation word alone so a caller can report the rule for a set whose targets do not all parse.
Types ¶
type IPSetImplicitMemberError ¶
IPSetImplicitMemberError reports a member list that carries a remove operation alongside a member with no operation word. Member is the first implicit member in list order and Remove the first remove operation, so a message can name both.
func (*IPSetImplicitMemberError) Error ¶
func (e *IPSetImplicitMemberError) Error() string
type IPSetMember ¶
type IPSetMember struct {
Operation IPSetOperation
Kind IPSetMemberKind
Target string
}
IPSetMember is one parsed ipset member. Target is canonical for the address-bearing kinds — a masked prefix, an address or range rendered from its parsed form — and kept as written for host: and ipset: references, so two spellings of one member compare equal through String.
func ParseIPSetMember ¶
func ParseIPSetMember(member string) (IPSetMember, error)
ParseIPSetMember classifies one raw ipset member using the control plane's grammar: an optional add/remove operation word followed by a CIDR prefix, a bare address, an inclusive range A-B, a host:<alias> reference, or an ipset:<name> reference. It is syntax only: whether a referenced host or ipset exists is the caller's question, because answering it needs a policy.
func (IPSetMember) HostAlias ¶
func (m IPSetMember) HostAlias() (string, bool)
HostAlias returns the alias a host: member names — the key a caller looks up in the policy's hosts — and false for every other kind.
func (IPSetMember) String ¶
func (m IPSetMember) String() string
String renders the member in its canonical spelling: the operation word, when present, a space, and the canonical target.
type IPSetMemberKind ¶
type IPSetMemberKind string
IPSetMemberKind classifies what an ipset member's target names.
const ( // IPSetMemberPrefix is a CIDR prefix or a bare address. IPSetMemberPrefix IPSetMemberKind = "prefix" // IPSetMemberRange is an inclusive address range written A-B. IPSetMemberRange IPSetMemberKind = "range" // IPSetMemberHost is a host:<alias> reference into the policy's hosts. IPSetMemberHost IPSetMemberKind = "host" // IPSetMemberIPSet is an ipset:<name> reference to another ipset. IPSetMemberIPSet IPSetMemberKind = "ipset" )
type IPSetOperation ¶
type IPSetOperation string
IPSetOperation is the operation word an ipset member may carry. The control plane applies members in order: an implicit member and an add both contribute addresses, a remove withdraws them.
const ( // IPSetImplicit marks a member written without an operation word. IPSetImplicit IPSetOperation = "" IPSetAdd IPSetOperation = "add" IPSetRemove IPSetOperation = "remove" )
type Match ¶
type Match int
Match is the outcome of resolving a selector against a query.
A policy file does not contain every fact a membership decision depends on. Role autogroups resolve against control-plane state that only a matcher with a complete roster holds, so collapsing "cannot tell" into "does not match" yields a confident wrong answer — the one outcome a reachability or coverage verdict must never produce. Match keeps that third state addressable instead.
const ( // NoMatch means the selector provably does not name the query. NoMatch Match = iota // Matched means the selector names the query. Matched // Unresolvable means membership depends on control-plane state the matcher // does not have, so neither Matched nor NoMatch can be claimed. Unresolvable )
func AnySelectorMatchesTri ¶
func AnySelectorMatchesTri( selectors []string, query string, policy *tailscale.ACL, source string, ) (Match, string)
AnySelectorMatchesTri resolves a selector list against query. A match by any selector settles the list, so it outranks an earlier undecidable one. When nothing matches it returns Unresolvable along with the first selector that could not be decided, which callers surface as the reason.
func SelectorMatchesTri ¶
SelectorMatchesTri resolves one selector against query, reporting Unresolvable rather than NoMatch when the answer depends on control-plane state. source is the requesting identity, used only to resolve autogroup:self.
type Matcher ¶
type Matcher struct {
Policy *Policy
// Roles maps a user's login name, lowercased, to their tailnet role.
Roles map[string]tailscale.UserRole
// HaveRoles reports that Roles is a complete and authoritative user roster.
HaveRoles bool
}
Matcher resolves policy selectors against a query identity. Policy is the tailnet policy document and everything declared inside it; Roles and HaveRoles carry the live tailnet fact that document cannot supply — who holds which role, and whether a complete, authoritative roster was read.
When HaveRoles is false, role autogroups resolve to nothing and Recognized reports them as unresolved rather than unmatched, so a caller degrades to "coverage may be understated" instead of claiming a device is ungoverned. HaveRoles true with an empty Roles map is different: the complete roster was read and no user holds any role, so role autogroups are recognized and match nobody. Callers must leave HaveRoles false after a partial, truncated, or failed read. A static policy tool that has no tailnet access leaves it false.
func (Matcher) AnyMatches ¶
AnyMatches reports whether any selector names query. source is the requesting identity, used only to resolve autogroup:self.
func (Matcher) AnyMatchesTri ¶
AnyMatchesTri resolves a selector list against query. A match by any selector settles the list, so it outranks an earlier undecidable one. When nothing matches it returns Unresolvable along with the first selector that could not be decided, which callers surface as the reason.
func (Matcher) KnownExternalTailnetAlias ¶
KnownExternalTailnetAlias reports whether this matcher's policy declares alias as an external tailnet. It is the single lookup every cross-tailnet view goes through, so no two of them can disagree about which aliases exist.
A false result means "not declared by a policy whose declarations were read", which a matcher that never read them cannot distinguish from "not declared at all" — so such a matcher reports false for every alias. Use Recognized or MatchesTri when the difference matters; both keep an unreadable declaration undecidable rather than reporting it absent.
func (Matcher) Matches ¶
Matches reports whether selector names query. source is the requesting identity, used only to resolve autogroup:self.
func (Matcher) MatchesTri ¶
MatchesTri resolves one selector against query with everything this matcher holds, reporting Unresolvable rather than NoMatch when the answer depends on state the matcher does not have. source is the requesting identity, used only to resolve autogroup:self.
func (Matcher) Recognized ¶
Recognized reports whether this matcher understands selector well enough that a non-match means query is genuinely not named, rather than merely unevaluable. Every selector that is neither an autogroup nor a cross-tailnet reference is recognized — tags, users, groups, hosts, ipsets, and addresses all resolve from the policy document alone — as are the autogroup forms this package resolves. The one exception among them is an ipset the resolver cannot materialize: its non-match means the set could not be read, so it is reported unrecognized, exactly as MatchesTri reports it Unresolvable. A host whose value is not a literal address or prefix is the same kind of failure: expansion returns the value unchanged, so a non-match is not evidence that the host names nothing.
Role autogroups are recognized only when the user roster was read. Without that live tailnet fact no caller can decide them, so a non-match must be reported as unresolved rather than as absence of coverage.
A cross-tailnet selector is recognized only in the one case whose non-match is conclusive: it is well-formed, this policy's external-tailnet declarations were read, and its alias is not among them, so it is named by nothing here. A declared alias needs the other tailnet's roster, and a malformed selector is an authoring error, so neither is recognized.
Every other autogroup defers to KnownAutogroup rather than being re-listed here, and the cross-tailnet case defers to crossTailnetSpecFor for the same reason. Recognized is the policy-aware view of questions KnownAutogroup and IsCrossTailnetSelector answer for callers holding less, and a second copy of either list would drift the moment one learned a new form — the drift this package exists to prevent.
type Policy ¶
type Policy struct {
*tailscale.ACL
// HaveExternalTailnets reports that ExternalTailnets was read from the
// policy document. It draws the distinction HaveRoles draws for the user
// roster: a nil map with this set means the policy declares no external
// tailnets, which settles an undeclared alias as a definite non-match,
// while a nil map with it clear means the caller never parsed the field,
// which settles nothing. A caller that does not parse externalTailnets
// must leave it false, or a cross-tailnet selector it cannot evaluate is
// reported as provably unmatched.
HaveExternalTailnets bool
}
Policy is a tailnet policy document: the pinned SDK's ACL together with the read flags no SDK can carry. The ACL is embedded, so Groups, Hosts, IPSets, and ExternalTailnets stay promoted and the matching walk reads the document exactly as it did when Matcher held a bare *tailscale.ACL.
It exists so a hand-modeled policy field has one home rather than widening Matcher each time this repository models another. ExternalTailnets was the first: declarative node sharing is alpha, and tailscale.v2.ACL carried no externalTailnets field until v2.11.0. The SDK models it now, so the hand-modeled field is gone and the promoted one serves; what stays here is the read flag, which records a fact about the caller rather than the document.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package addrspace provides policy-agnostic address-space primitives for comparing route CIDRs and deriving site IDs from Tailscale 4via6 prefixes.
|
Package addrspace provides policy-agnostic address-space primitives for comparing route CIDRs and deriving site IDs from Tailscale 4via6 prefixes. |
|
Package alpha is the shared convention for hand-modeling a Tailscale feature that is alpha or unreleased in the pinned tailscale.com/client/tailscale/v2 SDK, so a tool in this repo can pick up an alpha feature early instead of waiting for the SDK to catch up.
|
Package alpha is the shared convention for hand-modeling a Tailscale feature that is alpha or unreleased in the pinned tailscale.com/client/tailscale/v2 SDK, so a tool in this repo can pick up an alpha feature early instead of waiting for the SDK to catch up. |
|
Package cliflags adapts stdlib-flag-era command lines to pflag parsing.
|
Package cliflags adapts stdlib-flag-era command lines to pflag parsing. |
|
Package hujsonedit provides schema-agnostic, comment-preserving RFC 6902 edits to HuJSON documents: build PatchOperation values with PatchAdd, PatchRemove, PatchComment, and PatchCommentOverwrite, then apply them with WritePatch to a file or with PatchBytes to a document held in memory.
|
Package hujsonedit provides schema-agnostic, comment-preserving RFC 6902 edits to HuJSON documents: build PatchOperation values with PatchAdd, PatchRemove, PatchComment, and PatchCommentOverwrite, then apply them with WritePatch to a file or with PatchBytes to a document held in memory. |
|
Package ipcalc provides policy-agnostic subnet arithmetic.
|
Package ipcalc provides policy-agnostic subnet arithmetic. |
|
Package posture evaluates Tailscale device-posture rule expressions against a captured device's facts.
|
Package posture evaluates Tailscale device-posture rule expressions against a captured device's facts. |
|
Package sanitize defines the repository-wide output-sanitization contract.
|
Package sanitize defines the repository-wide output-sanitization contract. |
|
sanitizetest
Package sanitizetest provides assertions for output-sanitization tests.
|
Package sanitizetest provides assertions for output-sanitization tests. |