diff options
author | Valery Piashchynski <[email protected]> | 2024-11-22 23:40:10 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2024-11-22 23:40:10 +0100 |
commit | dfb71afb844365a1fc0c819235f64a2417606a33 (patch) | |
tree | 9df5c495a93c60d1d0f7053e0448b7e7d7d06b64 | |
parent | bd93d5c005b48a086646145827e302e2fd5d5872 (diff) | |
parent | 642e83ef2771ef6b99571626ecc188c9e41aeeb7 (diff) |
[#2063]: fix: properly parse includes outside the Configuration plugin
-rw-r--r-- | internal/rpc/client.go | 17 | ||||
-rw-r--r-- | internal/rpc/client_test.go | 14 | ||||
-rw-r--r-- | internal/rpc/includes.go | 66 | ||||
-rw-r--r-- | internal/rpc/test/config_rpc_conn_err.yaml | 2 | ||||
-rw-r--r-- | internal/rpc/test/config_rpc_empty.yaml | 1 | ||||
-rw-r--r-- | internal/rpc/test/config_rpc_ok.yaml | 2 | ||||
-rw-r--r-- | internal/rpc/test/config_rpc_ok_env.yaml | 2 | ||||
-rw-r--r-- | internal/rpc/test/config_rpc_wrong.yaml | 2 | ||||
-rw-r--r-- | internal/rpc/test/include1/.rr-include.yaml | 5 | ||||
-rw-r--r-- | internal/rpc/test/include1/.rr.yaml | 11 |
10 files changed, 119 insertions, 3 deletions
diff --git a/internal/rpc/client.go b/internal/rpc/client.go index 26d60d24..ba739d87 100644 --- a/internal/rpc/client.go +++ b/internal/rpc/client.go @@ -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 { @@ -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") diff --git a/internal/rpc/client_test.go b/internal/rpc/client_test.go index 453491ff..4a060f33 100644 --- a/internal/rpc/client_test.go +++ b/internal/rpc/client_test.go @@ -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) diff --git a/internal/rpc/includes.go b/internal/rpc/includes.go new file mode 100644 index 00000000..87b00001 --- /dev/null +++ b/internal/rpc/includes.go @@ -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 +} diff --git a/internal/rpc/test/config_rpc_conn_err.yaml b/internal/rpc/test/config_rpc_conn_err.yaml index c7d31056..5438198f 100644 --- a/internal/rpc/test/config_rpc_conn_err.yaml +++ b/internal/rpc/test/config_rpc_conn_err.yaml @@ -1,2 +1,4 @@ +version: "3" + rpc: listen: tcp://127.0.0.1:55554 diff --git a/internal/rpc/test/config_rpc_empty.yaml b/internal/rpc/test/config_rpc_empty.yaml index e69de29b..5db6fe96 100644 --- a/internal/rpc/test/config_rpc_empty.yaml +++ b/internal/rpc/test/config_rpc_empty.yaml @@ -0,0 +1 @@ +version: "3" diff --git a/internal/rpc/test/config_rpc_ok.yaml b/internal/rpc/test/config_rpc_ok.yaml index c7d31056..5438198f 100644 --- a/internal/rpc/test/config_rpc_ok.yaml +++ b/internal/rpc/test/config_rpc_ok.yaml @@ -1,2 +1,4 @@ +version: "3" + rpc: listen: tcp://127.0.0.1:55554 diff --git a/internal/rpc/test/config_rpc_ok_env.yaml b/internal/rpc/test/config_rpc_ok_env.yaml index fd0d3f11..f5ae461e 100644 --- a/internal/rpc/test/config_rpc_ok_env.yaml +++ b/internal/rpc/test/config_rpc_ok_env.yaml @@ -1,2 +1,4 @@ +version: "3" + rpc: listen: ${RPC} diff --git a/internal/rpc/test/config_rpc_wrong.yaml b/internal/rpc/test/config_rpc_wrong.yaml index 8282ee1f..c4ff9c46 100644 --- a/internal/rpc/test/config_rpc_wrong.yaml +++ b/internal/rpc/test/config_rpc_wrong.yaml @@ -1,2 +1,4 @@ +version: "3" + rpc: $foo bar diff --git a/internal/rpc/test/include1/.rr-include.yaml b/internal/rpc/test/include1/.rr-include.yaml new file mode 100644 index 00000000..a145a947 --- /dev/null +++ b/internal/rpc/test/include1/.rr-include.yaml @@ -0,0 +1,5 @@ +version: "3" + +rpc: + listen: tcp://127.0.0.1:6010 + diff --git a/internal/rpc/test/include1/.rr.yaml b/internal/rpc/test/include1/.rr.yaml new file mode 100644 index 00000000..66de370b --- /dev/null +++ b/internal/rpc/test/include1/.rr.yaml @@ -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 |