Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport v1.14: ipsec: fixes for key rotation #27586

Merged
merged 2 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions daemon/cmd/datapath.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,9 @@ func setupIPSec() (int, uint8, error) {
if err != nil {
return 0, 0, err
}
if err := ipsec.SetIPSecSPI(spi); err != nil {
return 0, 0, err
}
node.SetIPsecKeyIdentity(spi)
return authKeySize, spi, nil
}
Expand Down
3 changes: 3 additions & 0 deletions daemon/cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,9 @@ func (c *clusterNodesClient) NodeDelete(node nodeTypes.Node) error {
return nil
}

func (c *clusterNodesClient) AllNodeValidateImplementation() {
}

func (c *clusterNodesClient) NodeValidateImplementation(node nodeTypes.Node) error {
// no-op
return nil
Expand Down
3 changes: 3 additions & 0 deletions pkg/auth/authmap_gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ func (r *authMapGarbageCollector) NodeDelete(deletedNode nodeTypes.Node) error {
return nil
}

func (r *authMapGarbageCollector) AllNodeValidateImplementation() {
}

func (r *authMapGarbageCollector) NodeValidateImplementation(node nodeTypes.Node) error {
return nil
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/datapath/fake/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ func (n *FakeNodeHandler) NodeDelete(node nodeTypes.Node) error {
return nil
}

func (n *FakeNodeHandler) AllNodeValidateImplementation() {
}

func (n *FakeNodeHandler) NodeValidateImplementation(node nodeTypes.Node) error {
return nil
}
Expand Down
21 changes: 16 additions & 5 deletions pkg/datapath/linux/ipsec/ipsec_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,6 @@ func LoadIPSecKeysFile(path string) (int, uint8, error) {
func loadIPSecKeys(r io.Reader) (int, uint8, error) {
var spi uint8
var keyLen int
scopedLog := log

ipSecLock.Lock()
defer ipSecLock.Unlock()
Expand Down Expand Up @@ -879,11 +878,17 @@ func loadIPSecKeys(r io.Reader) (int, uint8, error) {
ipSecKeysRemovalTime[oldSpi] = time.Now()
ipSecCurrentKeySPI = spi
}
return keyLen, spi, nil
}

func SetIPSecSPI(spi uint8) error {
scopedLog := log

if err := encrypt.MapUpdateContext(0, spi); err != nil {
scopedLog.WithError(err).Warn("cilium_encrypt_state map updated failed:")
return 0, 0, err
return err
}
return keyLen, spi, nil
return nil
}

// DeleteIPsecEncryptRoute removes nodes in main routing table by walking
Expand Down Expand Up @@ -927,15 +932,21 @@ func keyfileWatcher(ctx context.Context, watcher *fswatcher.Watcher, keyfilePath
// package
node.SetIPsecKeyIdentity(spi)

// NodeValidateImplementation will eventually call
// AllNodeValidateImplementation will eventually call
// nodeUpdate(), which is responsible for updating the
// IPSec policies and states for all the different EPs
// with ipsec.UpsertIPsecEndpoint()
nodeHandler.NodeValidateImplementation(*nodediscovery.LocalNode())
nodeHandler.AllNodeValidateImplementation()

// Publish the updated node information to k8s/KVStore
nodediscovery.UpdateLocalNode()

// Push SPI update into BPF datapath now that XFRM state
// is configured.
if err := SetIPSecSPI(spi); err != nil {
log.WithError(err).Errorf("Failed to set IPsec SPI")
continue
}
case err := <-watcher.Errors:
log.WithError(err).WithField(logfields.Path, keyfilePath).
Warning("Error encountered while watching file with fsnotify")
Expand Down
8 changes: 6 additions & 2 deletions pkg/datapath/linux/ipsec/ipsec_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,14 @@ func (p *IPSecSuitePrivileged) TestInvalidLoadKeys(c *C) {

func (p *IPSecSuitePrivileged) TestLoadKeys(c *C) {
keys := bytes.NewReader(keysDat)
_, _, err := loadIPSecKeys(keys)
_, spi, err := loadIPSecKeys(keys)
c.Assert(err, IsNil)
err = SetIPSecSPI(spi)
c.Assert(err, IsNil)
keys = bytes.NewReader(keysAeadDat)
_, _, err = loadIPSecKeys(keys)
_, spi, err = loadIPSecKeys(keys)
c.Assert(err, IsNil)
err = SetIPSecSPI(spi)
c.Assert(err, IsNil)
}

Expand Down
10 changes: 10 additions & 0 deletions pkg/datapath/linux/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -1753,6 +1753,16 @@ func (n *linuxNodeHandler) NodeValidateImplementation(nodeToValidate nodeTypes.N
return n.nodeUpdate(nil, &nodeToValidate, false)
}

// AllNodeValidateImplementation is called to validate the implementation of the
// node in the datapath for all existing nodes
func (n *linuxNodeHandler) AllNodeValidateImplementation() {
n.mutex.Lock()
defer n.mutex.Unlock()
for _, updateNode := range n.nodes {
n.nodeUpdate(nil, updateNode, false)
}
}

// NodeNeighDiscoveryEnabled returns whether node neighbor discovery is enabled
func (n *linuxNodeHandler) NodeNeighDiscoveryEnabled() bool {
return n.enableNeighDiscovery
Expand Down
4 changes: 4 additions & 0 deletions pkg/datapath/types/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ type NodeHandler interface {
// NodeDelete is called after a node has been deleted
NodeDelete(node nodeTypes.Node) error

// AllNodeValidateImplementation is called to validate the implementation
// of all nodes in the node cache.
AllNodeValidateImplementation()

// NodeValidateImplementation is called to validate the implementation of
// the node in the datapath. This function is intended to be run on an
// interval to ensure that the datapath is consistently converged.
Expand Down
4 changes: 4 additions & 0 deletions pkg/hubble/peer/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ func (h *handler) NodeDelete(n types.Node) error {
return nil
}

// AllNodeValidateImplementation implements
func (h handler) AllNodeValidateImplementation() {
}

// NodeValidateImplementation implements
// datapath.NodeHandler.NodeValidateImplementation. It is a no-op.
func (h handler) NodeValidateImplementation(_ types.Node) error {
Expand Down
3 changes: 3 additions & 0 deletions pkg/node/manager/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ func (n *signalNodeHandler) NodeDelete(node nodeTypes.Node) error {
return nil
}

func (n *signalNodeHandler) AllNodeValidateImplementation() {
}

func (n *signalNodeHandler) NodeValidateImplementation(node nodeTypes.Node) error {
if n.EnableNodeValidateImplementationEvent {
n.NodeValidateImplementationEvent <- node
Expand Down
5 changes: 5 additions & 0 deletions pkg/wireguard/agent/node_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ func (a *Agent) NodeDelete(node nodeTypes.Node) error {
return a.DeletePeer(node.Fullname())
}

// AllNodeValidateImplementation is called to validate the implementation of
// all nodes in the datapath.
func (a *Agent) AllNodeValidateImplementation() {
}

// NodeValidateImplementation is called to validate the implementation of
// the node in the datapath. This function is intended to be run on an
// interval to ensure that the datapath is consistently converged.
Expand Down