Add error handling to ParseDump
This commit is contained in:
parent
32b0b94f6c
commit
c786fca372
@ -460,7 +460,12 @@ func (m *Mesh) applyTopology() {
|
|||||||
m.errorCounter.WithLabelValues("apply").Inc()
|
m.errorCounter.WithLabelValues("apply").Inc()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
oldConf := wireguard.ParseDump(oldConfDump)
|
oldConf, err := wireguard.ParseDump(oldConfDump)
|
||||||
|
if err != nil {
|
||||||
|
level.Error(m.logger).Log("error", err)
|
||||||
|
m.errorCounter.WithLabelValues("apply").Inc()
|
||||||
|
return
|
||||||
|
}
|
||||||
natEndpoints := discoverNATEndpoints(nodes, peers, oldConf, m.logger)
|
natEndpoints := discoverNATEndpoints(nodes, peers, oldConf, m.logger)
|
||||||
nodes[m.hostname].DiscoveredEndpoints = natEndpoints
|
nodes[m.hostname].DiscoveredEndpoints = natEndpoints
|
||||||
t, err := NewTopology(nodes, peers, m.granularity, m.hostname, nodes[m.hostname].Endpoint.Port, m.priv, m.subnet, nodes[m.hostname].PersistentKeepalive, m.logger)
|
t, err := NewTopology(nodes, peers, m.granularity, m.hostname, nodes[m.hostname].Endpoint.Port, m.priv, m.subnet, nodes[m.hostname].PersistentKeepalive, m.logger)
|
||||||
|
@ -512,7 +512,7 @@ func (p *Peer) parseAllowedIPs(v string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ParseDump parses a given WireGuard dump and produces a Conf struct.
|
// ParseDump parses a given WireGuard dump and produces a Conf struct.
|
||||||
func ParseDump(buf []byte) *Conf {
|
func ParseDump(buf []byte) (*Conf, error) {
|
||||||
// from man wg, show section:
|
// from man wg, show section:
|
||||||
// If dump is specified, then several lines are printed;
|
// If dump is specified, then several lines are printed;
|
||||||
// the first contains in order separated by tab: private-key, public-key, listen-port, fw‐mark.
|
// the first contains in order separated by tab: private-key, public-key, listen-port, fw‐mark.
|
||||||
@ -528,6 +528,7 @@ func ParseDump(buf []byte) *Conf {
|
|||||||
port uint64
|
port uint64
|
||||||
sec int64
|
sec int64
|
||||||
pka int
|
pka int
|
||||||
|
line int
|
||||||
)
|
)
|
||||||
// First line is Interface
|
// First line is Interface
|
||||||
active = interfaceSection
|
active = interfaceSection
|
||||||
@ -538,7 +539,7 @@ func ParseDump(buf []byte) *Conf {
|
|||||||
switch active {
|
switch active {
|
||||||
case interfaceSection:
|
case interfaceSection:
|
||||||
if len(values) < dumpInterfaceLen {
|
if len(values) < dumpInterfaceLen {
|
||||||
break
|
return nil, fmt.Errorf("invalid interface line: missing fields (%d < %d)", len(values), dumpInterfaceLen)
|
||||||
}
|
}
|
||||||
iface = new(Interface)
|
iface = new(Interface)
|
||||||
for i := range values {
|
for i := range values {
|
||||||
@ -548,7 +549,7 @@ func ParseDump(buf []byte) *Conf {
|
|||||||
case dumpInterfaceListenPortIndex:
|
case dumpInterfaceListenPortIndex:
|
||||||
port, err = strconv.ParseUint(values[i], 10, 32)
|
port, err = strconv.ParseUint(values[i], 10, 32)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
return nil, fmt.Errorf("invalid interface line: error parsing listen-port: %w", err)
|
||||||
}
|
}
|
||||||
iface.ListenPort = uint32(port)
|
iface.ListenPort = uint32(port)
|
||||||
}
|
}
|
||||||
@ -558,7 +559,7 @@ func ParseDump(buf []byte) *Conf {
|
|||||||
active = peerSection
|
active = peerSection
|
||||||
case peerSection:
|
case peerSection:
|
||||||
if len(values) < dumpPeerLen {
|
if len(values) < dumpPeerLen {
|
||||||
break
|
return nil, fmt.Errorf("invalid peer line %d: missing fields (%d < %d)", line, len(values), dumpPeerLen)
|
||||||
}
|
}
|
||||||
peer = new(Peer)
|
peer = new(Peer)
|
||||||
|
|
||||||
@ -575,12 +576,18 @@ func ParseDump(buf []byte) *Conf {
|
|||||||
if values[i] == dumpNone {
|
if values[i] == dumpNone {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
peer.parseEndpoint(values[i])
|
err = peer.parseEndpoint(values[i])
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("invalid peer line %d: error parsing endpoint: %w", line, err)
|
||||||
|
}
|
||||||
case dumpPeerAllowedIPsIndex:
|
case dumpPeerAllowedIPsIndex:
|
||||||
if values[i] == dumpNone {
|
if values[i] == dumpNone {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
peer.parseAllowedIPs(values[i])
|
err = peer.parseAllowedIPs(values[i])
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("invalid peer line %d: error parsing allowed-ips: %w", line, err)
|
||||||
|
}
|
||||||
case dumpPeerLatestHandshakeIndex:
|
case dumpPeerLatestHandshakeIndex:
|
||||||
if values[i] == "0" {
|
if values[i] == "0" {
|
||||||
// Use go zero value, not unix 0 timestamp.
|
// Use go zero value, not unix 0 timestamp.
|
||||||
@ -589,7 +596,7 @@ func ParseDump(buf []byte) *Conf {
|
|||||||
}
|
}
|
||||||
sec, err = strconv.ParseInt(values[i], 10, 64)
|
sec, err = strconv.ParseInt(values[i], 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
return nil, fmt.Errorf("invalid peer line %d: error parsing latest-handshake: %w", line, err)
|
||||||
}
|
}
|
||||||
peer.LatestHandshake = time.Unix(sec, 0)
|
peer.LatestHandshake = time.Unix(sec, 0)
|
||||||
case dumpPeerPersistentKeepaliveIndex:
|
case dumpPeerPersistentKeepaliveIndex:
|
||||||
@ -598,7 +605,7 @@ func ParseDump(buf []byte) *Conf {
|
|||||||
}
|
}
|
||||||
pka, err = strconv.Atoi(values[i])
|
pka, err = strconv.Atoi(values[i])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
return nil, fmt.Errorf("invalid peer line %d: error parsing persistent-keepalive: %w", line, err)
|
||||||
}
|
}
|
||||||
peer.PersistentKeepalive = pka
|
peer.PersistentKeepalive = pka
|
||||||
}
|
}
|
||||||
@ -606,6 +613,7 @@ func ParseDump(buf []byte) *Conf {
|
|||||||
c.Peers = append(c.Peers, peer)
|
c.Peers = append(c.Peers, peer)
|
||||||
peer = nil
|
peer = nil
|
||||||
}
|
}
|
||||||
|
line++
|
||||||
}
|
}
|
||||||
return &c
|
return &c, nil
|
||||||
}
|
}
|
||||||
|
@ -345,7 +345,7 @@ key2 (none) 10.254.2.1:51820 100.64.4.0/24,10.69.76.55/32,100.64.3.0/24,10.66.25
|
|||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
|
|
||||||
dumpConf := ParseDump(tc.d)
|
dumpConf, _ := ParseDump(tc.d)
|
||||||
conf := Parse(tc.c)
|
conf := Parse(tc.c)
|
||||||
// Equal will ignore runtime fields and only compare configuration fields.
|
// Equal will ignore runtime fields and only compare configuration fields.
|
||||||
if !dumpConf.Equal(conf) {
|
if !dumpConf.Equal(conf) {
|
||||||
|
Loading…
Reference in New Issue
Block a user