mirror of
https://github.com/gravitl/netmaker.git
synced 2026-04-22 16:07:11 +08:00
edda2868fc
* feat(go): add user schema; * feat(go): migrate to user schema; * feat(go): add audit fields; * feat(go): remove unused fields from the network model; * feat(go): add network schema; * feat(go): migrate to network schema; * refactor(go): add comment to clarify migration logic; * fix(go): test failures; * fix(go): test failures; * feat(go): change membership table to store memberships at all scopes; * feat(go): add schema for access grants; * feat(go): remove nameservers from new networks table; ensure db passed for schema functions; * feat(go): set max conns for sqlite to 1; * fix(go): issues updating user account status; * refactor(go): remove converters and access grants; * refactor(go): add json tags in schema models; * refactor(go): rename file to migrate_v1_6_0.go; * refactor(go): add user groups and user roles tables; use schema tables; * refactor(go): inline get and list from schema package; * refactor(go): inline get network and list users from schema package; * fix(go): staticcheck issues; * fix(go): remove test not in use; fix test case; * fix(go): validate network; * fix(go): resolve static checks; * fix(go): new models errors; * fix(go): test errors; * fix(go): handle no records; * fix(go): add validations for user object; * fix(go): set correct extclient status; * fix(go): test error; * feat(go): make schema the base package; * feat(go): add host schema; * feat(go): use schema host everywhere; * feat(go): inline get host, list hosts and delete host; * feat(go): use non-ptr value; * feat(go): use save to upsert all fields; * feat(go): use save to upsert all fields; * feat(go): save turn endpoint as string; * feat(go): check for gorm error record not found; * fix(go): test failures; * fix(go): update all network fields; * fix(go): update all network fields; * feat(go): add paginated list networks api; * feat(go): add paginated list users api; * feat(go): add paginated list hosts api; * feat(go): add pagination to list groups api; * fix(go): comment; * fix(go): implement marshal and unmarshal text for custom types; * fix(go): implement marshal and unmarshal json for custom types; * fix(go): just use the old model for unmarshalling; * fix(go): implement marshal and unmarshal json for custom types; * feat(go): remove paginated list networks api; * feat(go): use custom paginated response object; * fix(go): ensure default values for page and per_page are used when not passed; * fix(go): rename v1.6.0 to v1.5.1; * fix(go): check for gorm.ErrRecordNotFound instead of database.IsEmptyRecord; * fix(go): use host id, not pending host id; * feat(go): add filters to paginated apis; * feat(go): add filters to paginated apis; * feat(go): remove check for max username length; * feat(go): add filters to count as well; * feat(go): use library to check email address validity; * feat(go): ignore pagination if params not passed; * fix(go): pagination issues; * fix(go): check exists before using; * fix(go): remove debug log; * fix(go): use gorm err record not found; * fix(go): use gorm err record not found; * fix(go): use user principal name when creating pending user; * fix(go): use schema package for consts; * fix(go): prevent disabling superadmin user; Co-authored-by: tenki-reviewer[bot] <262613592+tenki-reviewer[bot]@users.noreply.github.com> * fix(go): swap is admin and is superadmin; Co-authored-by: tenki-reviewer[bot] <262613592+tenki-reviewer[bot]@users.noreply.github.com> * fix(go): remove dead code block; https://github.com/gravitl/netmaker/pull/3910#discussion_r2928837937 * fix(go): incorrect message when trying to disable self; https://github.com/gravitl/netmaker/pull/3910#discussion_r2928837934 * fix(go): use correct header; Co-authored-by: tenki-reviewer[bot] <262613592+tenki-reviewer[bot]@users.noreply.github.com> * fix(go): return after error response; Co-authored-by: tenki-reviewer[bot] <262613592+tenki-reviewer[bot]@users.noreply.github.com> * fix(go): use correct order of params; https://github.com/gravitl/netmaker/pull/3910#discussion_r2929593036 * fix(go): set default values for page and page size; use v2 instead of /list; * Update logic/auth.go Co-authored-by: tenki-reviewer[bot] <262613592+tenki-reviewer[bot]@users.noreply.github.com> * Update schema/user_roles.go Co-authored-by: tenki-reviewer[bot] <262613592+tenki-reviewer[bot]@users.noreply.github.com> * fix(go): syntax error; * fix(go): set default values when page and per_page are not passed or 0; * fix(go): use uuid.parse instead of uuid.must parse; * fix(go): review errors; * fix(go): review errors; * Update controllers/user.go Co-authored-by: tenki-reviewer[bot] <262613592+tenki-reviewer[bot]@users.noreply.github.com> * Update controllers/user.go Co-authored-by: tenki-reviewer[bot] <262613592+tenki-reviewer[bot]@users.noreply.github.com> * NM-163: fix errors: * Update db/types/options.go Co-authored-by: tenki-reviewer[bot] <262613592+tenki-reviewer[bot]@users.noreply.github.com> * fix(go): persist return user in event; * Update db/types/options.go Co-authored-by: tenki-reviewer[bot] <262613592+tenki-reviewer[bot]@users.noreply.github.com> * NM-163: duplicate lines of code * NM-163: fix(go): fix missing return and filter parsing in user controller - Add missing return after error response in updateUserAccountStatus to prevent double-response and spurious ext-client side-effects - Use switch statements in listUsers to skip unrecognized account_status and mfa_status filter values * fix(go): check for both min and max page size; * fix(go): enclose transfer superadmin in transaction; * fix(go): review errors; * fix(go): remove free tier checks; * fix(go): review fixes; --------- Co-authored-by: VishalDalwadi <dalwadivishal26@gmail.com> Co-authored-by: Vishal Dalwadi <51291657+VishalDalwadi@users.noreply.github.com> Co-authored-by: tenki-reviewer[bot] <262613592+tenki-reviewer[bot]@users.noreply.github.com>
185 lines
5.4 KiB
Go
185 lines
5.4 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/gravitl/netmaker/db"
|
|
"github.com/gravitl/netmaker/logger"
|
|
"github.com/gravitl/netmaker/models"
|
|
"github.com/gravitl/netmaker/schema"
|
|
)
|
|
|
|
const (
|
|
// ZOMBIE_TIMEOUT - timeout in hours for checking zombie status
|
|
ZOMBIE_TIMEOUT = 6
|
|
// ZOMBIE_DELETE_TIME - timeout in minutes for zombie node deletion
|
|
ZOMBIE_DELETE_TIME = 10
|
|
)
|
|
|
|
var (
|
|
zombies []uuid.UUID
|
|
hostZombies []uuid.UUID
|
|
newZombie chan uuid.UUID = make(chan (uuid.UUID), 10)
|
|
newHostZombie chan uuid.UUID = make(chan (uuid.UUID), 10)
|
|
)
|
|
|
|
// CheckZombies - checks if new node has same hostid as existing node
|
|
// if so, existing node is added to zombie node quarantine list
|
|
// also cleans up nodes past their expiration date
|
|
func CheckZombies(newnode *models.Node) {
|
|
nodes, err := GetNetworkNodes(newnode.Network)
|
|
if err != nil {
|
|
logger.Log(1, "Failed to retrieve network nodes", newnode.Network, err.Error())
|
|
return
|
|
}
|
|
for _, node := range nodes {
|
|
if node.ID == newnode.ID {
|
|
//skip self
|
|
continue
|
|
}
|
|
if node.HostID == newnode.HostID {
|
|
logger.Log(0, "adding ", node.ID.String(), " to zombie list")
|
|
newZombie <- node.ID
|
|
}
|
|
}
|
|
}
|
|
|
|
// checkForZombieHosts - checks if new host has the same macAddress as an existing host
|
|
// if true, existing host is added to host zombie collection
|
|
func checkForZombieHosts(h *schema.Host) {
|
|
hosts, err := (&schema.Host{}).ListAll(db.WithContext(context.TODO()))
|
|
if err != nil {
|
|
logger.Log(3, "error retrieving all hosts", err.Error())
|
|
}
|
|
for _, existing := range hosts {
|
|
if existing.ID == h.ID {
|
|
//probably an unnecessary check as new host should not be in database yet, but just in case
|
|
//skip self
|
|
continue
|
|
}
|
|
if existing.MacAddress.String() == h.MacAddress.String() {
|
|
//add to hostZombies
|
|
newHostZombie <- existing.ID
|
|
//add all nodes belonging to host to zombile list
|
|
for _, node := range existing.Nodes {
|
|
id, err := uuid.Parse(node)
|
|
if err != nil {
|
|
logger.Log(3, "error parsing uuid from host.Nodes", err.Error())
|
|
continue
|
|
}
|
|
newHostZombie <- id
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// ManageZombies - goroutine which adds/removes/deletes nodes from the zombie node quarantine list
|
|
func ManageZombies(ctx context.Context) {
|
|
logger.Log(2, "Zombie management started")
|
|
go InitializeZombies()
|
|
go checkPendingRemovalNodes()
|
|
// Zombie Nodes Cleanup Four Times a Day
|
|
ticker := time.NewTicker(time.Hour * ZOMBIE_TIMEOUT)
|
|
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
ticker.Stop()
|
|
close(DeleteNodesCh)
|
|
return
|
|
case id := <-newZombie:
|
|
zombies = append(zombies, id)
|
|
case id := <-newHostZombie:
|
|
hostZombies = append(hostZombies, id)
|
|
case <-ticker.C: // run this check 4 times a day
|
|
logger.Log(3, "checking for zombie nodes")
|
|
if len(zombies) > 0 {
|
|
for i := len(zombies) - 1; i >= 0; i-- {
|
|
node, err := GetNodeByID(zombies[i].String())
|
|
if err != nil {
|
|
logger.Log(1, "error retrieving zombie node", zombies[i].String(), err.Error())
|
|
logger.Log(1, "deleting ", node.ID.String(), " from zombie list")
|
|
zombies = append(zombies[:i], zombies[i+1:]...)
|
|
continue
|
|
}
|
|
if time.Since(node.LastCheckIn) > time.Minute*ZOMBIE_DELETE_TIME {
|
|
if err := DeleteNode(&node, true); err != nil {
|
|
logger.Log(1, "error deleting zombie node", zombies[i].String(), err.Error())
|
|
continue
|
|
}
|
|
node.PendingDelete = true
|
|
node.Action = models.NODE_DELETE
|
|
DeleteNodesCh <- &node
|
|
logger.Log(1, "deleting zombie node", node.ID.String())
|
|
zombies = append(zombies[:i], zombies[i+1:]...)
|
|
}
|
|
}
|
|
}
|
|
if len(hostZombies) > 0 {
|
|
logger.Log(3, "checking host zombies")
|
|
for i := len(hostZombies) - 1; i >= 0; i-- {
|
|
host := &schema.Host{ID: hostZombies[i]}
|
|
err := host.Get(db.WithContext(context.TODO()))
|
|
if err != nil {
|
|
logger.Log(1, "error retrieving zombie host", err.Error())
|
|
logger.Log(1, "deleting ", host.ID.String(), " from zombie list")
|
|
hostZombies = append(hostZombies[:i], hostZombies[i+1:]...)
|
|
continue
|
|
}
|
|
if len(host.Nodes) == 0 {
|
|
if err := RemoveHost(host, true); err != nil {
|
|
logger.Log(0, "error deleting zombie host", host.ID.String(), err.Error())
|
|
}
|
|
hostZombies = append(hostZombies[:i], hostZombies[i+1:]...)
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
func checkPendingRemovalNodes() {
|
|
nodes, _ := GetAllNodes()
|
|
for _, node := range nodes {
|
|
node := node
|
|
pendingDelete := node.PendingDelete || node.Action == models.NODE_DELETE
|
|
if pendingDelete {
|
|
DeleteNode(&node, true)
|
|
DeleteNodesCh <- &node
|
|
continue
|
|
}
|
|
}
|
|
}
|
|
|
|
// InitializeZombies - populates the zombie quarantine list (should be called from initialization)
|
|
func InitializeZombies() {
|
|
nodes, err := GetAllNodes()
|
|
if err != nil {
|
|
logger.Log(1, "failed to retrieve nodes", err.Error())
|
|
return
|
|
}
|
|
for _, node := range nodes {
|
|
othernodes, err := GetNetworkNodes(node.Network)
|
|
if err != nil {
|
|
logger.Log(1, "failled to retrieve nodes for network", node.Network, err.Error())
|
|
continue
|
|
}
|
|
for _, othernode := range othernodes {
|
|
if node.ID == othernode.ID {
|
|
continue
|
|
}
|
|
if node.HostID == othernode.HostID {
|
|
if node.LastCheckIn.After(othernode.LastCheckIn) {
|
|
newZombie <- othernode.ID
|
|
logger.Log(1, "adding", othernode.ID.String(), "to zombie list")
|
|
} else {
|
|
newZombie <- node.ID
|
|
logger.Log(1, "adding", node.ID.String(), "to zombie list")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|