Skip to content

Commit

Permalink
[#2063]: fix: properly parse includes outside the Configuration plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
rustatian authored Nov 22, 2024
2 parents bd93d5c + 642e83e commit dfb71af
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 3 deletions.
17 changes: 14 additions & 3 deletions internal/rpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ func NewClient(cfg string, flags []string) (*rpc.Client, error) {
return nil, err
}

// automatically inject ENV variables using ${ENV} pattern
expandEnvViper(v)

// override config Flags
if len(flags) > 0 {
for _, f := range flags {
Expand All @@ -48,6 +45,20 @@ func NewClient(cfg string, flags []string) (*rpc.Client, error) {
}
}

ver := v.Get(versionKey)
if ver == nil {
return nil, fmt.Errorf("rr configuration file should contain a version e.g: version: 3")
}

if _, ok := ver.(string); !ok {
return nil, fmt.Errorf("version should be a string: `version: \"3\"`, actual type is: %T", ver)
}

err = handleInclude(ver.(string), v)
if err != nil {
return nil, fmt.Errorf("failed to handle includes: %w", err)
}

// rpc.listen might be set by the -o flags or env variable
if !v.IsSet(rpcPlugin.PluginName) {
return nil, errors.New("rpc service not specified in the configuration. Tip: add\n rpc:\n\r listen: rr_rpc_address")
Expand Down
14 changes: 14 additions & 0 deletions internal/rpc/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,20 @@ func TestNewClient_SuccessfullyConnected(t *testing.T) {
defer func() { assert.NoError(t, c.Close()) }()
}

func TestNewClient_WithIncludes(t *testing.T) {
l, err := net.Listen("tcp", "127.0.0.1:6010")
assert.NoError(t, err)

defer func() { assert.NoError(t, l.Close()) }()

c, err := rpc.NewClient("test/include1/.rr.yaml", nil)

assert.NotNil(t, c)
assert.NoError(t, err)

assert.NoError(t, c.Close())
}

func TestNewClient_SuccessfullyConnectedOverride(t *testing.T) {
l, err := net.Listen("tcp", "127.0.0.1:55554")
assert.NoError(t, err)
Expand Down
66 changes: 66 additions & 0 deletions internal/rpc/includes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package rpc

import (
"github.com/roadrunner-server/errors"
"github.com/spf13/viper"
)

const (
versionKey string = "version"
includeKey string = "include"
defaultConfigVersion string = "3"
prevConfigVersion string = "2.7"
)

func getConfiguration(path string) (map[string]any, string, error) {
v := viper.New()
v.SetConfigFile(path)
err := v.ReadInConfig()
if err != nil {
return nil, "", err
}

// get configuration version
ver := v.Get(versionKey)
if ver == nil {
return nil, "", errors.Str("rr configuration file should contain a version e.g: version: 2.7")
}

if _, ok := ver.(string); !ok {
return nil, "", errors.Errorf("type of version should be string, actual: %T", ver)
}

// automatically inject ENV variables using ${ENV} pattern
expandEnvViper(v)

return v.AllSettings(), ver.(string), nil
}

func handleInclude(rootVersion string, v *viper.Viper) error {
// automatically inject ENV variables using ${ENV} pattern
// root config
expandEnvViper(v)

ifiles := v.GetStringSlice(includeKey)
if ifiles == nil {
return nil
}

for _, file := range ifiles {
config, version, err := getConfiguration(file)
if err != nil {
return err
}

if version != rootVersion {
return errors.Str("version in included file must be the same as in root")
}

// overriding configuration
for key, val := range config {
v.Set(key, val)
}
}

return nil
}
2 changes: 2 additions & 0 deletions internal/rpc/test/config_rpc_conn_err.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
version: "3"

rpc:
listen: tcp://127.0.0.1:55554
1 change: 1 addition & 0 deletions internal/rpc/test/config_rpc_empty.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version: "3"
2 changes: 2 additions & 0 deletions internal/rpc/test/config_rpc_ok.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
version: "3"

rpc:
listen: tcp://127.0.0.1:55554
2 changes: 2 additions & 0 deletions internal/rpc/test/config_rpc_ok_env.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
version: "3"

rpc:
listen: ${RPC}
2 changes: 2 additions & 0 deletions internal/rpc/test/config_rpc_wrong.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
version: "3"

rpc:
$foo bar
5 changes: 5 additions & 0 deletions internal/rpc/test/include1/.rr-include.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
version: "3"

rpc:
listen: tcp://127.0.0.1:6010

11 changes: 11 additions & 0 deletions internal/rpc/test/include1/.rr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: "3"

server:
command: "php app-with-domain-specific-routes.php"

logs:
level: debug
mode: development

include:
- test/include1/.rr-include.yaml

0 comments on commit dfb71af

Please sign in to comment.