This is an automated email from the ASF dual-hosted git repository.
justxuewei pushed a commit to branch 3.0
in repository https://gitbox.apache.org/repos/asf/dubbo-go.git
The following commit(s) were added to refs/heads/3.0 by this push:
new b4ad6a383 feat: expose TLSConfig for config api (#2245)
b4ad6a383 is described below
commit b4ad6a3831ebd5f61fead4cb20505641fc5e4865
Author: Sekfung Lau <[email protected]>
AuthorDate: Wed Mar 15 13:47:49 2023 +0800
feat: expose TLSConfig for config api (#2245)
* feat: expose TLSConfig for config api
Signed-off-by: sekfung <[email protected]>
* fix: integration test
Signed-off-by: sekfung <[email protected]>
* fix: add license header
Signed-off-by: sekfung <[email protected]>
---------
Signed-off-by: sekfung <[email protected]>
---
common/constant/key.go | 1 +
config/root_config.go | 13 ++++++++++++
config/tls_config.go | 52 +++++++++++++++++++++++++++++++++++++++++++++++
config/tls_config_test.go | 45 ++++++++++++++++++++++++++++++++++++++++
4 files changed, 111 insertions(+)
diff --git a/common/constant/key.go b/common/constant/key.go
index 68c7d4526..1c8737ea9 100644
--- a/common/constant/key.go
+++ b/common/constant/key.go
@@ -234,6 +234,7 @@ const (
LoggerConfigPrefix = "dubbo.logger"
CustomConfigPrefix = "dubbo.custom"
ProfilesConfigPrefix = "dubbo.profiles"
+ TLSConfigPrefix = "dubbo.tls_config"
)
const (
diff --git a/config/root_config.go b/config/root_config.go
index ee0387575..d621dd4a0 100644
--- a/config/root_config.go
+++ b/config/root_config.go
@@ -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)
@@ -225,6 +232,7 @@ func newEmptyRootConfig() *RootConfig {
Logger: NewLoggerConfigBuilder().Build(),
Custom: NewCustomConfigBuilder().Build(),
Shutdown: NewShutDownConfigBuilder().Build(),
+ TLSConfig: NewTLSConfigBuilder().Build(),
}
return newRootConfig
}
@@ -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
}
diff --git a/config/tls_config.go b/config/tls_config.go
index 018f61af2..97c79ec2c 100644
--- a/config/tls_config.go
+++ b/config/tls_config.go
@@ -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"`
@@ -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
@@ -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
+}
diff --git a/config/tls_config_test.go b/config/tls_config_test.go
new file mode 100644
index 000000000..a99c51964
--- /dev/null
+++ b/config/tls_config_test.go
@@ -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)
+
+}