Skip to content

Commit

Permalink
add jwt support (#844)
Browse files Browse the repository at this point in the history
  • Loading branch information
tianxiaoliang committed May 12, 2020
1 parent f61a697 commit edda520
Show file tree
Hide file tree
Showing 39 changed files with 774 additions and 134 deletions.
4 changes: 2 additions & 2 deletions chassis_api.go
Expand Up @@ -30,8 +30,8 @@ import (
// prometheus reporter for circuit breaker metrics
_ "github.com/go-chassis/go-chassis/third_party/forked/afex/hystrix-go/hystrix/reporter"
// aes package handles security related plugins
_ "github.com/go-chassis/go-chassis/security/plugins/aes"
_ "github.com/go-chassis/go-chassis/security/plugins/plain"
_ "github.com/go-chassis/go-chassis/security/cipher/plugins/aes"
_ "github.com/go-chassis/go-chassis/security/cipher/plugins/plain"
//config servers
_ "github.com/go-chassis/go-archaius/source/remote"
_ "github.com/go-chassis/go-archaius/source/remote/kie"
Expand Down
8 changes: 1 addition & 7 deletions control/panel_test.go
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/go-chassis/go-chassis/core/invocation"
"github.com/go-chassis/go-chassis/core/lager"
"github.com/stretchr/testify/assert"
"os"
"testing"
)

Expand All @@ -17,7 +16,6 @@ func TestInstallPlugin(t *testing.T) {

}
func TestInit(t *testing.T) {

lager.Init(&lager.Options{
LoggerLevel: "INFO",
RollingPolicy: "size",
Expand All @@ -27,15 +25,11 @@ func TestInit(t *testing.T) {
Infra: "",
},
}
gopath := os.Getenv("GOPATH")
os.Setenv("CHASSIS_HOME", gopath+"/src/github.com/go-chassis/go-chassis/examples/discovery/client/")
err := config.Init()
assert.NoError(t, err)
opts := control.Options{
Infra: config.GlobalDefinition.Panel.Infra,
Address: config.GlobalDefinition.Panel.Settings["address"],
}
err = control.Init(opts)
err := control.Init(opts)
assert.NoError(t, err)
opts.Infra = "xxx"
err = control.Init(opts)
Expand Down
7 changes: 0 additions & 7 deletions control/servicecomb/consumer_qps_operation_meta_test.go
@@ -1,21 +1,14 @@
package servicecomb_test

import (
"os"
"testing"

arhcaiusPanel "github.com/go-chassis/go-chassis/control/servicecomb"
"github.com/go-chassis/go-chassis/core/config"
"github.com/go-chassis/go-chassis/core/invocation"
"github.com/go-chassis/go-chassis/examples/schemas/helloworld"
"github.com/stretchr/testify/assert"
)

func init() {
gopath := os.Getenv("GOPATH")
os.Setenv("CHASSIS_HOME", gopath+"/src/github.com/go-chassis/go-chassis/examples/discovery/client/")
config.Init()
}
func TestGetConsumerKey(t *testing.T) {
i := &invocation.Invocation{
MicroServiceName: "service1",
Expand Down
30 changes: 18 additions & 12 deletions control/servicecomb/panel_test.go
@@ -1,39 +1,47 @@
package servicecomb_test

import (
_ "github.com/go-chassis/go-chassis/initiator"

"github.com/go-chassis/go-archaius"
"github.com/go-chassis/go-chassis/control"
_ "github.com/go-chassis/go-chassis/control/servicecomb"
"github.com/go-chassis/go-chassis/core/common"
"github.com/go-chassis/go-chassis/core/config"
"github.com/go-chassis/go-chassis/core/invocation"
"github.com/go-chassis/go-chassis/core/loadbalancer"
_ "github.com/go-chassis/go-chassis/initiator"
"github.com/stretchr/testify/assert"
"os"
"testing"
)

func init() {
gopath := os.Getenv("GOPATH")
os.Setenv("CHASSIS_HOME", gopath+"/src/github.com/go-chassis/go-chassis/examples/discovery/client/")
config.Init()
archaius.Init(archaius.WithMemorySource())
archaius.Set("cse.loadbalance.strategy.name", loadbalancer.StrategyRandom)
archaius.Set("cse.loadbalance.strategy.Server.name", loadbalancer.StrategyLatency)
archaius.Set("cse.loadbalance.strategy.name", loadbalancer.StrategyLatency)
archaius.Set("cse.flowcontrol.Consumer.qps.limit.Server", 100)
archaius.Set("cse.isolation.Consumer.maxConcurrentRequests", 100)
err := config.ReadLBFromArchaius()
if err != nil {
panic(err)
}
err = config.ReadHystrixFromArchaius()
if err != nil {
panic(err)
}
}
func TestPanel_GetLoadBalancing(t *testing.T) {

opts := control.Options{
Infra: "archaius",
}
err := control.Init(opts)
assert.NoError(t, err)

t.Log("lb")
inv := invocation.Invocation{
SourceMicroService: "",
MicroServiceName: "Server",
MicroServiceName: "Server",
}
c := control.DefaultPanel.GetLoadBalancing(inv)
assert.Equal(t, loadbalancer.StrategyRandom, c.Strategy)
assert.Equal(t, loadbalancer.StrategyLatency, c.Strategy)

inv = invocation.Invocation{
SourceMicroService: "",
Expand All @@ -49,11 +57,9 @@ func TestPanel_GetLoadBalancing(t *testing.T) {
c = control.DefaultPanel.GetLoadBalancing(inv)
assert.Equal(t, loadbalancer.StrategyLatency, c.Strategy)

t.Log("cb ")
command, cb := control.DefaultPanel.GetCircuitBreaker(inv, common.Consumer)
assert.Equal(t, "Consumer.fake", command)
assert.Equal(t, 100, cb.MaxConcurrentRequests)
t.Log("rl ")
inv.MicroServiceName = "Server"
rl := control.DefaultPanel.GetRateLimiting(inv, common.Consumer)
assert.Equal(t, 100, rl.Rate)
Expand Down
1 change: 1 addition & 0 deletions control/servicecomb/transfer.go
Expand Up @@ -63,6 +63,7 @@ func saveEachLB(k string, raw model.LoadBalancingSpec) string { // return update
SessionTimeoutInSeconds: raw.SessionStickinessRule.SessionTimeoutInSeconds,
SuccessiveFailedTimes: raw.SessionStickinessRule.SuccessiveFailedTimes,
}
openlogging.Info(fmt.Sprintf("save lb config [%s] [%v]", k, raw))
setDefaultLBValue(&c)
LBConfigCache.Set(k, c, 0)
return k
Expand Down
9 changes: 2 additions & 7 deletions control/servicecomb/transfer_test.go
@@ -1,7 +1,6 @@
package servicecomb_test

import (
"os"
"testing"

"github.com/go-chassis/go-chassis/control"
Expand Down Expand Up @@ -40,7 +39,7 @@ func TestSaveDefaultToLBCache(t *testing.T) {
t.Log("==delete outdated key")
servicecomb.SaveToLBCache(&model.LoadBalancing{
Strategy: map[string]string{
"name": loadbalancer.StrategyRoundRobin,
"name": loadbalancer.StrategyLatency,
},
AnyService: map[string]model.LoadBalancingSpec{
"test": {
Expand All @@ -63,15 +62,11 @@ func TestSaveToCBCache(t *testing.T) {
Infra: "",
},
}
gopath := os.Getenv("GOPATH")
os.Setenv("CHASSIS_HOME", gopath+"/src/github.com/go-chassis/go-chassis/examples/discovery/client/")
err := config.Init()
assert.NoError(t, err)
opts := control.Options{
Infra: config.GlobalDefinition.Panel.Infra,
Address: config.GlobalDefinition.Panel.Settings["address"],
}
err = control.Init(opts)
control.Init(opts)
servicecomb.SaveToCBCache(config.GetHystrixConfig())
c, _ := servicecomb.CBConfigCache.Get("Consumer")
assert.Equal(t, 100, c.(hystrix.CommandConfig).MaxConcurrentRequests)
Expand Down
33 changes: 8 additions & 25 deletions core/config/config.go
Expand Up @@ -173,13 +173,11 @@ func ReadGlobalConfigFromArchaius() error {
func ReadLBFromArchaius() error {
lbMutex.Lock()
defer lbMutex.Unlock()
lbDef := model.LBWrapper{}
err := archaius.UnmarshalConfig(&lbDef)
lbConfig = &model.LBWrapper{}
err := archaius.UnmarshalConfig(lbConfig)
if err != nil {
return err
}
lbConfig = &lbDef

return nil
}

Expand All @@ -194,33 +192,15 @@ func ReadMonitorFromArchaius() error {
return nil
}

//ReadMonitorFromFile read monitor config from local file conf/monitoring.yaml
func ReadMonitorFromFile() error {
defPath := fileutil.MonitoringConfigPath()
data, err := ioutil.ReadFile(defPath)
if err != nil {
openlogging.Error("Get monitor config from file failed. " + err.Error())
return err
}
MonitorCfgDef = &model.MonitorCfg{}
err = yaml.Unmarshal(data, &MonitorCfgDef)
if err != nil {
openlogging.Error("Get monitor config from file failed. " + err.Error())
return err
}
return nil
}

// ReadHystrixFromArchaius is unmarshal hystrix configuration file(circuit_breaker.yaml)
func ReadHystrixFromArchaius() error {
cbMutex.RLock()
defer cbMutex.RUnlock()
hystrixCnf := model.HystrixConfigWrapper{}
err := archaius.UnmarshalConfig(&hystrixCnf)
HystrixConfig = &model.HystrixConfigWrapper{}
err := archaius.UnmarshalConfig(&HystrixConfig)
if err != nil {
return err
}
HystrixConfig = &hystrixCnf
return nil
}

Expand Down Expand Up @@ -301,7 +281,10 @@ func GetLoadBalancing() *model.LoadBalancing {

//GetHystrixConfig return cb config
func GetHystrixConfig() *model.HystrixConfig {
return HystrixConfig.HystrixConfig
if HystrixConfig != nil {
return HystrixConfig.HystrixConfig
}
return nil
}

// Init is initialize the configuration directory, archaius, route rule, and schema
Expand Down
2 changes: 1 addition & 1 deletion core/registry/heartbeat_test.go
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/go-chassis/go-chassis/core/registry"
_ "github.com/go-chassis/go-chassis/core/registry/servicecenter"
"github.com/go-chassis/go-chassis/pkg/runtime"
_ "github.com/go-chassis/go-chassis/security/plugins/plain"
_ "github.com/go-chassis/go-chassis/security/cipher/plugins/plain"
"github.com/stretchr/testify/assert"
"os"
"path/filepath"
Expand Down
2 changes: 1 addition & 1 deletion core/registry/servicecenter/cache_test.go
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/go-chassis/go-chassis/pkg/runtime"
"github.com/go-chassis/go-chassis/pkg/scclient"
"github.com/go-chassis/go-chassis/pkg/util/tags"
_ "github.com/go-chassis/go-chassis/security/plugins/plain"
_ "github.com/go-chassis/go-chassis/security/cipher/plugins/plain"
"github.com/hashicorp/go-version"
"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/util/rand"
Expand Down
2 changes: 1 addition & 1 deletion core/registry/servicecenter/client_test.go
Expand Up @@ -4,7 +4,7 @@ import (
"github.com/go-chassis/go-chassis/core/config"
"github.com/go-chassis/go-chassis/core/lager"
client "github.com/go-chassis/go-chassis/pkg/scclient"
_ "github.com/go-chassis/go-chassis/security/plugins/plain"
_ "github.com/go-chassis/go-chassis/security/cipher/plugins/plain"
"github.com/stretchr/testify/assert"
"net/url"
"os"
Expand Down
2 changes: 1 addition & 1 deletion core/registry/servicecenter/servicecenter_test.go
Expand Up @@ -11,7 +11,7 @@ import (
_ "github.com/go-chassis/go-chassis/core/registry/servicecenter"
"github.com/go-chassis/go-chassis/pkg/runtime"
"github.com/go-chassis/go-chassis/pkg/scclient"
_ "github.com/go-chassis/go-chassis/security/plugins/plain"
_ "github.com/go-chassis/go-chassis/security/cipher/plugins/plain"
"github.com/stretchr/testify/assert"
)

Expand Down
8 changes: 4 additions & 4 deletions security/common/common.go → core/tls/common.go
@@ -1,20 +1,20 @@
package common
package tls

import (
"crypto/tls"
"crypto/x509"
"encoding/pem"
"errors"
"fmt"
"github.com/go-chassis/go-chassis/security/cipher"
"io/ioutil"
"strings"

security2 "github.com/go-chassis/foundation/security"
"github.com/go-chassis/go-chassis/core/common"
"github.com/go-chassis/go-chassis/pkg/string"
"github.com/go-chassis/go-chassis/security"
//this import used for plain cipher
_ "github.com/go-chassis/go-chassis/security/plugins/plain"
_ "github.com/go-chassis/go-chassis/security/cipher/plugins/plain"
)

//SSLConfig struct stores the necessary info for SSL configuration
Expand Down Expand Up @@ -132,7 +132,7 @@ func getTLSConfig(sslConfig *SSLConfig, role string) (tlsConfig *tls.Config, err
var certs []tls.Certificate
if !(role == common.Client && sslConfig.KeyFile == "" && sslConfig.CertFile == "") {
var cipherPlugin security2.Cipher
if f, err := security.GetCipherNewFunc(sslConfig.CipherPlugin); err != nil {
if f, err := cipher.GetCipherNewFunc(sslConfig.CipherPlugin); err != nil {
return nil, fmt.Errorf("get cipher plugin [%s] failed, %v", sslConfig.CipherPlugin, err)
} else if cipherPlugin = f(); cipherPlugin == nil {
return nil, errors.New("invalid cipher plugin")
Expand Down

0 comments on commit edda520

Please sign in to comment.