Skip to content

Commit

Permalink
golangci-lint --fix
Browse files Browse the repository at this point in the history
  • Loading branch information
kradalby committed Nov 13, 2021
1 parent dae34ca commit 2634215
Show file tree
Hide file tree
Showing 17 changed files with 62 additions and 77 deletions.
3 changes: 1 addition & 2 deletions acls.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"strings"

"github.com/rs/zerolog/log"

"github.com/tailscale/hujson"
"inet.af/netaddr"
"tailscale.com/tailcfg"
Expand All @@ -25,7 +24,7 @@ const (
errorInvalidPortFormat = Error("invalid port format")
)

// LoadACLPolicy loads the ACL policy from the specify path, and generates the ACL rules
// LoadACLPolicy loads the ACL policy from the specify path, and generates the ACL rules.
func (h *Headscale) LoadACLPolicy(path string) error {
policyFile, err := os.Open(path)
if err != nil {
Expand Down
16 changes: 8 additions & 8 deletions acls_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"inet.af/netaddr"
)

// ACLPolicy represents a Tailscale ACL Policy
// ACLPolicy represents a Tailscale ACL Policy.
type ACLPolicy struct {
Groups Groups `json:"Groups"`
Hosts Hosts `json:"Hosts"`
Expand All @@ -17,30 +17,30 @@ type ACLPolicy struct {
Tests []ACLTest `json:"Tests"`
}

// ACL is a basic rule for the ACL Policy
// ACL is a basic rule for the ACL Policy.
type ACL struct {
Action string `json:"Action"`
Users []string `json:"Users"`
Ports []string `json:"Ports"`
}

// Groups references a series of alias in the ACL rules
// Groups references a series of alias in the ACL rules.
type Groups map[string][]string

// Hosts are alias for IP addresses or subnets
// Hosts are alias for IP addresses or subnets.
type Hosts map[string]netaddr.IPPrefix

// TagOwners specify what users (namespaces?) are allow to use certain tags
// TagOwners specify what users (namespaces?) are allow to use certain tags.
type TagOwners map[string][]string

// ACLTest is not implemented, but should be use to check if a certain rule is allowed
// ACLTest is not implemented, but should be use to check if a certain rule is allowed.
type ACLTest struct {
User string `json:"User"`
Allow []string `json:"Allow"`
Deny []string `json:"Deny,omitempty"`
}

// UnmarshalJSON allows to parse the Hosts directly into netaddr objects
// UnmarshalJSON allows to parse the Hosts directly into netaddr objects.
func (h *Hosts) UnmarshalJSON(data []byte) error {
hosts := Hosts{}
hs := make(map[string]string)
Expand Down Expand Up @@ -68,7 +68,7 @@ func (h *Hosts) UnmarshalJSON(data []byte) error {
return nil
}

// IsZero is perhaps a bit naive here
// IsZero is perhaps a bit naive here.
func (p ACLPolicy) IsZero() bool {
if len(p.Groups) == 0 && len(p.Hosts) == 0 && len(p.ACLs) == 0 {
return true
Expand Down
10 changes: 4 additions & 6 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,22 @@ import (
"strings"
"time"

"github.com/rs/zerolog/log"

"github.com/gin-gonic/gin"
"github.com/klauspost/compress/zstd"
"github.com/rs/zerolog/log"
"gorm.io/gorm"
"tailscale.com/tailcfg"
"tailscale.com/types/wgkey"
)

// KeyHandler provides the Headscale pub key
// Listens in /key
// Listens in /key.
func (h *Headscale) KeyHandler(c *gin.Context) {
c.Data(200, "text/plain; charset=utf-8", []byte(h.publicKey.HexString()))
}

// RegisterWebAPI shows a simple message in the browser to point to the CLI
// Listens in /register
// Listens in /register.
func (h *Headscale) RegisterWebAPI(c *gin.Context) {
mKeyStr := c.Query("key")
if mKeyStr == "" {
Expand Down Expand Up @@ -55,7 +54,7 @@ func (h *Headscale) RegisterWebAPI(c *gin.Context) {
}

// RegistrationHandler handles the actual registration process of a machine
// Endpoint /machine/:id
// Endpoint /machine/:id.
func (h *Headscale) RegistrationHandler(c *gin.Context) {
body, _ := io.ReadAll(c.Request.Body)
mKeyStr := c.Param("id")
Expand Down Expand Up @@ -111,7 +110,6 @@ func (h *Headscale) RegistrationHandler(c *gin.Context) {

// We have the updated key!
if m.NodeKey == wgkey.Key(req.NodeKey).HexString() {

// The client sends an Expiry in the past if the client is requesting to expire the key (aka logout)
// https://github.com/tailscale/tailscale/blob/main/tailcfg/tailcfg.go#L648
if !req.Expiry.IsZero() && req.Expiry.UTC().Before(now) {
Expand Down
10 changes: 4 additions & 6 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,19 @@ import (
"time"

"github.com/coreos/go-oidc/v3/oidc"
"github.com/patrickmn/go-cache"
"golang.org/x/oauth2"

"github.com/gin-gonic/gin"
"github.com/grpc-ecosystem/go-grpc-middleware"
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
"github.com/philip-bui/grpc-zerolog"
"github.com/patrickmn/go-cache"
zerolog "github.com/philip-bui/grpc-zerolog"
zl "github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/soheilhy/cmux"
ginprometheus "github.com/zsais/go-gin-prometheus"
"golang.org/x/crypto/acme"
"golang.org/x/crypto/acme/autocert"
"golang.org/x/oauth2"
"golang.org/x/sync/errgroup"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
Expand Down Expand Up @@ -280,7 +279,6 @@ func (h *Headscale) grpcAuthenticationInterceptor(ctx context.Context,
req interface{},
info *grpc.UnaryServerInfo,
handler grpc.UnaryHandler) (interface{}, error) {

// Check if the request is coming from the on-server client.
// This is not secure, but it is to maintain maintainability
// with the "legacy" database-based client
Expand Down
5 changes: 2 additions & 3 deletions apple_mobileconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ import (
"net/http"
"text/template"

"github.com/rs/zerolog/log"

"github.com/gin-gonic/gin"
"github.com/gofrs/uuid"
"github.com/rs/zerolog/log"
)

// AppleMobileConfig shows a simple message in the browser to point to the CLI
// Listens in /register
// Listens in /register.
func (h *Headscale) AppleMobileConfig(c *gin.Context) {
t := template.Must(template.New("apple").Parse(`
<html>
Expand Down
2 changes: 1 addition & 1 deletion cmd/headscale/cli/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ omit the route you do not want to enable.
},
}

// routesToPtables converts the list of routes to a nice table
// routesToPtables converts the list of routes to a nice table.
func routesToPtables(routes *v1.Routes) pterm.TableData {
d := pterm.TableData{{"Route", "Enabled"}}

Expand Down
1 change: 0 additions & 1 deletion cmd/headscale/cli/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,6 @@ func getHeadscaleCLIClient() (context.Context, v1.HeadscaleServiceClient, *grpc.

// If the address is not set, we assume that we are on the server hosting headscale.
if address == "" {

log.Debug().
Str("socket", cfg.UnixSocket).
Msgf("HEADSCALE_CLI_ADDRESS environment is not set, connecting to unix socket.")
Expand Down
4 changes: 2 additions & 2 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (h *Headscale) openDB() (*gorm.DB, error) {
return db, nil
}

// getValue returns the value for the given key in KV
// getValue returns the value for the given key in KV.
func (h *Headscale) getValue(key string) (string, error) {
var row KV
if result := h.db.First(&row, "key = ?", key); errors.Is(
Expand All @@ -96,7 +96,7 @@ func (h *Headscale) getValue(key string) (string, error) {
return row.Value, nil
}

// setValue sets value for the given key in KV
// setValue sets value for the given key in KV.
func (h *Headscale) setValue(key string, value string) error {
kv := KV{
Key: key,
Expand Down
4 changes: 1 addition & 3 deletions derp.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ import (
"time"

"github.com/rs/zerolog/log"

"gopkg.in/yaml.v2"

"tailscale.com/tailcfg"
)

Expand Down Expand Up @@ -55,7 +53,7 @@ func loadDERPMapFromURL(addr url.URL) (*tailcfg.DERPMap, error) {
// DERPMap, it will _only_ look at the Regions, an integer.
// If a region exists in two of the given DERPMaps, the region
// form the _last_ DERPMap will be preserved.
// An empty DERPMap list will result in a DERPMap with no regions
// An empty DERPMap list will result in a DERPMap with no regions.
func mergeDERPMaps(derpMaps []*tailcfg.DERPMap) *tailcfg.DERPMap {
result := tailcfg.DERPMap{
OmitDefaultRegions: false,
Expand Down
29 changes: 14 additions & 15 deletions machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,17 @@ import (
"time"

"github.com/fatih/set"
v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
"github.com/rs/zerolog/log"
"google.golang.org/protobuf/types/known/timestamppb"

v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
"gorm.io/datatypes"
"gorm.io/gorm"
"inet.af/netaddr"
"tailscale.com/tailcfg"
"tailscale.com/types/wgkey"
)

// Machine is a Headscale client
// Machine is a Headscale client.
type Machine struct {
ID uint64 `gorm:"primary_key"`
MachineKey string `gorm:"type:varchar(64);unique_index"`
Expand Down Expand Up @@ -56,12 +55,12 @@ type (
MachinesP []*Machine
)

// For the time being this method is rather naive
// For the time being this method is rather naive.
func (m Machine) isAlreadyRegistered() bool {
return m.Registered
}

// isExpired returns whether the machine registration has expired
// isExpired returns whether the machine registration has expired.
func (m Machine) isExpired() bool {
return time.Now().UTC().After(*m.Expiry)
}
Expand Down Expand Up @@ -119,7 +118,7 @@ func (h *Headscale) getDirectPeers(m *Machine) (Machines, error) {
return machines, nil
}

// getShared fetches machines that are shared to the `Namespace` of the machine we are getting peers for
// getShared fetches machines that are shared to the `Namespace` of the machine we are getting peers for.
func (h *Headscale) getShared(m *Machine) (Machines, error) {
log.Trace().
Caller().
Expand All @@ -146,7 +145,7 @@ func (h *Headscale) getShared(m *Machine) (Machines, error) {
return peers, nil
}

// getSharedTo fetches the machines of the namespaces this machine is shared in
// getSharedTo fetches the machines of the namespaces this machine is shared in.
func (h *Headscale) getSharedTo(m *Machine) (Machines, error) {
log.Trace().
Caller().
Expand Down Expand Up @@ -228,7 +227,7 @@ func (h *Headscale) ListMachines() ([]Machine, error) {
return machines, nil
}

// GetMachine finds a Machine by name and namespace and returns the Machine struct
// GetMachine finds a Machine by name and namespace and returns the Machine struct.
func (h *Headscale) GetMachine(namespace string, name string) (*Machine, error) {
machines, err := h.ListMachinesInNamespace(namespace)
if err != nil {
Expand All @@ -243,7 +242,7 @@ func (h *Headscale) GetMachine(namespace string, name string) (*Machine, error)
return nil, fmt.Errorf("machine not found")
}

// GetMachineByID finds a Machine by ID and returns the Machine struct
// GetMachineByID finds a Machine by ID and returns the Machine struct.
func (h *Headscale) GetMachineByID(id uint64) (*Machine, error) {
m := Machine{}
if result := h.db.Preload("Namespace").Find(&Machine{ID: id}).First(&m); result.Error != nil {
Expand All @@ -252,7 +251,7 @@ func (h *Headscale) GetMachineByID(id uint64) (*Machine, error) {
return &m, nil
}

// GetMachineByMachineKey finds a Machine by ID and returns the Machine struct
// GetMachineByMachineKey finds a Machine by ID and returns the Machine struct.
func (h *Headscale) GetMachineByMachineKey(mKey string) (*Machine, error) {
m := Machine{}
if result := h.db.Preload("Namespace").First(&m, "machine_key = ?", mKey); result.Error != nil {
Expand All @@ -270,7 +269,7 @@ func (h *Headscale) UpdateMachine(m *Machine) error {
return nil
}

// DeleteMachine softs deletes a Machine from the database
// DeleteMachine softs deletes a Machine from the database.
func (h *Headscale) DeleteMachine(m *Machine) error {
err := h.RemoveSharedMachineFromAllNamespaces(m)
if err != nil && err != errorMachineNotShared {
Expand All @@ -287,7 +286,7 @@ func (h *Headscale) DeleteMachine(m *Machine) error {
return h.RequestMapUpdates(namespaceID)
}

// HardDeleteMachine hard deletes a Machine from the database
// HardDeleteMachine hard deletes a Machine from the database.
func (h *Headscale) HardDeleteMachine(m *Machine) error {
err := h.RemoveSharedMachineFromAllNamespaces(m)
if err != nil && err != errorMachineNotShared {
Expand All @@ -302,7 +301,7 @@ func (h *Headscale) HardDeleteMachine(m *Machine) error {
return h.RequestMapUpdates(namespaceID)
}

// GetHostInfo returns a Hostinfo struct for the machine
// GetHostInfo returns a Hostinfo struct for the machine.
func (m *Machine) GetHostInfo() (*tailcfg.Hostinfo, error) {
hostinfo := tailcfg.Hostinfo{}
if len(m.HostInfo) != 0 {
Expand Down Expand Up @@ -397,7 +396,7 @@ func (ms Machines) toNodes(
}

// toNode converts a Machine into a Tailscale Node. includeRoutes is false for shared nodes
// as per the expected behaviour in the official SaaS
// as per the expected behaviour in the official SaaS.
func (m Machine) toNode(
baseDomain string,
dnsConfig *tailcfg.DNSConfig,
Expand Down Expand Up @@ -572,7 +571,7 @@ func (m *Machine) toProto() *v1.Machine {
return machine
}

// RegisterMachine is executed from the CLI to register a new Machine using its MachineKey
// RegisterMachine is executed from the CLI to register a new Machine using its MachineKey.
func (h *Headscale) RegisterMachine(key string, namespace string) (*Machine, error) {
ns, err := h.GetNamespace(namespace)
if err != nil {
Expand Down
Loading

0 comments on commit 2634215

Please sign in to comment.