Skip to content

Updated osquery perf for #45550#48935

Open
getvictor wants to merge 3 commits into
mainfrom
victor/45550-osquery-perf-scep-certs
Open

Updated osquery perf for #45550#48935
getvictor wants to merge 3 commits into
mainfrom
victor/45550-osquery-perf-scep-certs

Conversation

@getvictor

@getvictor getvictor commented Jul 8, 2026

Copy link
Copy Markdown
Member

Related issue: Resolves #45550

Checklist for submitter

  • QA'd all new/changed functionality manually

Summary by CodeRabbit

  • New Features

    • Enhanced Windows MDM SCEP certificate simulation to track issued certificate specs per host during check-ins.
  • Bug Fixes

    • Certificate data generation is now stably ordered and consistent across refreshes.
    • SCEP certificate processing now ignores failed installs (no certificate) and records only valid issued cert details.
  • Testing

    • A small subset of simulated agents may withhold one SCEP certificate to exercise verification edge cases.

@getvictor getvictor requested a review from Copilot July 8, 2026 12:31
@getvictor

Copy link
Copy Markdown
Member Author

@coderabbitai full review

@getvictor

Copy link
Copy Markdown
Member Author

/agentic_review

@coderabbitai

coderabbitai Bot commented Jul 8, 2026

Copy link
Copy Markdown
Contributor
✅ Action performed

Full review finished.

Copilot AI 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.

Pull request overview

Updates osquery-perf to better simulate Windows MDM SCEP certificate issuance and reporting so Fleet can observe the expected certificate markers during perf/load scenarios related to Windows SCEP proxy behavior.

Changes:

  • Track and report certificates issued via Windows MDM SCEP exchanges in the certificates_windows detail query output.
  • Keep SCEP-issued cert reporting stable across refreshes (keyed/sorted by SCEP CSP unique ID).
  • Add a small (~5%) population of simulated hosts that intentionally withhold reporting their SCEP-issued certs to exercise the server’s verification backstop path.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
cmd/osquery-perf/certificates.go Adds storage/serialization of SCEP-issued certs into the simulated certificates detail query.
cmd/osquery-perf/agent.go Adds per-agent SCEP cert withholding behavior and stores issued SCEP certs on successful exchanges for later reporting.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread cmd/osquery-perf/certificates.go
@coderabbitai

coderabbitai Bot commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

This change updates the osquery-perf agent to simulate Windows MDM SCEP certificate behavior. The agent now tracks issued SCEP certificates per host, marks a deterministic subset of hosts as withholding them, and skips storing nil or withheld SCEP results during check-in. New helpers convert X.509 certificates into simulated certificate specs, store them under a mutex, and include them in generated certificate specs in sorted order.

Possibly related issues

Possibly related PRs

  • fleetdm/fleet#46777: Both PRs modify the Windows MDM SCEP CertificateInstall handling path in cmd/osquery-perf/agent.go.
  • fleetdm/fleet#48499: Both PRs extend cmd/osquery-perf/certificates.go and related certificate-spec generation behavior.
🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description only links the issue and a QA checkbox, leaving most required template sections and checklist items empty. Fill in the required checklist sections, including tests, changes-file guidance, and any applicable engineering/product notes.
Linked Issues check ⚠️ Warning The PR only adds osquery-perf SCEP simulation helpers and does not implement the Windows failure reporting requested by #45550. Implement the host_mdm_windows_profiles failure update and UI surfacing for proxied SCEP PKIOperation errors, or scope this as the optional test-only follow-up.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title is concise and accurately points to the osquery-perf update tied to issue #45550.
Out of Scope Changes check ✅ Passed The changes stay within osquery-perf and appear aligned with the optional simulation follow-up for #45550.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch victor/45550-osquery-perf-scep-certs

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@qodo-free-for-open-source-projects

qodo-free-for-open-source-projects Bot commented Jul 8, 2026

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0) 🎨 UX issues (0) 🔗 Cross-repo conflicts (0) 📜 Skill insights (0)

Grey Divider


Informational

1. Seed stream advanced globally ✓ Resolved 🐞 Bug ⚙ Maintainability
Description
newAgent unconditionally consumes the shared math/rand stream to set withholdSCEPCert, which
shifts subsequent rand-derived simulation decisions/strings across the entire run (even for agents
that will never do Windows MDM/SCEP). This reduces the usefulness of --seed for comparing runs,
because many unrelated random outcomes will differ after this PR.
Code

cmd/osquery-perf/agent.go[739]

+		withholdSCEPCert:         rand.Intn(100) < 5, // ~5%: withhold issued SCEP certs to exercise the verification backstop (test-only)
Evidence
The PR adds a new unconditional rand.Intn call during agent initialization. The codebase
explicitly relies on a single global math/rand RNG seeded from --seed, and helper functions like
randomString draw from that same stream; therefore, adding one extra draw shifts subsequent
random-derived behavior.

cmd/osquery-perf/agent.go[730-743]
cmd/osquery-perf/agent.go[3763-3765]
cmd/osquery-perf/agent.go[2094-2102]
cmd/osquery-perf/certificates.go[7-13]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
`newAgent` currently sets `withholdSCEPCert` by calling `rand.Intn(100) < 5`. Because osquery-perf uses a single shared `math/rand` RNG seeded from `--seed`, this extra draw advances the global RNG stream and changes many unrelated random outcomes later in the run.
## Issue Context
This affects seeded-run comparability: even if the only intended behavior change is Windows SCEP certificate reporting, non-Windows simulation behavior (and other Windows behaviors) can shift because they draw from the same global RNG.
## Fix Focus Areas
- cmd/osquery-perf/agent.go[736-742]
## Suggested fix
Make the withholding selection deterministic without advancing the shared RNG stream. For example:
- Derive it from `agentIndex` (e.g. `agentIndex%20==0` for ~5%).
- Or use a separate local RNG/source dedicated to this decision (not the shared global RNG), optionally seeded deterministically from `--seed` + `agentIndex`.
- Optionally gate the selection so it only applies to Windows MDM agents that can actually perform SCEP (e.g. only when `agentOS == "windows"` and MDM is enabled).

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider

Qodo Logo

Comment thread cmd/osquery-perf/agent.go Outdated
@codecov

codecov Bot commented Jul 8, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 36 lines in your changes missing coverage. Please review.
✅ Project coverage is 68.10%. Comparing base (9463a46) to head (66d8ddd).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
cmd/osquery-perf/certificates.go 0.00% 30 Missing ⚠️
cmd/osquery-perf/agent.go 0.00% 6 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #48935      +/-   ##
==========================================
- Coverage   68.11%   68.10%   -0.02%     
==========================================
  Files        3709     3709              
  Lines      235118   235153      +35     
  Branches    12464    12464              
==========================================
  Hits       160148   160148              
- Misses      60594    60631      +37     
+ Partials    14376    14374       -2     
Flag Coverage Δ
backend 69.71% <0.00%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@getvictor getvictor marked this pull request as ready for review July 8, 2026 13:27
@getvictor getvictor requested a review from a team as a code owner July 8, 2026 13:27

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@cmd/osquery-perf/agent.go`:
- Around line 737-738: The withholding cohort is currently derived from
agentIndex, which only selects every shard’s local 20th agent instead of a
consistent global 5% sample. Update the withholdSCEPCert assignment in agent.go
to base the decision on the global host index using hostIndexOffset plus
agentIndex, so the test-only failure cohort is stable across distributed runs.
Use the existing hostIndexOffset and agentIndex symbols in the same
host-generation logic to locate and adjust the condition.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: ce173bc4-5c56-4f99-b13a-31769380eebf

📥 Commits

Reviewing files that changed from the base of the PR and between 328489c and 66d8ddd.

📒 Files selected for processing (2)
  • cmd/osquery-perf/agent.go
  • cmd/osquery-perf/certificates.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • cmd/osquery-perf/certificates.go

Comment thread cmd/osquery-perf/agent.go
Comment on lines +737 to +738
// Every 20th host (5%) withholds its issued SCEP certs to exercise the server's verification backstop (test-only failure).
withholdSCEPCert: agentIndex%20 == 0,

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.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Inspect the relevant file and nearby context.
git ls-files cmd/osquery-perf/agent.go
wc -l cmd/osquery-perf/agent.go
sed -n '680,780p' cmd/osquery-perf/agent.go

# Find definitions and call sites for the relevant identifiers.
rg -n "withholdSCEPCert|agentIndex|hostIndexOffset|newAgent\(" cmd/osquery-perf/agent.go cmd/osquery-perf -S

Repository: fleetdm/fleet

Length of output: 8286


🏁 Script executed:

#!/bin/bash
set -euo pipefail

git ls-files cmd/osquery-perf/agent.go
wc -l cmd/osquery-perf/agent.go
sed -n '680,780p' cmd/osquery-perf/agent.go

rg -n "withholdSCEPCert|agentIndex|hostIndexOffset|newAgent\(" cmd/osquery-perf/agent.go cmd/osquery-perf -S

Repository: fleetdm/fleet

Length of output: 8286


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Show the newAgent signature and the call sites that pass hostIndexOffset / agentIndex.
rg -n "func newAgent|newAgent\(" cmd/osquery-perf/agent.go cmd/osquery-perf -S -A 4 -B 4

# Show any calculations that define agentIndex or hostIndexOffset in the same file.
rg -n "hostIndexOffset|agentIndex" cmd/osquery-perf/agent.go -S -A 3 -B 3

Repository: fleetdm/fleet

Length of output: 7544


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Read only the relevant slice with line numbers.
nl -ba cmd/osquery-perf/agent.go | sed -n '700,760p'

# Show the declaration/call site context for hostIndexOffset and agentIndex.
rg -n "hostIndexOffset|agentIndex|withholdSCEPCert" cmd/osquery-perf/agent.go -n -A 6 -B 6

Repository: fleetdm/fleet

Length of output: 191


🏁 Script executed:

#!/bin/bash
set -euo pipefail

python3 - <<'PY'
from pathlib import Path
p = Path("cmd/osquery-perf/agent.go")
lines = p.read_text().splitlines()
for start, end in [(2168,2208),(3884,3924),(728,742)]:
    print(f"\n--- {start}-{end} ---")
    for i in range(start, end+1):
        if i <= len(lines):
            print(f"{i:4d}: {lines[i-1]}")
PY

Repository: fleetdm/fleet

Length of output: 4188


🏁 Script executed:

#!/bin/bash
set -euo pipefail

python3 - <<'PY'
from pathlib import Path
p = Path("cmd/osquery-perf/agent.go")
lines = p.read_text().splitlines()

for start, end in [(3648, 3672), (3880, 3935), (2188, 2210)]:
    print(f"\n--- {start}-{end} ---")
    for i in range(start, end + 1):
        if i <= len(lines):
            print(f"{i:4d}: {lines[i-1]}")
PY

Repository: fleetdm/fleet

Length of output: 5219


Base the withholding cohort on the global host index. agentIndex is 1-based, so agentIndex%20 == 0 only withholds on each shard’s local 20th agent. Use (hostIndexOffset + agentIndex - 1) % 20 == 0 so the 5% cohort is consistent across distributed runs.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@cmd/osquery-perf/agent.go` around lines 737 - 738, The withholding cohort is
currently derived from agentIndex, which only selects every shard’s local 20th
agent instead of a consistent global 5% sample. Update the withholdSCEPCert
assignment in agent.go to base the decision on the global host index using
hostIndexOffset plus agentIndex, so the test-only failure cohort is stable
across distributed runs. Use the existing hostIndexOffset and agentIndex symbols
in the same host-generation logic to locate and adjust the condition.

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.

Surface SCEP failures that are proxied by Fleet (Windows)

3 participants