Skip to content

Commit dfb71af

Browse files
authored
[#2063]: fix: properly parse includes outside the Configuration plugin
2 parents bd93d5c + 642e83e commit dfb71af

10 files changed

+119
-3
lines changed

internal/rpc/client.go

+14-3
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@ func NewClient(cfg string, flags []string) (*rpc.Client, error) {
3333
return nil, err
3434
}
3535

36-
// automatically inject ENV variables using ${ENV} pattern
37-
expandEnvViper(v)
38-
3936
// override config Flags
4037
if len(flags) > 0 {
4138
for _, f := range flags {
@@ -48,6 +45,20 @@ func NewClient(cfg string, flags []string) (*rpc.Client, error) {
4845
}
4946
}
5047

48+
ver := v.Get(versionKey)
49+
if ver == nil {
50+
return nil, fmt.Errorf("rr configuration file should contain a version e.g: version: 3")
51+
}
52+
53+
if _, ok := ver.(string); !ok {
54+
return nil, fmt.Errorf("version should be a string: `version: \"3\"`, actual type is: %T", ver)
55+
}
56+
57+
err = handleInclude(ver.(string), v)
58+
if err != nil {
59+
return nil, fmt.Errorf("failed to handle includes: %w", err)
60+
}
61+
5162
// rpc.listen might be set by the -o flags or env variable
5263
if !v.IsSet(rpcPlugin.PluginName) {
5364
return nil, errors.New("rpc service not specified in the configuration. Tip: add\n rpc:\n\r listen: rr_rpc_address")

internal/rpc/client_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,20 @@ func TestNewClient_SuccessfullyConnected(t *testing.T) {
5252
defer func() { assert.NoError(t, c.Close()) }()
5353
}
5454

55+
func TestNewClient_WithIncludes(t *testing.T) {
56+
l, err := net.Listen("tcp", "127.0.0.1:6010")
57+
assert.NoError(t, err)
58+
59+
defer func() { assert.NoError(t, l.Close()) }()
60+
61+
c, err := rpc.NewClient("test/include1/.rr.yaml", nil)
62+
63+
assert.NotNil(t, c)
64+
assert.NoError(t, err)
65+
66+
assert.NoError(t, c.Close())
67+
}
68+
5569
func TestNewClient_SuccessfullyConnectedOverride(t *testing.T) {
5670
l, err := net.Listen("tcp", "127.0.0.1:55554")
5771
assert.NoError(t, err)

internal/rpc/includes.go

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package rpc
2+
3+
import (
4+
"github.com/roadrunner-server/errors"
5+
"github.com/spf13/viper"
6+
)
7+
8+
const (
9+
versionKey string = "version"
10+
includeKey string = "include"
11+
defaultConfigVersion string = "3"
12+
prevConfigVersion string = "2.7"
13+
)
14+
15+
func getConfiguration(path string) (map[string]any, string, error) {
16+
v := viper.New()
17+
v.SetConfigFile(path)
18+
err := v.ReadInConfig()
19+
if err != nil {
20+
return nil, "", err
21+
}
22+
23+
// get configuration version
24+
ver := v.Get(versionKey)
25+
if ver == nil {
26+
return nil, "", errors.Str("rr configuration file should contain a version e.g: version: 2.7")
27+
}
28+
29+
if _, ok := ver.(string); !ok {
30+
return nil, "", errors.Errorf("type of version should be string, actual: %T", ver)
31+
}
32+
33+
// automatically inject ENV variables using ${ENV} pattern
34+
expandEnvViper(v)
35+
36+
return v.AllSettings(), ver.(string), nil
37+
}
38+
39+
func handleInclude(rootVersion string, v *viper.Viper) error {
40+
// automatically inject ENV variables using ${ENV} pattern
41+
// root config
42+
expandEnvViper(v)
43+
44+
ifiles := v.GetStringSlice(includeKey)
45+
if ifiles == nil {
46+
return nil
47+
}
48+
49+
for _, file := range ifiles {
50+
config, version, err := getConfiguration(file)
51+
if err != nil {
52+
return err
53+
}
54+
55+
if version != rootVersion {
56+
return errors.Str("version in included file must be the same as in root")
57+
}
58+
59+
// overriding configuration
60+
for key, val := range config {
61+
v.Set(key, val)
62+
}
63+
}
64+
65+
return nil
66+
}
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
version: "3"
2+
13
rpc:
24
listen: tcp://127.0.0.1:55554
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
version: "3"

internal/rpc/test/config_rpc_ok.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
version: "3"
2+
13
rpc:
24
listen: tcp://127.0.0.1:55554
+2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
version: "3"
2+
13
rpc:
24
listen: ${RPC}
+2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
version: "3"
2+
13
rpc:
24
$foo bar
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
version: "3"
2+
3+
rpc:
4+
listen: tcp://127.0.0.1:6010
5+

internal/rpc/test/include1/.rr.yaml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
version: "3"
2+
3+
server:
4+
command: "php app-with-domain-specific-routes.php"
5+
6+
logs:
7+
level: debug
8+
mode: development
9+
10+
include:
11+
- test/include1/.rr-include.yaml

0 commit comments

Comments
 (0)