Skip to content

fix: sanitize Elasticsearch IDs used as Kubernetes label values#487

Open
Sonny00 wants to merge 1 commit intoTheHive-Project:masterfrom
Sonny00:fix/k8s-label-sanitization
Open

fix: sanitize Elasticsearch IDs used as Kubernetes label values#487
Sonny00 wants to merge 1 commit intoTheHive-Project:masterfrom
Sonny00:fix/k8s-label-sanitization

Conversation

@Sonny00
Copy link

@Sonny00 Sonny00 commented Mar 24, 2026

Problem

Cortex uses Elasticsearch document IDs as K8s labels (cortex-job-id). Some end with _, violating K8s rules (must start/end with alphanumeric) → 400 Bad Request errors.

Fix

Added sanitizeForK8sLabel():

keeps valid IDs unchanged
hex-encodes invalid ones (bijective, no collisions)
Applied to all labels and selectors.

Base64url-encoded Elasticsearch IDs can end with '_', which is invalid
for K8s labels. This causes intermittent job creation/deletion failures.
Fix uses conditional hex-encoding to ensure valid labels.
Copy link

@Gael13 Gael13 left a comment

Choose a reason for hiding this comment

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

Beau travail, bravo !

// LDH: lowercase, digits, hyphens. Must start/end with alphanumeric.
val sanitizedJobId = sanitizeForK8sLabel(job.id)
val sanitizedWorkerId = sanitizeForK8sLabel(job.workerId())
val ldh_jobid = sanitizedJobId.toLowerCase().replace('_', '-').replaceAll("[^a-z0-9-]", "").replaceAll("^-+|-+$", "")
Copy link

Choose a reason for hiding this comment

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

Est-ce que c'est possible d'avoir un seul replaceAll au lieu d'en avoir deux ? comme c'est remplacé par la même chose:

val ldh_jobid = sanitizedJobId.toLowerCase().replace('_', '-').replaceAll("[^a-z0-9-]|^-+|-+$", "")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants