Skip to content

[CWS] Introduce CWS K8S User Session tracking context - #18620

Merged
Gui774ume merged 11 commits into
mainfrom
will/inject-k8s-user-info
Nov 9, 2023
Merged

Gui774ume merged 11 commits into
mainfrom
will/inject-k8s-user-info

Conversation

@Gui774ume

@Gui774ume Gui774ume commented Aug 7, 2023 •

Copy link
Copy Markdown
Contributor

What does this PR do?

This PR introduces a User Sesison resolver for CWS (see below for an exaplaination about what it is, and why we introduced it). This new resolver is made of 3 different parts:

  • An admission mutating webhook that instruments both the pod creation and pod execution events:
    • During the pod creation, the mutation webhook will introduce a new volume (called datadog-cws-instrumentation) and a mount points to this volume in all the containers of the pod. It will also introduce a new init container with a mount point to the same volume. On init, this container will copy cws-instrumentation (see below) to the volume.
    • During the pod execution, the mutating webhook will override the command to first call cws-instrumentation with the K8S context as argument, and then the user command.
  • cws-instrumentation is a new command in /cmd which is called (thanks to the previous step) before any command is run in an instrumented pod. This binary will forward the k8s context it received as argument to the CWS agent using an eRPC call.
  • the User Session resolver, which contains all the required logic in CWS to receive the k8s context from an eRPC call, integrate it in the process context recorded by CWS, and add business logic to it (i.e. the ability to write rules for it).
sequenceDiagram
    autonumber
    participant D as Dev1
    participant API as API server
    participant AC as CWS Admission Controller
    participant C as Container1
    participant CWS as CWS agent
    API->>+AC: A new deployment is scheduled
    Note left of AC: A new volume is mounted in<br/>all containers
    Note left of AC: An init container is added to inject <br/>`cws-instrumentation` in the volume
    AC-->>-API: OK
    D->>+API: kubectl exec bash
    API->>+AC: "Dev1" wants to run "bash"<br/>in "Container1"
    Note left of AC: "bash" is replaced with<br/>"/cws/cws-instrumentation {credentials} bash"
    AC-->>-API: OK
    API->>+C: "/cws/cws-instrumentation {credentials} bash"
    activate C
    C->>+CWS: eRPC: store {credentials}<br/>for current session
    CWS-->>C: OK
    C->>C: exec "bash"
    C-->>D: Interractive session opened
    Note over D,C: User session
    D->>C: exit
    Note left of CWS: session {credentials}<br/>cleared
    C-->>D: OK
    deactivate C
    deactivate CWS
    deactivate API
Loading

Motivation

Currently, when CWS events are emitted, only the host user is reported (i.e. root or local Unix users). Although this is an interesting piece of context for services or applications that might run as privileged users, it doesn't really help understand what's happening when a developer or sysadmin run commands in production remotely. These commands are usually flagged by our default rules, and the lack of context make them harder to investigate. For Kubernetes environment, this PR introduces an additional piece of context for these events: the content of the v1.UserInfo structure. When Kubernetes is properly configured, CWS events will now contain the K8s username, UID and groups of the user that ran the commands remotely.

Additional Notes

  • For now, the cws-instrumentation image is stored in Dockerhub, but if this feature gets more traction, we will publish the image to more image registry.
  • We currently do not support "existing" containers, the CWS instrumentation will only work for workloads that are scheduled after the Datadog agent is deployed.
  • This feature should be considered experimental and exploratory for now. It doesn't deserve a changelog.

Describe how to test/QA your changes

To QA this feature:

  1. add the following CWS rule to an agent deployed in a K8S cluster:
 - id: user_session_test
   expression: exec.user_session.k8s_username != ""
  1. Deploy a pod in the cluster
  2. Run kubectl exec your_pod -- bash

You should have generated a CWS event with your K8S user context.

Reviewer's Checklist

  • If known, an appropriate milestone has been selected; otherwise the Triage milestone is set.
  • Use the major_change label if your change either has a major impact on the code base, is impacting multiple teams or is changing important well-established internals of the Agent. This label will be use during QA to make sure each team pay extra attention to the changed behavior. For any customer facing change use a releasenote.
  • A release note has been added or the changelog/no-changelog label has been applied.
  • Changed code has automated tests for its functionality.
  • Adequate QA/testing plan information is provided if the qa/skip-qa label is not applied.
  • At least one team/.. label has been applied, indicating the team(s) that should QA this change.
  • If applicable, docs team has been notified or an issue has been opened on the documentation repo.
  • If applicable, the need-change/operator and need-change/helm labels have been applied.
  • If applicable, the k8s/<min-version> label, indicating the lowest Kubernetes version compatible with this feature.
  • If applicable, the config template has been updated.

@Gui774ume Gui774ume added this to the 7.48.0 milestone Aug 7, 2023
@Gui774ume
Gui774ume requested review from a team as code owners August 7, 2023 09:22
@Gui774ume
Gui774ume force-pushed the will/inject-k8s-user-info branch 4 times, most recently from 4a98976 to 9b85b48 Compare August 7, 2023 09:59
@pr-commenter

pr-commenter Bot commented Aug 7, 2023 •

Copy link
Copy Markdown

Bloop Bleep... Dogbot Here

Regression Detector Results

Run ID: 0fd65884-1980-48f2-bbf1-7819f3d5bd0e
Baseline: 3eac899
Comparison: 5fb6ed0
Total datadog-agent CPUs: 7

Explanation

A regression test is an integrated performance test for datadog-agent in a repeatable rig, with varying configuration for datadog-agent. What follows is a statistical summary of a brief datadog-agent run for each configuration across SHAs given above. The goal of these tests are to determine quickly if datadog-agent performance is changed and to what degree by a pull request.

Because a target's optimization goal performance in each experiment will vary somewhat each time it is run, we can only estimate mean differences in optimization goal relative to the baseline target. We express these differences as a percentage change relative to the baseline target, denoted "Δ mean %". These estimates are made to a precision that balances accuracy and cost control. We represent this precision as a 90.00% confidence interval denoted "Δ mean % CI": there is a 90.00% chance that the true value of "Δ mean %" is in that interval.

We decide whether a change in performance is a "regression" -- a change worth investigating further -- if both of the following two criteria are true:

  1. The estimated |Δ mean %| ≥ 5.00%. This criterion intends to answer the question "Does the estimated change in mean optimization goal performance have a meaningful impact on your customers?". We assume that when |Δ mean %| < 5.00%, the impact on your customers is not meaningful. We also assume that a performance change in optimization goal is worth investigating whether it is an increase or decrease, so long as the magnitude of the change is sufficiently large.

  2. Zero is not in the 90.00% confidence interval "Δ mean % CI" about "Δ mean %". This statement is equivalent to saying that there is at least a 90.00% chance that the mean difference in optimization goal is not zero. This criterion intends to answer the question, "Is there a statistically significant difference in mean optimization goal performance?". It also means there is no more than a 10.00% chance this criterion reports a statistically significant difference when the true difference in mean optimization goal is zero -- a "false positive". We assume you are willing to accept a 10.00% chance of inaccurately detecting a change in performance when no true difference exists.

The table below, if present, lists those experiments that have experienced a statistically significant change in mean optimization goal performance between baseline and comparison SHAs with 90.00% confidence OR have been detected as newly erratic. Negative values of "Δ mean %" mean that baseline is faster, whereas positive values of "Δ mean %" mean that comparison is faster. Results that do not exhibit more than a ±5.00% change in their mean optimization goal are discarded. An experiment is erratic if its coefficient of variation is greater than 0.1. The abbreviated table will be omitted if no interesting change is observed.

No interesting changes in experiment optimization goals with confidence ≥ 90.00% and |Δ mean %| ≥ 5.00%.

Fine details of change detection per experiment.
experiment goal Δ mean % Δ mean % CI confidence
otel_to_otel_logs ingress throughput +1.24 [-0.35, +2.83] 80.00%
process_agent_real_time_mode egress throughput +0.14 [-2.38, +2.66] 7.30%
process_agent_standard_check egress throughput +0.09 [-3.45, +3.63] 3.43%
dogstatsd_string_interner_8MiB_50k ingress throughput +0.03 [-0.01, +0.08] 77.58%
idle egress throughput +0.03 [-2.41, +2.47] 1.52%
trace_agent_msgpack ingress throughput +0.02 [-0.12, +0.15] 15.50%
tcp_dd_logs_filter_exclude ingress throughput +0.01 [-0.07, +0.09] 15.21%
dogstatsd_string_interner_8MiB_100k ingress throughput +0.01 [-0.04, +0.06] 22.60%
dogstatsd_string_interner_128MiB_1k ingress throughput +0.00 [-0.13, +0.14] 4.55%
dogstatsd_string_interner_8MiB_1k ingress throughput +0.00 [-0.10, +0.11] 5.86%
dogstatsd_string_interner_64MiB_1k ingress throughput +0.00 [-0.13, +0.14] 3.68%
dogstatsd_string_interner_8MiB_100 ingress throughput +0.00 [-0.13, +0.13] 2.52%
dogstatsd_string_interner_8MiB_10k ingress throughput +0.00 [-0.02, +0.02] 16.71%
dogstatsd_string_interner_64MiB_100 ingress throughput +0.00 [-0.13, +0.14] 0.60%
dogstatsd_string_interner_128MiB_100 ingress throughput +0.00 [-0.14, +0.14] 0.14%
trace_agent_json ingress throughput +0.00 [-0.13, +0.13] 0.13%
uds_dogstatsd_to_api ingress throughput -0.02 [-0.18, +0.14] 14.92%
file_tree egress throughput -0.02 [-1.86, +1.82] 1.59%
process_agent_standard_check_with_stats egress throughput -0.21 [-2.21, +1.79] 13.49%
file_to_blackhole egress throughput -0.45 [-0.89, -0.01] 90.54%
tcp_syslog_to_blackhole ingress throughput -1.22 [-1.34, -1.09] 100.00%

@Gui774ume
Gui774ume force-pushed the will/inject-k8s-user-info branch from 9b85b48 to c3fd273 Compare August 7, 2023 15:04

@rtrieu rtrieu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion to change all instances of "Groups" and "Username" to all lowercase.

Comment thread docs/cloud-workload-security/secl.json Resolved
Comment thread docs/cloud-workload-security/secl.json Resolved
Comment thread docs/cloud-workload-security/secl.json Resolved
Comment thread docs/cloud-workload-security/secl.json Resolved
Comment thread docs/cloud-workload-security/secl.json Resolved
@Gui774ume
Gui774ume force-pushed the will/inject-k8s-user-info branch 7 times, most recently from 1d9cd3c to 6a67528 Compare August 8, 2023 16:55

@rtrieu rtrieu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@Gui774ume
Gui774ume force-pushed the will/inject-k8s-user-info branch from 6a67528 to 1d20fe6 Compare August 9, 2023 09:49
@Gui774ume
Gui774ume force-pushed the will/inject-k8s-user-info branch 3 times, most recently from ff206fd to 02277f8 Compare October 24, 2023 18:35
Comment thread .gitlab/binary_build/cws_injector.yml Resolved
Comment thread .gitlab/binary_build/cws_injector.yml Resolved
Comment thread tasks/cws_injector.py Resolved
@Gui774ume
Gui774ume force-pushed the will/inject-k8s-user-info branch 4 times, most recently from 730a195 to 31e3bad Compare October 26, 2023 10:42
Comment thread pkg/config/config.go Resolved
@Gui774ume
Gui774ume force-pushed the will/inject-k8s-user-info branch 2 times, most recently from 22a723c to 980f767 Compare November 3, 2023 14:50

@chouquette chouquette left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, the few cosmetic comments can be addressed in a followup PR :)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could also add go_deps here and drop the redefinition in the inheriting jobs

Comment on lines 12 to 13

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be defined in .cws_instrumentation-build_common (ditto for the arm64 job)

Comment on lines 19 to 21

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could also be in .cws_instrumentation-build_common

Comment thread .gitlab/deploy_cws_instrumentation.yml Outdated

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could explicitly reference the jobs that create the docker images here with needs: so that the jobs start ASAP instead of waiting for all stages prior to deploy_cws_instrumentation to complete
You'll need to specify artifacts: false to ensure you don't try to download the artifacts, even though I suppose there are none to download

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants