fix hostinfo db column spelling (#1642)

This commit is contained in:
Kristoffer Dalby 2023-12-10 15:22:26 +01:00 committed by GitHub
parent 2c8fc9b061
commit 790bbe5e8d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -12,6 +12,7 @@ import (
v1 "github.com/juanfont/headscale/gen/go/headscale/v1" v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
"github.com/juanfont/headscale/hscontrol/policy/matcher" "github.com/juanfont/headscale/hscontrol/policy/matcher"
"github.com/rs/zerolog/log"
"go4.org/netipx" "go4.org/netipx"
"google.golang.org/protobuf/types/known/timestamppb" "google.golang.org/protobuf/types/known/timestamppb"
"gorm.io/gorm" "gorm.io/gorm"
@ -62,7 +63,7 @@ type Node struct {
// it is _only_ used for reading and writing the key to the // it is _only_ used for reading and writing the key to the
// database and should not be used. // database and should not be used.
// Use Endpoints instead. // Use Endpoints instead.
HostinfoDatabaseField string `gorm:"column:hostinfo"` HostinfoDatabaseField string `gorm:"column:host_info"`
Hostinfo *tailcfg.Hostinfo `gorm:"-"` Hostinfo *tailcfg.Hostinfo `gorm:"-"`
IPAddresses NodeAddresses IPAddresses NodeAddresses
@ -241,25 +242,25 @@ func (nodes Nodes) FilterByIP(ip netip.Addr) Nodes {
// cannot be directly marshalled into database values are stored // cannot be directly marshalled into database values are stored
// correctly in the database. // correctly in the database.
// This currently means storing the keys as strings. // This currently means storing the keys as strings.
func (n *Node) BeforeSave(tx *gorm.DB) (err error) { func (node *Node) BeforeSave(tx *gorm.DB) error {
n.MachineKeyDatabaseField = n.MachineKey.String() node.MachineKeyDatabaseField = node.MachineKey.String()
n.NodeKeyDatabaseField = n.NodeKey.String() node.NodeKeyDatabaseField = node.NodeKey.String()
n.DiscoKeyDatabaseField = n.DiscoKey.String() node.DiscoKeyDatabaseField = node.DiscoKey.String()
var endpoints StringList var endpoints StringList
for _, addrPort := range n.Endpoints { for _, addrPort := range node.Endpoints {
endpoints = append(endpoints, addrPort.String()) endpoints = append(endpoints, addrPort.String())
} }
n.EndpointsDatabaseField = endpoints node.EndpointsDatabaseField = endpoints
hi, err := json.Marshal(n.Hostinfo) hi, err := json.Marshal(node.Hostinfo)
if err != nil { if err != nil {
return fmt.Errorf("failed to marshal Hostinfo to store in db: %w", err) return fmt.Errorf("failed to marshal Hostinfo to store in db: %w", err)
} }
n.HostinfoDatabaseField = string(hi) node.HostinfoDatabaseField = string(hi)
return return nil
} }
// AfterFind is a hook that ensures that Node objects fields that // AfterFind is a hook that ensures that Node objects fields that
@ -267,43 +268,45 @@ func (n *Node) BeforeSave(tx *gorm.DB) (err error) {
// correctly. // correctly.
// This currently unmarshals all the keys, stored as strings, into // This currently unmarshals all the keys, stored as strings, into
// the proper types. // the proper types.
func (n *Node) AfterFind(tx *gorm.DB) (err error) { func (node *Node) AfterFind(tx *gorm.DB) error {
var machineKey key.MachinePublic var machineKey key.MachinePublic
if err := machineKey.UnmarshalText([]byte(n.MachineKeyDatabaseField)); err != nil { if err := machineKey.UnmarshalText([]byte(node.MachineKeyDatabaseField)); err != nil {
return fmt.Errorf("failed to unmarshal machine key from db: %w", err) return fmt.Errorf("failed to unmarshal machine key from db: %w", err)
} }
n.MachineKey = machineKey node.MachineKey = machineKey
var nodeKey key.NodePublic var nodeKey key.NodePublic
if err := nodeKey.UnmarshalText([]byte(n.NodeKeyDatabaseField)); err != nil { if err := nodeKey.UnmarshalText([]byte(node.NodeKeyDatabaseField)); err != nil {
return fmt.Errorf("failed to unmarshal node key from db: %w", err) return fmt.Errorf("failed to unmarshal node key from db: %w", err)
} }
n.NodeKey = nodeKey node.NodeKey = nodeKey
var discoKey key.DiscoPublic var discoKey key.DiscoPublic
if err := discoKey.UnmarshalText([]byte(n.DiscoKeyDatabaseField)); err != nil { if err := discoKey.UnmarshalText([]byte(node.DiscoKeyDatabaseField)); err != nil {
return fmt.Errorf("failed to unmarshal disco key from db: %w", err) return fmt.Errorf("failed to unmarshal disco key from db: %w", err)
} }
n.DiscoKey = discoKey node.DiscoKey = discoKey
var endpoints []netip.AddrPort endpoints := make([]netip.AddrPort, len(node.EndpointsDatabaseField))
for _, ep := range n.EndpointsDatabaseField { for idx, ep := range node.EndpointsDatabaseField {
addrPort, err := netip.ParseAddrPort(ep) addrPort, err := netip.ParseAddrPort(ep)
if err != nil { if err != nil {
return fmt.Errorf("failed to parse endpoint from db: %w", err) return fmt.Errorf("failed to parse endpoint from db: %w", err)
} }
endpoints = append(endpoints, addrPort) endpoints[idx] = addrPort
} }
n.Endpoints = endpoints node.Endpoints = endpoints
var hi tailcfg.Hostinfo var hi tailcfg.Hostinfo
if err := json.Unmarshal([]byte(n.HostinfoDatabaseField), &hi); err != nil { if err := json.Unmarshal([]byte(node.HostinfoDatabaseField), &hi); err != nil {
log.Trace().Err(err).Msgf("Hostinfo content: %s", node.HostinfoDatabaseField)
return fmt.Errorf("failed to unmarshal Hostinfo from db: %w", err) return fmt.Errorf("failed to unmarshal Hostinfo from db: %w", err)
} }
n.Hostinfo = &hi node.Hostinfo = &hi
return return nil
} }
func (node *Node) Proto() *v1.Node { func (node *Node) Proto() *v1.Node {