Skip to content

Commit

Permalink
Escape $$ in labels and env (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
Galaxy102 authored Nov 12, 2024
1 parent d3aa07e commit 9ac4048
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions autocompose.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@
IGNORE_VALUES = [None, "", [], "null", {}, "default", 0, ",", "no"]


def shell_escape_string(input_string):
# Currently known issues:
# - Basic Auth strings (e.g. set via Træfik labels) contain $ characters, which must be doubled. See https://stackoverflow.com/a/40621373/5885325
replaced_string = input_string
for substitution in (
("$", "$$"),
):
replaced_string = replaced_string.replace(substitution[0], substitution[1])
return replaced_string


def list_container_names():
c = docker.from_env()
return [container.name for container in c.containers.list(all=True)]
Expand Down Expand Up @@ -282,6 +293,14 @@ def generate(cname, createvolumes=False):
except (KeyError, TypeError):
# No ports exposed/bound. Continue without them.
ports = None

# fixup strings in labels and env
if values["labels"] is not None:
for label_key, label_value in values["labels"].items():
values["labels"][label_key] = shell_escape_string(label_value)
if values["environment"] is not None:
for idx, env_variable in enumerate(values["environment"]):
values["environment"][idx] = shell_escape_string(env_variable)

# Iterate through values to finish building yaml dict.
for key in values:
Expand Down

0 comments on commit 9ac4048

Please sign in to comment.