From 99fac1379f1ed4a17a0d8f506e71e3be3d8e06f0 Mon Sep 17 00:00:00 2001 From: ehsan shariati Date: Thu, 20 Jul 2023 09:44:29 -0400 Subject: [PATCH] make sure list of authorized peers is unique --- cmd/blox/main.go | 21 ++++++++++++++++++--- exchange/fx_exchange.go | 1 + 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/cmd/blox/main.go b/cmd/blox/main.go index 3273d3c9..9077ade5 100644 --- a/cmd/blox/main.go +++ b/cmd/blox/main.go @@ -263,12 +263,27 @@ func updateConfig(p []peer.ID) error { return err } + // Create a map to hold unique peer IDs + uniquePeers := make(map[string]bool) + + // Add existing AuthorizedPeers to the map + for _, pidStr := range app.config.AuthorizedPeers { + uniquePeers[pidStr] = true + } + // Convert the slice of peer.ID to a slice of strings - app.config.AuthorizedPeers = make([]string, len(p)) - for i, pid := range p { - app.config.AuthorizedPeers[i] = pid.String() + for _, pid := range p { + // Convert the peer.ID to string + pidStr := pid.String() + // Check if the peer.ID is already in the map + if !uniquePeers[pidStr] { + // If it's not in the map, add it to the map and the slice + uniquePeers[pidStr] = true + app.config.AuthorizedPeers = append(app.config.AuthorizedPeers, pidStr) + } } + logger.Infof("Authorized peers: %v", app.config.AuthorizedPeers) // Write back the updated config to the file configData, err = yaml.Marshal(app.config) if err != nil { diff --git a/exchange/fx_exchange.go b/exchange/fx_exchange.go index 7513c7bb..b04d3e3c 100644 --- a/exchange/fx_exchange.go +++ b/exchange/fx_exchange.go @@ -280,6 +280,7 @@ func (e *FxExchange) handleAuthorization(from peer.ID, w http.ResponseWriter, r } else { delete(e.authorizedPeers, a.Subject) } + log.Infow("Authorizing peers for ", "a.Subject", a.Subject, "from", from) e.authorizedPeersLock.Unlock() if err := e.updateAuthorizePeers(ctx); err != nil { log.Errorw("failed to update authorized peers", "err", err)