Skip to content

False Positive: Metadata Label Is Invalid (bc3dabb6) for Terraform local variable interpolation #7944

@DLTKYugalGarg

Description

@DLTKYugalGarg

Query ID: bc3dabb6-fd50-40f8-b9ba-7429c9f1fb0e
Query Name: Metadata Label Is Invalid
Severity: LOW
Platform: Terraform

Problem Statement

KICS incorrectly flags Kubernetes service labels as invalid when using Terraform local.* variable interpolation, even though the resolved values are valid according to Kubernetes label syntax rules.

Expected Behavior

KICS should either:

  1. Resolve Terraform variables before validating label values, OR
  2. Skip validation when label values contain variable interpolations, OR
  3. Document this limitation in the query description

Actual Behavior

KICS validates the literal string local.variable_name against the regex pattern without resolving the Terraform variable.

Minimal Reproducible Example

# variables.tf
variable "resource_prefix" {
  type = string
  default = "my-app-"
}

variable "name" {
  type = string
  default = "service"
}

locals {
  resource_name = "${var.resource_prefix}${var.name}"
}

# service.tf
resource "kubernetes_service_v1" "example" {
  metadata {
    name      = "my-service"
    namespace = "default"
    labels = {
      app = local.resource_name  # KICS flags this as invalid
    }
  }

  spec {
    selector = {
      app = local.resource_name
    }

    port {
      port        = 80
      target_port = 8080
    }
  }
}

KICS Output

Metadata Label Is Invalid, Severity: LOW, Results: 1

Expected: kubernetes_service_v1[example].metadata.labels[app] has valid label
Actual: kubernetes_service_v1[example].metadata.labels[app] has invalid label

Analysis

The query uses this regex: ^(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])?$

The literal string local.resource_name fails validation, but when resolved it becomes "my-app-service" which is perfectly valid per Kubernetes RFC 1123.

Impact

Affects projects using Terraform variables (local.*, var.*, data.*) in Kubernetes labels.

Environment

  • KICS Version: 2.1.19
  • Platform: Terraform
  • Provider: hashicorp/kubernetes

Suggested Fix

Skip validation when label values contain Terraform interpolation syntax or use Terraform plan output for validation.

Related

Similar to #591 regarding variable interpolation support.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingcommunityCommunity contributionkubernetesKubernetes queryqueryNew query featureterraformTerraform query

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions