Skip to content

Commit

Permalink
feat: expose TLSConfig for config api (#2245)
Browse files Browse the repository at this point in the history
* feat: expose TLSConfig for config api

Signed-off-by: sekfung <sekfung.lau@gmail.com>

* fix: integration test

Signed-off-by: sekfung <sekfung.lau@gmail.com>

* fix: add license header

Signed-off-by: sekfung <sekfung.lau@gmail.com>

---------

Signed-off-by: sekfung <sekfung.lau@gmail.com>
  • Loading branch information
sekfung committed Mar 15, 2023
1 parent b492dba commit b4ad6a3
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 0 deletions.
1 change: 1 addition & 0 deletions common/constant/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ const (
LoggerConfigPrefix = "dubbo.logger"
CustomConfigPrefix = "dubbo.custom"
ProfilesConfigPrefix = "dubbo.profiles"
TLSConfigPrefix = "dubbo.tls_config"
)

const (
Expand Down
13 changes: 13 additions & 0 deletions config/root_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ func GetShutDown() *ShutdownConfig {
return NewShutDownConfigBuilder().Build()
}

func GetTLSConfig() *TLSConfig {
if err := check(); err == nil && rootConfig.TLSConfig != nil {
return rootConfig.TLSConfig
}
return NewTLSConfigBuilder().Build()
}

// getRegistryIds get registry ids
func (rc *RootConfig) getRegistryIds() []string {
ids := make([]string, 0)
Expand Down Expand Up @@ -225,6 +232,7 @@ func newEmptyRootConfig() *RootConfig {
Logger: NewLoggerConfigBuilder().Build(),
Custom: NewCustomConfigBuilder().Build(),
Shutdown: NewShutDownConfigBuilder().Build(),
TLSConfig: NewTLSConfigBuilder().Build(),
}
return newRootConfig
}
Expand Down Expand Up @@ -322,6 +330,11 @@ func (rb *RootConfigBuilder) SetShutDown(shutDownConfig *ShutdownConfig) *RootCo
return rb
}

func (rb *RootConfigBuilder) SetTLSConfig(tlsConfig *TLSConfig) *RootConfigBuilder {
rb.rootConfig.TLSConfig = tlsConfig
return rb
}

func (rb *RootConfigBuilder) Build() *RootConfig {
return rb.rootConfig
}
Expand Down
52 changes: 52 additions & 0 deletions config/tls_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ import (
"io/ioutil"
)

import (
"dubbo.apache.org/dubbo-go/v3/common/constant"
)

// TLSConfig tls config
type TLSConfig struct {
CACertFile string `yaml:"ca-cert-file" json:"ca-cert-file" property:"ca-cert-file"`
Expand All @@ -31,6 +35,10 @@ type TLSConfig struct {
TLSServerName string `yaml:"tls-server-name" json:"tls-server-name" property:"tls-server-name"`
}

func (t *TLSConfig) Prefix() string {
return constant.TLSConfigPrefix
}

// GetServerTlsConfig build server tls config from TLSConfig
func GetServerTlsConfig(opt *TLSConfig) (*tls.Config, error) {
//no TLS
Expand Down Expand Up @@ -91,3 +99,47 @@ func GetClientTlsConfig(opt *TLSConfig) (*tls.Config, error) {
}
return cfg, err
}

type TLSConfigBuilder struct {
tlsConfig *TLSConfig
}

func NewTLSConfigBuilder() *TLSConfigBuilder {
return &TLSConfigBuilder{}
}

func (tcb *TLSConfigBuilder) SetCACertFile(caCertFile string) *TLSConfigBuilder {
if tcb.tlsConfig == nil {
tcb.tlsConfig = &TLSConfig{}
}
tcb.tlsConfig.CACertFile = caCertFile
return tcb
}

func (tcb *TLSConfigBuilder) SetTLSCertFile(tlsCertFile string) *TLSConfigBuilder {
if tcb.tlsConfig == nil {
tcb.tlsConfig = &TLSConfig{}
}
tcb.tlsConfig.TLSCertFile = tlsCertFile
return tcb
}

func (tcb *TLSConfigBuilder) SetTLSKeyFile(tlsKeyFile string) *TLSConfigBuilder {
if tcb.tlsConfig == nil {
tcb.tlsConfig = &TLSConfig{}
}
tcb.tlsConfig.TLSKeyFile = tlsKeyFile
return tcb
}

func (tcb *TLSConfigBuilder) SetTLSServerName(tlsServerName string) *TLSConfigBuilder {
if tcb.tlsConfig == nil {
tcb.tlsConfig = &TLSConfig{}
}
tcb.tlsConfig.TLSServerName = tlsServerName
return tcb
}

func (tcb *TLSConfigBuilder) Build() *TLSConfig {
return tcb.tlsConfig
}
45 changes: 45 additions & 0 deletions config/tls_config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package config

import (
"testing"
)

import (
"github.com/stretchr/testify/assert"
)

import (
"dubbo.apache.org/dubbo-go/v3/common/constant"
)

func TestNewTLSConfigBuilder(t *testing.T) {
config := NewTLSConfigBuilder().
SetCACertFile("ca_cert_file").
SetTLSKeyFile("tls_key_file").
SetTLSServerName("tls_server_name").
SetTLSCertFile("tls_cert_file").
Build()
assert.Equal(t, config.CACertFile, "ca_cert_file")
assert.Equal(t, config.TLSCertFile, "tls_cert_file")
assert.Equal(t, config.TLSServerName, "tls_server_name")
assert.Equal(t, config.TLSKeyFile, "tls_key_file")
assert.Equal(t, config.Prefix(), constant.TLSConfigPrefix)

}

0 comments on commit b4ad6a3

Please sign in to comment.