Skip to content

Commit

Permalink
add preflight check and fix to add the logon user to crc-users group
Browse files Browse the repository at this point in the history
this is added back again as we are adding support for a chocolatey package
the problem this solves is that the chocolatey package scripts are ran  as
admin, and from an elevated powershell session we cannot easily capture the
logon username, to do this we need to start a powershell session without
administrator privileges from the eleveted powershell session but this
is not possible to do

it was removed in f98e79c
  • Loading branch information
anjannath authored and openshift-merge-robot committed Jan 18, 2023
1 parent c98dca9 commit 68244ec
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
13 changes: 13 additions & 0 deletions pkg/crc/preflight/preflight_checks_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,19 @@ func fixUserPartOfHyperVAdmins() error {
return errReboot
}

func checkUserPartOfCrcUsers() error {
_, _, err := powershell.Execute(fmt.Sprintf("Get-LocalGroupMember -Group 'crc-users' -Member '%s'", username()))
return err
}

func fixUserPartOfCrcUsers() error {
_, _, err := powershell.ExecuteAsAdmin("adding current user to crc-users group", fmt.Sprintf("Add-LocalGroupMember -Group 'crc-users' -Member '%s'", username()))
if err != nil {
return err
}
return errReboot
}

func checkIfHyperVVirtualSwitchExists() error {
switchName := hyperv.AlternativeNetwork

Expand Down
11 changes: 11 additions & 0 deletions pkg/crc/preflight/preflight_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,16 @@ var adminHelperServiceCheks = []Check{
},
}

var userPartOfCrcUsersGroupCheck = Check{
configKeySuffix: "check-user-in-crc-users-group",
checkDescription: "Checking if current user is in crc-users group",
check: checkUserPartOfCrcUsers,
fixDescription: "Adding logon user to crc-users group",
fix: fixUserPartOfCrcUsers,

labels: labels{Os: Windows},
}

var errReboot = errors.New("Please reboot your system and run 'crc setup' to complete the setup process")

func username() string {
Expand Down Expand Up @@ -189,6 +199,7 @@ func getAllPreflightChecks() []Check {
func getChecks(bundlePath string, preset crcpreset.Preset) []Check {
checks := []Check{}
checks = append(checks, hypervPreflightChecks...)
checks = append(checks, userPartOfCrcUsersGroupCheck)
checks = append(checks, vsockChecks...)
checks = append(checks, bundleCheck(bundlePath, preset))
checks = append(checks, genericCleanupChecks...)
Expand Down
10 changes: 5 additions & 5 deletions pkg/crc/preflight/preflight_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import (
func TestCountConfigurationOptions(t *testing.T) {
cfg := config.New(config.NewEmptyInMemoryStorage(), config.NewEmptyInMemorySecretStorage())
RegisterSettings(cfg)
assert.Len(t, cfg.AllConfigs(), 12)
assert.Len(t, cfg.AllConfigs(), 13)
}

func TestCountPreflights(t *testing.T) {
assert.Len(t, getPreflightChecks(false, network.SystemNetworkingMode, constants.GetDefaultBundlePath(preset.OpenShift), preset.OpenShift), 18)
assert.Len(t, getPreflightChecks(true, network.SystemNetworkingMode, constants.GetDefaultBundlePath(preset.OpenShift), preset.OpenShift), 18)
assert.Len(t, getPreflightChecks(false, network.SystemNetworkingMode, constants.GetDefaultBundlePath(preset.OpenShift), preset.OpenShift), 19)
assert.Len(t, getPreflightChecks(true, network.SystemNetworkingMode, constants.GetDefaultBundlePath(preset.OpenShift), preset.OpenShift), 19)

assert.Len(t, getPreflightChecks(false, network.UserNetworkingMode, constants.GetDefaultBundlePath(preset.OpenShift), preset.OpenShift), 17)
assert.Len(t, getPreflightChecks(true, network.UserNetworkingMode, constants.GetDefaultBundlePath(preset.OpenShift), preset.OpenShift), 17)
assert.Len(t, getPreflightChecks(false, network.UserNetworkingMode, constants.GetDefaultBundlePath(preset.OpenShift), preset.OpenShift), 18)
assert.Len(t, getPreflightChecks(true, network.UserNetworkingMode, constants.GetDefaultBundlePath(preset.OpenShift), preset.OpenShift), 18)
}

0 comments on commit 68244ec

Please sign in to comment.