Skip to content

Support inline environment variable expansion in conn_str for Sources #1369

@serdardalgic

Description

@serdardalgic

Is your feature request related to a problem? Please describe.
Currently, environment variable expansion for conn_str only works when the entire value starts with $. This means inline environment variables inside a connection string are not expanded.

For example, when I write to connection string as:

conn_str: postgresql://$PGWATCH_USER:$PGWATCH_USER_PASSWORD@$METRIC_DATABASE_ENDPOINT:$METRIC_DATABASE_PORT/$METRIC_DATABASE_DATABASE?sslmode=$METRIC_DATABASE_SSLMODE

It won't be expanded because the string doesn't start with $.

Describe the solution you'd like
Please consider applying the os.ExpandEnv() to conn_str unconditionally (or whenever it contains $), instead of only when it starts with $.

It's a pretty common pattern in containerized environments to have various connection parameters as separate environment variables.

Describe alternatives you've considered
A current workaround is to define the full connection string as a single environment variable:

conn_str: $PG_CONNECTION_STRING

This works, but requires composing the DSN outside pgwatch and introducing an additional environment variable.

Another option is preprocessing the YAML file using tools like envsubst or awk magic, but this introduces additional dependencies and setup complexity. (envsubst is not included in the pgwatch container by default).

Additional context
os.ExpandEnv() already supports inline substitution anywhere in a string, so this change would align behavior with the underlying Go functionality.

From a security perspective, this does not introduce shell execution or command evaluation, as os.ExpandEnv() performs only string substitution. The main consideration is operational safety: undefined variables are silently replaced with empty strings, and literal $.. substrings would be interpreted.

In most cases, if something is misconfigured, it will show up as either a parse error in the connection string, or a connection error in the logs. So the issue won't go unnoticed, even if the exact issue (like a missing env var) isn't always immediately clear.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestgoPull requests that update Go codesourcesWhat sources and in what way to monitor

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions