Skip to content

fix gitops relative paths for unassigned and org_settings#47512

Open
MagnusHJensen wants to merge 1 commit into
mainfrom
fix-gitops-relative-paths-for-unassigned
Open

fix gitops relative paths for unassigned and org_settings#47512
MagnusHJensen wants to merge 1 commit into
mainfrom
fix-gitops-relative-paths-for-unassigned

Conversation

@MagnusHJensen

@MagnusHJensen MagnusHJensen commented Jun 12, 2026

Copy link
Copy Markdown
Member

Related issue: Resolves #45661

I couldn't really find another good solution that would solve it all, as the path resolution is spread out, plus unassigned merging into global config definitely makes it more complex (root cause of the issue).

Checklist for submitter

If some of the following don't apply, delete the relevant line.

  • Changes file added for user-visible changes in changes/, orbit/changes/ or ee/fleetd-chrome/changes.
    See Changes files for more information.

  • Input data is properly validated, SELECT * is avoided, SQL injection is prevented (using placeholders for values in statements), JS inline code is prevented especially for url redirects, and untrusted data interpolated into shell scripts/commands is validated against shell metacharacters.

  • Timeouts are implemented and retries are limited to avoid infinite loops

  • If paths of existing endpoints are modified without backwards compatibility, checked the frontend/CLI for any necessary changes

Testing

  • Added/updated automated tests
  • QA'd all new/changed functionality manually

Summary by CodeRabbit

  • Bug Fixes
    • Fixed GitOps relative path resolution for configuration files, ensuring paths referenced in control and organization settings are properly resolved to their source directory.

@MagnusHJensen MagnusHJensen requested a review from a team as a code owner June 12, 2026 14:07
Copilot AI review requested due to automatic review settings June 12, 2026 14:07
@coderabbitai

coderabbitai Bot commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

This PR fixes GitOps relative path resolution failures when custom setup profiles are added to unassigned.yml. The fix introduces path-anchoring helpers in pkg/spec/gitops.go that convert relative file paths to absolute paths anchored at their containing directory, then integrates these helpers into org_settings parsing (for nested files) and controls extraction (for unassigned.yml). The fleetctl GitOps command now calls ResolveFilePathsAbs after loading controls so relative paths remain valid when applied under the global config. Integration and unit tests verify that paths using ../ references and same-directory layouts resolve correctly without double-anchoring.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 36.36% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the main changes: fixing GitOps relative path resolution for both unassigned.yml and org_settings, which aligns with the PR objectives.
Description check ✅ Passed The PR description includes all key checklist items completed: changes file added, input validation checked, automated tests added, and manual QA performed. The description references the related issue #45661.
Linked Issues check ✅ Passed The code changes fully address issue #45661 by implementing relative path resolution for unassigned.yml controls and org_settings, including helper methods ResolveFilePathsAbs and reanchorOrgSettingsPaths, plus comprehensive test coverage.
Out of Scope Changes check ✅ Passed All changes are directly related to fixing GitOps path resolution for unassigned.yml and org_settings as specified in issue #45661; no out-of-scope modifications detected.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ 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 fix-gitops-relative-paths-for-unassigned

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 and usage tips.

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

🧹 Nitpick comments (1)
pkg/spec/gitops_test.go (1)

1371-1464: ⚡ Quick win

Add coverage for nested yara_rules[].path re-anchoring.

reanchorOrgSettingsPaths also rewrites org_settings.yara_rules[].path, but this regression test currently validates only org_info and mdm path fields. Adding one focused subtest here would lock the new branch.

🧪 Suggested test addition
+	t.Run("yara_rules paths resolve relative to nested file", func(t *testing.T) {
+		gitops, tmpDir := setup(t, `
+server_settings:
+  server_url: https://fleet.example.com
+org_info:
+  contact_url: https://example.com/contact
+  org_name: Test Org
+yara_rules:
+  - path: ../yara/rule.yar
+secrets:
+`)
+		rules := gitops.OrgSettings["yara_rules"].([]any)
+		rule := rules[0].(map[string]any)
+		assert.Equal(t, filepath.Join(tmpDir, "yara/rule.yar"), rule["path"])
+	})
🤖 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 `@pkg/spec/gitops_test.go` around lines 1371 - 1464, Add a new subtest inside
TestGitOpsOrgSettingsNestedPathResolution that verifies reanchoring for
org_settings.yara_rules[].path: create nested settings via the existing setup
helper with an org.yml containing yara_rules: - path: ../yara/rule.yar, call
GitOpsFromFile through setup, then extract gitops.OrgSettings["yara_rules"]
(assert cast to []any -> map[string]any) and assert the "path" value equals
filepath.Join(tmpDir, "yara", "rule.yar"). This mirrors the existing
org_info/mdm checks and locks the reanchorOrgSettingsPaths behavior for
yara_rules[].path.
🤖 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.

Nitpick comments:
In `@pkg/spec/gitops_test.go`:
- Around line 1371-1464: Add a new subtest inside
TestGitOpsOrgSettingsNestedPathResolution that verifies reanchoring for
org_settings.yara_rules[].path: create nested settings via the existing setup
helper with an org.yml containing yara_rules: - path: ../yara/rule.yar, call
GitOpsFromFile through setup, then extract gitops.OrgSettings["yara_rules"]
(assert cast to []any -> map[string]any) and assert the "path" value equals
filepath.Join(tmpDir, "yara", "rule.yar"). This mirrors the existing
org_info/mdm checks and locks the reanchorOrgSettingsPaths behavior for
yara_rules[].path.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 8f865dfb-5702-48fd-a79f-6aa4c846f38e

📥 Commits

Reviewing files that changed from the base of the PR and between ac6aa73 and 9d78de0.

📒 Files selected for processing (5)
  • changes/45661-fix-gitops-relative-paths
  • cmd/fleetctl/fleetctl/gitops.go
  • cmd/fleetctl/fleetctl/gitops_test.go
  • pkg/spec/gitops.go
  • pkg/spec/gitops_test.go

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

Fixes GitOps relative path resolution edge cases where file-path fields defined in unassigned.yml / no-team.yml (and nested org_settings.path) were later re-resolved against a different base directory, causing “no such file or directory” failures.

Changes:

  • Reanchors relative paths inside nested org_settings.path content so they resolve from the nested file’s directory.
  • Adds GitOpsControls.ResolveFilePathsAbs and applies it when extracting unassigned/no-team controls so apply-time path resolution doesn’t double-anchor.
  • Adds unit/integration test coverage for issue #45661 and a user-visible change entry.

Reviewed changes

Copilot reviewed 4 out of 5 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
pkg/spec/gitops.go Reanchors nested org_settings path fields and introduces absolute-path resolution for certain controls fields.
pkg/spec/gitops_test.go Adds focused tests for nested org_settings path reanchoring and no-team controls path behavior.
cmd/fleetctl/fleetctl/gitops.go Resolves no-team/unassigned controls’ apply-time file paths to absolute paths during extraction.
cmd/fleetctl/fleetctl/gitops_test.go Adds end-to-end regression tests reproducing the unassigned/no-team relative path failures.
changes/45661-fix-gitops-relative-paths Documents the user-visible GitOps fix.

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

Comment thread pkg/spec/gitops.go
Comment on lines +705 to +706
c.MacOSSetup.MacOSSetupAssistant.Value = absPathFrom(baseDir, c.MacOSSetup.MacOSSetupAssistant.Value)
c.MacOSSetup.Script.Value = absPathFrom(baseDir, c.MacOSSetup.Script.Value)
Comment thread pkg/spec/gitops_test.go
// ../ from fleets/ climbs out of fleets/, so the result must not contain it.
assert.NotContains(t, got, "fleets", "path should not retain the fleets/ segment: %q", got)
}
assert.True(t, strings.HasSuffix(controls.MacOSSetup.MacOSSetupAssistant.Value, "lib/no-team/macos_enrollment.json"))
Comment thread pkg/spec/gitops_test.go
Comment on lines +1531 to +1533
assert.True(t, filepath.IsAbs(got), "expected absolute path, got %q", got)
assert.True(t, strings.HasSuffix(got, "generated/lib/no-team/macos_enrollment.json"), "got %q", got)
assert.Equal(t, 1, strings.Count(got, "generated/lib"), "the generated/ segment must appear once, not be double-anchored: %q", got)
@@ -0,0 +1 @@
- Fixed GitOps relative path lookup for controls.setup_experience.(apple_setup_assistant, macos_script, software.package_path) in unassigned.yml, and org_logo_paths under org_settings. No newline at end of file
Comment thread pkg/spec/gitops_test.go
Comment on lines +1495 to +1497
assert.True(t, filepath.IsAbs(got), "expected absolute path, got %q", got)
// ../ from fleets/ climbs out of fleets/, so the result must not contain it.
assert.NotContains(t, got, "fleets", "path should not retain the fleets/ segment: %q", got)
@codecov

codecov Bot commented Jun 12, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 77.14286% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 67.19%. Comparing base (0e9b27f) to head (9d78de0).
⚠️ Report is 4 commits behind head on main.

Files with missing lines Patch % Lines
pkg/spec/gitops.go 76.47% 7 Missing and 1 partial ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main   #47512   +/-   ##
=======================================
  Coverage   67.19%   67.19%           
=======================================
  Files        3489     3489           
  Lines      228536   228612   +76     
  Branches    11872    11872           
=======================================
+ Hits       153557   153622   +65     
- Misses      61154    61164   +10     
- Partials    13825    13826    +1     
Flag Coverage Δ
backend 68.83% <77.14%> (+<0.01%) ⬆️

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.

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.

GitOps: Relative path fails when adding custom setup profile to unassigned.yml

2 participants