From eb89a2220c548a96b63ae0f7941f41cc03bc800f Mon Sep 17 00:00:00 2001 From: GnomeZworc Date: Mon, 12 Oct 2026 13:05:24 +0200 Subject: [PATCH] =?UTF-8?q?f-41:=20l3vni:=20suppression=20d'une=20VPC=20ro?= =?UTF-8?q?ut=C3=A9e=20sans=20section=20bgp=20#41?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: GnomeZworc --- docs/exploitation/observabilite.rst | 3 ++- internal/dispatcher/agent/vpc_commands.go | 2 +- internal/frr/client.go | 4 ++-- internal/frr/client_test.go | 6 +++--- internal/frr/teardown.go | 21 ++++++++++++++++++--- internal/frr/teardown_test.go | 17 +++++++++++++++++ internal/l3vni/l3.go | 12 ++++-------- internal/l3vni/l3_test.go | 14 +++++++------- internal/vpc/delete.go | 5 ++--- 9 files changed, 56 insertions(+), 28 deletions(-) diff --git a/docs/exploitation/observabilite.rst b/docs/exploitation/observabilite.rst index d0b97e1..6602dfa 100644 --- a/docs/exploitation/observabilite.rst +++ b/docs/exploitation/observabilite.rst @@ -50,7 +50,8 @@ Avec une section ``bgp`` dans la configuration, il contrôle aussi le plan de co le propriétaire n'existe plus — ni ``running``, ni en ``creating``, ``deleting`` ou ``error`` — est signalée comme un **trou noir** : l'annonce attire du trafic que plus rien ne reçoit. -Un VPC routé alors que la section ``bgp`` a disparu de la configuration est signalé lui aussi. +Un VPC routé alors que la section ``bgp`` a disparu de la configuration est signalé lui aussi. Il +reste supprimable : le démontage lit l'AS de son instance dans la configuration courante de FRR. .. note:: diff --git a/internal/dispatcher/agent/vpc_commands.go b/internal/dispatcher/agent/vpc_commands.go index 5af1e5e..7404342 100644 --- a/internal/dispatcher/agent/vpc_commands.go +++ b/internal/dispatcher/agent/vpc_commands.go @@ -133,7 +133,7 @@ func (c DeleteVPCCommand) Execute(db *badger.DB, cfg *configuration.Config) erro case <-time.After(time.Duration(cfg.Dispatcher.PollSeconds) * time.Second): } } - if err := vpc.DeleteVPC(db, c.Name, cfg); err != nil { + if err := vpc.DeleteVPC(db, c.Name); err != nil { return err } current, err := state.Get(db, c.Key()) diff --git a/internal/frr/client.go b/internal/frr/client.go index 3f38204..d380e85 100644 --- a/internal/frr/client.go +++ b/internal/frr/client.go @@ -83,8 +83,8 @@ func (c *Client) RemoveVRF(v VRF) error { } func (c *Client) teardown(v VRF, plan func(VRF, string) []step) error { - if err := v.validateName(); err != nil { - return err + if v.ID == "" { + return fmt.Errorf("frr: vrf id must not be empty") } running, err := c.runningConfig() if err != nil { diff --git a/internal/frr/client_test.go b/internal/frr/client_test.go index ea68e1e..0f9ec9e 100644 --- a/internal/frr/client_test.go +++ b/internal/frr/client_test.go @@ -172,10 +172,10 @@ func TestRemoveVRFFailsWhenRoutingConfigurationIsStillThere(t *testing.T) { } } -func TestTeardownNeedsOnlyTheASNAndTheID(t *testing.T) { +func TestTeardownNeedsOnlyTheID(t *testing.T) { f := &fakeVtysh{running: []string{"", ""}} - if err := f.client().WithdrawVRF(VRF{ASN: 64600, ID: "a1b2"}); err != nil { - t.Errorf("démontage refusé : %v", err) + if err := f.client().WithdrawVRF(VRF{ID: "a1b2"}); err != nil { + t.Errorf("démontage sans asn refusé : %v", err) } f = &fakeVtysh{} if err := f.client().WithdrawVRF(VRF{ASN: 64600}); err == nil || len(f.calls) != 0 { diff --git a/internal/frr/teardown.go b/internal/frr/teardown.go index bcdd18d..fcbc407 100644 --- a/internal/frr/teardown.go +++ b/internal/frr/teardown.go @@ -2,6 +2,7 @@ package frr import ( "fmt" + "strconv" "strings" ) @@ -9,16 +10,17 @@ type step []string func routingTeardown(v VRF, running string) []step { lines := strings.Split(running, "\n") - bgp := hasLine(lines, v.bgpInstance()) + instance := vrfInstance(lines, v.Name()) + bgp := instance != "" var steps []step if bgp { - steps = append(steps, step{v.bgpInstance(), "address-family ipv4 unicast", "no redistribute kernel"}) + steps = append(steps, step{instance, "address-family ipv4 unicast", "no redistribute kernel"}) } if vni := vrfVNI(lines, v.Name()); vni != "" { steps = append(steps, step{"vrf " + v.Name(), "no vni " + vni}) } if bgp { - steps = append(steps, step{"no " + v.bgpInstance()}) + steps = append(steps, step{"no " + instance}) } if hasPrefix(lines, "route-map "+v.routeMap()+" ") { steps = append(steps, step{"no route-map " + v.routeMap()}) @@ -36,6 +38,19 @@ func vrfTeardown(v VRF, running string) []step { return nil } +func vrfInstance(lines []string, name string) string { + for _, l := range lines { + f := strings.Fields(l) + if len(f) != 5 || f[0] != "router" || f[1] != "bgp" || f[3] != "vrf" || f[4] != name { + continue + } + if _, err := strconv.ParseUint(f[2], 10, 32); err == nil { + return strings.Join(f, " ") + } + } + return "" +} + func hasLine(lines []string, want string) bool { for _, l := range lines { if strings.TrimRight(l, " \r") == want { diff --git a/internal/frr/teardown_test.go b/internal/frr/teardown_test.go index cd27049..210c943 100644 --- a/internal/frr/teardown_test.go +++ b/internal/frr/teardown_test.go @@ -97,6 +97,23 @@ func TestRoutingTeardownFollowsTheOrderFRRRequires(t *testing.T) { } } +func TestRoutingTeardownTakesTheASNOfTheInstanceFromFRR(t *testing.T) { + want := []step{ + {"router bgp 64600 vrf vrf-a1b2", "address-family ipv4 unicast", "no redistribute kernel"}, + {"vrf vrf-a1b2", "no vni 4001"}, + {"no router bgp 64600 vrf vrf-a1b2"}, + {"no route-map EXPORT-a1b2"}, + {"no ip prefix-list PL-a1b2"}, + } + for _, asn := range []uint32{0, 64601} { + v := labVRF(t) + v.ASN = asn + if got := routingTeardown(v, fullRunning); !reflect.DeepEqual(got, want) { + t.Errorf("asn %d :\n%q\nattendu :\n%q", asn, got, want) + } + } +} + func TestRoutingTeardownIgnoresAVRFWhoseNameExtendsOurs(t *testing.T) { if got := routingTeardown(labVRF(t), otherOnlyRunning); got != nil { t.Errorf("étapes visant vrf-a1b2c : %q", got) diff --git a/internal/l3vni/l3.go b/internal/l3vni/l3.go index f226ef2..f85b5a8 100644 --- a/internal/l3vni/l3.go +++ b/internal/l3vni/l3.go @@ -115,12 +115,8 @@ func createL3(h host, cfg *configuration.Config, v l3VPC) error { return nil } -func deleteL3(h host, cfg *configuration.Config, v l3VPC) error { - asn, err := bgpASN(cfg) - if err != nil { - return err - } - frrVRF := frr.VRF{ASN: asn, ID: v.id} +func deleteL3(h host, v l3VPC) error { + frrVRF := frr.VRF{ID: v.id} if err := h.WithdrawVRF(frrVRF); err != nil { return fmt.Errorf("l3vni %d: withdraw frr: %w", v.vni, err) } @@ -143,12 +139,12 @@ func Create(db *badger.DB, vpcName string, cfg *configuration.Config) error { return createL3(newSystem(), cfg, v) } -func Delete(db *badger.DB, vpcName string, cfg *configuration.Config) error { +func Delete(db *badger.DB, vpcName string) error { v, ok, err := loadRouted(db, vpcName) if err != nil || !ok { return err } - return deleteL3(newSystem(), cfg, v) + return deleteL3(newSystem(), v) } type Routing struct { diff --git a/internal/l3vni/l3_test.go b/internal/l3vni/l3_test.go index 5253986..230e196 100644 --- a/internal/l3vni/l3_test.go +++ b/internal/l3vni/l3_test.go @@ -173,16 +173,16 @@ func TestCreateL3StopsAtTheFirstFailure(t *testing.T) { func TestDeleteL3WithdrawsFRRThenTheKernelThenTheVRF(t *testing.T) { f := &fakeHost{} - if err := deleteL3(f, bgpConfig(), labL3(t)); err != nil { + if err := deleteL3(f, labL3(t)); err != nil { t.Fatal(err) } want := []string{ - "frr withdraw asn=64600 id=s7 vni=0", + "frr withdraw asn=0 id=s7 vni=0", "del l3-s7-o", "del vxl3-s7", "del brl3-s7", "del vrf-s7", - "frr remove asn=64600 id=s7 vni=0", + "frr remove asn=0 id=s7 vni=0", } if !reflect.DeepEqual(f.calls, want) { t.Errorf("appels :\n%s\nattendu :\n%s", strings.Join(f.calls, "\n"), strings.Join(want, "\n")) @@ -191,7 +191,7 @@ func TestDeleteL3WithdrawsFRRThenTheKernelThenTheVRF(t *testing.T) { func TestDeleteL3KeepsTheKernelWhenFRRRefusesTheWithdrawal(t *testing.T) { f := &fakeHost{fail: map[string]error{"frr withdraw": errors.New("% Please unconfigure l3vni 2700")}} - err := deleteL3(f, bgpConfig(), labL3(t)) + err := deleteL3(f, labL3(t)) if err == nil || !strings.Contains(err.Error(), "withdraw frr") { t.Errorf("erreur %v", err) } @@ -202,7 +202,7 @@ func TestDeleteL3KeepsTheKernelWhenFRRRefusesTheWithdrawal(t *testing.T) { func TestDeleteL3KeepsTheFRRVRFWhenAKernelLinkCannotBeDeleted(t *testing.T) { f := &fakeHost{fail: map[string]error{"del brl3-s7": errors.New("device busy")}} - if err := deleteL3(f, bgpConfig(), labL3(t)); err == nil || !strings.Contains(err.Error(), "delete brl3-s7") { + if err := deleteL3(f, labL3(t)); err == nil || !strings.Contains(err.Error(), "delete brl3-s7") { t.Errorf("erreur %v", err) } for _, c := range f.calls { @@ -212,9 +212,9 @@ func TestDeleteL3KeepsTheFRRVRFWhenAKernelLinkCannotBeDeleted(t *testing.T) { } } -func TestDeleteL3RequiresTheBGPSection(t *testing.T) { +func TestDeleteL3DoesNotDependOnTheAgentBGPSection(t *testing.T) { f := &fakeHost{} - if err := deleteL3(f, &configuration.Config{}, labL3(t)); err == nil || len(f.calls) != 0 { + if err := deleteL3(f, labL3(t)); err != nil || len(f.calls) != 6 { t.Errorf("erreur %v, appels %q", err, f.calls) } } diff --git a/internal/vpc/delete.go b/internal/vpc/delete.go index 700429c..d26a052 100644 --- a/internal/vpc/delete.go +++ b/internal/vpc/delete.go @@ -3,7 +3,6 @@ package vpc import ( "strings" - configuration "git.g3e.fr/syonad/two/internal/config/agent" "git.g3e.fr/syonad/two/internal/l3vni" "git.g3e.fr/syonad/two/internal/netif" "git.g3e.fr/syonad/two/internal/netns" @@ -12,13 +11,13 @@ import ( "github.com/dgraph-io/badger/v4" ) -func DeleteVPC(db *badger.DB, name string, cfg *configuration.Config) error { +func DeleteVPC(db *badger.DB, name string) error { if current, err := state.Get(db, "vpc/"+name); err != nil { return err } else if current == state.Deleting { vpcID := strings.SplitN(name, "-", 2)[1] - if err := l3vni.Delete(db, name, cfg); err != nil { + if err := l3vni.Delete(db, name); err != nil { return err }