From d929d7677b1232c0dc66829216920e966f748bdf Mon Sep 17 00:00:00 2001 From: Dmitry Shihovtsev Date: Sat, 6 Apr 2019 16:09:00 +0300 Subject: [PATCH] OpenVPN not fail on empty consumer config (cherry picked from commit c1bef8852752caced6449260178f8c7d48098e49) --- services/openvpn/service/manager.go | 14 +++++++++----- services/openvpn/service/manager_test.go | 20 ++++++++++++++++++++ 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/services/openvpn/service/manager.go b/services/openvpn/service/manager.go index 2d4ce14de7..f691684912 100644 --- a/services/openvpn/service/manager.go +++ b/services/openvpn/service/manager.go @@ -142,12 +142,16 @@ func (m *Manager) ProvideConfig(config json.RawMessage) (session.ServiceConfigur log.Info(logPrefix, "Config provider not initialized") return nil, nil, errors.New("Config provider not initialized") } - var c openvpn_service.ConsumerConfig - error := json.Unmarshal(config, &c) - if error != nil { - return nil, nil, errors.Wrap(error, "parsing consumer config failed") + + if config != nil && len(config) > 0 { // Older clients do not send any config, but we should keep back compatibility and not fail in this case. + var c openvpn_service.ConsumerConfig + err := json.Unmarshal(config, &c) + if err != nil { + return nil, nil, errors.Wrap(err, "parsing consumer config failed") + } + m.consumerConfig = c } - m.consumerConfig = c + return m.vpnServiceConfigProvider.ProvideConfig(config) } diff --git a/services/openvpn/service/manager_test.go b/services/openvpn/service/manager_test.go index 17ec9aba15..fd3436303b 100644 --- a/services/openvpn/service/manager_test.go +++ b/services/openvpn/service/manager_test.go @@ -18,8 +18,10 @@ package service import ( + "encoding/json" "testing" + "github.com/mysteriumnetwork/node/session" "github.com/stretchr/testify/assert" ) @@ -28,3 +30,21 @@ func TestManager_StopNotPanic(t *testing.T) { err := m.Stop() assert.NoError(t, err) } + +func TestManager_ProvideConfigNotFailOnEmptyConfig(t *testing.T) { + m := Manager{vpnServiceConfigProvider: &mockConfigProvider{}} + _, _, err := m.ProvideConfig([]byte("")) + assert.NoError(t, err) +} + +func TestManager_ProvideConfigNotFailOnNilConfig(t *testing.T) { + m := Manager{vpnServiceConfigProvider: &mockConfigProvider{}} + _, _, err := m.ProvideConfig(nil) + assert.NoError(t, err) +} + +type mockConfigProvider struct{} + +func (cp *mockConfigProvider) ProvideConfig(consumerKey json.RawMessage) (session.ServiceConfiguration, session.DestroyCallback, error) { + return nil, nil, nil +}