Skip to content

Commit

Permalink
Sanitize labels.
Browse files Browse the repository at this point in the history
  • Loading branch information
felixfontein committed Nov 5, 2024
1 parent 6daeff6 commit a3b0cd6
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 6 deletions.
9 changes: 9 additions & 0 deletions changelogs/fragments/985-label-sanitize.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
bugfixes:
- "docker_compose_v2_run - make sure to sanitize ``labels`` before sending them to the Docker Daemon (https://github.com/ansible-collections/community.docker/pull/985)."
- "docker_config - make sure to sanitize ``labels`` before sending them to the Docker Daemon (https://github.com/ansible-collections/community.docker/pull/985)."
- "docker_network - make sure to sanitize ``labels`` before sending them to the Docker Daemon (https://github.com/ansible-collections/community.docker/pull/985)."
- "docker_node - make sure to sanitize ``labels`` before sending them to the Docker Daemon (https://github.com/ansible-collections/community.docker/pull/985)."
- "docker_secret - make sure to sanitize ``labels`` before sending them to the Docker Daemon (https://github.com/ansible-collections/community.docker/pull/985)."
- "docker_swarm - make sure to sanitize ``labels`` before sending them to the Docker Daemon (https://github.com/ansible-collections/community.docker/pull/985)."
- "docker_swarm_service - make sure to sanitize ``labels`` and ``container_labels`` before sending them to the Docker Daemon (https://github.com/ansible-collections/community.docker/pull/985)."
- "docker_volume - make sure to sanitize ``labels`` before sending them to the Docker Daemon (https://github.com/ansible-collections/community.docker/pull/985)."
14 changes: 13 additions & 1 deletion plugins/module_utils/module_container/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
compare_generic,
normalize_healthcheck,
omit_none_from_dict,
sanitize_labels,
)

from ansible_collections.community.docker.plugins.module_utils._platform import (
Expand Down Expand Up @@ -651,6 +652,17 @@ def check_collision(t, name):
return values


def _preprocess_labels(module, values):
result = {}
if 'labels' in values:
labels = values['labels']
if labels is not None:
labels = dict(labels)
sanitize_labels(labels, 'labels', module=module)
result['labels'] = labels
return result


def _preprocess_log(module, values):
result = {}
if 'log_driver' not in values:
Expand Down Expand Up @@ -978,7 +990,7 @@ def _compare_platform(option, param_value, container_value):
)

OPTION_LABELS = (
OptionGroup()
OptionGroup(preprocess=_preprocess_labels)
.add_option('labels', type='dict', needs_no_suboptions=True)
)

Expand Down
24 changes: 24 additions & 0 deletions plugins/module_utils/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@

from ansible.module_utils.basic import env_fallback
from ansible.module_utils.common.collections import is_sequence
from ansible.module_utils.six import string_types
from ansible.module_utils.six.moves.urllib.parse import urlparse
from ansible.module_utils.common.text.converters import to_text


DEFAULT_DOCKER_HOST = 'unix:///var/run/docker.sock'
Expand Down Expand Up @@ -277,6 +279,28 @@ def get_legacy_docker_diffs(self):
return result


def sanitize_labels(labels, labels_field, client=None, module=None):
def fail(msg):
if client is not None:
client.module.fail(msg)
if module is not None:
module.fail_json(msg=msg)
raise ValueError(msg)

if labels is None:
return
for k, v in list(labels):
if not isinstance(k, string_types):
fail(
"The key {key!r} of {field} is not a string!".format(
field=labels_field, key=k))
if isinstance(v, (bool, float)):
fail(
"The value {value!r} for {key!r} of {field} is not a string or something than can be safely converted to a string!".format(
field=labels_field, key=k, value=v))
labels[k] = to_text(v)


def clean_dict_booleans_for_docker_api(data, allow_sequences=False):
'''
Go doesn't like Python booleans 'True' or 'False', while Ansible is just
Expand Down
3 changes: 3 additions & 0 deletions plugins/modules/docker_compose_v2_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,8 @@
common_compose_argspec_ex,
)

from ansible_collections.community.docker.plugins.module_utils.util import sanitize_labels


class ExecManager(BaseComposeManager):
def __init__(self, client):
Expand Down Expand Up @@ -416,6 +418,7 @@ def main():
needs_api_version=False,
**argspec_ex
)
sanitize_labels(client.module.params['labels'], 'labels', client)

try:
manager = ExecManager(client)
Expand Down
2 changes: 2 additions & 0 deletions plugins/modules/docker_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@
from ansible_collections.community.docker.plugins.module_utils.util import (
DockerBaseClass,
compare_generic,
sanitize_labels,
)
from ansible.module_utils.common.text.converters import to_native, to_bytes

Expand Down Expand Up @@ -414,6 +415,7 @@ def main():
min_docker_api_version='1.30',
option_minimal_versions=option_minimal_versions,
)
sanitize_labels(client.module.params['labels'], 'labels', client)

try:
results = dict(
Expand Down
2 changes: 2 additions & 0 deletions plugins/modules/docker_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@
DockerBaseClass,
DifferenceTracker,
clean_dict_booleans_for_docker_api,
sanitize_labels,
)
from ansible_collections.community.docker.plugins.module_utils._api.errors import DockerException

Expand Down Expand Up @@ -698,6 +699,7 @@ def main():
# "The docker server >= 1.10.0"
option_minimal_versions=option_minimal_versions,
)
sanitize_labels(client.module.params['labels'], 'labels', client)

try:
cm = DockerNetworkManager(client)
Expand Down
3 changes: 3 additions & 0 deletions plugins/modules/docker_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@
from ansible.module_utils.common.text.converters import to_native

from ansible_collections.community.docker.plugins.module_utils.swarm import AnsibleDockerSwarmClient
from ansible_collections.community.docker.plugins.module_utils.util import sanitize_labels


class TaskParameters(DockerBaseClass):
Expand All @@ -172,6 +173,8 @@ def __init__(self, client):
for key, value in client.module.params.items():
setattr(self, key, value)

sanitize_labels(self.labels, "labels", client)


class SwarmNodeManager(DockerBaseClass):

Expand Down
2 changes: 2 additions & 0 deletions plugins/modules/docker_secret.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@
from ansible_collections.community.docker.plugins.module_utils.util import (
DockerBaseClass,
compare_generic,
sanitize_labels,
)
from ansible.module_utils.common.text.converters import to_native, to_bytes

Expand Down Expand Up @@ -384,6 +385,7 @@ def main():
mutually_exclusive=mutually_exclusive,
min_docker_version='2.1.0',
)
sanitize_labels(client.module.params['labels'], 'labels', client)

try:
results = dict(
Expand Down
2 changes: 2 additions & 0 deletions plugins/modules/docker_swarm.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@
)
from ansible_collections.community.docker.plugins.module_utils.util import (
DifferenceTracker,
sanitize_labels,
)

from ansible_collections.community.docker.plugins.module_utils.swarm import AnsibleDockerSwarmClient
Expand Down Expand Up @@ -714,6 +715,7 @@ def main():
min_docker_version='1.10.0',
option_minimal_versions=option_minimal_versions,
)
sanitize_labels(client.module.params['labels'], 'labels', client)

try:
results = dict(
Expand Down
11 changes: 6 additions & 5 deletions plugins/modules/docker_swarm_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,7 @@
convert_duration_to_nanosecond,
parse_healthcheck,
clean_dict_booleans_for_docker_api,
sanitize_labels,
)

from ansible.module_utils.basic import human_to_bytes
Expand Down Expand Up @@ -1531,10 +1532,9 @@ def from_ansible_params(
secret_ids,
config_ids,
network_ids,
docker_api_version,
docker_py_version,
client,
):
s = DockerService(docker_api_version, docker_py_version)
s = DockerService(client.docker_api_version, client.docker_py_version)
s.image = image_digest
s.args = ap['args']
s.endpoint_mode = ap['endpoint_mode']
Expand All @@ -1546,7 +1546,9 @@ def from_ansible_params(
s.hosts = ap['hosts']
s.tty = ap['tty']
s.labels = ap['labels']
sanitize_labels(s.labels, 'labels', client)
s.container_labels = ap['container_labels']
sanitize_labels(s.container_labels, 'container_labels', client)
s.sysctls = ap['sysctls']
s.mode = ap['mode']
s.stop_signal = ap['stop_signal']
Expand Down Expand Up @@ -2488,8 +2490,7 @@ def run(self):
secret_ids,
config_ids,
network_ids,
self.client.docker_api_version,
self.client.docker_py_version
self.client,
)
except Exception as e:
return self.client.fail(
Expand Down
2 changes: 2 additions & 0 deletions plugins/modules/docker_volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
from ansible_collections.community.docker.plugins.module_utils.util import (
DockerBaseClass,
DifferenceTracker,
sanitize_labels,
)
from ansible_collections.community.docker.plugins.module_utils._api.errors import (
APIError,
Expand Down Expand Up @@ -296,6 +297,7 @@ def main():
supports_check_mode=True,
# "The docker server >= 1.9.0"
)
sanitize_labels(client.module.params['labels'], 'labels', client)

try:
cm = DockerVolumeManager(client)
Expand Down

0 comments on commit a3b0cd6

Please sign in to comment.