Skip to content

Commit 313fc0a

Browse files
Extract constants and improve Windows posture collector
- Add constants for Windows-specific strings and patterns - Replace hardcoded OS name with windowsOSName constant - Extract service names, command keywords, and firewall patterns into named constants - Use constants for antivirus detection, disk encryption checks, and password policy - Maintain consistency with Linux collector improvements - Use existing optimized strings.ToLower() pattern Constants added: - windowsOSName, windowsDefenderFirewall - firewallStateOn, antivirusEnabled, protectionOn, fullyEncrypted - minPasswordLength, ruleNamePrefix Co-authored-by: Amp <[email protected]> Amp-Thread-ID: https://ampcode.com/threads/T-5be4213f-26eb-400c-bb7b-d4c79b7ee6fe
1 parent b98650e commit 313fc0a

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

agent/internal/posture/windows.go

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,22 @@ const (
1111
displayNamePrefixLen = len(displayNamePrefix)
1212
powershellCmd = "powershell"
1313
powershellFlag = "-Command"
14+
15+
// OS constants
16+
windowsOSName = "Windows"
17+
18+
// Service names
19+
windowsDefenderFirewall = "Windows Defender Firewall"
20+
21+
// Command keywords and patterns
22+
firewallStateOn = "state on"
23+
antivirusEnabled = "true"
24+
protectionOn = "protection on"
25+
fullyEncrypted = "fully encrypted"
26+
minPasswordLength = "minimum password length"
27+
28+
// Rule prefix for firewall rule counting
29+
ruleNamePrefix = "Rule Name:"
1430
)
1531

1632
// WindowsCollector collects device posture on Windows systems
@@ -20,7 +36,7 @@ type WindowsCollector struct{}
2036
func (c *WindowsCollector) CollectPosture() (*DevicePosture, error) {
2137
posture := &DevicePosture{
2238
OS: OperatingSystem{
23-
Name: "Windows",
39+
Name: windowsOSName,
2440
Arch: runtime.GOARCH,
2541
},
2642
}
@@ -83,7 +99,7 @@ func (c *WindowsCollector) collectOSInfo(os *OperatingSystem) error {
8399

84100
// collectFirewallStatus checks Windows Defender Firewall status
85101
func (_ *WindowsCollector) collectFirewallStatus(fw *FirewallStatus) error {
86-
fw.Service = "Windows Defender Firewall"
102+
fw.Service = windowsDefenderFirewall
87103

88104
// Check firewall state using netsh
89105
output, err := runCommand("netsh", "advfirewall", "show", "allprofiles", "state")
@@ -92,7 +108,7 @@ func (_ *WindowsCollector) collectFirewallStatus(fw *FirewallStatus) error {
92108
}
93109

94110
// Check if any profile is enabled
95-
fw.Enabled = strings.Contains(strings.ToLower(output), "state on")
111+
fw.Enabled = strings.Contains(strings.ToLower(output), firewallStateOn)
96112

97113
// Count rules (simplified)
98114
if fw.Enabled {
@@ -101,7 +117,7 @@ func (_ *WindowsCollector) collectFirewallStatus(fw *FirewallStatus) error {
101117
lines := strings.Split(ruleOutput, newlineSeparator)
102118
ruleCount := initialCapacity
103119
for _, line := range lines {
104-
if strings.HasPrefix(strings.TrimSpace(line), RuleNamePrefix) {
120+
if strings.HasPrefix(strings.TrimSpace(line), ruleNamePrefix) {
105121
ruleCount++
106122
}
107123
}
@@ -116,7 +132,7 @@ func (_ *WindowsCollector) collectFirewallStatus(fw *FirewallStatus) error {
116132
func (_ *WindowsCollector) checkAntiVirus() bool {
117133
// Check Windows Defender status
118134
output, err := runCommand(powershellCmd, powershellFlag, "Get-MpComputerStatus | Select-Object AntivirusEnabled")
119-
if err == nil && strings.Contains(strings.ToLower(output), "true") {
135+
if err == nil && strings.Contains(strings.ToLower(output), antivirusEnabled) {
120136
return true
121137
}
122138

@@ -163,8 +179,8 @@ func (_ *WindowsCollector) checkDiskEncryption() bool {
163179
}
164180

165181
lowerOutput := strings.ToLower(output)
166-
return strings.Contains(lowerOutput, "protection on") ||
167-
strings.Contains(lowerOutput, "fully encrypted")
182+
return strings.Contains(lowerOutput, protectionOn) ||
183+
strings.Contains(lowerOutput, fullyEncrypted)
168184
}
169185

170186
// checkScreenLock checks screen lock/password policy
@@ -182,7 +198,7 @@ func (_ *WindowsCollector) checkScreenLock() bool {
182198
if err == nil {
183199
lines := strings.Split(output, "\n")
184200
for _, line := range lines {
185-
if strings.Contains(strings.ToLower(line), "minimum password length") {
201+
if strings.Contains(strings.ToLower(line), minPasswordLength) {
186202
if strings.Contains(line, "0") {
187203
return false // No password required
188204
}

0 commit comments

Comments
 (0)