Skip to content

Commit c948fc8

Browse files
committed
Adds labels and network aliases
1 parent 741246d commit c948fc8

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

lib/scenario_runner.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -973,10 +973,56 @@ def setup_services(self):
973973
if env_var_check_errors:
974974
raise RuntimeError('Docker container environment setup has problems:\n\n'.join(env_var_check_errors))
975975

976+
if 'labels' in service:
977+
labels_check_errors = []
978+
for docker_label_var in service['labels']:
979+
# https://docs.docker.com/reference/compose-file/services/#labels
980+
if isinstance(docker_label_var, str) and '=' in docker_label_var:
981+
label_key, label_value = docker_label_var.split('=')
982+
elif isinstance(service['labels'], dict):
983+
label_key, label_value = str(docker_label_var), str(service['labels'][docker_label_var])
984+
else:
985+
986+
raise RuntimeError('Environment variable needs to be a string with = or dict and non-empty. We do not allow the feature of forwarding variables from the host OS!')
987+
988+
# Check the key of the environment var
989+
if not self._allow_unsafe and re.search(r'^[A-Za-z_]+[A-Za-z0-9_.]*$', label_key) is None:
990+
if self._skip_unsafe:
991+
warn_message= arrows(f"Found label key with wrong format. Only ^[A-Za-z_]+[A-Za-z0-9_.]*$ allowed: {label_key} - Skipping")
992+
print(TerminalColors.WARNING, warn_message, TerminalColors.ENDC)
993+
continue
994+
labels_check_errors.append(f"- key '{label_key}' has wrong format. Only ^[A-Za-z_]+[A-Za-z0-9_.]*$ is allowed - Maybe consider using --allow-unsafe or --skip-unsafe")
995+
996+
# Check the value of the environment var
997+
# We only forbid long values (>1024), every character is allowed.
998+
# The value is directly passed to the container and is not evaluated on the host system, so there is no security related reason to forbid special characters.
999+
if not self._allow_unsafe and len(label_value) > 1024:
1000+
if self._skip_unsafe:
1001+
print(TerminalColors.WARNING, arrows(f"Found label value with size {len(label_value)} (max allowed length is 1024) - Skipping label '{label_value}'"), TerminalColors.ENDC)
1002+
continue
1003+
labels_check_errors.append(f"- value of label '{label_key}' is too long {len(label_key)} (max allowed length is 1024) - Maybe consider using --allow-unsafe or --skip-unsafe")
1004+
1005+
docker_run_string.append('-l')
1006+
docker_run_string.append(f"{label_key}={label_value}")
1007+
1008+
if labels_check_errors:
1009+
raise RuntimeError('Docker container labels that have problems:\n\n'.join(labels_check_errors))
1010+
9761011
if 'networks' in service:
9771012
for network in service['networks']:
9781013
docker_run_string.append('--net')
9791014
docker_run_string.append(network)
1015+
print('-------------------------------------------------------------')
1016+
print(service['networks'])
1017+
print(network)
1018+
print('-------------------------------------------------------------')
1019+
if service['networks'][network]:
1020+
if service['networks'][network].get('aliases', None):
1021+
for alias in service['networks'][network]['aliases']:
1022+
docker_run_string.append('--network-alias')
1023+
docker_run_string.append(alias)
1024+
print(f"Adding network alias {alias} for network {network} in service {service_name}")
1025+
9801026
elif self.__join_default_network:
9811027
# only join default network if no other networks provided
9821028
# if this is true only one entry is in self.__networks

lib/schema_checker.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,18 @@ def check_usage_scenario(self, usage_scenario):
107107
Optional("type"): Use(self.valid_service_types),
108108
Optional("image"): And(str, Use(self.not_empty)),
109109
Optional("build"): Or(Or({And(str, Use(self.not_empty)):And(str, Use(self.not_empty))},list),And(str, Use(self.not_empty))),
110-
Optional("networks"): self.single_or_list(Use(self.contains_no_invalid_chars)),
110+
Optional("networks"): Or(
111+
self.single_or_list(Use(self.contains_no_invalid_chars)),
112+
{
113+
Use(self.contains_no_invalid_chars):
114+
Or(
115+
None,
116+
{ "aliases": [Use(self.contains_no_invalid_chars)] }
117+
)
118+
}
119+
),
111120
Optional("environment"): self.single_or_list(Or(dict,And(str, Use(self.not_empty)))),
121+
Optional("labels"): self.single_or_list(Or(dict,And(str, Use(self.not_empty)))),
112122
Optional("ports"): self.single_or_list(Or(And(str, Use(self.not_empty)), int)),
113123
Optional('depends_on'): Or([And(str, Use(self.not_empty))],dict),
114124
Optional('deploy'):Or({

0 commit comments

Comments
 (0)