Skip to content

Commit 914a7a4

Browse files
committed
Fix macOS posture magic numbers
- Replace version parsing magic numbers with constants - Fix array index calculations with named constants - Use defaultsCommand constant for macOS system calls - Replace -1 with notFoundIndex constant - Use keyValueParts for string splitting
1 parent 0f32532 commit 914a7a4

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

agent/internal/posture/constants.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,8 @@ const (
4343
MinMacOSVersion = 12
4444
MinFirewallRules = 3
4545
initialCapacity = 0
46+
indexIncrement = 1
47+
notFoundIndex = -1
48+
versionPartIndex = 2
49+
defaultsCommand = "defaults"
4650
)

agent/internal/posture/macos.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,14 @@ func (c *MacOSCollector) collectOSInfo(os *OperatingSystem) error {
5454

5555
if strings.HasPrefix(line, macOSVersionKey) {
5656
parts := strings.SplitN(line, colonSeparator, keyValueParts)
57-
if len(parts) == 2 {
57+
if len(parts) == keyValueParts {
5858
versionInfo := strings.TrimSpace(parts[1])
5959
// Parse "macOS Monterey 12.6.1 (21G217)"
6060
if strings.Contains(versionInfo, "(") {
6161
buildStart := strings.LastIndex(versionInfo, "(")
6262
buildEnd := strings.LastIndex(versionInfo, ")")
63-
if buildStart != -1 && buildEnd != -1 {
64-
os.Build = versionInfo[buildStart+1 : buildEnd]
63+
if buildStart != notFoundIndex && buildEnd != notFoundIndex {
64+
os.Build = versionInfo[buildStart+indexIncrement : buildEnd]
6565
versionInfo = strings.TrimSpace(versionInfo[:buildStart])
6666
}
6767
}
@@ -72,12 +72,12 @@ func (c *MacOSCollector) collectOSInfo(os *OperatingSystem) error {
7272
os.Name = strings.Join(parts[:2], spaceSeparator)
7373
os.Version = parts[2]
7474
} else if len(parts) >= 2 {
75-
os.Version = parts[len(parts)-1]
75+
os.Version = parts[len(parts)-indexIncrement]
7676
}
7777
}
7878
} else if strings.HasPrefix(line, macOSKernelKey) {
7979
parts := strings.SplitN(line, colonSeparator, keyValueParts)
80-
if len(parts) == 2 {
80+
if len(parts) == keyValueParts {
8181
os.Kernel = strings.TrimSpace(parts[1])
8282
}
8383
}
@@ -94,7 +94,7 @@ func (c *MacOSCollector) collectFirewallStatus(fw *FirewallStatus) error {
9494
fw.Service = macOSFirewallService
9595

9696
// Check if firewall is enabled
97-
output, err := runCommand("defaults", "read", "/Library/Preferences/com.apple.alf", "globalstate")
97+
output, err := runCommand(defaultsCommand, "read", "/Library/Preferences/com.apple.alf", "globalstate")
9898
if err != nil {
9999
return err
100100
}
@@ -172,7 +172,7 @@ func (c *MacOSCollector) checkDiskEncryption() bool {
172172
// checkScreenLock checks if screen lock/password is required
173173
func (c *MacOSCollector) checkScreenLock() bool {
174174
// Check if password is required after screensaver
175-
output, err := runCommand("defaults", "read", "com.apple.screensaver", macOSScreenPassword)
175+
output, err := runCommand(defaultsCommand, "read", "com.apple.screensaver", macOSScreenPassword)
176176
if err == nil && strings.Contains(output, macOSPasswordEnabled) {
177177
return true
178178
}
@@ -185,7 +185,7 @@ func (c *MacOSCollector) checkScreenLock() bool {
185185
}
186186

187187
// Check System Preferences security settings
188-
output, err = runCommand("defaults", "read", "com.apple.screensaver", macOSScreenDelay)
188+
output, err = runCommand(defaultsCommand, "read", "com.apple.screensaver", macOSScreenDelay)
189189
if err == nil {
190190
return true
191191
}

0 commit comments

Comments
 (0)