Skip to content

Commit

Permalink
Fixed errors in files according to golangci-lint rules
Browse files Browse the repository at this point in the history
  • Loading branch information
hopleus committed Oct 17, 2024
1 parent 8289a22 commit 3a9efd5
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test-integration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ jobs:
- TestACLNamedHostsCanReach
- TestACLDevice1CanAccessDevice2
- TestPolicyUpdateWhileRunningWithCLIInDatabase
- TestAuthNodeApproval
- TestOIDCAuthenticationPingAll
- TestOIDCExpireNodesBasedOnTokenExpiry
- TestAuthWebFlowAuthenticationPingAll
- TestAuthWebFlowLogoutAndRelogin
- TestAuthNodeApproval
- TestUserCommand
- TestPreAuthKeyCommand
- TestPreAuthKeyCommandWithoutExpiry
Expand Down
4 changes: 3 additions & 1 deletion cmd/headscale/cli/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func init() {
approveNodeCmd.Flags().Uint64P("identifier", "i", 0, "Node identifier (ID)")
err = approveNodeCmd.MarkFlagRequired("identifier")
if err != nil {
log.Fatalf(err.Error())
log.Fatalf("%v", err)
}
nodeCmd.AddCommand(approveNodeCmd)

Expand Down Expand Up @@ -226,6 +226,7 @@ var approveNodeCmd = &cobra.Command{
fmt.Sprintf("Error converting ID to integer: %s", err),
output,
)

return
}
ctx, client, conn, cancel := newHeadscaleCLIWithConfig()
Expand All @@ -244,6 +245,7 @@ var approveNodeCmd = &cobra.Command{
),
output,
)

return
}
SuccessOutput(response.GetNode(), "Node approved", output)
Expand Down
2 changes: 1 addition & 1 deletion hscontrol/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ func (h *Headscale) handleAuthKey(
node.AuthKeyID = ptr.To(pak.ID)
}

if node.Approved == false {
if !node.Approved {
node.Approved = nodeApproved
}

Expand Down
21 changes: 12 additions & 9 deletions hscontrol/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ func init() {
schema.RegisterSerializer("text", TextSerialiser{})
}

var errDatabaseNotSupported = errors.New("database type not supported")
var (
errDatabaseNotSupported = errors.New("database type not supported")
errNoNodeApprovedColumnInDatabase = errors.New("no node approved column in database")
)

// KV is a key-value store in a psql table. For future use...
// TODO(kradalby): Is this used for anything?
Expand Down Expand Up @@ -489,26 +492,26 @@ func NewHeadscaleDatabase(
},
{
ID: "202410071005",
Migrate: func(tx *gorm.DB) error {
err = tx.AutoMigrate(&types.PreAuthKey{})
Migrate: func(db *gorm.DB) error {
err = db.AutoMigrate(&types.PreAuthKey{})
if err != nil {
return err
}

err = tx.AutoMigrate(&types.Node{})
err = db.AutoMigrate(&types.Node{})
if err != nil {
return err
}

if tx.Migrator().HasColumn(&types.Node{}, "approved") {
if db.Migrator().HasColumn(&types.Node{}, "approved") {
nodes := types.Nodes{}
if err := tx.Find(&nodes).Error; err != nil {
if err := db.Find(&nodes).Error; err != nil {
log.Error().Err(err).Msg("Error accessing db")
}

for item, node := range nodes {
if node.IsApproved() == false {
err = tx.Model(nodes[item]).Updates(types.Node{
if !node.IsApproved() {
err = db.Model(nodes[item]).Updates(types.Node{
Approved: true,
}).Error
if err != nil {
Expand All @@ -525,7 +528,7 @@ func NewHeadscaleDatabase(
return nil
}

return fmt.Errorf("no node approved column in DB")
return errNoNodeApprovedColumnInDatabase
},
Rollback: func(db *gorm.DB) error { return nil },
},
Expand Down
22 changes: 11 additions & 11 deletions hscontrol/db/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,17 +265,17 @@ func isTailscaleReservedIP(ip netip.Addr) bool {
// it will be added.
// If a prefix type has been removed (IPv4 or IPv6), it
// will remove the IPs in that family from the node.
func (hsdb *HSDatabase) BackfillNodeIPs(i *IPAllocator) ([]string, error) {
func (hsdb *HSDatabase) BackfillNodeIPs(ip *IPAllocator) ([]string, error) {
var err error
var ret []string
err = hsdb.Write(func(tx *gorm.DB) error {
if i == nil {
err = hsdb.Write(func(db *gorm.DB) error {
if ip == nil {
return errors.New("backfilling IPs: ip allocator was nil")
}

log.Trace().Msgf("starting to backfill IPs")

nodes, err := ListNodes(tx)
nodes, err := ListNodes(db)
if err != nil {
return fmt.Errorf("listing nodes to backfill IPs: %w", err)
}
Expand All @@ -285,8 +285,8 @@ func (hsdb *HSDatabase) BackfillNodeIPs(i *IPAllocator) ([]string, error) {

changed := false
// IPv4 prefix is set, but node ip is missing, alloc
if i.prefix4 != nil && node.IPv4 == nil {
ret4, err := i.nextLocked(i.prev4, i.prefix4)
if ip.prefix4 != nil && node.IPv4 == nil {
ret4, err := ip.nextLocked(ip.prev4, ip.prefix4)
if err != nil {
return fmt.Errorf("failed to allocate ipv4 for node(%d): %w", node.ID, err)
}
Expand All @@ -297,8 +297,8 @@ func (hsdb *HSDatabase) BackfillNodeIPs(i *IPAllocator) ([]string, error) {
}

// IPv6 prefix is set, but node ip is missing, alloc
if i.prefix6 != nil && node.IPv6 == nil {
ret6, err := i.nextLocked(i.prev6, i.prefix6)
if ip.prefix6 != nil && node.IPv6 == nil {
ret6, err := ip.nextLocked(ip.prev6, ip.prefix6)
if err != nil {
return fmt.Errorf("failed to allocate ipv6 for node(%d): %w", node.ID, err)
}
Expand All @@ -309,21 +309,21 @@ func (hsdb *HSDatabase) BackfillNodeIPs(i *IPAllocator) ([]string, error) {
}

// IPv4 prefix is not set, but node has IP, remove
if i.prefix4 == nil && node.IPv4 != nil {
if ip.prefix4 == nil && node.IPv4 != nil {
ret = append(ret, fmt.Sprintf("removing IPv4 %q from Node(%d) %q", node.IPv4.String(), node.ID, node.Hostname))
node.IPv4 = nil
changed = true
}

// IPv6 prefix is not set, but node has IP, remove
if i.prefix6 == nil && node.IPv6 != nil {
if ip.prefix6 == nil && node.IPv6 != nil {
ret = append(ret, fmt.Sprintf("removing IPv6 %q from Node(%d) %q", node.IPv6.String(), node.ID, node.Hostname))
node.IPv6 = nil
changed = true
}

if changed {
err := tx.Save(node).Error
err := db.Save(node).Error
if err != nil {
return fmt.Errorf("saving node(%d) after adding IPs: %w", node.ID, err)
}
Expand Down
4 changes: 2 additions & 2 deletions hscontrol/db/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ func (hsdb *HSDatabase) NodeSetApprove(nodeID types.NodeID, approved bool) error
})
}

// NodeSetApprove takes a Node struct and a set approval option
// NodeSetApprove takes a Node struct and a set approval option.
func NodeSetApprove(tx *gorm.DB,
nodeID types.NodeID, approved bool,
) error {
Expand Down Expand Up @@ -371,7 +371,7 @@ func (hsdb *HSDatabase) RegisterNodeFromAuthCallback(
node.User = *user
node.RegisterMethod = registrationMethod

if node.IsApproved() == false && manualApprovedNode == false {
if !node.IsApproved() && manualApprovedNode == false {
node.Approved = true
}

Expand Down
2 changes: 1 addition & 1 deletion hscontrol/db/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func (s *Suite) TestListPeersWithoutNonAuthorized(c *check.C) {
}

node := types.Node{
ID: types.NodeID(index),
ID: types.NodeID(int64(index)),
MachineKey: machineKey.Public(),
NodeKey: nodeKey.Public(),
Hostname: "testnode" + strconv.Itoa(index),
Expand Down
5 changes: 5 additions & 0 deletions hscontrol/types/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ const (
DatabaseSqlite = "sqlite3"
)

const (
SchemaHttp = "http"
SchemaHttps = "https"
)

var ErrCannotParsePrefix = errors.New("cannot parse prefix")

type StateUpdateType int
Expand Down
2 changes: 1 addition & 1 deletion hscontrol/types/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (node Node) IsExpired() bool {

// IsApproved returns whether the node is approved.
func (node Node) IsApproved() bool {
return node.Approved == true
return node.Approved
}

// IsEphemeral returns if the node is registered as an Ephemeral node.
Expand Down
17 changes: 9 additions & 8 deletions integration/auth_approval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import (
"context"
"crypto/tls"
"fmt"
v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
"github.com/juanfont/headscale/gen/go/headscale/v1"
"github.com/juanfont/headscale/hscontrol/types"
"github.com/juanfont/headscale/integration/hsic"
"github.com/samber/lo"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -56,7 +57,7 @@ func TestAuthNodeApproval(t *testing.T) {
status, err := client.Status()
assertNoErr(t, err)
assert.Equal(t, "NeedsMachineAuth", status.BackendState)
assert.Len(t, status.Peers(), 0)
assert.Empty(t, status.Peers())
}

headscale, err := scenario.Headscale()
Expand All @@ -74,11 +75,11 @@ func TestAuthNodeApproval(t *testing.T) {
},
&allNodes,
)
assert.Nil(t, err)
assert.NoError(t, err)

for _, node := range allNodes {
_, err = headscale.Execute([]string{
"headscale", "nodes", "approve", "--identifier", fmt.Sprintf("%d", node.Id),
"headscale", "nodes", "approve", "--identifier", fmt.Sprintf("%d", node.GetId()),
})
assertNoErr(t, err)
}
Expand Down Expand Up @@ -113,7 +114,7 @@ func TestAuthNodeApproval(t *testing.T) {
err = scenario.WaitForTailscaleSync()
assertNoErrSync(t, err)

//assertClientsState(t, allClients)
// assertClientsState(t, allClients)

allAddrs := lo.Map(allIps, func(x netip.Addr, index int) string {
return x.String()
Expand Down Expand Up @@ -236,11 +237,11 @@ func (s *AuthApprovalScenario) runHeadscaleRegister(userStr string, loginURL *ur
}

log.Printf("loginURL: %s", loginURL)
loginURL.Host = fmt.Sprintf("%s:8080", headscale.GetIP())
loginURL.Scheme = "http"
loginURL.Host = fmt.Sprintf("%s:%d", headscale.GetIP(), 8080)
loginURL.Scheme = types.SchemaHttp

if len(headscale.GetCert()) > 0 {
loginURL.Scheme = "https"
loginURL.Scheme = types.SchemaHttps
}

insecureTransport := &http.Transport{
Expand Down
2 changes: 1 addition & 1 deletion integration/hsic/hsic.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func WithTuning(batchTimeout time.Duration, mapSessionChanSize int) Option {
}
}

// WithManualApproveNewNode allows devices to access the network only after manual approval
// WithManualApproveNewNode allows devices to access the network only after manual approval.
func WithManualApproveNewNode() Option {
return func(hsic *HeadscaleInContainer) {
hsic.env["HEADSCALE_NODE_MANAGEMENT_MANUAL_APPROVE_NEW_NODE"] = "true"
Expand Down

0 comments on commit 3a9efd5

Please sign in to comment.