github.com/true-sqn/fabric@v2.1.1+incompatible/core/deliverservice/config_test.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package deliverservice_test
     8  
     9  import (
    10  	"bytes"
    11  	"io/ioutil"
    12  	"os"
    13  	"path/filepath"
    14  	"testing"
    15  	"time"
    16  
    17  	"github.com/stretchr/testify/assert"
    18  	"github.com/stretchr/testify/require"
    19  
    20  	"github.com/hyperledger/fabric/core/deliverservice"
    21  	"github.com/hyperledger/fabric/internal/pkg/comm"
    22  	"github.com/spf13/viper"
    23  )
    24  
    25  func TestSecureOptsConfig(t *testing.T) {
    26  	cwd, err := os.Getwd()
    27  	require.NoError(t, err)
    28  	certPath := filepath.Join(cwd, "testdata", "cert.pem")
    29  	keyPath := filepath.Join(cwd, "testdata", "key.pem")
    30  
    31  	certBytes, err := ioutil.ReadFile(filepath.Join("testdata", "cert.pem"))
    32  	require.NoError(t, err)
    33  
    34  	keyBytes, err := ioutil.ReadFile(filepath.Join("testdata", "key.pem"))
    35  	require.NoError(t, err)
    36  
    37  	t.Run("client specific cert", func(t *testing.T) {
    38  		viper.Reset()
    39  		defer viper.Reset()
    40  
    41  		viper.Set("peer.tls.enabled", true)
    42  		viper.Set("peer.tls.clientAuthRequired", true)
    43  		viper.Set("peer.tls.clientCert.file", certPath)
    44  		viper.Set("peer.tls.clientKey.file", keyPath)
    45  
    46  		coreConfig := deliverservice.GlobalConfig()
    47  
    48  		assert.True(t, coreConfig.SecOpts.UseTLS)
    49  		assert.True(t, coreConfig.SecOpts.RequireClientCert)
    50  		assert.Equal(t, keyBytes, coreConfig.SecOpts.Key)
    51  		assert.Equal(t, certBytes, coreConfig.SecOpts.Certificate)
    52  	})
    53  
    54  	t.Run("fallback cert", func(t *testing.T) {
    55  		viper.Reset()
    56  		defer viper.Reset()
    57  
    58  		viper.Set("peer.tls.enabled", true)
    59  		viper.Set("peer.tls.clientAuthRequired", true)
    60  		viper.Set("peer.tls.cert.file", certPath)
    61  		viper.Set("peer.tls.key.file", keyPath)
    62  
    63  		coreConfig := deliverservice.GlobalConfig()
    64  
    65  		assert.True(t, coreConfig.SecOpts.UseTLS)
    66  		assert.True(t, coreConfig.SecOpts.RequireClientCert)
    67  		assert.Equal(t, keyBytes, coreConfig.SecOpts.Key)
    68  		assert.Equal(t, certBytes, coreConfig.SecOpts.Certificate)
    69  	})
    70  
    71  	t.Run("no cert", func(t *testing.T) {
    72  		viper.Reset()
    73  		defer viper.Reset()
    74  
    75  		viper.Set("peer.tls.enabled", true)
    76  		viper.Set("peer.tls.clientAuthRequired", true)
    77  
    78  		assert.Panics(t, func() { deliverservice.GlobalConfig() })
    79  	})
    80  }
    81  
    82  func TestGlobalConfig(t *testing.T) {
    83  	viper.Reset()
    84  	defer viper.Reset()
    85  
    86  	viper.Set("peer.tls.enabled", true)
    87  	viper.Set("peer.deliveryclient.reConnectBackoffThreshold", "25s")
    88  	viper.Set("peer.deliveryclient.reconnectTotalTimeThreshold", "20s")
    89  	viper.Set("peer.deliveryclient.connTimeout", "10s")
    90  	viper.Set("peer.keepalive.deliveryClient.interval", "5s")
    91  	viper.Set("peer.keepalive.deliveryClient.timeout", "2s")
    92  
    93  	coreConfig := deliverservice.GlobalConfig()
    94  
    95  	expectedConfig := &deliverservice.DeliverServiceConfig{
    96  		PeerTLSEnabled:              true,
    97  		ReConnectBackoffThreshold:   25 * time.Second,
    98  		ReconnectTotalTimeThreshold: 20 * time.Second,
    99  		ConnectionTimeout:           10 * time.Second,
   100  		KeepaliveOptions: comm.KeepaliveOptions{
   101  			ClientInterval:    time.Second * 5,
   102  			ClientTimeout:     time.Second * 2,
   103  			ServerInterval:    time.Hour * 2,
   104  			ServerTimeout:     time.Second * 20,
   105  			ServerMinInterval: time.Minute,
   106  		},
   107  		SecOpts: comm.SecureOptions{
   108  			UseTLS: true,
   109  		},
   110  	}
   111  
   112  	assert.Equal(t, expectedConfig, coreConfig)
   113  }
   114  
   115  func TestGlobalConfigDefault(t *testing.T) {
   116  	viper.Reset()
   117  	defer viper.Reset()
   118  
   119  	coreConfig := deliverservice.GlobalConfig()
   120  
   121  	expectedConfig := &deliverservice.DeliverServiceConfig{
   122  		PeerTLSEnabled:              false,
   123  		ReConnectBackoffThreshold:   deliverservice.DefaultReConnectBackoffThreshold,
   124  		ReconnectTotalTimeThreshold: deliverservice.DefaultReConnectTotalTimeThreshold,
   125  		ConnectionTimeout:           deliverservice.DefaultConnectionTimeout,
   126  		KeepaliveOptions:            comm.DefaultKeepaliveOptions,
   127  	}
   128  
   129  	assert.Equal(t, expectedConfig, coreConfig)
   130  }
   131  
   132  func TestLoadOverridesMap(t *testing.T) {
   133  	defer viper.Reset()
   134  
   135  	t.Run("GreenPath", func(t *testing.T) {
   136  		config := `
   137                    peer:
   138                      deliveryclient:
   139                        addressOverrides:
   140                          - from: addressFrom1
   141                            to: addressTo1
   142                            caCertsFile: testdata/cert.pem
   143                          - from: addressFrom2
   144                            to: addressTo2
   145                            caCertsFile: testdata/cert.pem
   146                  `
   147  
   148  		viper.Reset()
   149  		viper.SetConfigType("yaml")
   150  		viper.ReadConfig(bytes.NewBuffer([]byte(config)))
   151  		res, err := deliverservice.LoadOverridesMap()
   152  		require.NoError(t, err)
   153  		require.Len(t, res, 2)
   154  		ep1, ok := res["addressFrom1"]
   155  		require.True(t, ok)
   156  		assert.Equal(t, "addressTo1", ep1.Address)
   157  		ep2, ok := res["addressFrom2"]
   158  		require.True(t, ok)
   159  		assert.Equal(t, "addressTo2", ep2.Address)
   160  	})
   161  
   162  	t.Run("MissingCAFiles", func(t *testing.T) {
   163  		config := `
   164                    peer:
   165                      deliveryclient:
   166                        addressOverrides:
   167                          - from: addressFrom1
   168                            to: addressTo1
   169                            caCertsFile: missing/cert.pem
   170                          - from: addressFrom2
   171                            to: addressTo2
   172                            caCertsFile: testdata/cert.pem
   173                  `
   174  
   175  		viper.Reset()
   176  		viper.SetConfigType("yaml")
   177  		viper.ReadConfig(bytes.NewBuffer([]byte(config)))
   178  		res, err := deliverservice.LoadOverridesMap()
   179  		require.NoError(t, err)
   180  		assert.Len(t, res, 1)
   181  	})
   182  
   183  	t.Run("EmptyCAFiles", func(t *testing.T) {
   184  		config := `
   185                    peer:
   186                      deliveryclient:
   187                        addressOverrides:
   188                          - from: addressFrom1
   189                            to: addressTo1
   190                          - from: addressFrom2
   191                            to: addressTo2
   192                  `
   193  
   194  		viper.Reset()
   195  		viper.SetConfigType("yaml")
   196  		viper.ReadConfig(bytes.NewBuffer([]byte(config)))
   197  		res, err := deliverservice.LoadOverridesMap()
   198  		require.NoError(t, err)
   199  		assert.Len(t, res, 2)
   200  	})
   201  
   202  	t.Run("BadYaml", func(t *testing.T) {
   203  		config := `
   204                    peer:
   205                      deliveryclient:
   206                        addressOverrides: foo
   207                  `
   208  
   209  		viper.Reset()
   210  		viper.SetConfigType("yaml")
   211  		viper.ReadConfig(bytes.NewBuffer([]byte(config)))
   212  		_, err := deliverservice.LoadOverridesMap()
   213  		require.EqualError(t, err, "could not unmarshal peer.deliveryclient.addressOverrides: '': source data must be an array or slice, got string")
   214  	})
   215  
   216  	t.Run("EmptyYaml", func(t *testing.T) {
   217  		config := `
   218                    peer:
   219                      deliveryclient:
   220                  `
   221  
   222  		viper.Reset()
   223  		viper.SetConfigType("yaml")
   224  		viper.ReadConfig(bytes.NewBuffer([]byte(config)))
   225  		res, err := deliverservice.LoadOverridesMap()
   226  		require.NoError(t, err)
   227  		assert.Nil(t, res)
   228  	})
   229  }