Skip to content

Commit

Permalink
fix: test case issue
Browse files Browse the repository at this point in the history
  • Loading branch information
tikazyq committed Nov 19, 2024
1 parent 2d558c1 commit 3dc66e4
Show file tree
Hide file tree
Showing 16 changed files with 102 additions and 238 deletions.
21 changes: 0 additions & 21 deletions core/config/base.go

This file was deleted.

11 changes: 0 additions & 11 deletions core/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,9 @@ func TestInitConfig(t *testing.T) {
require.NoError(t, err, "Failed to initialize config")

// Test default values
assert.Equal(t, "global.edition.community", viper.GetString("edition"), "Unexpected default value for edition")
assert.Equal(t, "localhost", viper.GetString("mongo.host"), "Unexpected default value for mongo.host")
assert.Equal(t, 27017, viper.GetInt("mongo.port"), "Unexpected default value for mongo.port")
assert.Equal(t, "crawlab_test", viper.GetString("mongo.db"), "Unexpected default value for mongo.db")
assert.Equal(t, "0.0.0.0", viper.GetString("server.host"), "Unexpected default value for server.host")
assert.Equal(t, 8000, viper.GetInt("server.port"), "Unexpected default value for server.port")
assert.Equal(t, "localhost", viper.GetString("grpc.host"), "Unexpected default value for grpc.host")
assert.Equal(t, 9666, viper.GetInt("grpc.port"), "Unexpected default value for grpc.port")
assert.Equal(t, "Crawlab2021!", viper.GetString("grpc.authKey"), "Unexpected default value for grpc.authKey")
assert.Equal(t, "http://localhost:8000", viper.GetString("api.endpoint"), "Unexpected default value for api.endpoint")
assert.Equal(t, "/var/log/crawlab", viper.GetString("log.path"), "Unexpected default value for log.path")

// Test environment variable override
os.Setenv("CRAWLAB_MONGO_HOST", "mongodb.example.com")
Expand Down Expand Up @@ -74,7 +66,4 @@ server:
assert.Equal(t, "mongodb.custom.com", viper.GetString("mongo.host"), "Unexpected value for mongo.host from config file")
assert.Equal(t, 27018, viper.GetInt("mongo.port"), "Unexpected value for mongo.port from config file")
assert.Equal(t, 8001, viper.GetInt("server.port"), "Unexpected value for server.port from config file")

// Values not in config file should still use defaults
assert.Equal(t, "Crawlab2021!", viper.GetString("grpc.authKey"), "Unexpected default value for grpc.authKey")
}
23 changes: 0 additions & 23 deletions core/config/path.go

This file was deleted.

13 changes: 0 additions & 13 deletions core/constants/grpc.go

This file was deleted.

4 changes: 2 additions & 2 deletions core/grpc/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ func (c *GrpcClient) connect() (err error) {
// connection options
opts := []grpc.DialOption{
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithChainUnaryInterceptor(middlewares.GetAuthTokenUnaryChainInterceptor()),
grpc.WithChainStreamInterceptor(middlewares.GetAuthTokenStreamChainInterceptor()),
grpc.WithChainUnaryInterceptor(middlewares.GetGrpcClientAuthTokenUnaryChainInterceptor()),
grpc.WithChainStreamInterceptor(middlewares.GetGrpcClientAuthTokenStreamChainInterceptor()),
}

// create new client connection
Expand Down
15 changes: 8 additions & 7 deletions core/grpc/middlewares/auth_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ package middlewares

import (
"context"
"github.com/crawlab-team/crawlab/core/constants"
"github.com/crawlab-team/crawlab/core/errors"
"github.com/crawlab-team/crawlab/core/utils"
"github.com/grpc-ecosystem/go-grpc-middleware/auth"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)

func GetAuthTokenFunc() grpc_auth.AuthFunc {
const GrpcHeaderAuthorization = "authorization"

func GetGrpcServerAuthTokenFunc() grpc_auth.AuthFunc {
return func(ctx context.Context) (ctx2 context.Context, err error) {
// authentication (token verification)
md, ok := metadata.FromIncomingContext(ctx)
Expand All @@ -19,7 +20,7 @@ func GetAuthTokenFunc() grpc_auth.AuthFunc {
}

// auth key from incoming context
res, ok := md[constants.GrpcHeaderAuthorization]
res, ok := md[GrpcHeaderAuthorization]
if !ok {
return ctx, errors.ErrorGrpcUnauthorized
}
Expand All @@ -38,18 +39,18 @@ func GetAuthTokenFunc() grpc_auth.AuthFunc {
}
}

func GetAuthTokenUnaryChainInterceptor() grpc.UnaryClientInterceptor {
func GetGrpcClientAuthTokenUnaryChainInterceptor() grpc.UnaryClientInterceptor {
// set auth key
md := metadata.Pairs(constants.GrpcHeaderAuthorization, utils.GetAuthKey())
md := metadata.Pairs(GrpcHeaderAuthorization, utils.GetAuthKey())
return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
ctx = metadata.NewOutgoingContext(context.Background(), md)
return invoker(ctx, method, req, reply, cc, opts...)
}
}

func GetAuthTokenStreamChainInterceptor() grpc.StreamClientInterceptor {
func GetGrpcClientAuthTokenStreamChainInterceptor() grpc.StreamClientInterceptor {
// set auth key
md := metadata.Pairs(constants.GrpcHeaderAuthorization, utils.GetAuthKey())
md := metadata.Pairs(GrpcHeaderAuthorization, utils.GetAuthKey())
return func(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string, streamer grpc.Streamer, opts ...grpc.CallOption) (grpc.ClientStream, error) {
ctx = metadata.NewOutgoingContext(context.Background(), md)
s, err := streamer(ctx, desc, cc, method, opts...)
Expand Down
4 changes: 2 additions & 2 deletions core/grpc/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,11 @@ func newGrpcServer() *GrpcServer {
svr.svr = grpc.NewServer(
grpcmiddleware.WithUnaryServerChain(
grpcrecovery.UnaryServerInterceptor(recoveryOpts...),
grpcauth.UnaryServerInterceptor(middlewares.GetAuthTokenFunc()),
grpcauth.UnaryServerInterceptor(middlewares.GetGrpcServerAuthTokenFunc()),
),
grpcmiddleware.WithStreamServerChain(
grpcrecovery.StreamServerInterceptor(recoveryOpts...),
grpcauth.StreamServerInterceptor(middlewares.GetAuthTokenFunc()),
grpcauth.StreamServerInterceptor(middlewares.GetGrpcServerAuthTokenFunc()),
),
)

Expand Down
1 change: 0 additions & 1 deletion core/interfaces/node_config_service.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package interfaces

type NodeConfigService interface {
WithConfigPath
Init() error
Reload() error
GetBasicNodeInfo() Entity
Expand Down
2 changes: 0 additions & 2 deletions core/interfaces/node_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@ package interfaces

type NodeService interface {
Module
WithConfigPath
GetConfigService() NodeConfigService
}
65 changes: 6 additions & 59 deletions core/node/config/config.go
Original file line number Diff line number Diff line change
@@ -1,68 +1,15 @@
package config

import (
"github.com/crawlab-team/crawlab/core/constants"
"github.com/crawlab-team/crawlab/core/entity"
"github.com/crawlab-team/crawlab/core/utils"
"github.com/spf13/viper"
)

type Config entity.NodeInfo

type Options struct {
Key string
Name string
IsMaster bool
AuthKey string
MaxRunners int
}

var DefaultMaxRunner = 8

var DefaultConfigOptions = &Options{
Key: utils.NewUUIDString(),
IsMaster: utils.IsMaster(),
AuthKey: constants.DefaultGrpcAuthKey,
MaxRunners: 0,
}

func NewConfig(opts *Options) (cfg *Config) {
if opts == nil {
opts = DefaultConfigOptions
}
if opts.Key == "" {
if viper.GetString("node.key") != "" {
opts.Key = viper.GetString("node.key")
} else {
opts.Key = utils.NewUUIDString()
}
}
if opts.Name == "" {
if viper.GetString("node.name") != "" {
opts.Name = viper.GetString("node.name")
} else {
opts.Name = opts.Key
}
}
if opts.AuthKey == "" {
if viper.GetString("grpc.authKey") != "" {
opts.AuthKey = viper.GetString("grpc.authKey")
} else {
opts.AuthKey = constants.DefaultGrpcAuthKey
}
}
if opts.MaxRunners == 0 {
if viper.GetInt("task.handler.maxRunners") != 0 {
opts.MaxRunners = viper.GetInt("task.handler.maxRunners")
} else {
opts.MaxRunners = DefaultMaxRunner
}
}
return &Config{
Key: opts.Key,
Name: opts.Name,
IsMaster: opts.IsMaster,
AuthKey: opts.AuthKey,
MaxRunners: opts.MaxRunners,
func newConfig() (cfg *entity.NodeInfo) {
return &entity.NodeInfo{
Key: utils.GetNodeKey(),
Name: utils.GetNodeName(),
IsMaster: utils.IsMaster(),
MaxRunners: utils.GetNodeMaxRunners(),
}
}
49 changes: 19 additions & 30 deletions core/node/config/config_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package config

import (
"encoding/json"
"github.com/crawlab-team/crawlab/core/config"
"github.com/apex/log"
"github.com/crawlab-team/crawlab/core/entity"
"github.com/crawlab-team/crawlab/core/interfaces"
"github.com/crawlab-team/crawlab/core/utils"
Expand All @@ -13,38 +13,42 @@ import (
)

type Service struct {
cfg *Config
path string
cfg *entity.NodeInfo
}

func (svc *Service) Init() (err error) {
metadataConfigPath := utils.GetMetadataConfigPath()

// check config directory path
configDirPath := filepath.Dir(svc.path)
configDirPath := filepath.Dir(metadataConfigPath)
if !utils.Exists(configDirPath) {
if err := os.MkdirAll(configDirPath, os.FileMode(0766)); err != nil {
return trace.TraceError(err)
}
}

if !utils.Exists(svc.path) {
// not exists, set to default config
// and create a config file for persistence
svc.cfg = NewConfig(nil)
if !utils.Exists(metadataConfigPath) {
// not exists, set to default config, and create a config file for persistence
svc.cfg = newConfig()
data, err := json.Marshal(svc.cfg)
if err != nil {
return trace.TraceError(err)
log.Errorf("marshal config error: %v", err)
return err
}
if err := os.WriteFile(svc.path, data, os.FileMode(0766)); err != nil {
return trace.TraceError(err)
if err := os.WriteFile(metadataConfigPath, data, os.FileMode(0766)); err != nil {
log.Errorf("write config file error: %v", err)
return err
}
} else {
// exists, read and set to config
data, err := os.ReadFile(svc.path)
data, err := os.ReadFile(metadataConfigPath)
if err != nil {
return trace.TraceError(err)
log.Errorf("read config file error: %v", err)
return err
}
if err := json.Unmarshal(data, svc.cfg); err != nil {
return trace.TraceError(err)
log.Errorf("unmarshal config error: %v", err)
return err
}
}

Expand Down Expand Up @@ -86,27 +90,12 @@ func (svc *Service) GetMaxRunners() (res int) {
return svc.cfg.MaxRunners
}

func (svc *Service) GetConfigPath() (path string) {
return svc.path
}

func (svc *Service) SetConfigPath(path string) {
svc.path = path
}

func newNodeConfigService() (svc2 interfaces.NodeConfigService, err error) {
// cfg
cfg := NewConfig(nil)

// config service
svc := &Service{
cfg: cfg,
cfg: newConfig(),
}

// normalize config path
cfgPath := config.GetConfigPath()
svc.SetConfigPath(cfgPath)

// init
if err := svc.Init(); err != nil {
return nil, err
Expand Down
Loading

0 comments on commit 3dc66e4

Please sign in to comment.