Skip to content

Falco: Unexpected connection to K8s API Server from container

Kubernetestested on RKE2 v1.37.2, Falco 0.45.2

The rule Contact K8S API Server From Container is loud on purpose: a container that talks to the API can be an attacker with a stolen token. If your own job does it regularly, the noise hides real hits of the same rule.

Symptom

kubectl -n falco logs -l app.kubernetes.io/name=falco -c falco --tail=-1 \
  | grep 'Contact K8S' | jq -r .output | fold -w 86
10:28:55.224711757: Notice Unexpected connection to K8s API Server from container | co
nnection=10.42.0.132:40924->10.43.0.1:443 lport=40924 rport=443 fd_type=ipv4 fd_proto=
tcp evt_type=connect user=root user_uid=0 user_loginuid=-1 process=kubectl proc_exepat
h=/bin/kubectl parent=containerd-shim command=kubectl delete pods -n tag13 --field-sel
ector=status.phase==Succeeded terminal=0 container_id=6eaeb4308ebe container_name=kube
ctl container_image_repository=registry.k8s.io/kubectl container_image_tag=v1.37.2 k8s
_pod_name=aufraeumer-1-cvmcn k8s_ns_name=tag13

In the course it's a CronJob that deletes finished pods every ten minutes – so the same alert six times an hour. Falco blocks nothing, it only reports.

Diagnosis

Read the rule the way Falco loaded it – directly in the pod.

kubectl -n falco exec ds/falco -c falco -- \
  grep -A14 'rule: Contact K8S API Server From Container' /etc/falco/falco_rules.yaml \
  | sed -n '1p;/condition/,$p'
- rule: Contact K8S API Server From Container
  condition: >
    evt.type=connect
    and (fd.typechar=4 or fd.typechar=6)
    and container
    and k8s_api_server
    and not k8s_containers
    and not user_known_contact_k8s_api_server_activities

Causes

Legitimate jobYour own container talks to the API

k8s_containers is a fixed list of known images plus kube-system. Your own job running kubectl isn't in it and triggers the rule on every run.

Real hitStolen token

That is exactly what the rule is for. That's why the exception should be as narrow as possible – rather than disabling the rule.

LOAD_ERR_VALIDATEAn exception without values takes Falco down

Every exception needs values. "schema validation: ok" is misleading: the rule engine rejects the file afterwards and Falco exits. The DaemonSet replaces one pod at a time – one node is blind, and Helm only reports a timeout.

Error: /etc/falco/rules.d/tag13-ausnahmen.yaml: Invalid
LOAD_ERR_VALIDATE (Error validating rule/macro/list/exception objects): Rule
  exception must have values property with a list of values

Fix

Append a narrow exception to the existing rule: this namespace, this image, this process. override with exceptions: append leaves condition and output untouched. The chart puts customRules into /etc/falco/rules.d/.
cat > ausnahme.yaml <<'EOF'
customRules:
  tag13-ausnahmen.yaml: |-
    - rule: Contact K8S API Server From Container
      exceptions:
      - name: tag13_aufraeumer
        fields: [k8s.ns.name, container.image.repository, proc.name]
        comps: [=, =, =]
        values:
        - [tag13, registry.k8s.io/kubectl, kubectl]
      override:
        exceptions: append
EOF
helm upgrade falco falcosecurity/falco -n falco --version 9.2.2 \
  -f falco-values.yaml -f ausnahme.yaml --wait --timeout 5m | grep -E 'STATUS|REV'
STATUS: deployed
REVISION: 3

Don't: Disabling the rule. Real hits then go unnoticed too – in the course it still fires for a different pod after the exception.

▶
Break it yourself and find it – free
Debugging Kubernetes, day 13: tracing Falco alerts, fixing false alarms with narrow exceptions and finding broken rule files – with a drill to reproduce each one.
Go to day 13 →

FAQ

Why are the old alerts gone after the Helm upgrade?

Every Helm upgrade restarts the Falco pods, and their log starts over. In production, alerts therefore belong in a central system, not in the pod log.

Related error patterns

More