Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Collect audit logs from kube-apiserver #1666

Merged
merged 12 commits into from
Sep 22, 2023
Merged

Collect audit logs from kube-apiserver #1666

merged 12 commits into from
Sep 22, 2023

Conversation

jaehnri
Copy link
Contributor

@jaehnri jaehnri commented Aug 18, 2023

This is how the audit logs are shown in OpenSearch:

{
    "_id": "5f2b9baf-cbd4-44bf-b81f-b5ad28e0aff1",
    "_index": "logs-v0.5.4-000003",
    "_score": null,
    "_source": {
        "anomaly_level": "",
        "Body.value": "{\"kind\":\"Event\",\"apiVersion\":\"audit.k8s.io/v1\",\"level\":\"Metadata\",\"auditID\":\"234fa224-b400-404e-931e-46effd64a321\",\"stage\":\"ResponseStarted\",\"requestURI\":\"/api/v1/pods?allowWatchBookmarks=true\\u0026resourceVersion=3963946\\u0026timeout=5m12s\\u0026timeoutSeconds=312\\u0026watch=true\",\"verb\":\"watch\",\"user\":{\"username\":\"system:kube-controller-manager\",\"groups\":[\"system:authenticated\"]},\"sourceIPs\":[\"127.0.0.1\"],\"userAgent\":\"k3s/v1.26.7+k3s1 (linux/amd64) kubernetes/e47cfc0/shared-informers\",\"objectRef\":{\"resource\":\"pods\",\"apiVersion\":\"v1\"},\"responseStatus\":{\"metadata\":{},\"code\":200},\"requestReceivedTimestamp\":\"2023-08-30T19:46:38.513630Z\",\"stageTimestamp\":\"2023-08-30T19:46:38.514778Z\",\"annotations\":{\"authorization.k8s.io/decision\":\"allow\",\"authorization.k8s.io/reason\":\"RBAC: allowed by ClusterRoleBinding \\\"system:kube-controller-manager\\\" of ClusterRole \\\"system:kube-controller-manager\\\" to User \\\"system:kube-controller-manager\\\"\"}}",
        "cluster_id": "e1967963-f4ec-4e4a-afa1-a766ff0ddb18",
        "deployment": "",
        "k8s.auditlog.audit_id": "234fa224-b400-404e-931e-46effd64a321",
        "k8s.auditlog.level": "Metadata",
        "k8s.auditlog.resource": "pods",
        "k8s.auditlog.stage": "ResponseStarted",
        "k8s.auditlog.stage_timestamp": "2023-08-30T19:46:38.514778Z",
        "kubernetes_component": "",
        "log": "",
        "log_type": "controlplane",
        "namespace_name": "",
        "pod_name": "",
        "service": "",
        "SeverityNumber": 0,
        "template_matched": "",
        "time": 1693424798514,
        "TraceFlags": 0
    },
    "_version": 1,
    "fields": {
        "time": [
            "2023-08-30T19:46:38.514Z"
        ]
    },
    "sort": [
        1693424798514
    ]
}

As the audit log is a bit complex, I chose a few properties that make more sense to be parsed from the JSON log:

  • "k8s.auditlog.audit_id": "234fa224-b400-404e-931e-46effd64a321",
  • "k8s.auditlog.level": "Metadata";
  • "k8s.auditlog.resource": "pods";
  • "k8s.auditlog.stage": "ResponseStarted";
  • "k8s.auditlog.stage_timestamp": "2023-08-30T19:46:38.514778Z"

The whole log can still be checked in the body.
image

Testing config

For testing, I used the following Kubernetes auditLog configuration:

apiVersion: audit.k8s.io/v1
kind: Policy
rules:
  - level: Metadata
    resources:
    - group: ""
      resources: ["pods"]

And this logging.opni.io configuration in a K3s cluster:

...
spec:
  kubeAuditLogs:
    auditFilename: audit.log
    enabled: true
    pathPrefix: /var/lib/rancher/k3s/server/logs
  provider: k3s
  selector: {}

@jaehnri jaehnri changed the title Collect kube-apiserver audit logs path Collect audit logs from kube-apiserver Aug 18, 2023
@jaehnri jaehnri marked this pull request as ready for review August 28, 2023 23:11
@jaehnri jaehnri marked this pull request as draft August 29, 2023 18:29
@jaehnri jaehnri force-pushed the kube-audit-logs branch 3 times, most recently from b8589cc to d606a5d Compare August 30, 2023 20:40
@jaehnri jaehnri marked this pull request as ready for review August 30, 2023 21:26
dbason
dbason previously requested changes Sep 11, 2023
pkg/resources/collector/templates.go Outdated Show resolved Hide resolved
pkg/resources/collector/templates.go Show resolved Hide resolved
@jaehnri jaehnri requested a review from dbason September 15, 2023 18:35
@jaehnri jaehnri force-pushed the kube-audit-logs branch 2 times, most recently from ca40d3f to 52c1507 Compare September 15, 2023 18:41
Signed-off-by: Joao Henri <joao.rocha@suse.com>
Signed-off-by: Joao Henri <joao.rocha@suse.com>
Signed-off-by: Joao Henri <joao.rocha@suse.com>
Signed-off-by: Joao Henri <joao.rocha@suse.com>
Signed-off-by: Joao Henri <joao.rocha@suse.com>
Signed-off-by: Joao Henri <joao.rocha@suse.com>
Signed-off-by: Joao Henri <joao.rocha@suse.com>
Signed-off-by: Joao Henri <joao.rocha@suse.com>
Signed-off-by: Joao Henri <joao.rocha@suse.com>
Signed-off-by: Joao Henri <joao.rocha@suse.com>
Signed-off-by: Joao Henri <joao.rocha@suse.com>
Signed-off-by: Joao Henri <joao.henri@suse.com>
@jaehnri jaehnri merged commit 49cd69f into main Sep 22, 2023
2 checks passed
@jaehnri jaehnri deleted the kube-audit-logs branch September 22, 2023 03:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants