github.com/true-sqn/fabric@v2.1.1+incompatible/internal/pkg/peer/orderers/connection_test.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package orderers_test
     8  
     9  import (
    10  	"bytes"
    11  	"crypto/x509"
    12  	"io/ioutil"
    13  	"sort"
    14  
    15  	. "github.com/onsi/ginkgo"
    16  	. "github.com/onsi/gomega"
    17  
    18  	"github.com/hyperledger/fabric/common/flogging"
    19  	"github.com/hyperledger/fabric/internal/pkg/peer/orderers"
    20  )
    21  
    22  type comparableEndpoint struct {
    23  	Address  string
    24  	Subjects [][]byte
    25  }
    26  
    27  // stripEndpoints makes a comparable version of the endpoints specified.
    28  // This is necessary because the x509 Cert pool's contents don't appear to be comparable
    29  // in a deterministic way (usually the same, but sometimes they differ), plus, the endpoint
    30  // contains a channel which is not comparable.
    31  func stripEndpoints(endpoints []*orderers.Endpoint) []comparableEndpoint {
    32  	endpointsWithChannelStripped := make([]comparableEndpoint, len(endpoints))
    33  	for i, endpoint := range endpoints {
    34  		subjects := endpoint.CertPool.Subjects()
    35  		sort.Slice(subjects, func(i, j int) bool {
    36  			return bytes.Compare(subjects[i], subjects[j]) >= 0
    37  		})
    38  		endpointsWithChannelStripped[i].Address = endpoint.Address
    39  		endpointsWithChannelStripped[i].Subjects = subjects
    40  	}
    41  	return endpointsWithChannelStripped
    42  }
    43  
    44  var _ = Describe("Connection", func() {
    45  	var (
    46  		cert1 []byte
    47  		cert2 []byte
    48  		cert3 []byte
    49  
    50  		org1 orderers.OrdererOrg
    51  		org2 orderers.OrdererOrg
    52  
    53  		org1CertPool     *x509.CertPool
    54  		org2CertPool     *x509.CertPool
    55  		overrideCertPool *x509.CertPool
    56  
    57  		cs *orderers.ConnectionSource
    58  
    59  		endpoints []*orderers.Endpoint
    60  	)
    61  
    62  	BeforeEach(func() {
    63  		var err error
    64  		cert1, err = ioutil.ReadFile("testdata/tlsca.example.com-cert.pem")
    65  		Expect(err).NotTo(HaveOccurred())
    66  
    67  		cert2, err = ioutil.ReadFile("testdata/tlsca.org1.example.com-cert.pem")
    68  		Expect(err).NotTo(HaveOccurred())
    69  
    70  		cert3, err = ioutil.ReadFile("testdata/tlsca.org2.example.com-cert.pem")
    71  		Expect(err).NotTo(HaveOccurred())
    72  
    73  		org1 = orderers.OrdererOrg{
    74  			Addresses: []string{"org1-address1", "org1-address2"},
    75  			RootCerts: [][]byte{cert1, cert2},
    76  		}
    77  
    78  		org2 = orderers.OrdererOrg{
    79  			Addresses: []string{"org2-address1", "org2-address2"},
    80  			RootCerts: [][]byte{cert3},
    81  		}
    82  
    83  		org1CertPool = x509.NewCertPool()
    84  		added := org1CertPool.AppendCertsFromPEM(cert1)
    85  		Expect(added).To(BeTrue())
    86  		added = org1CertPool.AppendCertsFromPEM(cert2)
    87  		Expect(added).To(BeTrue())
    88  
    89  		org2CertPool = x509.NewCertPool()
    90  		added = org2CertPool.AppendCertsFromPEM(cert3)
    91  		Expect(added).To(BeTrue())
    92  
    93  		overrideCertPool = x509.NewCertPool()
    94  		added = overrideCertPool.AppendCertsFromPEM(cert2)
    95  		Expect(added).To(BeTrue())
    96  
    97  		cs = orderers.NewConnectionSource(flogging.MustGetLogger("peer.orderers"), map[string]*orderers.Endpoint{
    98  			"override-address": {
    99  				Address:  "re-mapped-address",
   100  				CertPool: overrideCertPool,
   101  			},
   102  		})
   103  		cs.Update(nil, map[string]orderers.OrdererOrg{
   104  			"org1": org1,
   105  			"org2": org2,
   106  		})
   107  
   108  		endpoints = cs.Endpoints()
   109  	})
   110  
   111  	It("holds endpoints for all of the defined orderers", func() {
   112  		Expect(stripEndpoints(endpoints)).To(ConsistOf(
   113  			stripEndpoints([]*orderers.Endpoint{
   114  				{
   115  					Address:  "org1-address1",
   116  					CertPool: org1CertPool,
   117  				},
   118  				{
   119  					Address:  "org1-address2",
   120  					CertPool: org1CertPool,
   121  				},
   122  				{
   123  					Address:  "org2-address1",
   124  					CertPool: org2CertPool,
   125  				},
   126  				{
   127  					Address:  "org2-address2",
   128  					CertPool: org2CertPool,
   129  				},
   130  			}),
   131  		))
   132  	})
   133  
   134  	It("does not mark any of the endpoints as refreshed", func() {
   135  		for _, endpoint := range endpoints {
   136  			Expect(endpoint.Refreshed).NotTo(BeClosed())
   137  		}
   138  	})
   139  
   140  	When("an update does not modify the endpoint set", func() {
   141  		BeforeEach(func() {
   142  			cs.Update(nil, map[string]orderers.OrdererOrg{
   143  				"org1": org1,
   144  				"org2": org2,
   145  			})
   146  		})
   147  
   148  		It("does not update the endpoints", func() {
   149  			newEndpoints := cs.Endpoints()
   150  			Expect(newEndpoints).To(Equal(endpoints))
   151  		})
   152  
   153  		It("does not close any of the refresh channels", func() {
   154  			for _, endpoint := range endpoints {
   155  				Expect(endpoint.Refreshed).NotTo(BeClosed())
   156  			}
   157  		})
   158  	})
   159  
   160  	When("an update change's an org's TLS CA", func() {
   161  		BeforeEach(func() {
   162  			org1.RootCerts = [][]byte{cert1}
   163  
   164  			cs.Update(nil, map[string]orderers.OrdererOrg{
   165  				"org1": org1,
   166  				"org2": org2,
   167  			})
   168  		})
   169  
   170  		It("creates a new set of orderer endpoints", func() {
   171  			newOrg1CertPool := x509.NewCertPool()
   172  			added := newOrg1CertPool.AppendCertsFromPEM(cert1)
   173  			Expect(added).To(BeTrue())
   174  
   175  			newEndpoints := cs.Endpoints()
   176  			Expect(stripEndpoints(newEndpoints)).To(ConsistOf(
   177  				stripEndpoints([]*orderers.Endpoint{
   178  					{
   179  						Address:  "org1-address1",
   180  						CertPool: newOrg1CertPool,
   181  					},
   182  					{
   183  						Address:  "org1-address2",
   184  						CertPool: newOrg1CertPool,
   185  					},
   186  					{
   187  						Address:  "org2-address1",
   188  						CertPool: org2CertPool,
   189  					},
   190  					{
   191  						Address:  "org2-address2",
   192  						CertPool: org2CertPool,
   193  					},
   194  				}),
   195  			))
   196  		})
   197  
   198  		It("closes the refresh channel for all of the old endpoints", func() {
   199  			for _, endpoint := range endpoints {
   200  				Expect(endpoint.Refreshed).To(BeClosed())
   201  			}
   202  		})
   203  	})
   204  
   205  	When("an update change's an org's endpoint addresses", func() {
   206  		BeforeEach(func() {
   207  			org1.Addresses = []string{"org1-address1", "org1-address3"}
   208  			cs.Update(nil, map[string]orderers.OrdererOrg{
   209  				"org1": org1,
   210  				"org2": org2,
   211  			})
   212  		})
   213  
   214  		It("creates a new set of orderer endpoints", func() {
   215  			newEndpoints := cs.Endpoints()
   216  			Expect(stripEndpoints(newEndpoints)).To(ConsistOf(
   217  				stripEndpoints([]*orderers.Endpoint{
   218  					{
   219  						Address:  "org1-address1",
   220  						CertPool: org1CertPool,
   221  					},
   222  					{
   223  						Address:  "org1-address3",
   224  						CertPool: org1CertPool,
   225  					},
   226  					{
   227  						Address:  "org2-address1",
   228  						CertPool: org2CertPool,
   229  					},
   230  					{
   231  						Address:  "org2-address2",
   232  						CertPool: org2CertPool,
   233  					},
   234  				}),
   235  			))
   236  		})
   237  
   238  		It("closes the refresh channel for all of the old endpoints", func() {
   239  			for _, endpoint := range endpoints {
   240  				Expect(endpoint.Refreshed).To(BeClosed())
   241  			}
   242  		})
   243  	})
   244  
   245  	When("an update references an overridden org endpoint address", func() {
   246  		BeforeEach(func() {
   247  			cs.Update(nil, map[string]orderers.OrdererOrg{
   248  				"org1": {
   249  					Addresses: []string{"org1-address1", "override-address"},
   250  					RootCerts: [][]byte{cert1, cert2},
   251  				},
   252  			})
   253  		})
   254  
   255  		It("creates a new set of orderer endpoints with overrides", func() {
   256  			newEndpoints := cs.Endpoints()
   257  			Expect(stripEndpoints(newEndpoints)).To(ConsistOf(
   258  				stripEndpoints([]*orderers.Endpoint{
   259  					{
   260  						Address:  "org1-address1",
   261  						CertPool: org1CertPool,
   262  					},
   263  					{
   264  						Address:  "re-mapped-address",
   265  						CertPool: overrideCertPool,
   266  					},
   267  				}),
   268  			))
   269  		})
   270  	})
   271  
   272  	When("an update removes an ordering organization", func() {
   273  		BeforeEach(func() {
   274  			cs.Update(nil, map[string]orderers.OrdererOrg{
   275  				"org2": org2,
   276  			})
   277  		})
   278  
   279  		It("creates a new set of orderer endpoints", func() {
   280  			newEndpoints := cs.Endpoints()
   281  			Expect(stripEndpoints(newEndpoints)).To(ConsistOf(
   282  				stripEndpoints([]*orderers.Endpoint{
   283  					{
   284  						Address:  "org2-address1",
   285  						CertPool: org2CertPool,
   286  					},
   287  					{
   288  						Address:  "org2-address2",
   289  						CertPool: org2CertPool,
   290  					},
   291  				}),
   292  			))
   293  		})
   294  
   295  		It("closes the refresh channel for all of the old endpoints", func() {
   296  			for _, endpoint := range endpoints {
   297  				Expect(endpoint.Refreshed).To(BeClosed())
   298  			}
   299  		})
   300  
   301  		When("the org is added back", func() {
   302  			BeforeEach(func() {
   303  				cs.Update(nil, map[string]orderers.OrdererOrg{
   304  					"org1": org1,
   305  					"org2": org2,
   306  				})
   307  			})
   308  
   309  			It("returns to the set of orderer endpoints", func() {
   310  				newEndpoints := cs.Endpoints()
   311  				Expect(stripEndpoints(newEndpoints)).To(ConsistOf(
   312  					stripEndpoints([]*orderers.Endpoint{
   313  						{
   314  							Address:  "org1-address1",
   315  							CertPool: org1CertPool,
   316  						},
   317  						{
   318  							Address:  "org1-address2",
   319  							CertPool: org1CertPool,
   320  						},
   321  						{
   322  							Address:  "org2-address1",
   323  							CertPool: org2CertPool,
   324  						},
   325  						{
   326  							Address:  "org2-address2",
   327  							CertPool: org2CertPool,
   328  						},
   329  					}),
   330  				))
   331  			})
   332  		})
   333  	})
   334  
   335  	When("an update modifies the global endpoints but does does not affect the org endpoints", func() {
   336  		BeforeEach(func() {
   337  			cs.Update(nil, map[string]orderers.OrdererOrg{
   338  				"org1": org1,
   339  				"org2": org2,
   340  			})
   341  		})
   342  
   343  		It("does not update the endpoints", func() {
   344  			newEndpoints := cs.Endpoints()
   345  			Expect(newEndpoints).To(Equal(endpoints))
   346  		})
   347  
   348  		It("does not close any of the refresh channels", func() {
   349  			for _, endpoint := range endpoints {
   350  				Expect(endpoint.Refreshed).NotTo(BeClosed())
   351  			}
   352  		})
   353  	})
   354  
   355  	When("the configuration does not contain orderer org endpoints", func() {
   356  		var (
   357  			globalCertPool *x509.CertPool
   358  		)
   359  
   360  		BeforeEach(func() {
   361  			org1.Addresses = nil
   362  			org2.Addresses = nil
   363  
   364  			globalCertPool = x509.NewCertPool()
   365  			added := globalCertPool.AppendCertsFromPEM(cert1)
   366  			Expect(added).To(BeTrue())
   367  			added = globalCertPool.AppendCertsFromPEM(cert2)
   368  			Expect(added).To(BeTrue())
   369  			added = globalCertPool.AppendCertsFromPEM(cert3)
   370  			Expect(added).To(BeTrue())
   371  
   372  			cs.Update([]string{"global-addr1", "global-addr2"}, map[string]orderers.OrdererOrg{
   373  				"org1": org1,
   374  				"org2": org2,
   375  			})
   376  		})
   377  
   378  		It("creates endpoints for the global addrs", func() {
   379  			newEndpoints := cs.Endpoints()
   380  			Expect(stripEndpoints(newEndpoints)).To(ConsistOf(
   381  				stripEndpoints([]*orderers.Endpoint{
   382  					{
   383  						Address:  "global-addr1",
   384  						CertPool: globalCertPool,
   385  					},
   386  					{
   387  						Address:  "global-addr2",
   388  						CertPool: globalCertPool,
   389  					},
   390  				}),
   391  			))
   392  		})
   393  
   394  		It("closes the refresh channel for all of the old endpoints", func() {
   395  			for _, endpoint := range endpoints {
   396  				Expect(endpoint.Refreshed).To(BeClosed())
   397  			}
   398  		})
   399  
   400  		When("the global list of addresses grows", func() {
   401  			BeforeEach(func() {
   402  				cs.Update([]string{"global-addr1", "global-addr2", "global-addr3"}, map[string]orderers.OrdererOrg{
   403  					"org1": org1,
   404  					"org2": org2,
   405  				})
   406  			})
   407  
   408  			It("creates endpoints for the global addrs", func() {
   409  				newEndpoints := cs.Endpoints()
   410  				Expect(stripEndpoints(newEndpoints)).To(ConsistOf(
   411  					stripEndpoints([]*orderers.Endpoint{
   412  						{
   413  							Address:  "global-addr1",
   414  							CertPool: globalCertPool,
   415  						},
   416  						{
   417  							Address:  "global-addr2",
   418  							CertPool: globalCertPool,
   419  						},
   420  						{
   421  							Address:  "global-addr3",
   422  							CertPool: globalCertPool,
   423  						},
   424  					}),
   425  				))
   426  			})
   427  
   428  			It("closes the refresh channel for all of the old endpoints", func() {
   429  				for _, endpoint := range endpoints {
   430  					Expect(endpoint.Refreshed).To(BeClosed())
   431  				}
   432  			})
   433  		})
   434  
   435  		When("the global set of addresses shrinks", func() {
   436  			BeforeEach(func() {
   437  				cs.Update([]string{"global-addr1"}, map[string]orderers.OrdererOrg{
   438  					"org1": org1,
   439  					"org2": org2,
   440  				})
   441  			})
   442  
   443  			It("creates endpoints for the global addrs", func() {
   444  				newEndpoints := cs.Endpoints()
   445  				Expect(stripEndpoints(newEndpoints)).To(ConsistOf(
   446  					stripEndpoints([]*orderers.Endpoint{
   447  						{
   448  							Address:  "global-addr1",
   449  							CertPool: globalCertPool,
   450  						},
   451  					}),
   452  				))
   453  			})
   454  
   455  			It("closes the refresh channel for all of the old endpoints", func() {
   456  				for _, endpoint := range endpoints {
   457  					Expect(endpoint.Refreshed).To(BeClosed())
   458  				}
   459  			})
   460  		})
   461  
   462  		When("the global set of addresses is modified", func() {
   463  			BeforeEach(func() {
   464  				cs.Update([]string{"global-addr1", "global-addr3"}, map[string]orderers.OrdererOrg{
   465  					"org1": org1,
   466  					"org2": org2,
   467  				})
   468  			})
   469  
   470  			It("creates endpoints for the global addrs", func() {
   471  				newEndpoints := cs.Endpoints()
   472  				Expect(stripEndpoints(newEndpoints)).To(ConsistOf(
   473  					stripEndpoints([]*orderers.Endpoint{
   474  						{
   475  							Address:  "global-addr1",
   476  							CertPool: globalCertPool,
   477  						},
   478  						{
   479  							Address:  "global-addr3",
   480  							CertPool: globalCertPool,
   481  						},
   482  					}),
   483  				))
   484  			})
   485  
   486  			It("closes the refresh channel for all of the old endpoints", func() {
   487  				for _, endpoint := range endpoints {
   488  					Expect(endpoint.Refreshed).To(BeClosed())
   489  				}
   490  			})
   491  		})
   492  
   493  		When("an update to the global addrs references an overridden org endpoint address", func() {
   494  			BeforeEach(func() {
   495  				cs.Update([]string{"global-addr1", "override-address"}, map[string]orderers.OrdererOrg{
   496  					"org1": org1,
   497  					"org2": org2,
   498  				},
   499  				)
   500  			})
   501  
   502  			It("creates a new set of orderer endpoints with overrides", func() {
   503  				newEndpoints := cs.Endpoints()
   504  				Expect(stripEndpoints(newEndpoints)).To(ConsistOf(
   505  					stripEndpoints([]*orderers.Endpoint{
   506  						{
   507  							Address:  "global-addr1",
   508  							CertPool: globalCertPool,
   509  						},
   510  						{
   511  							Address:  "re-mapped-address",
   512  							CertPool: overrideCertPool,
   513  						},
   514  					}),
   515  				))
   516  			})
   517  		})
   518  
   519  		When("an orderer org adds an endpoint", func() {
   520  			BeforeEach(func() {
   521  				org1.Addresses = []string{"new-org1-address"}
   522  				cs.Update([]string{"global-addr1", "global-addr2"}, map[string]orderers.OrdererOrg{
   523  					"org1": org1,
   524  					"org2": org2,
   525  				})
   526  			})
   527  
   528  			It("removes the global endpoints and uses only the org level ones", func() {
   529  				newEndpoints := cs.Endpoints()
   530  				Expect(stripEndpoints(newEndpoints)).To(ConsistOf(
   531  					stripEndpoints([]*orderers.Endpoint{
   532  						{
   533  							Address:  "new-org1-address",
   534  							CertPool: org1CertPool,
   535  						},
   536  					}),
   537  				))
   538  			})
   539  
   540  			It("closes the refresh channel for all of the old endpoints", func() {
   541  				for _, endpoint := range endpoints {
   542  					Expect(endpoint.Refreshed).To(BeClosed())
   543  				}
   544  			})
   545  		})
   546  	})
   547  })