Skip to content

Commit

Permalink
Load ACL policy on headscale startup
Browse files Browse the repository at this point in the history
  • Loading branch information
juanfont committed Jul 4, 2021
1 parent 401e6ae commit 202d6b5
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
9 changes: 7 additions & 2 deletions acls.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const errorInvalidTag = Error("invalid tag")
const errorInvalidNamespace = Error("invalid namespace")
const errorInvalidPortFormat = Error("invalid port format")

func (h *Headscale) LoadPolicy(path string) error {
func (h *Headscale) LoadAclPolicy(path string) error {
policyFile, err := os.Open(path)
if err != nil {
return err
Expand All @@ -40,7 +40,12 @@ func (h *Headscale) LoadPolicy(path string) error {
}

h.aclPolicy = &policy
return err
rules, err := h.generateACLRules()
if err != nil {
return err
}
h.aclRules = rules
return nil
}

func (h *Headscale) generateACLRules() (*[]tailcfg.FilterRule, error) {
Expand Down
2 changes: 1 addition & 1 deletion api.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ func (h *Headscale) getMapResponse(mKey wgkey.Key, req tailcfg.MapRequest, m Mac
DNS: []netaddr.IP{},
SearchPaths: []string{},
Domain: "foobar@example.com",
PacketFilter: tailcfg.FilterAllowAll,
PacketFilter: *h.aclRules,
DERPMap: h.cfg.DerpMap,
UserProfiles: []tailcfg.UserProfile{},
}
Expand Down
3 changes: 3 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type Headscale struct {
privateKey *wgkey.Private

aclPolicy *ACLPolicy
aclRules *[]tailcfg.FilterRule

pollMu sync.Mutex
clientsPolling map[uint64]chan []byte // this is by all means a hackity hack
Expand Down Expand Up @@ -84,7 +85,9 @@ func NewHeadscale(cfg Config) (*Headscale, error) {
dbString: dbString,
privateKey: privKey,
publicKey: &pubKey,
aclRules: &tailcfg.FilterAllowAll, // default allowall
}

err = h.initDB()
if err != nil {
return nil, err
Expand Down
7 changes: 7 additions & 0 deletions cmd/headscale/cli/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ func getHeadscaleApp() (*headscale.Headscale, error) {
if err != nil {
return nil, err
}

// We are doing this here, as in the future could be cool to have it also hot-reload
err = h.LoadAclPolicy(absPath(viper.GetString("acl_policy_path")))
if err != nil {
log.Printf("Could not load the ACL policy: %s", err)
}

return h, nil
}

Expand Down

0 comments on commit 202d6b5

Please sign in to comment.