Expanding env variable into multiple strings in json config

1. Caddy version (caddy version):

2.3.0 but I can upgrade

2. How I run Caddy:

from within docker container

a. System environment:

docker image

b. Command:

CMD ["caddy", "run", "--config", "/etc/caddy/config.json"]

c. Service/unit/compose file:

Paste full file contents here.
Make sure backticks stay on their own lines,
and the post looks nice in the preview pane.

d. My complete Caddyfile or JSON config:

{
  "storage": { "module": "dynamodb", "table": "CertMagic", "aws_region": "eu-central-1" },
  "apps": {
    "tls": {
      "automation": {
        "policies": [
          {
            "subjects": ["siasky.xyz", "*.siasky.xyz", "*.hns.siasky.xyz"],
            "issuers": [
              { "challenges": { "dns": { "provider": { "max_retries": 50, "name": "route53" } } }, "module": "acme" }
            ]
          },
          { "issuers": [{ "module": "internal" }], "on_demand": true }
        ]
      }
    }
  }
}

3. The problem I’m having:

Is it possible to use environment variable to replace multiple tokens in json config just as I would do in Caddyfile?

I have env variable that is
SSL_CERTIFICATE_STRING=siasky.xyz, *.siasky.xyz, *.hns.siasky.xyz

In Caddyfile you can use {$SSL_CERTIFICATE_STRING} to expand this string into multiple tokens and “subjects” ends up as an array of strings. I can’t find any mention of how to do that from json config though. I need to use json config because I’m also using “storage” module that doesn’t have a directive for Caddyfile.

What I’d like to do is basically sth like

...
   "subjects": "{$SSL_CERTIFICATE_STRING}",
...

or

...
   "subjects": ["{$SSL_CERTIFICATE_STRING}"],
...

I don’t want to have 3 env variables for each domain though because I want this config to be flexible - some of servers that I’m running have 5 domains, some have 3.

4. Error messages and/or full log output:

5. What I already tried:

6. Links to relevant resources:

The {$VAR} style environment placeholders are Caddyfile-specific. Those are replaced at Caddyfile-adapt time, i.e. before the Caddyfile is transformed into a JSON config.

The {env.VAR} style does work in JSON, but only in locations where the given module actually attempts to replace the config value.

It’s not possible to expand one config value into multiple; trying to support that would add significant complexity to Caddy which isn’t worth it.

Instead, you should write a script that replaces the values in your JSON before passing it to Caddy. You might use jq for this (Google it – command line tool to manipulate JSON)

1 Like

Thanks for the reply, sounds good :+1:

1 Like

This topic was automatically closed after 30 days. New replies are no longer allowed.