Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions licensing/advanced_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
1 change: 1 addition & 0 deletions licensing/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ import (
func init() {
token.AirGappedFn = func() bool { return settings.Global().AirGapped }
token.LoadJwksCoseFn = settings.JwksCoseBytes
settings.HonorMachinePolicyFn = AllowsMachinePolicy
}
22 changes: 22 additions & 0 deletions licensing/machine_policy.go
Original file line number Diff line number Diff line change
@@ -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)
}
53 changes: 53 additions & 0 deletions licensing/machine_policy_test.go
Original file line number Diff line number Diff line change
@@ -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()
}
6 changes: 6 additions & 0 deletions settings/firewall_settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 8 additions & 0 deletions settings/security_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,24 +51,32 @@ 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()
prefs.SECURITY_POLICY_ROOTS = oldSecurityRoots
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)
})

Expand Down
129 changes: 120 additions & 9 deletions settings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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(`%([^%]+)%`)

Expand Down Expand Up @@ -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))
}

Expand All @@ -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
}
Expand All @@ -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))
}
}
Expand Down
13 changes: 13 additions & 0 deletions settings/settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
}

Expand Down
Loading