winutil: refactor methods to get values from registry to also return (#9536)

errors
Updates tailscale/corp#14879

Signed-off-by: Claire Wang <claire@tailscale.com>
This commit is contained in:
Claire Wang
2023-09-26 13:15:11 -04:00
committed by GitHub
parent c608660d12
commit e3d6236606
9 changed files with 80 additions and 58 deletions

View File

@@ -49,7 +49,10 @@ func (p PreferenceOptionPolicy) ShouldEnable(userChoice bool) bool {
// "always" and "never" remove the user's ability to make a selection. If not
// present or set to a different value, "user-decides" is the default.
func GetPreferenceOptionPolicy(name string) PreferenceOptionPolicy {
opt := winutil.GetPolicyString(name, "user-decides")
opt, err := winutil.GetPolicyString(name)
if opt == "" || err != nil {
return showChoiceByPolicy
}
switch opt {
case "always":
return alwaysByPolicy
@@ -81,7 +84,10 @@ func (p VisibilityPolicy) Show() bool {
// true) or "hide" (return true). If not present or set to a different value,
// "show" (return false) is the default.
func GetVisibilityPolicy(name string) VisibilityPolicy {
opt := winutil.GetPolicyString(name, "show")
opt, err := winutil.GetPolicyString(name)
if opt == "" || err != nil {
return visibleByPolicy
}
switch opt {
case "hide":
return hiddenByPolicy
@@ -96,8 +102,8 @@ func GetVisibilityPolicy(name string) VisibilityPolicy {
// understands. If the registry value is "" or can not be processed,
// defaultValue is returned instead.
func GetDurationPolicy(name string, defaultValue time.Duration) time.Duration {
opt := winutil.GetPolicyString(name, "")
if opt == "" {
opt, err := winutil.GetPolicyString(name)
if opt == "" || err != nil {
return defaultValue
}
v, err := time.ParseDuration(opt)

View File

@@ -13,45 +13,49 @@ import (
const RegBase = regBase
// GetPolicyString looks up a registry value in the local machine's path for
// system policies, or returns the given default if it can't.
// system policies, or returns empty string and the error.
// Use this function to read values that may be set by sysadmins via the MSI
// installer or via GPO. For registry settings that you do *not* want to be
// visible to sysadmin tools, use GetRegString instead.
//
// This function will only work on GOOS=windows. Trying to run it on any other
// OS will always return the default value.
func GetPolicyString(name, defval string) string {
return getPolicyString(name, defval)
// OS will always return an empty string and ErrNoValue.
// If value does not exist or another error happens, returns empty string and error.
func GetPolicyString(name string) (string, error) {
return getPolicyString(name)
}
// GetPolicyInteger looks up a registry value in the local machine's path for
// system policies, or returns the given default if it can't.
// system policies, or returns 0 and the associated error.
// Use this function to read values that may be set by sysadmins via the MSI
// installer or via GPO. For registry settings that you do *not* want to be
// visible to sysadmin tools, use GetRegInteger instead.
//
// This function will only work on GOOS=windows. Trying to run it on any other
// OS will always return the default value.
func GetPolicyInteger(name string, defval uint64) uint64 {
return getPolicyInteger(name, defval)
// OS will always return 0 and ErrNoValue.
// If value does not exist or another error happens, returns 0 and error.
func GetPolicyInteger(name string) (uint64, error) {
return getPolicyInteger(name)
}
// GetRegString looks up a registry path in the local machine path, or returns
// the given default if it can't.
// an empty string and error.
//
// This function will only work on GOOS=windows. Trying to run it on any other
// OS will always return the default value.
func GetRegString(name, defval string) string {
return getRegString(name, defval)
// OS will always return an empty string and ErrNoValue.
// If value does not exist or another error happens, returns empty string and error.
func GetRegString(name string) (string, error) {
return getRegString(name)
}
// GetRegInteger looks up a registry path in the local machine path, or returns
// the given default if it can't.
// 0 and the error.
//
// This function will only work on GOOS=windows. Trying to run it on any other
// OS will always return the default value.
func GetRegInteger(name string, defval uint64) uint64 {
return getRegInteger(name, defval)
// OS will always return 0 and ErrNoValue.
// If value does not exist or another error happens, returns 0 and error.
func GetRegInteger(name string) (uint64, error) {
return getRegInteger(name)
}
// IsSIDValidPrincipal determines whether the SID contained in uid represents a

View File

@@ -6,6 +6,7 @@
package winutil
import (
"errors"
"fmt"
"os/user"
"runtime"
@@ -13,13 +14,15 @@ import (
const regBase = ``
func getPolicyString(name, defval string) string { return defval }
var ErrNoValue = errors.New("no value because registry is unavailable on this OS")
func getPolicyInteger(name string, defval uint64) uint64 { return defval }
func getPolicyString(name string) (string, error) { return "", ErrNoValue }
func getRegString(name, defval string) string { return defval }
func getPolicyInteger(name string) (uint64, error) { return 0, ErrNoValue }
func getRegInteger(name string, defval uint64) uint64 { return defval }
func getRegString(name string) (string, error) { return "", ErrNoValue }
func getRegInteger(name string) (uint64, error) { return 0, ErrNoValue }
func isSIDValidPrincipal(uid string) bool { return false }

View File

@@ -29,6 +29,9 @@ const (
// ErrNoShell is returned when the shell process is not found.
var ErrNoShell = errors.New("no Shell process is present")
// ErrNoValue is returned when the value doesn't exist in the registry.
var ErrNoValue = registry.ErrNotExist
// GetDesktopPID searches the PID of the process that's running the
// currently active desktop. Returns ErrNoShell if the shell is not present.
// Usually the PID will be for explorer.exe.
@@ -47,44 +50,44 @@ func GetDesktopPID() (uint32, error) {
return pid, nil
}
func getPolicyString(name, defval string) string {
func getPolicyString(name string) (string, error) {
s, err := getRegStringInternal(regPolicyBase, name)
if err != nil {
// Fall back to the legacy path
return getRegString(name, defval)
return getRegString(name)
}
return s
return s, err
}
func getPolicyInteger(name string, defval uint64) uint64 {
func getRegString(name string) (string, error) {
s, err := getRegStringInternal(regBase, name)
if err != nil {
return "", err
}
return s, err
}
func getPolicyInteger(name string) (uint64, error) {
i, err := getRegIntegerInternal(regPolicyBase, name)
if err != nil {
// Fall back to the legacy path
return getRegInteger(name, defval)
return getRegInteger(name)
}
return i
return i, err
}
func getRegString(name, defval string) string {
s, err := getRegStringInternal(regBase, name)
if err != nil {
return defval
}
return s
}
func getRegInteger(name string, defval uint64) uint64 {
func getRegInteger(name string) (uint64, error) {
i, err := getRegIntegerInternal(regBase, name)
if err != nil {
return defval
return 0, err
}
return i
return i, err
}
func getRegStringInternal(subKey, name string) (string, error) {
key, err := registry.OpenKey(registry.LOCAL_MACHINE, subKey, registry.READ)
if err != nil {
if err != registry.ErrNotExist {
if err != ErrNoValue {
log.Printf("registry.OpenKey(%v): %v", subKey, err)
}
return "", err
@@ -93,7 +96,7 @@ func getRegStringInternal(subKey, name string) (string, error) {
val, _, err := key.GetStringValue(name)
if err != nil {
if err != registry.ErrNotExist {
if err != ErrNoValue {
log.Printf("registry.GetStringValue(%v): %v", name, err)
}
return "", err
@@ -114,7 +117,7 @@ func GetRegStrings(name string, defval []string) []string {
func getRegStringsInternal(subKey, name string) ([]string, error) {
key, err := registry.OpenKey(registry.LOCAL_MACHINE, subKey, registry.READ)
if err != nil {
if err != registry.ErrNotExist {
if err != ErrNoValue {
log.Printf("registry.OpenKey(%v): %v", subKey, err)
}
return nil, err
@@ -123,7 +126,7 @@ func getRegStringsInternal(subKey, name string) ([]string, error) {
val, _, err := key.GetStringsValue(name)
if err != nil {
if err != registry.ErrNotExist {
if err != ErrNoValue {
log.Printf("registry.GetStringValue(%v): %v", name, err)
}
return nil, err
@@ -154,7 +157,7 @@ func DeleteRegValue(name string) error {
func deleteRegValueInternal(subKey, name string) error {
key, err := registry.OpenKey(registry.LOCAL_MACHINE, subKey, registry.SET_VALUE)
if err == registry.ErrNotExist {
if err == ErrNoValue {
return nil
}
if err != nil {
@@ -164,7 +167,7 @@ func deleteRegValueInternal(subKey, name string) error {
defer key.Close()
err = key.DeleteValue(name)
if err == registry.ErrNotExist {
if err == ErrNoValue {
err = nil
}
return err
@@ -173,7 +176,7 @@ func deleteRegValueInternal(subKey, name string) error {
func getRegIntegerInternal(subKey, name string) (uint64, error) {
key, err := registry.OpenKey(registry.LOCAL_MACHINE, subKey, registry.READ)
if err != nil {
if err != registry.ErrNotExist {
if err != ErrNoValue {
log.Printf("registry.OpenKey(%v): %v", subKey, err)
}
return 0, err
@@ -182,7 +185,7 @@ func getRegIntegerInternal(subKey, name string) (uint64, error) {
val, _, err := key.GetIntegerValue(name)
if err != nil {
if err != registry.ErrNotExist {
if err != ErrNoValue {
log.Printf("registry.GetIntegerValue(%v): %v", name, err)
}
return 0, err