Skip to content

Commit

Permalink
preflight: add check, fix and cleanup funcs for enabling SMB share on…
Browse files Browse the repository at this point in the history
… windows

earlier these were part of the MSI since we want to enable this only
when user has set 'enable-shared-dirs' setting, this is moved to the
preflight package where we can check this config value before hand

this should remove at least of the warnings reported by virustotal
for the MSI

https://www.virustotal.com/gui/file/31b402dcc1da24265074a21a26018d6cde8eef0b63c77a18f89eb079b6556790
  • Loading branch information
anjannath committed Feb 18, 2025
1 parent 168e685 commit aac3023
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 11 deletions.
63 changes: 63 additions & 0 deletions pkg/crc/preflight/preflight_checks_windows.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package preflight

import (
"errors"
"fmt"
"os/user"
"strconv"
"strings"

Expand Down Expand Up @@ -209,3 +211,64 @@ func checkAdminHelperNamedPipeAccessible() error {
}
return nil
}

func checkFileAndPrinterSharingIsEnabled() error {
cmd := `(Get-NetFirewallRule -Group '@FirewallAPI.dll,-28502' | Where-Object {$_.Profile -eq 'Private, Public'}).Enabled`
stdout, stderr, err := powershell.Execute(cmd)
if err != nil {
return fmt.Errorf("unable to check if Printer and File Sharing is enabled %v: %s", err, stderr)
}
if strings.Contains(stdout, "False") {
return errors.New("Printer and File Sharing is disabled")
}
return nil
}

func fixFileAndPrinterSharing() error {
cmd := `Set-NetFirewallRule -Group '@FirewallAPI.dll,-28502' -Enabled True -Profile 'Private,Public'`
stdout, stderr, err := powershell.ExecuteAsAdmin("to enable Printer and File Sharing", cmd)
if err != nil {
return fmt.Errorf("unable to check if Printer and File Sharing is enabled %v: %s: %s", err, stdout, stderr)
}
return nil
}

func checkCRCSmbShareCreated() error {
cmd := `Get-SmbShare -Name crc-dir0`
stdout, stderr, err := powershell.Execute(cmd)
if err != nil {
return fmt.Errorf("unable to check if Printer and File Sharing is enabled %v: %s: %s", err, stdout, stderr)
}
return nil
}

func fixCRCSmbShareCreated() error {
u, err := user.Current()
if err != nil {
return fmt.Errorf("unable to get user information for homedir and username: %v", err)
}
cmd := fmt.Sprintf(`New-SmbShare -Name 'crc-dir0' -Path '%s' -FullAccess '%s'`, u.HomeDir, username())
_, stderr, err := powershell.ExecuteAsAdmin("create new SMB share for home directory", cmd)
if err != nil {
return fmt.Errorf("unable to get create new SMB share %v: %s", err, stderr)
}
return nil
}

func removeSmbShare() error {
cmd := `Remove-SmbShare -Name 'crc-dir0' -Force`
_, stderr, err := powershell.ExecuteAsAdmin("remove SMB share for home directory", cmd)
if err != nil {
return fmt.Errorf("unable to get create new SMB share %v: %s", err, stderr)
}
return nil
}

func removeFirewallRuleAllowingPrinterAndFileSharing() error {
cmd := `Set-NetFirewallRule -Group '@FirewallAPI.dll,-28502' -Enabled False -Profile 'Private,Public'`
stdout, stderr, err := powershell.ExecuteAsAdmin("to disable Printer and File Sharing", cmd)
if err != nil {
logging.Warnf("unable to turn off Printer and File Sharing %v: %s: %s", err, stdout, stderr)
}
return nil
}
2 changes: 1 addition & 1 deletion pkg/crc/preflight/preflight_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ var daemonLaunchdChecks = []Check{
// Passing 'SystemNetworkingMode' to getPreflightChecks currently achieves this
// as there are no user networking specific checks
func getAllPreflightChecks() []Check {
return getPreflightChecks(true, network.SystemNetworkingMode, constants.GetDefaultBundlePath(preset.OpenShift), preset.OpenShift, false)
return getPreflightChecks(true, network.SystemNetworkingMode, constants.GetDefaultBundlePath(preset.OpenShift), preset.OpenShift, false, false)
}

func getChecks(_ network.Mode, bundlePath string, preset crcpreset.Preset, enableBundleQuayFallback bool) []Check {
Expand Down
8 changes: 4 additions & 4 deletions pkg/crc/preflight/preflight_darwin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ func TestCountConfigurationOptions(t *testing.T) {
}

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

assert.Len(t, getPreflightChecks(true, network.UserNetworkingMode, constants.GetDefaultBundlePath(preset.OpenShift), preset.OpenShift, false), 20)
assert.Len(t, getPreflightChecks(true, network.UserNetworkingMode, constants.GetDefaultBundlePath(preset.OpenShift), preset.OpenShift, false), 20)
assert.Len(t, getPreflightChecks(true, network.UserNetworkingMode, constants.GetDefaultBundlePath(preset.OpenShift), preset.OpenShift, false, false), 20)
assert.Len(t, getPreflightChecks(true, network.UserNetworkingMode, constants.GetDefaultBundlePath(preset.OpenShift), preset.OpenShift, false, false), 20)
}
30 changes: 29 additions & 1 deletion pkg/crc/preflight/preflight_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,32 @@ var userPartOfCrcUsersAndHypervAdminsGroupCheck = Check{
labels: labels{Os: Windows},
}

// Checks to verify and setup SMB share is created and file sharing is enabled
var smbShareCreatedAndFileSharingEnabledChecks = []Check{
{
configKeySuffix: "check-file-sharing-enabled",
checkDescription: "Checking if Printer and File Sharing is enabled",
check: checkFileAndPrinterSharingIsEnabled,
fixDescription: "Enabling Printer and File Sharing",
fix: fixFileAndPrinterSharing,
cleanupDescription: "Disabling Printer and File Sharing",
cleanup: removeFirewallRuleAllowingPrinterAndFileSharing,

labels: labels{Os: Windows, SharedDir: Enabled},
},
{
configKeySuffix: "check-smb-share-exists",
checkDescription: "Checking if SMB share crc-dir0 exists",
check: checkCRCSmbShareCreated,
fixDescription: "Creating SMB share crc-dir0",
fix: fixCRCSmbShareCreated,
cleanupDescription: "Removing SMB share crc-dir0",
cleanup: removeSmbShare,

labels: labels{Os: Windows, SharedDir: Enabled},
},
}

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

func username() string {
Expand Down Expand Up @@ -202,7 +228,7 @@ func checkVsock() error {
// Passing 'UserNetworkingMode' to getPreflightChecks currently achieves this
// as there are no system networking specific checks
func getAllPreflightChecks() []Check {
return getPreflightChecks(true, network.UserNetworkingMode, constants.GetDefaultBundlePath(preset.OpenShift), preset.OpenShift, false)
return getPreflightChecks(true, network.UserNetworkingMode, constants.GetDefaultBundlePath(preset.OpenShift), preset.OpenShift, true, true)
}

func getChecks(bundlePath string, preset crcpreset.Preset, enableBundleQuayFallback bool) []Check {
Expand All @@ -219,12 +245,14 @@ func getChecks(bundlePath string, preset crcpreset.Preset, enableBundleQuayFallb
checks = append(checks, daemonTaskChecks...)
checks = append(checks, adminHelperServiceCheks...)
checks = append(checks, sshPortCheck())
checks = append(checks, smbShareCreatedAndFileSharingEnabledChecks...)
return checks
}

func getPreflightChecks(_ bool, networkMode network.Mode, bundlePath string, preset crcpreset.Preset, enableBundleQuayFallback, sharedDirEnabled bool) []Check {
filter := newFilter()
filter.SetNetworkMode(networkMode)
filter.SetSharedDirStatus(sharedDirEnabled)

return filter.Apply(getChecks(bundlePath, preset, enableBundleQuayFallback))
}
16 changes: 11 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,19 @@ import (
func TestCountConfigurationOptions(t *testing.T) {
cfg := config.New(config.NewEmptyInMemoryStorage(), config.NewEmptyInMemorySecretStorage())
RegisterSettings(cfg)
assert.Len(t, cfg.AllConfigs(), 16)
assert.Len(t, cfg.AllConfigs(), 18)
}

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

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

assert.Len(t, getPreflightChecks(false, network.SystemNetworkingMode, constants.GetDefaultBundlePath(preset.OpenShift), preset.OpenShift, false, true), 25)
assert.Len(t, getPreflightChecks(true, network.SystemNetworkingMode, constants.GetDefaultBundlePath(preset.OpenShift), preset.OpenShift, false, true), 25)

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

0 comments on commit aac3023

Please sign in to comment.