From 733b05b83185d3c1dd0c783462d91a10400c0ae2 Mon Sep 17 00:00:00 2001 From: ISHIDA Wataru Date: Wed, 20 Apr 2016 08:39:13 +0000 Subject: policy: don't allow same policy assigned twice Signed-off-by: ISHIDA Wataru --- server/server.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'server/server.go') diff --git a/server/server.go b/server/server.go index 0a447783..5f6068a5 100644 --- a/server/server.go +++ b/server/server.go @@ -2830,20 +2830,34 @@ func (server *BgpServer) handleGrpcModPolicyAssignment(grpcReq *GrpcRequest) err return err } ps := make([]*table.Policy, 0, len(assignment.Policies)) + seen := make(map[string]bool) for _, x := range assignment.Policies { p, ok := server.policy.PolicyMap[x.Name] if !ok { return fmt.Errorf("not found policy %s", x.Name) } + if seen[x.Name] { + return fmt.Errorf("duplicated policy %s", x.Name) + } + seen[x.Name] = true ps = append(ps, p) } cur := server.policy.GetPolicy(id, dir) + switch arg.Operation { case api.Operation_ADD, api.Operation_REPLACE: if arg.Operation == api.Operation_REPLACE || cur == nil { err = server.policy.SetPolicy(id, dir, ps) } else { - err = server.policy.SetPolicy(id, dir, append(cur, ps...)) + seen = make(map[string]bool) + ps = append(cur, ps...) + for _, x := range ps { + if seen[x.Name()] { + return fmt.Errorf("duplicated policy %s", x.Name()) + } + seen[x.Name()] = true + } + err = server.policy.SetPolicy(id, dir, ps) } if err != nil { return err -- cgit v1.2.3