id: gcloud-gke-default-service-account

info:
  name: GKE Clusters Using Default Service Account
  author: princechaddha
  severity: medium
  description: |
    Ensure that your Google Kubernetes Engine (GKE) clusters are configured to use user-managed service accounts instead of the default service account managed by Google Cloud. The default service account has broad permissions across your GCP project, which violates the Principle of Least Privilege (POLP).
  impact: |
    Using the default service account increases security risks as it has broad permissions that could be exploited if compromised, potentially leading to unauthorized access across project resources.
  remediation: |
    Create a custom service account with minimal required permissions and update your node pools to use it instead of the default service account.
  reference:
    - https://cloud.google.com/kubernetes-engine/docs/how-to/hardening-your-cluster#use_least_privilege_sa
    - https://www.trendmicro.com/cloudoneconformity/knowledge-base/gcp/GKE/remove-default-service-account.html
  tags: cloud,devops,gcp,gcloud,gke,kubernetes,security,iam,gcp-cloud-config

flow: |
  code(1)
  for(let projectId of iterate(template.projectIds)){
    set("projectId", projectId)
    code(2)
    for(let cluster of iterate(template.clusters)){
      cluster = JSON.parse(cluster)
      set("clusterName", cluster.name)
      set("location", cluster.location)
      code(3)
      for(let nodePool of iterate(template.nodePools)){
        nodePool = JSON.parse(nodePool)
        set("nodePoolName", nodePool.name)
        code(4)
      }
    }
  }

self-contained: true

code:
  - engine:
      - sh
      - bash
    source: |
      gcloud projects list --format="json(projectId)"

    extractors:
      - type: json
        name: projectIds
        internal: true
        json:
          - '.[].projectId'

  - engine:
      - sh
      - bash
    source: |
      gcloud container clusters list --project $projectId --format="json(name,location)"

    extractors:
      - type: json
        name: clusters
        internal: true
        json:
          - '.[]'

  - engine:
      - sh
      - bash
    source: |
      gcloud container node-pools list --cluster $clusterName --location $location --project $projectId --format="json(name)"

    extractors:
      - type: json
        name: nodePools
        internal: true
        json:
          - '.[]'

  - engine:
      - sh
      - bash
    source: |
      gcloud container node-pools describe $nodePoolName --cluster $clusterName --location $location --project $projectId --format="value(config.serviceAccount)"

    matchers:
      - type: word
        words:
          - "default"

    extractors:
      - type: dsl
        dsl:
          - '"Node pool " + nodePoolName + " in GKE cluster " + clusterName + " (" + location + ") of project " + projectId + " is using the default service account"'
# digest: 490a00463044022076bf265981036c9db2bd630fe371e3f583723757c70fb00e17ef74491570539702205602607493cebac53057a0e78004697ef29ab4be20df016ae99f5efd401bce44:922c64590222798bb761d5b6d8e72950