google-cloud-platformgoogle-kubernetes-enginegoogle-cloud-stackdriver

GKE cluster creator in GCP


How can we get the cluster owner details in GKE. Logging part only contains the entry with service account operations and there is no entry with principal email of userId anywhere.

It seems very difficult to get the name of the user who created the GKE cluster.

we have exported complete json file of logs but did not the user entry who actually click on create cluster button. I think this is very common use case to know GKE cluster creator, not sure if we are missing something.

Query:

resource.type="k8s_cluster"
resource.labels.cluster_name="clusterName"
resource.labels.location="us-central1"
-protoPayload.methodName="io.k8s.core.v1.configmaps.update"
-protoPayload.methodName="io.k8s.coordination.v1.leases.update"
-protoPayload.methodName="io.k8s.core.v1.endpoints.update"
severity=DEFAULT
-protoPayload.authenticationInfo.principalEmail="system:addon-manager"
-protoPayload.methodName="io.k8s.apiserver.flowcontrol.v1beta1.flowschemas.status.patch"
-protoPayload.methodName="io.k8s.certificates.v1.certificatesigningrequests.create"
-protoPayload.methodName="io.k8s.core.v1.resourcequotas.delete"
-protoPayload.methodName="io.k8s.core.v1.pods.create"
-protoPayload.methodName="io.k8s.apiregistration.v1.apiservices.create"

I have referred the link below, but it did not help either.

https://cloud.google.com/blog/products/management-tools/finding-your-gke-logs


Solution

  • Audit Logs and specifically Admin Activity Logs

    And, there's a "trick": The activity audit log entries include the API method. You can find the API method that interests you. This isn't super straightforward but it's relatively easy. You can start by scoping to the service. For GKE, the service is container.googleapis.com.

    NOTE APIs Explorer and Kubenetes Engine API (but really container.googleapis.com) and projects.locations.clusters.create. The mechanism breaks down a little here as the protoPayload.methodName is a variant of the underlying REST method name.

    And so you can use logs explorer with the following very broad query:

    logName="projects/{PROJECT}/logs/cloudaudit.googleapis.com%2Factivity"
    container.googleapis.com
    

    NOTE replace {PROJECT} with the value.

    And then refine this based on what's returned:

    logName="projects/{PROJECT}/logs/cloudaudit.googleapis.com%2Factivity"
    protoPayload.serviceName="container.googleapis.com"
    protoPayload.methodName="google.container.v1beta1.ClusterManager.CreateCluster"
    

    NOTE I mentioned that it isn't super straightforward because, as you can see in the above, I'd used gcloud beta container clusters create and so I need the google.container.v1beta1.ClusterManager.CreateCluster method but, it was easy to determine this from the logs.

    And, who dunnit?

    protoPayload: {
      authenticationInfo: {
        principalEmail: "{me}"
      }
    }
    

    So:

    PROJECT="[YOUR-PROJECT]"
    
    FILTER="
    logName=\"projects/${PROJECT}/logs/cloudaudit.googleapis.com%2Factivity\"
    protoPayload.serviceName=\"container.googleapis.com\"
    protoPayload.methodName=\"google.container.v1beta1.ClusterManager.CreateCluster\"
    "
    
    gcloud logging  read "${FILTER}" \
    --project=${PROJECT} \
    --format="value(protoPayload.authenticationInfo.principalEmail)"