Skip to content

Commit

Permalink
Split code into modules
Browse files Browse the repository at this point in the history
This is a massive commit that restructures the code into modules:

db/
    All functions related to modifying the Database

types/
    All type definitions and methods that can be exclusivly used on
    these types without dependencies

policy/
    All Policy related code, now without dependencies on the Database.

policy/matcher/
    Dedicated code to match machines in a list of FilterRules

Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com>
  • Loading branch information
kradalby committed May 26, 2023
1 parent 14e29a7 commit feb1536
Show file tree
Hide file tree
Showing 51 changed files with 4,669 additions and 4,282 deletions.
4 changes: 2 additions & 2 deletions cmd/headscale/cli/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strconv"

v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
"github.com/juanfont/headscale/hscontrol"
"github.com/juanfont/headscale/hscontrol/types"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
"google.golang.org/grpc/status"
Expand Down Expand Up @@ -277,7 +277,7 @@ func routesToPtables(routes []*v1.Route) pterm.TableData {

continue
}
if prefix == hscontrol.ExitRouteV4 || prefix == hscontrol.ExitRouteV6 {
if prefix == types.ExitRouteV4 || prefix == types.ExitRouteV6 {
isPrimaryStr = "-"
} else {
isPrimaryStr = strconv.FormatBool(route.IsPrimary)
Expand Down
5 changes: 4 additions & 1 deletion cmd/headscale/cli/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
"github.com/juanfont/headscale/hscontrol"
"github.com/juanfont/headscale/hscontrol/policy"
"github.com/juanfont/headscale/hscontrol/util"
"github.com/rs/zerolog/log"
"google.golang.org/grpc"
Expand Down Expand Up @@ -41,13 +42,15 @@ func getHeadscaleApp() (*hscontrol.Headscale, error) {

if cfg.ACL.PolicyPath != "" {
aclPath := util.AbsolutePathFromConfigPath(cfg.ACL.PolicyPath)
err = app.LoadACLPolicyFromPath(aclPath)
pol, err := policy.LoadACLPolicyFromPath(aclPath)
if err != nil {
log.Fatal().
Str("path", aclPath).
Err(err).
Msg("Could not load the ACL policy")
}

app.ACLPolicy = pol
}

return app, nil
Expand Down
5 changes: 1 addition & 4 deletions hscontrol/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ const (
// TODO(juan): remove this once https://github.com/juanfont/headscale/issues/727 is fixed.
registrationHoldoff = time.Second * 5
reservedResponseHeaderSize = 4
RegisterMethodAuthKey = "authkey"
RegisterMethodOIDC = "oidc"
RegisterMethodCLI = "cli"
)

var ErrRegisterMethodCLIDoesNotSupportExpire = errors.New(
Expand Down Expand Up @@ -56,7 +53,7 @@ func (h *Headscale) HealthHandler(
}
}

if err := h.db.pingDB(req.Context()); err != nil {
if err := h.db.PingDB(req.Context()); err != nil {
respond(err)

return
Expand Down
11 changes: 6 additions & 5 deletions hscontrol/api_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@ package hscontrol
import (
"time"

"github.com/juanfont/headscale/hscontrol/types"
"github.com/juanfont/headscale/hscontrol/util"
"github.com/rs/zerolog/log"
"tailscale.com/tailcfg"
)

func (h *Headscale) generateMapResponse(
mapRequest tailcfg.MapRequest,
machine *Machine,
machine *types.Machine,
) (*tailcfg.MapResponse, error) {
log.Trace().
Str("func", "generateMapResponse").
Str("machine", mapRequest.Hostinfo.Hostname).
Msg("Creating Map response")
node, err := h.db.toNode(*machine, h.aclPolicy, h.cfg.BaseDomain, h.cfg.DNSConfig)
node, err := h.db.TailNode(*machine, h.ACLPolicy, h.cfg.DNSConfig)
if err != nil {
log.Error().
Caller().
Expand All @@ -27,7 +28,7 @@ func (h *Headscale) generateMapResponse(
return nil, err
}

peers, err := h.db.getValidPeers(h.aclPolicy, h.aclRules, machine)
peers, err := h.db.GetValidPeers(h.aclRules, machine)
if err != nil {
log.Error().
Caller().
Expand All @@ -38,9 +39,9 @@ func (h *Headscale) generateMapResponse(
return nil, err
}

profiles := h.db.getMapResponseUserProfiles(*machine, peers)
profiles := h.db.GetMapResponseUserProfiles(*machine, peers)

nodePeers, err := h.db.toNodes(peers, h.aclPolicy, h.cfg.BaseDomain, h.cfg.DNSConfig)
nodePeers, err := h.db.TailNodes(peers, h.ACLPolicy, h.cfg.DNSConfig)
if err != nil {
log.Error().
Caller().
Expand Down

0 comments on commit feb1536

Please sign in to comment.