Skip to content

Commit f10e993

Browse files
Update posture collection and improve server tests
Co-authored-by: Amp <[email protected]> Amp-Thread-ID: https://ampcode.com/threads/T-5be4213f-26eb-400c-bb7b-d4c79b7ee6fe
1 parent 0902bc0 commit f10e993

File tree

3 files changed

+10
-8
lines changed

3 files changed

+10
-8
lines changed

agent/internal/posture/constants.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ package posture
33
const (
44
UnknownValue = "Unknown"
55
UnknownService = "unknown"
6+
emptyString = ""
67
newline = "\n"
78
colonSeparator = ":"
89
spaceSeparator = " "
910
minPartsTriple = 3
11+
keyValueParts = 2
1012
macOSVersionKey = "System Version:"
1113
macOSKernelKey = "Kernel Version:"
1214
macOSFirewallService = "pf"

agent/internal/posture/posture.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func (p *DevicePosture) ToJSON() (string, error) {
7070

7171
// CalculateTrustScore calculates an overall trust score based on posture
7272
func (p *DevicePosture) CalculateTrustScore() {
73-
score := 0
73+
score := DefaultRules
7474

7575
if p.OS.Supported {
7676
score += TrustBonusOS
@@ -140,12 +140,12 @@ func parseKeyValue(text string) map[string]string {
140140

141141
for scanner.Scan() {
142142
line := strings.TrimSpace(scanner.Text())
143-
if line == "" || strings.HasPrefix(line, "#") {
143+
if line == emptyString || strings.HasPrefix(line, "#") {
144144
continue
145145
}
146146

147147
parts := strings.SplitN(line, "=", 2)
148-
if len(parts) == 2 {
148+
if len(parts) == keyValueParts {
149149
key := strings.TrimSpace(parts[0])
150150
value := strings.Trim(strings.TrimSpace(parts[1]), "\"")
151151
result[key] = value
@@ -160,5 +160,5 @@ func parseInt(s string) int {
160160
if val, err := strconv.Atoi(s); err == nil {
161161
return val
162162
}
163-
return 0
163+
return DefaultRules
164164
}

services/authz/server/server_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -483,8 +483,8 @@ func TestServer_lookupDevice(t *testing.T) {
483483

484484
result := server.lookupDevice(context.Background(), "")
485485

486-
if result["posture"] != "unknown" {
487-
t.Errorf("Expected posture 'unknown' for empty device ID, got %v", result["posture"])
486+
if result["posture"] != statusUnknown {
487+
t.Errorf("Expected posture %q for empty device ID, got %v", statusUnknown, result["posture"])
488488
}
489489
})
490490

@@ -501,8 +501,8 @@ func TestServer_lookupDevice(t *testing.T) {
501501

502502
result := server.lookupDevice(ctx, testDeviceID)
503503

504-
if result["posture"] != "unknown" {
505-
t.Errorf("Expected posture 'unknown' for service error, got %v", result["posture"])
504+
if result["posture"] != statusUnknown {
505+
t.Errorf("Expected posture %q for service error, got %v", statusUnknown, result["posture"])
506506
}
507507
})
508508
}

0 commit comments

Comments
 (0)