Search code examples
systemd

systemd, multiline variable in environmentfile , where new line is significant


I'm using systemd on debian jessie to control a service to which I'm feeding environment variables through the EnvironmentFile=/etc/default/myservice file

in this file I have a variable which is a public key

  JWT_PUB_KEY="-----BEGIN FOO BAR KEY-----
  MIIBgjAcBgoqhkiG9w0BDAEDMA4ECKZesfWLQOiDAgID6ASCAWBu7izm8N4V
  2puRO/Mdt+Y8ceywxiC0cE57nrbmvaTSvBwTg9b/xyd8YC6QK7lrhC9Njgp/
  ...
  -----END FOO BAR KEY-----"

putting it like that does not please systemd which report an error (though doing a source in bash of the same file works correctly)

the documentation of systemd report that you can have multiline variable by ending each file with a \ but that it concatenate each line (so my program receive the whole under one line, which is no more a valid public key)

Is there a known way to preserve the end of line ? without resorting to hack like putting \n which i them 'interpret' in my application code ?


Solution

  • As you suspected, Systemd accepts \n inside environment variable definitions. You need not perform and special parsing, just add appropriate \ns where they are needed and escape the actual newlines. Systemd should handle the rest and turn them into a linefeed literal. In your case this would look something like this:

    JWT_PUB_KEY="-----BEGIN FOO BAR KEY-----\n\
    MIIBgjAcBgoqhkiG9w0BDAEDMA4ECKZesfWLQOiDAgID6ASCAWBu7izm8N4V\n\
    2puRO/Mdt+Y8ceywxiC0cE57nrbmvaTSvBwTg9b/xyd8YC6QK7lrhC9Njgp/\n\
    ...\n\
    -----END FOO BAR KEY-----"