changing alias index prefix from '$' to '#', to avoid unquoted/unescaped dollar sign being treated as interpolation

This commit is contained in:
yusing 2024-09-27 00:34:14 +08:00
parent 8cca83723c
commit 345a4417a6
2 changed files with 36 additions and 44 deletions

View file

@ -16,7 +16,7 @@ type DockerProvider struct {
dockerHost, hostname string dockerHost, hostname string
} }
var AliasRefRegex = regexp.MustCompile(`\$\d+`) var AliasRefRegex = regexp.MustCompile(`#\d+`)
func DockerProviderImpl(dockerHost string) (ProviderImpl, E.NestedError) { func DockerProviderImpl(dockerHost string) (ProviderImpl, E.NestedError) {
hostname, err := D.ParseDockerHostname(dockerHost) hostname, err := D.ParseDockerHostname(dockerHost)
@ -102,7 +102,7 @@ func (p *DockerProvider) OnEvent(event W.Event, routes R.Routes) (res EventResul
entries.RangeAll(func(alias string, entry *M.RawEntry) { entries.RangeAll(func(alias string, entry *M.RawEntry) {
if routes.Has(alias) { if routes.Has(alias) {
b.Add(E.AlreadyExist("alias", alias)) b.Add(E.Duplicated("alias", alias))
} else { } else {
if route, err := R.NewRoute(entry); err.HasError() { if route, err := R.NewRoute(entry); err.HasError() {
b.Add(err) b.Add(err)

View file

@ -27,41 +27,36 @@ func TestApplyLabelFieldValidity(t *testing.T) {
"POST /upload/{$}", "POST /upload/{$}",
"GET /static", "GET /static",
} }
setHeaders := ` middlewaresExpect := D.NestedLabelMap{
X_Custom_Header1: value1 "middleware1": {
X_Custom_Header1: value2 "prop1": "value1",
X_Custom_Header2: value3 "prop2": "value2",
`[1:] },
setHeadersExpect := map[string]string{ "middleware2": {
"X_Custom_Header1": "value1, value2", "prop3": "value3",
"X_Custom_Header2": "value3", "prop4": "value4",
} },
hideHeaders := `
- X-Custom-Header1
- X-Custom-Header2
`[1:]
hideHeadersExpect := []string{
"X-Custom-Header1",
"X-Custom-Header2",
} }
var p DockerProvider var p DockerProvider
entries, err := p.entriesFromContainerLabels(D.FromDocker(&types.Container{ entries, err := p.entriesFromContainerLabels(D.FromDocker(&types.Container{
Names: dummyNames, Names: dummyNames,
Labels: map[string]string{ Labels: map[string]string{
D.LabelAliases: "a,b", D.LabelAliases: "a,b",
D.LabelIdleTimeout: common.IdleTimeoutDefault, D.LabelIdleTimeout: common.IdleTimeoutDefault,
D.LabelStopMethod: common.StopMethodDefault, D.LabelStopMethod: common.StopMethodDefault,
D.LabelStopSignal: "SIGTERM", D.LabelStopSignal: "SIGTERM",
D.LabelStopTimeout: common.StopTimeoutDefault, D.LabelStopTimeout: common.StopTimeoutDefault,
D.LabelWakeTimeout: common.WakeTimeoutDefault, D.LabelWakeTimeout: common.WakeTimeoutDefault,
"proxy.*.no_tls_verify": "true", "proxy.*.no_tls_verify": "true",
"proxy.*.scheme": "https", "proxy.*.scheme": "https",
"proxy.*.host": "app", "proxy.*.host": "app",
"proxy.*.port": "4567", "proxy.*.port": "4567",
"proxy.a.no_tls_verify": "true", "proxy.a.no_tls_verify": "true",
"proxy.a.path_patterns": pathPatterns, "proxy.a.path_patterns": pathPatterns,
"proxy.a.set_headers": setHeaders, "proxy.a.middlewares.middleware1.prop1": "value1",
"proxy.a.hide_headers": hideHeaders, "proxy.a.middlewares.middleware1.prop2": "value2",
"proxy.a.middlewares.middleware2.prop3": "value3",
"proxy.a.middlewares.middleware2.prop4": "value4",
}, },
Ports: []types.Port{ Ports: []types.Port{
{Type: "tcp", PrivatePort: 4567, PublicPort: 8888}, {Type: "tcp", PrivatePort: 4567, PublicPort: 8888},
@ -88,11 +83,8 @@ X_Custom_Header2: value3
ExpectDeepEqual(t, a.PathPatterns, pathPatternsExpect) ExpectDeepEqual(t, a.PathPatterns, pathPatternsExpect)
ExpectEqual(t, len(b.PathPatterns), 0) ExpectEqual(t, len(b.PathPatterns), 0)
ExpectDeepEqual(t, a.SetHeaders, setHeadersExpect) ExpectDeepEqual(t, a.Middlewares, middlewaresExpect)
ExpectEqual(t, len(b.SetHeaders), 0) ExpectEqual(t, len(b.Middlewares), 0)
ExpectDeepEqual(t, a.HideHeaders, hideHeadersExpect)
ExpectEqual(t, len(b.HideHeaders), 0)
ExpectEqual(t, a.IdleTimeout, common.IdleTimeoutDefault) ExpectEqual(t, a.IdleTimeout, common.IdleTimeoutDefault)
ExpectEqual(t, b.IdleTimeout, common.IdleTimeoutDefault) ExpectEqual(t, b.IdleTimeout, common.IdleTimeoutDefault)
@ -149,11 +141,11 @@ func TestApplyLabelWithRef(t *testing.T) {
Names: dummyNames, Names: dummyNames,
Labels: map[string]string{ Labels: map[string]string{
D.LabelAliases: "a,b,c", D.LabelAliases: "a,b,c",
"proxy.$1.host": "localhost", "proxy.#1.host": "localhost",
"proxy.*.port": "1111", "proxy.*.port": "1111",
"proxy.$1.port": "4444", "proxy.#1.port": "4444",
"proxy.$2.port": "9999", "proxy.#2.port": "9999",
"proxy.$3.scheme": "https", "proxy.#3.scheme": "https",
}, },
Ports: []types.Port{ Ports: []types.Port{
{Type: "tcp", PrivatePort: 3333, PublicPort: 9999}, {Type: "tcp", PrivatePort: 3333, PublicPort: 9999},
@ -182,8 +174,8 @@ func TestApplyLabelWithRefIndexError(t *testing.T) {
Names: dummyNames, Names: dummyNames,
Labels: map[string]string{ Labels: map[string]string{
D.LabelAliases: "a,b", D.LabelAliases: "a,b",
"proxy.$1.host": "localhost", "proxy.#1.host": "localhost",
"proxy.$4.scheme": "https", "proxy.#4.scheme": "https",
}}, "") }}, "")
_, err := p.entriesFromContainerLabels(c) _, err := p.entriesFromContainerLabels(c)
ExpectError(t, E.ErrOutOfRange, err.Error()) ExpectError(t, E.ErrOutOfRange, err.Error())
@ -193,7 +185,7 @@ func TestApplyLabelWithRefIndexError(t *testing.T) {
Names: dummyNames, Names: dummyNames,
Labels: map[string]string{ Labels: map[string]string{
D.LabelAliases: "a,b", D.LabelAliases: "a,b",
"proxy.$0.host": "localhost", "proxy.#0.host": "localhost",
}}, "")) }}, ""))
ExpectError(t, E.ErrOutOfRange, err.Error()) ExpectError(t, E.ErrOutOfRange, err.Error())
ExpectTrue(t, strings.Contains(err.String(), "index out of range")) ExpectTrue(t, strings.Contains(err.String(), "index out of range"))