← Policy catalog
</> ValidatingAdmissionPolicy · Apache-2.0

Exposes an Admin or Metrics Port Externally

service-admin-ports

severityModerate resourceServices productKubernetes bundles1

Admin, debug, metrics and management listeners are usually unauthenticated, so publishing them on a LoadBalancer or NodePort hands outside callers internal telemetry, profiling and heap dumps, and sometimes live control over the workload.

Rejects unless

!(
  (object.spec.type == 'LoadBalancer' || object.spec.type == 'NodePort')
  &&
  object.spec.ports.exists(p,
    has(p.name) &&
    p.name.matches('(?i).*(admin|debug|metrics|mgmt|management).*')
  )
)

Externally reachable Services (type LoadBalancer/NodePort) must not expose admin/debug/metrics ports. Split these endpoints into a separate internal-only Service.

Remediation

Move the admin, debug, metrics and management ports to a separate ClusterIP Service and leave only application ports on the externally reachable one. Fields: spec.type, spec.ports[*].name.

Applies to

  • services · v1 · CREATE, UPDATE

Manifest

apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingAdmissionPolicy
metadata:
  name: service-admin-ports
  annotations:
    kubeapt.io/uuid: "4bb23409-36e0-41b0-8511-c6511ac6b211"
    security.kubeapt.io/displayName: "Exposes an Admin or Metrics Port Externally"
    security.kubeapt.io/description: "Admin, debug, metrics and management listeners are usually unauthenticated, so publishing them on a LoadBalancer or NodePort hands outside callers internal telemetry, profiling and heap dumps, and sometimes live control over the workload."
    security.kubeapt.io/resource: "Services"
    security.kubeapt.io/severity: "Moderate"
    security.kubeapt.io/remediation: "Move the admin, debug, metrics and management ports to a separate ClusterIP Service and leave only application ports on the externally reachable one. Fields: spec.type, spec.ports[*].name."
    security.kubeapt.io/product: "Kubernetes"
spec:
  failurePolicy: Fail
  matchConstraints:
    resourceRules:
    - apiGroups:
      - ''
      apiVersions:
      - v1
      operations:
      - CREATE
      - UPDATE
      resources:
      - services
  validations:
  - expression: |
      !(
        (object.spec.type == 'LoadBalancer' || object.spec.type == 'NodePort')
        &&
        object.spec.ports.exists(p,
          has(p.name) &&
          p.name.matches('(?i).*(admin|debug|metrics|mgmt|management).*')
        )
      )
    message: Externally reachable Services (type LoadBalancer/NodePort) must not expose admin/debug/metrics ports. Split these endpoints into a separate internal-only Service.

Save it as service-admin-ports.yaml — the commands below assume that name.

Validate with kubeapt

Check a workload against this one policy, before anything is installed on a cluster.

$ kubeapt validate -p ./service-admin-ports.yaml -r ./workload.yaml

Or without the file, from the published policy set — -r takes a directory too:

$ kubeapt policies download && kubeapt validate --policy-name service-admin-ports -r ./manifests

Or against every namespace in the cluster your kubeconfig points at:

$ kubeapt validate --policy-name service-admin-ports -A