trustzone_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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_test
  14. import (
  15. "context"
  16. "fmt"
  17. "time"
  18. "github.com/ipfs/go-log"
  19. "github.com/libp2p/go-libp2p"
  20. connmanager "github.com/libp2p/go-libp2p/p2p/net/connmgr"
  21. . "github.com/onsi/ginkgo/v2"
  22. . "github.com/onsi/gomega"
  23. "github.com/mudler/edgevpn/pkg/blockchain"
  24. "github.com/mudler/edgevpn/pkg/logger"
  25. node "github.com/mudler/edgevpn/pkg/node"
  26. "github.com/mudler/edgevpn/pkg/protocol"
  27. "github.com/mudler/edgevpn/pkg/trustzone"
  28. . "github.com/mudler/edgevpn/pkg/trustzone"
  29. . "github.com/mudler/edgevpn/pkg/trustzone/authprovider/ecdsa"
  30. )
  31. var _ = Describe("trustzone", func() {
  32. token := node.GenerateNewConnectionData().Base64()
  33. logg := logger.New(log.LevelDebug)
  34. ll := node.Logger(logg)
  35. Context("ECDSA auth", func() {
  36. It("authorize nodes", func() {
  37. ctx, cancel := context.WithCancel(context.Background())
  38. defer cancel()
  39. ctx2, cancel2 := context.WithCancel(context.Background())
  40. defer cancel2()
  41. privKey, pubKey, err := GenerateKeys()
  42. Expect(err).ToNot(HaveOccurred())
  43. pg := NewPeerGater(false)
  44. dur := 5 * time.Second
  45. provider, err := ECDSA521Provider(logg, string(privKey))
  46. aps := []trustzone.AuthProvider{provider}
  47. pguardian := trustzone.NewPeerGuardian(logg, aps...)
  48. cm, err := connmanager.NewConnManager(
  49. 1,
  50. 5,
  51. connmanager.WithGracePeriod(80*time.Second),
  52. )
  53. permStore := &blockchain.MemoryStore{}
  54. e, _ := node.New(
  55. node.WithLibp2pAdditionalOptions(libp2p.ConnectionManager(cm)),
  56. node.WithNetworkService(
  57. pg.UpdaterService(dur),
  58. pguardian.Challenger(dur, false),
  59. ),
  60. node.EnableGenericHub,
  61. node.GenericChannelHandlers(pguardian.ReceiveMessage),
  62. // node.WithPeerGater(pg),
  63. node.WithDiscoveryInterval(10*time.Second),
  64. node.FromBase64(true, true, token, nil, nil), node.WithStore(permStore), ll)
  65. pguardian2 := trustzone.NewPeerGuardian(logg, aps...)
  66. e2, _ := node.New(
  67. node.WithLibp2pAdditionalOptions(libp2p.ConnectionManager(cm)),
  68. node.WithNetworkService(
  69. pg.UpdaterService(dur),
  70. pguardian2.Challenger(dur, false),
  71. ),
  72. node.EnableGenericHub,
  73. node.GenericChannelHandlers(pguardian2.ReceiveMessage),
  74. // node.WithPeerGater(pg),
  75. node.WithDiscoveryInterval(10*time.Second),
  76. node.FromBase64(true, true, token, nil, nil), node.WithStore(&blockchain.MemoryStore{}), ll)
  77. l, err := e.Ledger()
  78. Expect(err).ToNot(HaveOccurred())
  79. l2, err := e2.Ledger()
  80. Expect(err).ToNot(HaveOccurred())
  81. go e.Start(ctx2)
  82. time.Sleep(10 * time.Second)
  83. go e2.Start(ctx)
  84. l.Persist(ctx, 2*time.Second, 20*time.Second, protocol.TrustZoneAuthKey, "ecdsa", string(pubKey))
  85. Eventually(func() bool {
  86. _, exists := l2.GetKey(protocol.TrustZoneAuthKey, "ecdsa")
  87. fmt.Println("Ledger2", l2.CurrentData())
  88. fmt.Println("Ledger1", l.CurrentData())
  89. return exists
  90. }, 60*time.Second, 1*time.Second).Should(BeTrue())
  91. Eventually(func() bool {
  92. _, exists := l2.GetKey(protocol.TrustZoneKey, e.Host().ID().String())
  93. fmt.Println("Ledger2", l2.CurrentData())
  94. fmt.Println("Ledger1", l.CurrentData())
  95. return exists
  96. }, 60*time.Second, 1*time.Second).Should(BeTrue())
  97. Eventually(func() bool {
  98. _, exists := l.GetKey(protocol.TrustZoneKey, e2.Host().ID().String())
  99. fmt.Println("Ledger2", l2.CurrentData())
  100. fmt.Println("Ledger1", l.CurrentData())
  101. return exists
  102. }, 60*time.Second, 1*time.Second).Should(BeTrue())
  103. cancel2()
  104. e, err = node.New(
  105. node.WithLibp2pAdditionalOptions(libp2p.ConnectionManager(cm)),
  106. node.WithNetworkService(
  107. pg.UpdaterService(dur),
  108. pguardian.Challenger(dur, false),
  109. ),
  110. node.EnableGenericHub,
  111. node.GenericChannelHandlers(pguardian.ReceiveMessage),
  112. node.WithPeerGater(pg),
  113. node.WithDiscoveryInterval(10*time.Second),
  114. node.FromBase64(true, true, token, nil, nil), node.WithStore(permStore), ll)
  115. Expect(err).ToNot(HaveOccurred())
  116. l, err = e.Ledger()
  117. Expect(err).ToNot(HaveOccurred())
  118. e.Start(ctx)
  119. Eventually(func() bool {
  120. if e.Host() == nil {
  121. return false
  122. }
  123. _, exists := l2.GetKey(protocol.TrustZoneKey, e.Host().ID().String())
  124. fmt.Println("Ledger2", l2.CurrentData())
  125. fmt.Println("Ledger1", l.CurrentData())
  126. return exists
  127. }, 60*time.Second, 1*time.Second).Should(BeTrue())
  128. Eventually(func() bool {
  129. _, exists := l.GetKey(protocol.TrustZoneKey, e.Host().ID().String())
  130. fmt.Println("Ledger2", l2.CurrentData())
  131. fmt.Println("Ledger1", l.CurrentData())
  132. return exists
  133. }, 60*time.Second, 1*time.Second).Should(BeTrue())
  134. })
  135. })
  136. })