-
Notifications
You must be signed in to change notification settings - Fork 123
feat(sandbox): give each workspace an offline and an online principal #812
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
Open
Vasanthdev2004
wants to merge
5
commits into
feat/windows-sandbox-identity
Choose a base branch
from
feat/windows-sandbox-offline-online
base: feat/windows-sandbox-identity
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7002dda
feat(sandbox): give each workspace an offline and an online principal
Vasanthdev2004 0d3e304
fix(sandbox): prove ownership before deleting a sandbox account
Vasanthdev2004 5c9223e
fix(sandbox): assert the filters cover principals, and report a retai…
Vasanthdev2004 26ab091
fix(sandbox): spare adopted principals when dual-role setup rolls back
Vasanthdev2004 c4edd17
test(sandbox): stub the password reset and pin the resolved group SIDs
Vasanthdev2004 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
153 changes: 153 additions & 0 deletions
153
internal/sandbox/windows_dualrole_rollback_windows_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| //go:build windows | ||
|
|
||
| package sandbox | ||
|
|
||
| import ( | ||
| "errors" | ||
| "testing" | ||
|
|
||
| "golang.org/x/sys/windows" | ||
| ) | ||
|
|
||
| // The outer rollback must only delete principals this run created. | ||
| // | ||
| // Dual-role setup provisions offline then online. If the offline role adopts an | ||
| // account that already existed and anything afterwards fails, the outer rollback | ||
| // used to delete it anyway, turning a partial re-setup failure into the loss of | ||
| // a principal that was working before the run started. Two roles make that the | ||
| // common case rather than an unlucky one: the first role usually succeeds, so | ||
| // there is nearly always something for a later failure to destroy. | ||
| func TestDualRoleSetupRollbackSparesAdoptedPrincipals(t *testing.T) { | ||
| for name, testCase := range map[string]struct { | ||
| createdByRole map[windowsSandboxRole]bool | ||
| wantRemoved []windowsSandboxRole | ||
| }{ | ||
| "offline adopted, online created": { | ||
| createdByRole: map[windowsSandboxRole]bool{ | ||
| windowsSandboxRoleOffline: false, | ||
| windowsSandboxRoleOnline: true, | ||
| }, | ||
| // Only the one this run made. Deleting the adopted offline principal | ||
| // is the data loss this guards against. | ||
| wantRemoved: []windowsSandboxRole{windowsSandboxRoleOnline}, | ||
| }, | ||
| "both adopted": { | ||
| createdByRole: map[windowsSandboxRole]bool{ | ||
| windowsSandboxRoleOffline: false, | ||
| windowsSandboxRoleOnline: false, | ||
| }, | ||
| wantRemoved: nil, | ||
| }, | ||
| "both created": { | ||
| createdByRole: map[windowsSandboxRole]bool{ | ||
| windowsSandboxRoleOffline: true, | ||
| windowsSandboxRoleOnline: true, | ||
| }, | ||
| wantRemoved: []windowsSandboxRole{windowsSandboxRoleOffline, windowsSandboxRoleOnline}, | ||
| }, | ||
| } { | ||
| t.Run(name, func(t *testing.T) { | ||
| var removed []windowsSandboxRole | ||
| restoreDualRoleSeams(t, testCase.createdByRole, &removed) | ||
|
|
||
| // Fail on the SECOND role, not the first. ACL application happens per | ||
| // role inside the loop, so failing the first aborts before the second | ||
| // is provisioned at all and the rollback never has more than one | ||
| // principal to consider. The case worth testing is the one review | ||
| // described: the first role succeeds, the second fails, and the | ||
| // question is whether the first gets destroyed on the way out. | ||
| applies := 0 | ||
| applyWindowsACLPlanFn = func(WindowsACLPlan) (func() error, error) { | ||
| applies++ | ||
| if applies < 2 { | ||
| return func() error { return nil }, nil | ||
| } | ||
| return nil, errors.New("ACL apply refused") | ||
| } | ||
|
|
||
| if _, err := setupWindowsSandboxPrincipal(windowsSandboxTestConfig()); err == nil { | ||
| t.Fatal("setup reported success despite an injected ACL failure") | ||
| } | ||
| assertRolesEqual(t, removed, testCase.wantRemoved) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // The same contract on the success path: the rollback handed back to the caller | ||
| // for a LATER setup step to invoke must be just as reluctant. | ||
| func TestDualRoleSetupReturnedRollbackSparesAdoptedPrincipals(t *testing.T) { | ||
| var removed []windowsSandboxRole | ||
| restoreDualRoleSeams(t, map[windowsSandboxRole]bool{ | ||
| windowsSandboxRoleOffline: false, | ||
| windowsSandboxRoleOnline: true, | ||
| }, &removed) | ||
|
|
||
| rollback, err := setupWindowsSandboxPrincipal(windowsSandboxTestConfig()) | ||
| if err != nil { | ||
| t.Fatalf("setup: %v", err) | ||
| } | ||
| if len(removed) != 0 { | ||
| t.Fatalf("setup removed principals before anything failed: %v", removed) | ||
| } | ||
| if err := rollback(); err != nil { | ||
| t.Fatalf("rollback: %v", err) | ||
| } | ||
| assertRolesEqual(t, removed, []windowsSandboxRole{windowsSandboxRoleOnline}) | ||
| } | ||
|
|
||
| func restoreDualRoleSeams(t *testing.T, createdByRole map[windowsSandboxRole]bool, removed *[]windowsSandboxRole) { | ||
| t.Helper() | ||
| prevProvision := provisionWindowsSandboxPrincipalForSetupFn | ||
| prevApply := applyWindowsACLPlanFn | ||
| prevRemove := removeWindowsSandboxPrincipalForSetupFn | ||
| t.Cleanup(func() { | ||
| provisionWindowsSandboxPrincipalForSetupFn = prevProvision | ||
| applyWindowsACLPlanFn = prevApply | ||
| removeWindowsSandboxPrincipalForSetupFn = prevRemove | ||
| }) | ||
|
|
||
| provisionWindowsSandboxPrincipalForSetupFn = func(_ WindowsSandboxCommandConfig, role windowsSandboxRole) (windowsSandboxIdentity, bool, error) { | ||
| sid, err := windows.CreateWellKnownSid(windows.WinLocalSystemSid) | ||
| if err != nil { | ||
| return windowsSandboxIdentity{}, false, err | ||
| } | ||
| return windowsSandboxIdentity{Username: "zero-sbx-test", SID: sid}, createdByRole[role], nil | ||
| } | ||
| applyWindowsACLPlanFn = func(WindowsACLPlan) (func() error, error) { | ||
| return func() error { return nil }, nil | ||
| } | ||
| removeWindowsSandboxPrincipalForSetupFn = func(_ WindowsSandboxCommandConfig, role windowsSandboxRole) error { | ||
| *removed = append(*removed, role) | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| func windowsSandboxTestConfig() WindowsSandboxCommandConfig { | ||
| return WindowsSandboxCommandConfig{ | ||
| SandboxHome: `C:\sandboxhome`, | ||
| CommandCWD: `C:\ws`, | ||
| WorkspaceRoots: []string{`C:\ws`}, | ||
| PermissionProfile: PermissionProfile{ | ||
| FileSystem: FileSystemPolicy{ | ||
| Kind: FileSystemRestricted, | ||
| WriteRoots: []WritableRoot{{Root: `C:\ws`}}, | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func assertRolesEqual(t *testing.T, got []windowsSandboxRole, want []windowsSandboxRole) { | ||
| t.Helper() | ||
| if len(got) != len(want) { | ||
| t.Fatalf("removed %v, want %v", got, want) | ||
| } | ||
| seen := map[windowsSandboxRole]bool{} | ||
| for _, role := range got { | ||
| seen[role] = true | ||
| } | ||
| for _, role := range want { | ||
| if !seen[role] { | ||
| t.Fatalf("removed %v, want %v", got, want) | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.