abiosoft/caddy-yaml

Fails to parse valid yaml

rayjlinden opened this issue · 3 comments

I wanted to try out this adapter. I used a little utility to convert the json to yaml. Tried it out and got this error:

{"level":"info","ts":1611044403.9202049,"msg":"using provided configuration","config_file":"/config.yml","config_adapter":"yaml"}
run: adapting config using yaml: template: yaml:464: bad character U+0022 '"'

I know it is valid yaml. More odd is there is no double-quote character on line 464

I know this is not much info to go on. Any suggestions on how I can figure out what is wrong?

The one thing I'm wondering about is I have things like:

              - handler: headers
                response:
                  set:
                    X-Frame-Options:
                    - SAMEORIGIN
              - handler: reverse_proxy
                headers:
                  request:
                    set:
                      Host:
                      - "{http.request.host}"
                      X-Forwarded-For:
                      - "{http.request.remote}"
                      X-Real-Ip:
                      - "{http.request.remote}"

I wonder if the use of these header values is conflicting with your x- processing...

I'm pretty sure the root of the problem is how X- elements are handled.

I switched to the iamd3vil/caddy_yaml_adapter version and everything works fine.

Looking through the code it looks like the regex looking for "x-" lines does not understand if the use is at the top level or not.

Just a suggestion it seems like it might be easier to first parse the yaml and then just extract the top-level elements that include x- references, rather than trying to rip out the x- lines via a regexp.

ankon commented

Late, but still for future research: This might actually be caused by the way this adapter injects environment variables, see

caddy-yaml/template.go

Lines 37 to 54 in 64fbdd0

func envVarsTemplate() string {
var builder strings.Builder
line := func(key, val string) string {
// avoid quoted string
if len(val) > 0 && val[0] == '"' {
if v, err := strconv.Unquote(val); err == nil {
val = v
}
}
return tplWrap(fmt.Sprintf(`$%s := "%s"`, key, val))
}
for _, env := range os.Environ() {
tmp := strings.SplitN(env, "=", 2)
key, val := tmp[0], tmp[1]
fmt.Fprintln(&builder, line(key, val))
}
return builder.String()
}

Basically it assumes that environment variable names are valid go identifiers (they are not!) and then puts in $name := value assignments into the template. This might produce all kinds of interesting problems depending on how your environment looks like.

iamd3vil/caddy_yaml_adapter doesn't seem to try to support environment variable templating.