mirror of
https://github.com/go-kratos/kratos.git
synced 2025-03-17 21:07:54 +02:00
fix(config/env): fix env.load() index out of range (#1252)
* fix(config/env): env.load() may panic
This commit is contained in:
parent
9808ceb7a8
commit
025ae38acc
12
config/env/env.go
vendored
12
config/env/env.go
vendored
@ -30,15 +30,13 @@ func (e *env) load(envStrings []string) []*config.KeyValue {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(e.prefixs) > 0 {
|
if len(e.prefixs) > 0 {
|
||||||
p, ok := matchPrefix(e.prefixs, envstr)
|
p, ok := matchPrefix(e.prefixs, k)
|
||||||
if !ok || len(p) == len(k) {
|
if !ok || len(p) == len(k) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
// trim prefix
|
// trim prefix
|
||||||
k = k[len(p):]
|
k = strings.TrimPrefix(k, p)
|
||||||
if k[0] == '_' {
|
k = strings.TrimPrefix(k, "_")
|
||||||
k = k[1:]
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(k) != 0 {
|
if len(k) != 0 {
|
||||||
@ -59,8 +57,8 @@ func (e *env) Watch() (config.Watcher, error) {
|
|||||||
return w, nil
|
return w, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func matchPrefix(prefixs []string, s string) (string, bool) {
|
func matchPrefix(prefixes []string, s string) (string, bool) {
|
||||||
for _, p := range prefixs {
|
for _, p := range prefixes {
|
||||||
if strings.HasPrefix(s, p) {
|
if strings.HasPrefix(s, p) {
|
||||||
return p, true
|
return p, true
|
||||||
}
|
}
|
||||||
|
95
config/env/env_test.go
vendored
95
config/env/env_test.go
vendored
@ -281,6 +281,44 @@ func Test_env_load(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
name: "empty prefix",
|
||||||
|
fields: fields{
|
||||||
|
prefixs: []string{""},
|
||||||
|
},
|
||||||
|
args: args{
|
||||||
|
envStrings: []string{
|
||||||
|
"__SERVICE_NAME=kratos_app",
|
||||||
|
"__ADDR=192.168.0.1",
|
||||||
|
"__AGE=20",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
want: []*config.KeyValue{
|
||||||
|
{Key: "_SERVICE_NAME", Value: []byte("kratos_app"), Format: ""},
|
||||||
|
{Key: "_ADDR", Value: []byte("192.168.0.1"), Format: ""},
|
||||||
|
{Key: "_AGE", Value: []byte("20"), Format: ""},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
name: "underscore prefix",
|
||||||
|
fields: fields{
|
||||||
|
prefixs: []string{"_"},
|
||||||
|
},
|
||||||
|
args: args{
|
||||||
|
envStrings: []string{
|
||||||
|
"__SERVICE_NAME=kratos_app",
|
||||||
|
"__ADDR=192.168.0.1",
|
||||||
|
"__AGE=20",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
want: []*config.KeyValue{
|
||||||
|
{Key: "SERVICE_NAME", Value: []byte("kratos_app"), Format: ""},
|
||||||
|
{Key: "ADDR", Value: []byte("192.168.0.1"), Format: ""},
|
||||||
|
{Key: "AGE", Value: []byte("20"), Format: ""},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
name: "with prefixes",
|
name: "with prefixes",
|
||||||
fields: fields{
|
fields: fields{
|
||||||
@ -299,6 +337,32 @@ func Test_env_load(t *testing.T) {
|
|||||||
{Key: "AGE", Value: []byte("20"), Format: ""},
|
{Key: "AGE", Value: []byte("20"), Format: ""},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
name: "should not panic #1",
|
||||||
|
fields: fields{
|
||||||
|
prefixs: []string{"FOO"},
|
||||||
|
},
|
||||||
|
args: args{
|
||||||
|
envStrings: []string{
|
||||||
|
"FOO=123",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
want: nil,
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
name: "should not panic #2",
|
||||||
|
fields: fields{
|
||||||
|
prefixs: []string{"FOO=1"},
|
||||||
|
},
|
||||||
|
args: args{
|
||||||
|
envStrings: []string{
|
||||||
|
"FOO=123",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
want: nil,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
@ -312,3 +376,34 @@ func Test_env_load(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_matchPrefix(t *testing.T) {
|
||||||
|
type args struct {
|
||||||
|
prefixes []string
|
||||||
|
s string
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args args
|
||||||
|
want string
|
||||||
|
wantOk bool
|
||||||
|
}{
|
||||||
|
{args: args{prefixes: nil, s: "foo=123"}, want: "", wantOk: false},
|
||||||
|
{args: args{prefixes: []string{""}, s: "foo=123"}, want: "", wantOk: true},
|
||||||
|
{args: args{prefixes: []string{"foo"}, s: "foo=123"}, want: "foo", wantOk: true},
|
||||||
|
{args: args{prefixes: []string{"foo=1"}, s: "foo=123"}, want: "foo=1", wantOk: true},
|
||||||
|
{args: args{prefixes: []string{"foo=1234"}, s: "foo=123"}, want: "", wantOk: false},
|
||||||
|
{args: args{prefixes: []string{"bar"}, s: "foo=123"}, want: "", wantOk: false},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
got, gotOk := matchPrefix(tt.args.prefixes, tt.args.s)
|
||||||
|
if got != tt.want {
|
||||||
|
t.Errorf("matchPrefix() got = %v, want %v", got, tt.want)
|
||||||
|
}
|
||||||
|
if gotOk != tt.wantOk {
|
||||||
|
t.Errorf("matchPrefix() gotOk = %v, wantOk %v", gotOk, tt.wantOk)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user