Skip to content

Commit

Permalink
make sure list of authorized peers is unique (#159)
Browse files Browse the repository at this point in the history
  • Loading branch information
ehsan6sha committed Jul 22, 2023
1 parent 525fcdc commit 9bea4ee
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
21 changes: 18 additions & 3 deletions cmd/blox/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions exchange/fx_exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 9bea4ee

Please sign in to comment.