Skip to content

Commit

Permalink
feat: tls sample (#469)
Browse files Browse the repository at this point in the history
* tls sample

* tls sample
  • Loading branch information
ZLBer authored Dec 5, 2022
1 parent e7a8938 commit fdaedb8
Show file tree
Hide file tree
Showing 43 changed files with 2,494 additions and 0 deletions.
59 changes: 59 additions & 0 deletions tls/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Use TLS encryption in Dubbo go

## Usage

0. Generate the required certificate and secret key

This example provides the generated certificate and secret key under the directory `tls/x509`

1. Configure dubbogo.yaml

Client TLS configuration:

```yaml
dubbo:
tls_config:
ca-cert-file: ../../../x509/server_ca_cert.pem
tls-cert-file: ../../../x509/client2_cert.pem
tls-key-file: ../../../x509/client2_key.pem
tls-server-name: dubbogo.test.example.com
```
Server TLS configuration:
```yaml
dubbo:
tls_config:
ca-cert-file: ../../../x509/client_ca_cert.pem
tls-cert-file: ../../../x509/server2_cert.pem
tls-key-file: ../../../x509/server2_key.pem
tls-server-name: dubbogo.test.example.com
```
2. Startup example
This example provides TLS encryption examples of Dubbo, Grpc and Triple communication modes, respectively located in
`tls/dubbo` 、`tls/grpc` 、`tls/triple`。 Enter the folder to launch the sample.

Take tls/dubbo as an example:

Start the server:

Enter 'tls/dubbo/go server/cmd' and start 'server.go`

The TLS configuration takes effect when you see the following logs

```
2022-12-01T23:39:30.690+0800 INFO getty/getty_ server. go:78 Getty Server initialized the TLSConfig configuration
```
Start client:
Enter 'tls/dubbo/go client/cmd' and start 'client.go`
The TLS configuration takes effect when you see the following logs
```
2022-12-01T23:40:05.998+0800 INFO grpc/client. go:90 Grpc Client initialized the TLSConfig configuration
```
54 changes: 54 additions & 0 deletions tls/README_zh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# 在Dubbo-go中使用TLS加密

## 使用方法
0.生成所需要的证书和秘钥
本示例提供已经生成好的证书和秘钥,在目录`tls/x509`

1.配置dubbogo.yaml

客户端TLS配置:

```yaml
dubbo:
tls_config:
ca-cert-file: ../../../x509/server_ca_cert.pem
tls-cert-file: ../../../x509/client2_cert.pem
tls-key-file: ../../../x509/client2_key.pem
tls-server-name: dubbogo.test.example.com
```
服务端TLS配置:
```yaml
dubbo:
tls_config:
ca-cert-file: ../../../x509/client_ca_cert.pem
tls-cert-file: ../../../x509/server2_cert.pem
tls-key-file: ../../../x509/server2_key.pem
tls-server-name: dubbogo.test.example.com
```
2. 启动示例
本示例提供了Dubbo、Grpc、Triple三种通信方式的TLS加密示例,分别位于`tls/dubbo` 、`tls/grpc` 、`tls/triple`。进入文件夹即可启动示例。

以tls/dubbo为例:

启动服务端:

进入`tls/dubbo/go-server/cmd`,启动`server.go`

看到如下日志,则TLS配置生效

```
2022-12-01T23:39:30.690+0800 INFO getty/getty_server.go:78 Getty Server initialized the TLSConfig configuration
```
启动客户端:
进入`tls/dubbo/go-client/cmd`,启动`client.go`
看到如下日志,则TLS配置生效
```
2022-12-01T23:40:05.998+0800 INFO grpc/client.go:90 Grpc Client initialized the TLSConfig configuration
```
104 changes: 104 additions & 0 deletions tls/dubbo/go-client/cmd/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* 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 main

import (
"context"
)

import (
"dubbo.apache.org/dubbo-go/v3/config"
_ "dubbo.apache.org/dubbo-go/v3/imports"
"github.com/dubbogo/gost/log/logger"

hessian "github.com/apache/dubbo-go-hessian2"
)

import (
"github.com/apache/dubbo-go-samples/tls/dubbo/go-client/pkg"
)

var (
userProvider = &pkg.UserProvider{}
)

// need to setup environment variable "DUBBO_GO_CONFIG_PATH" to "conf/dubbogo.yaml" before run
func main() {
hessian.RegisterJavaEnum(pkg.Gender(pkg.MAN))
hessian.RegisterJavaEnum(pkg.Gender(pkg.WOMAN))
hessian.RegisterPOJO(&pkg.User{})

config.SetConsumerService(userProvider)

err := config.Load()
if err != nil {
panic(err)
}

logger.Infof("\n\ntest")
test()
}

func test() {
logger.Infof("\n\n\nstart to test dubbo")
reqUser := &pkg.User{}
reqUser.ID = "003"
user, err := userProvider.GetUser(context.TODO(), reqUser)
if err != nil {
panic(err)
}
logger.Infof("response result: %v", user)

logger.Infof("\n\n\nstart to test dubbo - enum")
gender, err := userProvider.GetGender(context.TODO(), 1)
if err != nil {
panic(err)
}
logger.Infof("response result: %v", gender)

logger.Infof("\n\n\nstart to test dubbo - GetUser0")
ret, err := userProvider.GetUser0("003", "Moorse")
if err != nil {
panic(err)
}
logger.Infof("response result: %v", ret)

logger.Infof("\n\n\nstart to test dubbo - GetUsers")
ret1, err := userProvider.GetUsers([]string{"002", "003"})
if err != nil {
panic(err)
}
logger.Infof("response result: %v", ret1)

logger.Infof("\n\n\nstart to test dubbo - getUser")

var i int32 = 1
user, err = userProvider.GetUser2(context.TODO(), i)
if err != nil {
panic(err)
}
logger.Infof("response result: %v", user)

logger.Infof("\n\n\nstart to test dubbo - getErr")
reqUser.ID = "003"
_, err = userProvider.GetErr(context.TODO(), reqUser)
if err == nil {
panic("err is nil")
}
logger.Infof("getErr - error: %v", err)
}
17 changes: 17 additions & 0 deletions tls/dubbo/go-client/conf/dubbogo.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# dubbo client yaml configure file

dubbo:
consumer:
references:
UserProvider:
url: dubbo://localhost:20000
protocol: dubbo
interface: org.apache.dubbo.sample.UserProvider
logger:
zap-config:
level: info
tls_config:
ca-cert-file: ../../../x509/server_ca_cert.pem
tls-cert-file: ../../../x509/client1_cert.pem
tls-key-file: ../../../x509/client1_key.pem
tls-server-name: dubbogo.test.example.com
103 changes: 103 additions & 0 deletions tls/dubbo/go-client/pkg/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* 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 pkg

import (
"context"
"fmt"
"strconv"
"time"
)

import (
hessian "github.com/apache/dubbo-go-hessian2"
)

type Gender hessian.JavaEnum

const (
MAN hessian.JavaEnum = iota
WOMAN
)

var genderName = map[hessian.JavaEnum]string{
MAN: "MAN",
WOMAN: "WOMAN",
}

var genderValue = map[string]hessian.JavaEnum{
"MAN": MAN,
"WOMAN": WOMAN,
}

func (g Gender) JavaClassName() string {
return "org.apache.dubbo.sample.Gender"
}

func (g Gender) String() string {
s, ok := genderName[hessian.JavaEnum(g)]
if ok {
return s
}

return strconv.Itoa(int(g))
}

func (g Gender) EnumValue(s string) hessian.JavaEnum {
v, ok := genderValue[s]
if ok {
return v
}

return hessian.InvalidJavaEnum
}

type User struct {
// !!! Cannot define lowercase names of variable
ID string `hessian:"id"`
Name string
Age int32
Time time.Time
Sex Gender // notice: java enum Object <--> go string
}

func (u User) String() string {
return fmt.Sprintf(
"User{ID:%s, Name:%s, Age:%d, Time:%s, Sex:%s}",
u.ID, u.Name, u.Age, u.Time, u.Sex,
)
}

func (u *User) JavaClassName() string {
return "org.apache.dubbo.sample.User"
}

type UserProvider struct {
GetUsers func(req []string) ([]*User, error)
GetErr func(ctx context.Context, req *User) (*User, error)

GetUser func(ctx context.Context, req *User) (*User, error)

GetUserNew func(ctx context.Context, req1, req2 *User) (*User, error)

GetUser0 func(id string, name string) (User, error)
GetUser2 func(ctx context.Context, req int32) (*User, error) `dubbo:"getUser"`
GetUser3 func() error
GetGender func(ctx context.Context, i int32) (Gender, error)
Echo func(ctx context.Context, req interface{}) (interface{}, error) // Echo represent EchoFilter will be used
}
Loading

0 comments on commit fdaedb8

Please sign in to comment.