Skip to content

WIP: Add better filter capabilities to kubernetes tools #744

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 62 additions & 4 deletions holmes/plugins/toolsets/kubernetes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,70 @@ toolsets:
command: "kubectl describe {{ kind }} {{ name }}{% if namespace %} -n {{ namespace }}{% endif %}"

- name: "kubectl_get_by_name"
description: "Run `kubectl get <kind> <name> --show-labels`"
command: "kubectl get --show-labels -o wide {{ kind }} {{ name }}{% if namespace %} -n {{ namespace }}{% endif %}"
description: "Run `kubectl get <kind> <name> --show-labels`. Supports regex filtering with include_pattern and exclude_pattern."
script: |
output=$(kubectl get --show-labels -o wide {{ kind }} {{ name }}{% if namespace %} -n {{ namespace }}{% endif %} 2>&1)
exit_code=$?

if [ $exit_code -ne 0 ]; then
echo "$output"
exit $exit_code
fi

# Apply filters if provided
filtered_output="$output"

{% if include_pattern %}
# Apply include pattern (keep lines matching the pattern)
filtered_output=$(echo "$filtered_output" | grep -E "{{ include_pattern }}" || echo "No matches found for include pattern: {{ include_pattern }}")
{% endif %}

{% if exclude_pattern %}
# Apply exclude pattern (remove lines matching the pattern)
filtered_output=$(echo "$filtered_output" | grep -vE "{{ exclude_pattern }}" || echo "$filtered_output")
{% endif %}

Comment on lines +32 to +44
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Unescaped user-supplied regex ⇒ command-injection vector

{{ include_pattern }} / {{ exclude_pattern }} are inserted verbatim inside a double-quoted string that is shell-parsed by grep.
A crafted value containing " ; malicious_cmd # would prematurely close the quotes and execute arbitrary commands.

Mitigate by:

  1. Quoting the patterns with printf %q or similar after rendering, or
  2. Passing the pattern via a variable and using grep -E -- "$pattern".

Example patch (conceptual):

-{% if include_pattern %}
-          filtered_output=$(echo "$filtered_output" | grep -E "{{ include_pattern }}" || echo "No matches found for include pattern: {{ include_pattern }}")
-{% endif %}
+{% if include_pattern %}
+          inc_pattern="{{ include_pattern }}"
+          filtered_output=$(echo "$filtered_output" | grep -E -- "$inc_pattern" || { 
+              echo "No matches found for include pattern: $inc_pattern"; exit 0; })
+{% endif %}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
# Apply filters if provided
filtered_output="$output"
{% if include_pattern %}
# Apply include pattern (keep lines matching the pattern)
filtered_output=$(echo "$filtered_output" | grep -E "{{ include_pattern }}" || echo "No matches found for include pattern: {{ include_pattern }}")
{% endif %}
{% if exclude_pattern %}
# Apply exclude pattern (remove lines matching the pattern)
filtered_output=$(echo "$filtered_output" | grep -vE "{{ exclude_pattern }}" || echo "$filtered_output")
{% endif %}
# Apply filters if provided
filtered_output="$output"
{% if include_pattern %}
# Apply include pattern (keep lines matching the pattern)
inc_pattern="{{ include_pattern }}"
filtered_output=$(echo "$filtered_output" | grep -E -- "$inc_pattern" || {
echo "No matches found for include pattern: $inc_pattern"; exit 0; })
{% endif %}
{% if exclude_pattern %}
# Apply exclude pattern (remove lines matching the pattern)
filtered_output=$(echo "$filtered_output" | grep -vE "{{ exclude_pattern }}" || echo "$filtered_output")
{% endif %}
🤖 Prompt for AI Agents
In holmes/plugins/toolsets/kubernetes.yaml around lines 32 to 44, the
include_pattern and exclude_pattern are directly inserted into grep commands
inside double quotes, creating a command injection risk. To fix this, avoid
embedding the patterns directly in the command string; instead, assign the
rendered patterns to shell variables and pass them safely to grep using the
syntax grep -E -- "$pattern". This prevents shell interpretation of special
characters and mitigates injection vulnerabilities.

echo "$filtered_output"

- name: "kubectl_get_by_kind_in_namespace"
description: "Run `kubectl get <kind> -n <namespace> --show-labels` to get all resources of a given type in namespace"
command: "kubectl get --show-labels -o wide {{ kind }} -n {{namespace}}"
description: "Run `kubectl get <kind> -n <namespace> --show-labels` to get all resources of a given type in namespace. Supports regex filtering with include_pattern and exclude_pattern."
script: |
output=$(kubectl get --show-labels -o wide {{ kind }} -n {{namespace}} 2>&1)
exit_code=$?

if [ $exit_code -ne 0 ]; then
echo "$output"
exit $exit_code
fi

# Save header line
header=$(echo "$output" | head -n 1)
body=$(echo "$output" | tail -n +2)

# Apply filters if provided
filtered_body="$body"

{% if include_pattern %}
# Apply include pattern (keep lines matching the pattern)
filtered_body=$(echo "$filtered_body" | grep -E "{{ include_pattern }}" || echo "")
if [ -z "$filtered_body" ]; then
echo "$header"
echo "No matches found for include pattern: {{ include_pattern }}"
exit 0
fi
{% endif %}

{% if exclude_pattern %}
# Apply exclude pattern (remove lines matching the pattern)
temp=$(echo "$filtered_body" | grep -vE "{{ exclude_pattern }}" || echo "")
if [ -n "$temp" ]; then
filtered_body="$temp"
fi
{% endif %}

# Output header and filtered body
echo "$header"
echo "$filtered_body"

- name: "kubectl_get_by_kind_in_cluster"
description: "Run `kubectl get -A <kind> --show-labels` to get all resources of a given type in the cluster"
Expand Down
Loading