diff --git a/licensing/advanced_proxy.go b/licensing/advanced_proxy.go index f73c782..3ab34ef 100644 --- a/licensing/advanced_proxy.go +++ b/licensing/advanced_proxy.go @@ -3,17 +3,16 @@ package license import ( "common/settings" "common/token" - "strings" ) var accessTokenForAdvancedProxy = func() string { - return strings.TrimSpace(settings.Global().AccessToken) + return settings.PeekAccessToken() } // AllowsAdvancedProxy reports whether IWA (NTLM/Negotiate/SSPI) and WinHTTP -// PAC/WPAD proxy features are authorized. Requires a non-expired governance -// entitlement. Distro/Audit (and Community) keep basic proxy URL + basic/bearer auth. +// PAC/WPAD proxy features are authorized. Same governance gate as AllowsMachinePolicy. +// Distro/Audit (and Community) keep basic proxy URL + basic/bearer auth. func AllowsAdvancedProxy() bool { - // No time-insensitive cache: exp can elapse while the same JWT is still configured. + // Keep a dedicated token hook for tests; entitlement matches AllowsMachinePolicy. return hasCommercialEntitlement(accessTokenForAdvancedProxy(), token.EntitlementGovernance) } diff --git a/licensing/hooks.go b/licensing/hooks.go index 2c45348..bc4e527 100644 --- a/licensing/hooks.go +++ b/licensing/hooks.go @@ -8,4 +8,5 @@ import ( func init() { token.AirGappedFn = func() bool { return settings.Global().AirGapped } token.LoadJwksCoseFn = settings.JwksCoseBytes + settings.HonorMachinePolicyFn = AllowsMachinePolicy } diff --git a/licensing/machine_policy.go b/licensing/machine_policy.go new file mode 100644 index 0000000..871305c --- /dev/null +++ b/licensing/machine_policy.go @@ -0,0 +1,22 @@ +package license + +import ( + "common/settings" + "common/token" +) + +var accessTokenForMachinePolicy = func() string { + // Peek — never call settings.Global() here: Load → honorMachinePolicy → this + // would recurse forever. + return settings.PeekAccessToken() +} + +// AllowsMachinePolicy reports whether HKLM/HKCU Policies under +// Software\Policies\Author Software\nvm are honored for preference overrides, +// version/module firewall lists, and forced runtime gates. +// Requires a non-expired governance entitlement (same gate as AllowsAdvancedProxy). +// Licensing secrets (AccessToken / AccessKey / JwksCose) remain readable from +// the Policies hive so the entitlement can be established. +func AllowsMachinePolicy() bool { + return hasCommercialEntitlement(accessTokenForMachinePolicy(), token.EntitlementGovernance) +} diff --git a/licensing/machine_policy_test.go b/licensing/machine_policy_test.go new file mode 100644 index 0000000..56f132b --- /dev/null +++ b/licensing/machine_policy_test.go @@ -0,0 +1,53 @@ +package license + +import ( + "common/token" + "testing" + "time" +) + +func TestAllowsMachinePolicyGovernanceOnly(t *testing.T) { + withMachinePolicyToken(t, mustMintAccessToken(t, "governance", false), func() { + if !AllowsMachinePolicy() { + t.Fatal("governance license should allow machine policy") + } + }) +} + +func TestAllowsMachinePolicyRejectsAuditAndBuild(t *testing.T) { + withMachinePolicyToken(t, mustMintAccessTokenEntitlements(t, false, token.EntitlementAudit, token.EntitlementBuild), func() { + if AllowsMachinePolicy() { + t.Fatal("audit/build must not honor machine policy hive") + } + }) +} + +func TestAllowsMachinePolicyRejectsMissing(t *testing.T) { + withMachinePolicyToken(t, "", func() { + if AllowsMachinePolicy() { + t.Fatal("missing token must not honor machine policy") + } + }) +} + +func TestAllowsMachinePolicyGrace(t *testing.T) { + withMachinePolicyToken(t, mustMintAccessTokenExpiringAt(t, "governance", time.Now().Add(-time.Hour)), func() { + if !AllowsMachinePolicy() { + t.Fatal("governance in grace should allow machine policy") + } + }) + withMachinePolicyToken(t, mustMintAccessTokenExpiringAt(t, "governance", time.Now().Add(-FeatureGracePeriod-time.Hour)), func() { + if AllowsMachinePolicy() { + t.Fatal("governance past grace must not honor machine policy") + } + }) +} + +func withMachinePolicyToken(t *testing.T, raw string, fn func()) { + t.Helper() + withCommercialTrustOK(t) + orig := accessTokenForMachinePolicy + accessTokenForMachinePolicy = func() string { return raw } + t.Cleanup(func() { accessTokenForMachinePolicy = orig }) + fn() +} diff --git a/settings/firewall_settings_test.go b/settings/firewall_settings_test.go index 84745ba..b5ea4fa 100644 --- a/settings/firewall_settings_test.go +++ b/settings/firewall_settings_test.go @@ -15,15 +15,21 @@ func TestEnforcementSource(t *testing.T) { oldSecurity := append([]string(nil), prefs.SECURITY_POLICY_ROOTS...) oldMachine := prefs.MACHINE_PREFERENCE_ROOT oldUser := prefs.USER_PREFERENCE_ROOT + oldMachinePolicy := prefs.MACHINE_POLICY_ROOT + oldHonor := settings.HonorMachinePolicyFn root := "HKCU/Software/NVMTest/enforcement_source" prefs.SECURITY_POLICY_ROOTS = []string{root + "/policy"} + prefs.MACHINE_POLICY_ROOT = root + "/policy" prefs.MACHINE_PREFERENCE_ROOT = root + "/machine" prefs.USER_PREFERENCE_ROOT = root + "/user" + settings.HonorMachinePolicyFn = func() bool { return true } t.Cleanup(func() { _ = exec.Command("reg", "delete", `HKCU\Software\NVMTest\enforcement_source`, "/f").Run() prefs.SECURITY_POLICY_ROOTS = oldSecurity + prefs.MACHINE_POLICY_ROOT = oldMachinePolicy prefs.MACHINE_PREFERENCE_ROOT = oldMachine prefs.USER_PREFERENCE_ROOT = oldUser + settings.HonorMachinePolicyFn = oldHonor }) if err := registry.Put([]string{"https://user.example/trust"}, root+"/user/ApprovedModules"); err != nil { diff --git a/settings/security_policy_test.go b/settings/security_policy_test.go index f11ccc3..648d691 100644 --- a/settings/security_policy_test.go +++ b/settings/security_policy_test.go @@ -51,15 +51,21 @@ func TestLoadAirGappedFromHKLMPolicyOnly(t *testing.T) { oldMachinePolicyRoot := prefs.MACHINE_POLICY_ROOT oldUserPolicyRoot := prefs.USER_POLICY_ROOT oldUserPreferenceRoot := prefs.USER_PREFERENCE_ROOT + oldPolicyRoots := append([]string(nil), prefs.POLICY_ROOTS...) oldRoots := append([]string(nil), prefs.ROOTS...) oldRoot := prefs.ROOT + oldHonor := settings.HonorMachinePolicyFn prefs.MACHINE_POLICY_ROOT = securityPolicyTestRoot + "/airgap_machine_policy" prefs.USER_POLICY_ROOT = securityPolicyTestRoot + "/airgap_user_policy" prefs.USER_PREFERENCE_ROOT = securityPolicyTestRoot + "/airgap_user_preference" prefs.ROOT = prefs.USER_PREFERENCE_ROOT prefs.SECURITY_POLICY_ROOTS = []string{prefs.MACHINE_POLICY_ROOT} + prefs.POLICY_ROOTS = []string{prefs.MACHINE_POLICY_ROOT, prefs.USER_POLICY_ROOT} prefs.ROOTS = []string{prefs.MACHINE_POLICY_ROOT, prefs.USER_POLICY_ROOT, prefs.USER_PREFERENCE_ROOT} + // AirGapped licensing bootstrap always reads MACHINE_POLICY; preference overrides + // from Policies still require Governance. + settings.HonorMachinePolicyFn = func() bool { return true } t.Cleanup(func() { _ = exec.Command("reg", "delete", `HKCU\Software\NVMTest\security_policy`, "/f").Run() @@ -67,8 +73,10 @@ func TestLoadAirGappedFromHKLMPolicyOnly(t *testing.T) { prefs.MACHINE_POLICY_ROOT = oldMachinePolicyRoot prefs.USER_POLICY_ROOT = oldUserPolicyRoot prefs.USER_PREFERENCE_ROOT = oldUserPreferenceRoot + prefs.POLICY_ROOTS = oldPolicyRoots prefs.ROOTS = oldRoots prefs.ROOT = oldRoot + settings.HonorMachinePolicyFn = oldHonor settings.Load(true) }) diff --git a/settings/settings.go b/settings/settings.go index 780eb45..f454493 100644 --- a/settings/settings.go +++ b/settings/settings.go @@ -28,6 +28,99 @@ var loaded bool // When true and ShimBun is absent from the registry, DefaultValue("shim_bun") is true. var CertifiedBuild bool +// HonorMachinePolicyFn is wired by the license package. When false/nil, values under +// Software\Policies\Author Software\nvm are ignored for preference overrides (except +// licensing secrets needed to establish the entitlement). +var HonorMachinePolicyFn func() bool + +func honorMachinePolicy() bool { + if HonorMachinePolicyFn == nil { + return false + } + return HonorMachinePolicyFn() +} + +func normalizeRootPath(root string) string { + return strings.ToUpper(strings.ReplaceAll(strings.TrimSpace(root), `\`, "/")) +} + +func isPoliciesRoot(root string) bool { + normalized := normalizeRootPath(root) + if normalized == "" { + return false + } + if normalizeRootPath(prefs.MACHINE_POLICY_ROOT) == normalized { + return true + } + if normalizeRootPath(prefs.USER_POLICY_ROOT) == normalized { + return true + } + for _, p := range prefs.POLICY_ROOTS { + if normalizeRootPath(p) == normalized { + return true + } + } + return false +} + +// effectiveSettingRoots returns registry roots for preference merge. Policies hives +// are included only when HonorMachinePolicyFn reports a governance entitlement. +func effectiveSettingRoots() []string { + if honorMachinePolicy() { + return append([]string(nil), prefs.ROOTS...) + } + out := make([]string, 0, len(prefs.ROOTS)) + for _, root := range prefs.ROOTS { + if isPoliciesRoot(root) { + continue + } + out = append(out, root) + } + return out +} + +// licensingBootstrapRoots always includes machine Policies + Preferences so +// AccessToken / AccessKey can be read before deciding whether to honor Policies. +func licensingBootstrapRoots() []string { + roots := make([]string, 0, 2) + if root := strings.TrimSpace(prefs.MACHINE_POLICY_ROOT); root != "" { + roots = append(roots, root) + } + if root := strings.TrimSpace(prefs.MACHINE_PREFERENCE_ROOT); root != "" { + roots = append(roots, root) + } + return roots +} + +func seedLicensingFromMachineHives() { + for _, root := range licensingBootstrapRoots() { + values, err := registry.GetAll(root) + if err != nil || len(values) == 0 { + continue + } + if raw, ok := values["AccessToken"]; ok && strings.TrimSpace(globalSettings.AccessToken) == "" { + if normalized, ok := normalizeRegistryStringValue(raw); ok { + globalSettings.AccessToken = normalized + } + } + if raw, ok := values["AccessKey"]; ok && strings.TrimSpace(globalSettings.AccessKey) == "" { + if normalized, ok := normalizeRegistryStringValue(raw); ok { + globalSettings.AccessKey = normalized + } + } + if raw, ok := values["AirGapped"]; ok { + switch v := raw.(type) { + case bool: + globalSettings.AirGapped = v + case uint32: + globalSettings.AirGapped = v != 0 + case int: + globalSettings.AirGapped = v != 0 + } + } + } +} + func List() []string { t := reflect.TypeOf(Settings{}) options := make([]string, 0, t.NumField()) @@ -94,14 +187,22 @@ func Load(reload ...bool) { } globalSettings = Settings{} + // Licensing secrets may live only under Policies; seed them before the + // governance gate decides whether the rest of that hive is honored. + seedLicensingFromMachineHives() - values, err := registry.GetAll(prefs.ROOTS[0]) + roots := effectiveSettingRoots() + if len(roots) == 0 { + return + } + + values, err := registry.GetAll(roots[0]) if err != nil { return } - if len(prefs.ROOTS) > 1 { - for _, root := range prefs.ROOTS[1:] { + if len(roots) > 1 { + for _, root := range roots[1:] { data, err := registry.GetAll(root) if err != nil { continue @@ -224,7 +325,7 @@ func isSecurityPolicySetting(name string) bool { } func securityPolicyRegKeys(name string) []string { - if len(prefs.SECURITY_POLICY_ROOTS) == 0 || !isSecurityPolicySetting(name) { + if !honorMachinePolicy() || len(prefs.SECURITY_POLICY_ROOTS) == 0 || !isSecurityPolicySetting(name) { return nil } @@ -306,7 +407,7 @@ func joinedRegKey(root, name string) string { } func applySecurityPolicyOverrides() { - if len(prefs.SECURITY_POLICY_ROOTS) == 0 { + if !honorMachinePolicy() || len(prefs.SECURITY_POLICY_ROOTS) == 0 { return } @@ -480,6 +581,12 @@ func Global() Settings { return globalSettings } +// PeekAccessToken returns the in-memory AccessToken without triggering Load. +// Used by license gates during Load (seedLicensing runs before preference merge). +func PeekAccessToken() string { + return strings.TrimSpace(globalSettings.AccessToken) +} + // expandEnvVars expands Windows-style %VAR% environment variable references in path. var winEnvVar = regexp.MustCompile(`%([^%]+)%`) @@ -724,8 +831,9 @@ func lookupRegKeys(name string) []string { return keys } - keys := make([]string, 0, len(prefs.ROOTS)) - for _, root := range prefs.ROOTS { + roots := effectiveSettingRoots() + keys := make([]string, 0, len(roots)) + for _, root := range roots { keys = append(keys, regkey(name, root)) } @@ -737,6 +845,10 @@ func lookupRegKeys(name string) []string { } func policyRegKeys(name string) []string { + if !honorMachinePolicy() { + return nil + } + if keys := securityPolicyRegKeys(name); len(keys) > 0 { return keys } @@ -748,8 +860,7 @@ func policyRegKeys(name string) []string { keys := make([]string, 0, len(prefs.ROOTS)) for _, root := range prefs.ROOTS { - normalizedRoot := strings.ToUpper(strings.ReplaceAll(strings.TrimSpace(root), `\`, "/")) - if strings.Contains(normalizedRoot, "/POLICIES/") { + if isPoliciesRoot(root) { keys = append(keys, regkey(name, root)) } } diff --git a/settings/settings_test.go b/settings/settings_test.go index 3c46519..9910bbb 100644 --- a/settings/settings_test.go +++ b/settings/settings_test.go @@ -56,12 +56,25 @@ func withPolicyRoot(t *testing.T) { oldRoot := prefs.ROOT oldRoots := append([]string(nil), prefs.ROOTS...) + oldMachinePolicy := prefs.MACHINE_POLICY_ROOT + oldUserPolicy := prefs.USER_POLICY_ROOT + oldPolicyRoots := append([]string(nil), prefs.POLICY_ROOTS...) + oldHonor := settings.HonorMachinePolicyFn + prefs.ROOT = testRegistryRoot + prefs.MACHINE_POLICY_ROOT = testPolicyRegistryRoot + prefs.USER_POLICY_ROOT = testPolicyRegistryRoot + "/user" + prefs.POLICY_ROOTS = []string{prefs.MACHINE_POLICY_ROOT, prefs.USER_POLICY_ROOT} prefs.ROOTS = []string{testPolicyRegistryRoot, prefs.ROOT} + settings.HonorMachinePolicyFn = func() bool { return true } t.Cleanup(func() { prefs.ROOT = oldRoot prefs.ROOTS = oldRoots + prefs.MACHINE_POLICY_ROOT = oldMachinePolicy + prefs.USER_POLICY_ROOT = oldUserPolicy + prefs.POLICY_ROOTS = oldPolicyRoots + settings.HonorMachinePolicyFn = oldHonor }) }