Encode invalid toml table keys

Signed-off-by: Brad Davidson <brad.davidson@rancher.com>
This commit is contained in:
Brad Davidson 2024-04-12 23:46:56 +00:00
parent 81cd630f87
commit 3f506b9ef0
No known key found for this signature in database
GPG Key ID: FFB7A9376A9349B9
3 changed files with 14 additions and 2 deletions

View File

@ -3,6 +3,7 @@ package templates
import (
"bytes"
"net/url"
"strings"
"text/template"
"github.com/rancher/wharfie/pkg/registries"
@ -62,7 +63,7 @@ skip_verify = true
{{ end }}
[host]
{{ range $e := .Endpoints -}}
[host."{{ $e.URL }}"]
[host."{{ $e.URL | keyencode }}"]
capabilities = ["pull", "resolve"]
{{- if $e.OverridePath }}
override_path = true
@ -79,7 +80,7 @@ skip_verify = true
{{- end }}
{{ end }}
{{- if $e.Rewrites }}
[host."{{ $e.URL }}".rewrite]
[host."{{ $e.URL | keyencode }}".rewrite]
{{- range $pattern, $replace := $e.Rewrites }}
"{{ $pattern }}" = "{{ $replace }}"
{{- end }}
@ -105,3 +106,12 @@ func ParseHostsTemplateFromConfig(templateBuffer string, config interface{}) (st
}
return out.String(), nil
}
// keyEncode replaces invalid table keys with escaped unicode equivalents.
func keyEncode(s string) string {
// Square brackets are not valid in toml table keys, see
// https://github.com/containerd/containerd/issues/10055
s = strings.ReplaceAll(s, "[", "\\u005B")
s = strings.ReplaceAll(s, "]", "\\u005D")
return s
}

View File

@ -121,6 +121,7 @@ enable_keychain = true
// Linux config templates do not need fixups
var templateFuncs = template.FuncMap{
"keyencode": keyEncode,
"deschemify": func(s string) string {
return s
},

View File

@ -145,6 +145,7 @@ oom_score = 0
// Windows config templates need named pipe addresses fixed up
var templateFuncs = template.FuncMap{
"keyencode": keyEncode,
"deschemify": func(s string) string {
if strings.HasPrefix(s, "npipe:") {
u, err := url.Parse(s)