mirror of
https://github.com/juanfont/headscale.git
synced 2024-12-25 21:55:52 -05:00
Check all errors for db.Save
This commit is contained in:
parent
405de9e0f8
commit
52cc3bc8eb
@ -57,7 +57,10 @@ func (h *Headscale) CreateAPIKey(
|
|||||||
Hash: hash,
|
Hash: hash,
|
||||||
Expiration: expiration,
|
Expiration: expiration,
|
||||||
}
|
}
|
||||||
h.db.Save(&key)
|
|
||||||
|
if err := h.db.Save(&key).Error; err != nil {
|
||||||
|
return "", nil, fmt.Errorf("failed to save API key to database: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
return keyStr, &key, nil
|
return keyStr, &key, nil
|
||||||
}
|
}
|
||||||
|
32
machine.go
32
machine.go
@ -367,23 +367,30 @@ func (h *Headscale) SetTags(machine *Machine, tags []string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
h.setLastStateChangeToNow(machine.Namespace.Name)
|
h.setLastStateChangeToNow(machine.Namespace.Name)
|
||||||
h.db.Save(machine)
|
|
||||||
|
if err := h.db.Save(machine).Error; err != nil {
|
||||||
|
return fmt.Errorf("failed to update tags for machine in the database: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ExpireMachine takes a Machine struct and sets the expire field to now.
|
// ExpireMachine takes a Machine struct and sets the expire field to now.
|
||||||
func (h *Headscale) ExpireMachine(machine *Machine) {
|
func (h *Headscale) ExpireMachine(machine *Machine) error {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
machine.Expiry = &now
|
machine.Expiry = &now
|
||||||
|
|
||||||
h.setLastStateChangeToNow(machine.Namespace.Name)
|
h.setLastStateChangeToNow(machine.Namespace.Name)
|
||||||
|
|
||||||
h.db.Save(machine)
|
if err := h.db.Save(machine).Error; err != nil {
|
||||||
|
return fmt.Errorf("failed to expire machine in the database: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// RefreshMachine takes a Machine struct and sets the expire field to now.
|
// RefreshMachine takes a Machine struct and sets the expire field.
|
||||||
func (h *Headscale) RefreshMachine(machine *Machine, expiry time.Time) {
|
func (h *Headscale) RefreshMachine(machine *Machine, expiry time.Time) error {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
|
|
||||||
machine.LastSuccessfulUpdate = &now
|
machine.LastSuccessfulUpdate = &now
|
||||||
@ -391,7 +398,11 @@ func (h *Headscale) RefreshMachine(machine *Machine, expiry time.Time) {
|
|||||||
|
|
||||||
h.setLastStateChangeToNow(machine.Namespace.Name)
|
h.setLastStateChangeToNow(machine.Namespace.Name)
|
||||||
|
|
||||||
h.db.Save(machine)
|
if err := h.db.Save(machine).Error; err != nil {
|
||||||
|
return fmt.Errorf("failed to refresh machine (update expiration) in the database: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteMachine softs deletes a Machine from the database.
|
// DeleteMachine softs deletes a Machine from the database.
|
||||||
@ -761,7 +772,9 @@ func (h *Headscale) RegisterMachine(machine Machine,
|
|||||||
|
|
||||||
machine.IPAddresses = ips
|
machine.IPAddresses = ips
|
||||||
|
|
||||||
h.db.Save(&machine)
|
if err := h.db.Save(machine).Error; err != nil {
|
||||||
|
return nil, fmt.Errorf("failed register(save) machine in the database: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
log.Trace().
|
log.Trace().
|
||||||
Caller().
|
Caller().
|
||||||
@ -821,7 +834,10 @@ func (h *Headscale) EnableRoutes(machine *Machine, routeStrs ...string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
machine.EnabledRoutes = newRoutes
|
machine.EnabledRoutes = newRoutes
|
||||||
h.db.Save(&machine)
|
|
||||||
|
if err := h.db.Save(machine).Error; err != nil {
|
||||||
|
return fmt.Errorf("failed enable routes for machine in the database: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ import (
|
|||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -60,7 +61,10 @@ func (h *Headscale) CreatePreAuthKey(
|
|||||||
CreatedAt: &now,
|
CreatedAt: &now,
|
||||||
Expiration: expiration,
|
Expiration: expiration,
|
||||||
}
|
}
|
||||||
h.db.Save(&key)
|
|
||||||
|
if err := h.db.Save(&key).Error; err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to create key in the database: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
return &key, nil
|
return &key, nil
|
||||||
}
|
}
|
||||||
@ -114,9 +118,13 @@ func (h *Headscale) ExpirePreAuthKey(k *PreAuthKey) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// UsePreAuthKey marks a PreAuthKey as used.
|
// UsePreAuthKey marks a PreAuthKey as used.
|
||||||
func (h *Headscale) UsePreAuthKey(k *PreAuthKey) {
|
func (h *Headscale) UsePreAuthKey(k *PreAuthKey) error {
|
||||||
k.Used = true
|
k.Used = true
|
||||||
h.db.Save(k)
|
if err := h.db.Save(k).Error; err != nil {
|
||||||
|
return fmt.Errorf("failed to update key used status in the database: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// checkKeyValidity does the heavy lifting for validation of the PreAuthKey coming from a node
|
// checkKeyValidity does the heavy lifting for validation of the PreAuthKey coming from a node
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
package headscale
|
package headscale
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"inet.af/netaddr"
|
"inet.af/netaddr"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -108,7 +110,10 @@ func (h *Headscale) EnableNodeRoute(
|
|||||||
}
|
}
|
||||||
|
|
||||||
machine.EnabledRoutes = enabledRoutes
|
machine.EnabledRoutes = enabledRoutes
|
||||||
h.db.Save(&machine)
|
|
||||||
|
if err := h.db.Save(&machine).Error; err != nil {
|
||||||
|
return fmt.Errorf("failed to update node routes in the database: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user