You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by ti...@apache.org on 2020/05/07 08:30:53 UTC

[servicecomb-mesher] branch master updated: Dubbo 协议增加schema配置 (#117)

This is an automated email from the ASF dual-hosted git repository.

tianxiaoliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-mesher.git


The following commit(s) were added to refs/heads/master by this push:
     new 04678c1  Dubbo 协议增加schema配置 (#117)
04678c1 is described below

commit 04678c1ca3d48a2e38b441ed70339f831e360b7b
Author: t-xinlin <t_...@sina.com>
AuthorDate: Thu May 7 16:30:44 2020 +0800

    Dubbo 协议增加schema配置 (#117)
    
    * Add ut to http server  module
    
    * Fix ut test error
    
    * As consumer.
    
    * Fix
    
    * Fix: go sec error
    
    * Fix: go sec error
    
    * Format shell script to unix
    
    * Fix goSec error
    
    * Fix: goSec warnings
    
    * Fix goSec
    
    * Fix goSec
    
    * Format shell
    
    * Fix goSec shell
    
    * Fix goSec shell error
    
    * Fix goSec error
    
    * Fix goSec error
    
    * Fix goSec shell error
    
    * format shell file
    
    * Dubbo protocol ddd schema setting
    
    * fix goSec shell error
    
    * fix goSec shell error
    
    * 必须判断error不为空情况
    
    * 去除冗余信息,简化数据
    
    Co-authored-by: “t_xinlin@sina.com <Happy100>
---
 proxy/protocol/dubbo/proxy/dubbo_proxy_ouput.go |   6 +-
 proxy/protocol/dubbo/server/server.go           |  42 +++++
 proxy/protocol/errors_test.go                   |  29 ++++
 proxy/protocol/http/http_server_test.go         | 221 ++++++++++++++++++++++++
 scripts/travis/goSecureChecker.sh               |  16 +-
 5 files changed, 304 insertions(+), 10 deletions(-)

diff --git a/proxy/protocol/dubbo/proxy/dubbo_proxy_ouput.go b/proxy/protocol/dubbo/proxy/dubbo_proxy_ouput.go
index fcd1b61..a6c2bfc 100755
--- a/proxy/protocol/dubbo/proxy/dubbo_proxy_ouput.go
+++ b/proxy/protocol/dubbo/proxy/dubbo_proxy_ouput.go
@@ -22,6 +22,7 @@ import (
 	"encoding/json"
 	"fmt"
 	"github.com/apache/servicecomb-mesher/proxy/cmd"
+	stringutil "github.com/go-chassis/foundation/string"
 	"net/http"
 	"net/url"
 
@@ -208,8 +209,9 @@ func Handle(ctx *dubbo.InvokeContext) error {
 	var err error
 	err = SetLocalServiceAddress(inv) //select local service
 	if err != nil {
-		openlogging.Error(err.Error())
-		return err
+		openlogging.GetLogger().Warn(err.Error())
+	} else {
+		IsProvider = true
 	}
 
 	var c *handler.Chain
diff --git a/proxy/protocol/dubbo/server/server.go b/proxy/protocol/dubbo/server/server.go
index 5fd7361..7fd8942 100644
--- a/proxy/protocol/dubbo/server/server.go
+++ b/proxy/protocol/dubbo/server/server.go
@@ -18,6 +18,11 @@
 package server
 
 import (
+	"fmt"
+	"github.com/go-chassis/go-chassis/core/config"
+	"github.com/go-chassis/go-chassis/core/config/schema"
+	"github.com/go-mesh/openlogging"
+	"gopkg.in/yaml.v2"
 	"net"
 	"sync"
 	"time"
@@ -91,6 +96,7 @@ func (d *DubboServer) String() string {
 
 //Init is a method to initialize the server
 func (d *DubboServer) Init() error {
+	initSchema()
 	d.connMgr = NewConnectMgr()
 	lager.Logger.Info("Dubbo server init success.")
 	return nil
@@ -159,3 +165,39 @@ func (d *DubboServer) AcceptLoop(l *net.TCPListener) {
 
 	defer l.Close()
 }
+
+// initSchema is a method to ini the schema ids
+func initSchema() {
+	m := make(map[string]string, 0)
+	service := config.MicroserviceDefinition
+	if len(service.ServiceDescription.Schemas) == 0 {
+		return
+	}
+
+	for _, inter := range service.ServiceDescription.Schemas {
+		if len(inter) == 0 {
+			openlogging.GetLogger().Warnf("interfaces is empty")
+			break
+		}
+		schemaContent := struct {
+			Swagger string            `yaml:"swagger"`
+			Info    map[string]string `yaml:"info"`
+		}{
+			Swagger: "2.0",
+			Info: map[string]string{
+				"version":          "1.0.0",
+				"title":            fmt.Sprintf("swagger definition for %s", inter),
+				"x-java-interface": inter,
+			},
+		}
+
+		b, err := yaml.Marshal(&schemaContent)
+		if err != nil {
+			break
+		}
+
+		m[inter] = string(b)
+	}
+
+	schema.SetSchemaInfoByMap(m)
+}
diff --git a/proxy/protocol/errors_test.go b/proxy/protocol/errors_test.go
new file mode 100644
index 0000000..859d1a7
--- /dev/null
+++ b/proxy/protocol/errors_test.go
@@ -0,0 +1,29 @@
+/*
+ * 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 protocol
+
+import (
+	"github.com/stretchr/testify/assert"
+	"testing"
+)
+
+func TestProxyError_Error(t *testing.T) {
+	errorMSG := "error msg"
+	roxyError := ProxyError{errorMSG}
+	assert.Equal(t, errorMSG, roxyError.Error())
+}
diff --git a/proxy/protocol/http/http_server_test.go b/proxy/protocol/http/http_server_test.go
new file mode 100644
index 0000000..3dd6855
--- /dev/null
+++ b/proxy/protocol/http/http_server_test.go
@@ -0,0 +1,221 @@
+/*
+ * 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 http
+
+import (
+	"errors"
+	"github.com/apache/servicecomb-mesher/proxy/common"
+	"github.com/apache/servicecomb-mesher/proxy/pkg/runtime"
+	"github.com/go-chassis/go-chassis/core/config"
+	"github.com/go-chassis/go-chassis/core/config/model"
+	"github.com/go-chassis/go-chassis/core/lager"
+	"github.com/go-chassis/go-chassis/core/server"
+	"github.com/stretchr/testify/assert"
+	"net"
+	"net/http"
+	"sync"
+	"testing"
+	"time"
+)
+
+func init() {
+	lager.Init(&lager.Options{LoggerLevel: "INFO", RollingPolicy: "size"})
+}
+
+func externalIP() (net.IP, error) {
+	ifaces, err := net.Interfaces()
+	if err != nil {
+		return nil, err
+	}
+	for _, iface := range ifaces {
+		if iface.Flags&net.FlagUp == 0 {
+			continue // interface down
+		}
+		if iface.Flags&net.FlagLoopback != 0 {
+			continue // loopback interface
+		}
+		addrs, err := iface.Addrs()
+		if err != nil {
+			return nil, err
+		}
+		for _, addr := range addrs {
+			ip := getIpFromAddr(addr)
+			if ip == nil {
+				continue
+			}
+			return ip, nil
+		}
+	}
+	return nil, errors.New("connected to the network?")
+}
+
+func getIpFromAddr(addr net.Addr) net.IP {
+	var ip net.IP
+	switch v := addr.(type) {
+	case *net.IPNet:
+		ip = v.IP
+	case *net.IPAddr:
+		ip = v.IP
+	}
+	if ip == nil || ip.IsLoopback() {
+		return nil
+	}
+	ip = ip.To4()
+	if ip == nil {
+		return nil // not an ipv4 address
+	}
+
+	return ip
+}
+
+func TestHttpServer(t *testing.T) {
+	config.Init()
+
+	protoMap := make(map[string]model.Protocol)
+	config.GlobalDefinition = &model.GlobalCfg{
+		Cse: model.CseStruct{
+			Protocols: protoMap,
+		},
+	}
+
+	defaultChain := make(map[string]string)
+	defaultChain["default"] = ""
+
+	config.GlobalDefinition.Cse.Handler.Chain.Provider = defaultChain
+	config.GlobalDefinition.Cse.Handler.Chain.Consumer = defaultChain
+
+	f, err := server.GetServerFunc("http")
+	assert.NoError(t, err)
+
+	// case split port error
+	s := f(server.Options{
+		Address:   "0.0.0.130201",
+		ChainName: "default",
+	})
+	err = s.Start()
+	assert.Error(t, err)
+
+	// case invalid host
+	s = f(server.Options{
+		Address:   "2.2.2.1990:30201",
+		ChainName: "default",
+	})
+	err = s.Start()
+	assert.Error(t, err)
+
+	// case listening error
+	s = f(server.Options{
+		Address:   "99.0.0.1:30201",
+		ChainName: "default",
+	})
+	err = s.Start()
+	assert.Error(t, err)
+
+	// case listen on 127.0.0.1
+	s = f(server.Options{
+		Address:   "127.0.0.1:30201",
+		ChainName: "default",
+	})
+	err = s.Start()
+	assert.NoError(t, err)
+
+	s.Stop()
+	time.Sleep(time.Second * 5)
+
+	// case forbidden to listen on 0.0.0.0
+	runtime.Role = common.RoleSidecar
+	s = f(server.Options{
+		Address:   "0.0.0.0:50201",
+		ChainName: "default",
+	})
+	err = s.Start()
+	assert.Error(t, err)
+
+	// start sider
+	eIP, err := externalIP()
+	runtime.Role = common.RoleSidecar
+	if err != nil {
+		return
+	}
+	s = f(server.Options{
+		Address:   eIP.String() + ":30303",
+		ChainName: "default",
+	})
+
+	runtime.Role = common.RoleSidecar
+	err = s.Start()
+	assert.NoError(t, err)
+
+	s.Stop()
+	time.Sleep(time.Second * 5)
+}
+
+func TestHttpServer_Start(t *testing.T) {
+	config.Init()
+
+	protoMap := make(map[string]model.Protocol)
+	config.GlobalDefinition = &model.GlobalCfg{
+		Cse: model.CseStruct{
+			Protocols: protoMap,
+		},
+	}
+
+	defaultChain := make(map[string]string)
+	defaultChain["default"] = ""
+
+	config.GlobalDefinition.Cse.Handler.Chain.Provider = defaultChain
+	config.GlobalDefinition.Cse.Handler.Chain.Consumer = defaultChain
+
+	f, err := server.GetServerFunc("http")
+	assert.NoError(t, err)
+	s := f(server.Options{
+		Address:   "127.0.0.1:40201",
+		ChainName: "default",
+	})
+
+	s.Register(map[string]string{})
+
+	err = s.Start()
+	assert.NoError(t, err)
+
+	name := s.String()
+	assert.Equal(t, "http", name)
+
+	var wg sync.WaitGroup
+	wg.Add(1)
+	go func(wg *sync.WaitGroup) {
+		defer wg.Done()
+		resp, err := http.Get("http://127.0.0.1:40201")
+		assert.NoError(t, err)
+		if err != nil {
+			return
+		}
+		defer resp.Body.Close()
+
+	}(&wg)
+
+	wg.Wait()
+
+	err = s.Stop()
+	assert.NoError(t, err)
+}
+
+func TestGenTag(t *testing.T) {
+	str := genTag("s1", "s2", "s3")
+	assert.Equal(t, "s1.s2.s3", str)
+}
diff --git a/scripts/travis/goSecureChecker.sh b/scripts/travis/goSecureChecker.sh
index afe890a..aa90c80 100644
--- a/scripts/travis/goSecureChecker.sh
+++ b/scripts/travis/goSecureChecker.sh
@@ -1,4 +1,5 @@
-#!/usr/bin/env bash
+#!/bin/sh
+
 # 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.
@@ -14,15 +15,14 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-gosec ./... > result.txt
-cat result.txt
-rm -rf result.txt
-issueCount=$(gosec ./... | grep "Issues"  |awk -F":" '{print $2}')
-if [ $? == 0 ] && [[ $issueCount -le 35 ]] ; then
+gosec -fmt=json -out=results.json ./...
+cat results.json
+issueCount=$(tail -7 results.json | grep '"found":' | sed 's/[[:space:]]//g' | awk -F":" '{print $2}')
+rm -rf results.json
+if (($? == 0 && 35 > $issueCount)); then
 	echo "No GoSecure warnings found"
 	exit 0
 else
-  echo "GoSecure Warnings found"
+	echo "GoSecure Warnings found"
 	exit 1
 fi
-