feat(windows): simplify tray controls and allow installer-user config edits
This commit is contained in:
1 parent
d59b2d5463
commit
ae5e979761
18 files changed
+611
-88
No files matched your search
@@ -11,6 +11,6 @@ func runInteractiveInstaller(io.Writer, io.Writer) error {
|
||||
return errors.New("the v1 interactive installer is implemented for Windows only")
|
||||
}
|
||||
|
||||
func installPackagedDefault(io.Writer) error {
|
||||
func installPackagedDefault(string, io.Writer) error {
|
||||
return errors.New("the v1 packaged installer is implemented for Windows only")
|
||||
}
|
||||
@@ -73,6 +73,10 @@ type shellExecuteInfo struct {
|
||||
}
|
||||
|
||||
func runElevatedInstaller(executable string) error {
|
||||
userSID, err := installerConfigUserSID("")
|
||||
if err != nil {
|
||||
return fmt.Errorf("identify installing user: %w", err)
|
||||
}
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
// Shell extensions may require COM. Balance both S_OK and S_FALSE, while
|
||||
@@ -91,7 +95,7 @@ func runElevatedInstaller(executable string) error {
|
||||
Size: uint32(unsafe.Sizeof(shellExecuteInfo{})),
|
||||
Mask: 0x40 | 0x100, // SEE_MASK_NOCLOSEPROCESS | SEE_MASK_NOASYNC.
|
||||
Verb: installerUTF16("runas"), File: file,
|
||||
Parameters: installerUTF16("--install-default"), Show: windows.SW_SHOWNORMAL,
|
||||
Parameters: installerUTF16("--install-default --config-user-sid " + userSID.String()), Show: windows.SW_SHOWNORMAL,
|
||||
}
|
||||
result, _, callErr := installerShellExecute.Call(uintptr(unsafe.Pointer(&info)))
|
||||
if result == 0 {
|
||||
@@ -141,8 +145,8 @@ func startInstalledTray() error {
|
||||
// installPackagedDefault executes only in the UAC-elevated child. It preserves
|
||||
// a pre-existing operator configuration, atomically updates the executable,
|
||||
// installs/updates the SCM service, and starts it.
|
||||
func installPackagedDefault(_ io.Writer) error {
|
||||
err := installPackagedDefaultFiles()
|
||||
func installPackagedDefault(userSID string, _ io.Writer) error {
|
||||
err := installPackagedDefaultFiles(userSID)
|
||||
if err != nil {
|
||||
showInstallerMessage("RVBox could not be installed.\n\n"+err.Error()+"\n\nNo existing configuration was replaced.", installerErrorIcon)
|
||||
return err
|
||||
@@ -152,10 +156,14 @@ func installPackagedDefault(_ io.Writer) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func installPackagedDefaultFiles() error {
|
||||
func installPackagedDefaultFiles(userSID string) error {
|
||||
if strings.TrimSpace(bootstrapServerURL) == "" {
|
||||
return errors.New("this RVBox package was built without a bootstrap server URL")
|
||||
}
|
||||
configUser, err := installerConfigUserSID(userSID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("validate config user: %w", err)
|
||||
}
|
||||
source, err := os.Executable()
|
||||
if err != nil {
|
||||
return fmt.Errorf("locate installer executable: %w", err)
|
||||
@@ -199,6 +207,11 @@ func installPackagedDefaultFiles() error {
|
||||
} else if err != nil {
|
||||
return fmt.Errorf("inspect existing configuration: %w", err)
|
||||
}
|
||||
// Apply this on upgrades too, without rewriting operator configuration.
|
||||
// Only the TOML gets user read/write; the spool retains its private ACL.
|
||||
if err := protectMachineConfig(configPath, configUser); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := windowsservice.Install(windowsservice.InstallSpec{ExecutablePath: targetExecutable, ConfigPath: configPath, Startup: windowsservice.StartupAutomatic}); err != nil {
|
||||
return fmt.Errorf("install and start RVBox service: %w", err)
|
||||
}
|
||||
@@ -257,11 +270,31 @@ func writeMachineConfig(path string, content []byte) error {
|
||||
_ = os.Remove(temporary)
|
||||
return fmt.Errorf("activate default configuration: %w", err)
|
||||
}
|
||||
return protectMachineConfig(path)
|
||||
return nil
|
||||
}
|
||||
|
||||
func protectMachineConfig(path string) error {
|
||||
descriptor, err := windows.SecurityDescriptorFromString("D:P(A;;FA;;;SY)(A;;FA;;;BA)")
|
||||
func installerConfigUserSID(requested string) (*windows.SID, error) {
|
||||
if requested != "" {
|
||||
return windows.StringToSid(requested)
|
||||
}
|
||||
var token windows.Token
|
||||
if err := windows.OpenProcessToken(windows.CurrentProcess(), windows.TOKEN_QUERY, &token); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer token.Close()
|
||||
user, err := token.GetTokenUser()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Copy away from the token-information buffer before returning.
|
||||
return windows.StringToSid(user.User.Sid.String())
|
||||
}
|
||||
|
||||
func protectMachineConfig(path string, user *windows.SID) error {
|
||||
if user == nil {
|
||||
return errors.New("config user SID is required")
|
||||
}
|
||||
descriptor, err := windows.SecurityDescriptorFromString(fmt.Sprintf("D:P(A;;FA;;;SY)(A;;FA;;;BA)(A;;GRGW;;;%s)", user.String()))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -3,7 +3,11 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"golang.org/x/sys/windows"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
"unsafe"
|
||||
)
|
||||
@@ -17,3 +21,53 @@ func TestInstallerShellExecuteInfoLayout_HP_WINCLI_07(t *testing.T) {
|
||||
t.Fatalf("SHELLEXECUTEINFOW layout mismatch: size=%d instance=%d process=%d", unsafe.Sizeof(info), unsafe.Offsetof(info.Instance), unsafe.Offsetof(info.Process))
|
||||
}
|
||||
}
|
||||
|
||||
func TestInstallerConfigACLAllowsOriginalUser_HP_WINCLI_08(t *testing.T) {
|
||||
user, err := installerConfigUserSID("")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
path := filepath.Join(t.TempDir(), "client.toml")
|
||||
want := []byte("[client]\nclient_id = \"preserved\"\n")
|
||||
if err := os.WriteFile(path, want, 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for i := 0; i < 2; i++ {
|
||||
if err := protectMachineConfig(path, user); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
file, err := os.OpenFile(path, os.O_RDWR, 0)
|
||||
if err != nil {
|
||||
t.Fatalf("original user cannot edit config: %v", err)
|
||||
}
|
||||
_ = file.Close()
|
||||
got, err := os.ReadFile(path)
|
||||
if err != nil || string(got) != string(want) {
|
||||
t.Fatalf("configuration rewritten: %q, %v", got, err)
|
||||
}
|
||||
sd, err := windows.GetNamedSecurityInfo(path, windows.SE_FILE_OBJECT, windows.DACL_SECURITY_INFORMATION)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
acl := sd.String()
|
||||
if !strings.Contains(acl, user.String()) || strings.Contains(acl, ";;;WD)") || strings.Contains(acl, ";;;BU)") || !strings.Contains(acl, "D:P") {
|
||||
t.Fatalf("config ACL widened beyond selected user: %s", acl)
|
||||
}
|
||||
if err := protectMachineConfig(path, nil); err == nil {
|
||||
t.Fatal("nil installer SID accepted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestInstallerConfigUserSIDValidation_BH_WINCLI_02(t *testing.T) {
|
||||
for _, value := range []string{"not-a-sid", "S-1-5-21-1)(A;;FA;;;WD)", "S-1-5-21-1\x00"} {
|
||||
if _, err := installerConfigUserSID(value); err == nil {
|
||||
t.Fatalf("invalid SID accepted: %q", value)
|
||||
}
|
||||
}
|
||||
const other = "S-1-5-21-111-222-333-444"
|
||||
user, err := installerConfigUserSID(other)
|
||||
if err != nil || user.String() != other {
|
||||
t.Fatalf("original user SID lost across elevation: %v, %v", user, err)
|
||||
}
|
||||
}
|
||||
+17
-3
@@ -12,6 +12,7 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
"runtime"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
rvboxv1 "github.com/rvbox/rvbox/gen/go/rvbox/v1"
|
||||
@@ -38,6 +39,10 @@ func main() {
|
||||
|
||||
var nativeTestContextFailures map[clientwindows.ExecutionContext]bool
|
||||
|
||||
// A single daemon process owns this reconciled-session fact. The local tray
|
||||
// reads it without requiring the optional observability HTTP listener.
|
||||
var clientSessionReady atomic.Bool
|
||||
|
||||
// bootstrapServerURL is set only for a packaged Windows installer. A fresh
|
||||
// machine has no service connection from which it could discover this value,
|
||||
// so release packaging must deliberately supply the intended WSS endpoint.
|
||||
@@ -69,6 +74,7 @@ func run(args []string, output, diagnostics io.Writer) error {
|
||||
checkConfig := flags.Bool("check-config", false, "validate client configuration and exit")
|
||||
install := flags.Bool("install-service", false, "install or update the machine-wide service")
|
||||
installDefault := flags.Bool("install-default", false, "install the packaged default configuration (internal installer use)")
|
||||
configUserSID := flags.String("config-user-sid", "", "original interactive installer user SID (internal installer use)")
|
||||
uninstall := flags.Bool("uninstall-service", false, "remove the machine-wide service")
|
||||
configure := flags.Bool("configure-service", false, "configure machine-wide service startup mode")
|
||||
startup := flags.String("startup", string(windowsservice.StartupAutomatic), "service startup mode: automatic or manual")
|
||||
@@ -91,6 +97,9 @@ func run(args []string, output, diagnostics io.Writer) error {
|
||||
if selected != 1 {
|
||||
return errors.New("select exactly one rvbox mode")
|
||||
}
|
||||
if *configUserSID != "" && !*installDefault {
|
||||
return errors.New("--config-user-sid is only valid with --install-default")
|
||||
}
|
||||
if *checkConfig {
|
||||
configured, err := loadClientConfig(*configPath)
|
||||
if err != nil {
|
||||
@@ -107,7 +116,7 @@ func run(args []string, output, diagnostics io.Writer) error {
|
||||
return windowsservice.Install(windowsservice.InstallSpec{ExecutablePath: executable, ConfigPath: *configPath, Startup: windowsservice.StartupAutomatic})
|
||||
}
|
||||
if *installDefault {
|
||||
return installPackagedDefault(diagnostics)
|
||||
return installPackagedDefault(*configUserSID, diagnostics)
|
||||
}
|
||||
if *uninstall {
|
||||
return windowsservice.Uninstall()
|
||||
@@ -162,6 +171,8 @@ func loadClientConfig(path string) (*config.Client, error) {
|
||||
}
|
||||
|
||||
func runClientDaemon(ctx context.Context, configPath string, diagnostics io.Writer) error {
|
||||
clientSessionReady.Store(false)
|
||||
defer clientSessionReady.Store(false)
|
||||
configured, err := loadClientConfig(configPath)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -245,8 +256,11 @@ func runClientDaemon(ctx context.Context, configPath string, diagnostics io.Writ
|
||||
Jitter: agent.CryptoJitter, Now: func() time.Time { return time.Now().UTC() },
|
||||
OnDispatch: executor.Dispatch, OnScriptReady: executor.ScriptReady, OnStdin: executor.Stdin,
|
||||
OnCloseStdin: executor.CloseStdin, OnSignal: executor.Signal, OnTerminate: executor.Terminate, EventReady: eventReady,
|
||||
Metrics: health,
|
||||
OnSessionActive: health.SetReady,
|
||||
Metrics: health,
|
||||
OnSessionActive: func(ready bool) {
|
||||
clientSessionReady.Store(ready)
|
||||
health.SetReady(ready)
|
||||
},
|
||||
OnSessionError: func(sessionErr error) {
|
||||
if diagnostics != nil {
|
||||
_, _ = fmt.Fprintf(diagnostics, "rvbox client session retry: %v\n", sessionErr)
|
||||
|
||||
@@ -123,8 +123,7 @@ func applyServiceDiagnosticsACL(path string) error {
|
||||
}
|
||||
|
||||
func runTray(configPath string, diagnostics io.Writer) error {
|
||||
_ = configPath // the tray obtains the canonical paths from the service.
|
||||
return windowstray.Run(context.Background(), diagnostics)
|
||||
return windowstray.Run(context.Background(), configPath, diagnostics)
|
||||
}
|
||||
|
||||
func runLauncher(channel string, _ io.Writer) error {
|
||||
@@ -139,7 +138,10 @@ func handleTrayRequest(ctx context.Context, configPath string, request windowstr
|
||||
response := windowstray.Frame{Action: windowstray.ActionStatus}
|
||||
switch request.Action {
|
||||
case windowstray.ActionStatus:
|
||||
response.Payload = []byte("RVBox service=running")
|
||||
response.Payload = []byte("disconnected")
|
||||
if clientSessionReady.Load() {
|
||||
response.Payload = []byte("running")
|
||||
}
|
||||
case windowstray.ActionOpenConfig:
|
||||
response.Payload = []byte(configPath)
|
||||
case windowstray.ActionOpenLog:
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
//go:build windows
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/rvbox/rvbox/internal/client/windowstray"
|
||||
)
|
||||
|
||||
func TestTrayConnectionStatusWithoutObservability_HP_WINCLI_09(t *testing.T) {
|
||||
previous := clientSessionReady.Load()
|
||||
defer clientSessionReady.Store(previous)
|
||||
for _, ready := range []bool{false, true, false} {
|
||||
clientSessionReady.Store(ready)
|
||||
response, err := handleTrayRequest(context.Background(), `C:\ProgramData\RVBox\client.toml`, windowstray.Frame{Action: windowstray.ActionStatus})
|
||||
want := "disconnected"
|
||||
if ready {
|
||||
want = "running"
|
||||
}
|
||||
if err != nil || string(response.Payload) != want {
|
||||
t.Fatalf("ready=%t response=%q err=%v", ready, response.Payload, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user