peerguardian.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. Copyright © 2021-2022 Ettore Di Giacinto <[email protected]>
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package trustzone
  14. import (
  15. "context"
  16. "time"
  17. "github.com/ipfs/go-log"
  18. "github.com/mudler/edgevpn/pkg/blockchain"
  19. "github.com/mudler/edgevpn/pkg/hub"
  20. "github.com/mudler/edgevpn/pkg/node"
  21. "github.com/mudler/edgevpn/pkg/protocol"
  22. )
  23. // PeerGuardian provides auth for peers from blockchain data
  24. type PeerGuardian struct {
  25. authProviders []AuthProvider
  26. logger log.StandardLogger
  27. }
  28. func NewPeerGuardian(logger log.StandardLogger, authProviders ...AuthProvider) *PeerGuardian {
  29. return &PeerGuardian{
  30. authProviders: authProviders,
  31. logger: logger,
  32. }
  33. }
  34. // ReceiveMessage is a GenericHandler for public channel to provide authentication.
  35. // We receive messages here and we select them based on 2 criterias:
  36. // - messages that are supposed to generate challenges for auth mechanisms.
  37. // Auth mechanisms should get user auth data from a special TZ dedicated to hashes that are manually added
  38. // - messages that are answers to such challenges and then means that the sender.ID should be added to the trust zone
  39. func (pg *PeerGuardian) ReceiveMessage(l *blockchain.Ledger, m *hub.Message, c chan *hub.Message) error {
  40. pg.logger.Debug("Peerguardian received message from", m.SenderID)
  41. for _, a := range pg.authProviders {
  42. _, exists := l.GetKey(protocol.TrustZoneKey, m.SenderID)
  43. trustAuth := l.CurrentData()[protocol.TrustZoneAuthKey]
  44. if !exists && a.Authenticate(m, c, trustAuth) {
  45. // try to authenticate it
  46. // Note we can also not be in a TZ here as we are not able to check (we miss node information at hand)
  47. // In any way nodes would ignore the messages, and that we hit Authenticate is useful for two (or more)
  48. // steps authenticators.
  49. l.Persist(context.Background(), 5*time.Second, 120*time.Second, protocol.TrustZoneKey, m.SenderID, "")
  50. return nil
  51. }
  52. }
  53. return nil
  54. }
  55. // Challenger is a NetworkService that should send challenges with all enabled authenticators until we are in TZ
  56. // note that might never happen as node might not have a satisfying authentication mechanism
  57. func (pg *PeerGuardian) Challenger(duration time.Duration, autocleanup bool) node.NetworkService {
  58. return func(ctx context.Context, c node.Config, n *node.Node, b *blockchain.Ledger) error {
  59. b.Announce(ctx, duration, func() {
  60. trustAuth := b.CurrentData()[protocol.TrustZoneAuthKey]
  61. _, exists := b.GetKey(protocol.TrustZoneKey, n.Host().ID().String())
  62. for _, a := range pg.authProviders {
  63. a.Challenger(exists, c, n, b, trustAuth)
  64. }
  65. // Automatically cleanup TZ from peers not anymore in the hub
  66. if autocleanup {
  67. peers, err := n.MessageHub.ListPeers()
  68. if err != nil {
  69. return
  70. }
  71. tz := b.CurrentData()[protocol.TrustZoneKey]
  72. for k, _ := range tz {
  73. PEER:
  74. for _, p := range peers {
  75. if p.String() == k {
  76. break PEER
  77. }
  78. }
  79. b.Delete(protocol.TrustZoneKey, k)
  80. }
  81. }
  82. })
  83. return nil
  84. }
  85. }
  86. // AuthProvider is a generic Blockchain authentity provider
  87. type AuthProvider interface {
  88. // Authenticate either generates challanges to pick up later or authenticates a node
  89. // from a message with the available auth data in the blockchain
  90. Authenticate(*hub.Message, chan *hub.Message, map[string]blockchain.Data) bool
  91. Challenger(inTrustZone bool, c node.Config, n *node.Node, b *blockchain.Ledger, trustData map[string]blockchain.Data)
  92. }