policy

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2026 License: AGPL-3.0 Imports: 11 Imported by: 0

Documentation

Overview

Package policy parses the mesh access-control policy and compiles it into packet-filter rules and peer visibility for each node.

A policy is HuJSON (JSON with comments and trailing commas):

{
  "hosts": {"office": "10.1.0.0/24"},
  "acls": [
    // Developers may SSH to servers and reach the office LAN.
    {"action": "accept", "src": ["tag:dev"], "dst": ["tag:server:22", "office:*"]},
  ],
  "autoApprovers": {"routes": {"10.1.0.0/24": ["tag:router"]}, "exitNode": ["tag:exit"]},
  "tests": [{"src": "tag:dev", "accept": ["tag:server:22"], "deny": ["tag:server:80"]}],
}

Selectors name traffic sources and destinations: "*" (anything), "tag:<name>" (nodes with that tag), a node name, a host alias from "hosts", or an IP address or CIDR prefix. Destinations add ports: "<selector>:<ports>" where ports is "*", a port, a range "8000-8999" or a comma-separated list of those. Anything not accepted by a rule is denied.

Index

Constants

View Source
const Default = `` /* 564-byte string literal not displayed */

Default is the policy of a new coordinator: every node may reach every other node (and every approved route) on any port.

Variables

This section is empty.

Functions

func ValidTag

func ValidTag(s string) bool

ValidTag reports whether s is a well-formed tag such as "tag:web".

Types

type ACL

type ACL struct {
	Action string   `json:"action"`
	Src    []string `json:"src"`
	Dst    []string `json:"dst"`
	// Proto restricts the rule to "tcp", "udp" or "icmp"; empty means any.
	Proto string `json:"proto,omitempty"`
}

ACL is one rule as written.

type AutoApprovers

type AutoApprovers struct {
	// Routes maps a prefix to the nodes that may advertise it or any prefix
	// inside it.
	Routes map[string][]string `json:"routes,omitempty"`
	// ExitNode lists nodes that may become exit nodes.
	ExitNode []string `json:"exitNode,omitempty"`
}

AutoApprovers names nodes whose advertised routes need no admin approval.

type Compiled

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

Compiled is a policy resolved against the current set of nodes.

func (*Compiled) Allowed

func (c *Compiled) Allowed(src, dst netip.Addr, proto uint8, port uint16) (bool, int)

Allowed reports whether a packet from src to dst would be admitted by the destination, returning the index of the first matching ACL.

func (*Compiled) CanReach

func (c *Compiled) CanReach(from, to Node) bool

CanReach reports whether some rule lets node from send anything to node to (its address or its routes).

func (*Compiled) FilterFor

func (c *Compiled) FilterFor(n Node) []filter.Rule

FilterFor returns the rules node n enforces on inbound traffic: those with a destination n delivers, narrowed to what n delivers.

func (*Compiled) Rules

func (c *Compiled) Rules() []filter.Rule

Rules returns every compiled rule, in policy order.

func (*Compiled) Visible

func (c *Compiled) Visible(a, b Node) bool

Visible reports whether two nodes need to know about each other: one of them may open connections to the other.

type Node

type Node struct {
	Name string
	IP   netip.Addr
	Tags []string
	// Routes are the node's approved subnet routes (0.0.0.0/0 for an exit
	// node).
	Routes []netip.Prefix
}

Node is what the policy needs to know about a mesh member.

type Policy

type Policy struct {
	Hosts         map[string]string `json:"hosts,omitempty"`
	ACLs          []ACL             `json:"acls"`
	AutoApprovers AutoApprovers     `json:"autoApprovers,omitzero"`
	Tests         []Test            `json:"tests,omitempty"`
	// contains filtered or unexported fields
}

Policy is a parsed policy document.

func Parse

func Parse(text []byte) (*Policy, error)

Parse parses and validates a policy.

func (*Policy) AutoApproved

func (p *Policy) AutoApproved(n Node, advertised []netip.Prefix) []netip.Prefix

AutoApproved returns the subset of advertised routes node n may use without an administrator's approval.

func (*Policy) Compile

func (p *Policy) Compile(nodes []Node) *Compiled

Compile resolves the policy's selectors against nodes.

func (*Policy) RunTests

func (p *Policy) RunTests(nodes []Node) []error

RunTests checks the policy's tests against nodes and returns one error per failed assertion.

type Test

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

Test is an assertion checked whenever the policy is saved.

Jump to

Keyboard shortcuts

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