Skip to content

Commit a77fe1d

Browse files
committed
refactor: extract reusable eval functions for health checks
**Added:** - Introduced `nonEmptyEval` helper to generalize checks for non-empty output - Added `replEval`, `dc01TrustsEval`, `forestTrustEval`, and `dcLocatorEval` functions to encapsulate specific health check evaluations **Changed:** - Refactored health check definitions to use new eval helpers instead of inline anonymous functions, improving readability and maintainability **Removed:** - Removed repetitive anonymous eval functions for domain controller, DNS, trust, and domain membership checks in favor of shared helpers
1 parent 98d560d commit a77fe1d

1 file changed

Lines changed: 63 additions & 92 deletions

File tree

‎cli/cmd/health_check.go‎

Lines changed: 63 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -133,137 +133,69 @@ func buildChecks() []healthCheck {
133133
name: "DC01 AD Domain Controller",
134134
host: "DC01",
135135
command: `(Get-ADDomainController -Filter *).Name -join ','`,
136-
eval: func(stdout string) (bool, string) {
137-
names := strings.TrimSpace(stdout)
138-
if names == "" {
139-
return false, "no domain controllers returned"
140-
}
141-
return true, names
142-
},
136+
eval: nonEmptyEval("no domain controllers returned"),
143137
},
144138
// DC01 - Replication
145139
{
146140
name: "DC01 AD Replication",
147141
host: "DC01",
148142
command: `$r = repadmin /replsummary 2>&1 | Out-String; if ($r -match 'fails/total.*[1-9]\d*/') { Write-Output "REPL_ERRORS:$r" } else { Write-Output "REPL_OK" }`,
149-
eval: func(stdout string) (bool, string) {
150-
if strings.Contains(stdout, "REPL_OK") {
151-
return true, "no replication failures"
152-
}
153-
return false, "replication errors detected"
154-
},
143+
eval: replEval,
155144
},
156145
// DC01 - Trusts
157146
{
158147
name: "DC01 Domain Trusts",
159148
host: "DC01",
160149
command: `Get-ADTrust -Filter * | ForEach-Object { "$($_.Name)|$($_.Direction)|$($_.TrustType)" }`,
161-
eval: func(stdout string) (bool, string) {
162-
lower := strings.ToLower(stdout)
163-
hasNorth := strings.Contains(lower, "north.sevenkingdoms.local")
164-
hasEssos := strings.Contains(lower, "essos.local")
165-
if hasNorth && hasEssos {
166-
return true, "north.sevenkingdoms.local + essos.local"
167-
}
168-
var missing []string
169-
if !hasNorth {
170-
missing = append(missing, "north.sevenkingdoms.local")
171-
}
172-
if !hasEssos {
173-
missing = append(missing, "essos.local")
174-
}
175-
return false, "missing: " + strings.Join(missing, ", ")
176-
},
150+
eval: dc01TrustsEval,
177151
},
178152
// DC02 - AD responding
179153
{
180154
name: "DC02 AD Domain Controller",
181155
host: "DC02",
182156
command: `(Get-ADDomainController -Filter *).Name -join ','`,
183-
eval: func(stdout string) (bool, string) {
184-
names := strings.TrimSpace(stdout)
185-
if names == "" {
186-
return false, "no domain controllers returned"
187-
}
188-
return true, names
189-
},
157+
eval: nonEmptyEval("no domain controllers returned"),
190158
},
191159
// DC02 - DNS cross-domain
192160
{
193161
name: "DC02 DNS (sevenkingdoms.local)",
194162
host: "DC02",
195163
command: `(Resolve-DnsName kingslanding.sevenkingdoms.local -ErrorAction Stop).IPAddress`,
196-
eval: func(stdout string) (bool, string) {
197-
ip := strings.TrimSpace(stdout)
198-
if ip == "" {
199-
return false, "DNS resolution failed"
200-
}
201-
return true, ip
202-
},
164+
eval: nonEmptyEval("DNS resolution failed"),
203165
},
204166
{
205167
name: "DC02 DNS (essos.local)",
206168
host: "DC02",
207169
command: `(Resolve-DnsName meereen.essos.local -ErrorAction Stop).IPAddress`,
208-
eval: func(stdout string) (bool, string) {
209-
ip := strings.TrimSpace(stdout)
210-
if ip == "" {
211-
return false, "DNS resolution failed"
212-
}
213-
return true, ip
214-
},
170+
eval: nonEmptyEval("DNS resolution failed"),
215171
},
216172
// DC03 - AD responding
217173
{
218174
name: "DC03 AD Domain Controller",
219175
host: "DC03",
220176
command: `(Get-ADDomainController -Filter *).Name -join ','`,
221-
eval: func(stdout string) (bool, string) {
222-
names := strings.TrimSpace(stdout)
223-
if names == "" {
224-
return false, "no domain controllers returned"
225-
}
226-
return true, names
227-
},
177+
eval: nonEmptyEval("no domain controllers returned"),
228178
},
229179
// DC03 - Forest trust
230180
{
231181
name: "DC03 Forest Trust",
232182
host: "DC03",
233183
command: `Get-ADTrust -Filter * | ForEach-Object { "$($_.Name)|$($_.ForestTransitive)" }`,
234-
eval: func(stdout string) (bool, string) {
235-
lower := strings.ToLower(stdout)
236-
if strings.Contains(lower, "sevenkingdoms.local") && strings.Contains(lower, "true") {
237-
return true, "sevenkingdoms.local (forest transitive)"
238-
}
239-
return false, "forest trust to sevenkingdoms.local not found"
240-
},
184+
eval: forestTrustEval,
241185
},
242186
// SRV02 - Domain membership
243187
{
244188
name: "SRV02 Domain Membership",
245189
host: "SRV02",
246190
command: `(Get-WmiObject Win32_ComputerSystem).Domain`,
247-
eval: func(stdout string) (bool, string) {
248-
domain := strings.TrimSpace(stdout)
249-
if domain == "" {
250-
return false, "not domain-joined"
251-
}
252-
return true, domain
253-
},
191+
eval: nonEmptyEval("not domain-joined"),
254192
},
255193
// SRV02 - DC reachable
256194
{
257195
name: "SRV02 DC Locator",
258196
host: "SRV02",
259197
command: `$r = nltest /dsgetdc: 2>&1 | Out-String; if ($r -match 'DC: \\\\(\S+)') { Write-Output $Matches[1] } else { Write-Output "FAIL" }`,
260-
eval: func(stdout string) (bool, string) {
261-
val := strings.TrimSpace(stdout)
262-
if val == "FAIL" || val == "" {
263-
return false, "cannot locate domain controller"
264-
}
265-
return true, val
266-
},
198+
eval: dcLocatorEval,
267199
},
268200
// SRV02 - IIS
269201
{
@@ -284,26 +216,14 @@ func buildChecks() []healthCheck {
284216
name: "SRV03 Domain Membership",
285217
host: "SRV03",
286218
command: `(Get-WmiObject Win32_ComputerSystem).Domain`,
287-
eval: func(stdout string) (bool, string) {
288-
domain := strings.TrimSpace(stdout)
289-
if domain == "" {
290-
return false, "not domain-joined"
291-
}
292-
return true, domain
293-
},
219+
eval: nonEmptyEval("not domain-joined"),
294220
},
295221
// SRV03 - DC reachable
296222
{
297223
name: "SRV03 DC Locator",
298224
host: "SRV03",
299225
command: `$r = nltest /dsgetdc: 2>&1 | Out-String; if ($r -match 'DC: \\\\(\S+)') { Write-Output $Matches[1] } else { Write-Output "FAIL" }`,
300-
eval: func(stdout string) (bool, string) {
301-
val := strings.TrimSpace(stdout)
302-
if val == "FAIL" || val == "" {
303-
return false, "cannot locate domain controller"
304-
}
305-
return true, val
306-
},
226+
eval: dcLocatorEval,
307227
},
308228
// SRV03 - IIS
309229
{
@@ -332,3 +252,54 @@ func serviceRunningEval(stdout string) (bool, string) {
332252
}
333253
return false, val
334254
}
255+
256+
// nonEmptyEval returns an eval func that passes when trimmed stdout is non-empty.
257+
func nonEmptyEval(failMsg string) func(string) (bool, string) {
258+
return func(stdout string) (bool, string) {
259+
val := strings.TrimSpace(stdout)
260+
if val == "" {
261+
return false, failMsg
262+
}
263+
return true, val
264+
}
265+
}
266+
267+
func replEval(stdout string) (bool, string) {
268+
if strings.Contains(stdout, "REPL_OK") {
269+
return true, "no replication failures"
270+
}
271+
return false, "replication errors detected"
272+
}
273+
274+
func dc01TrustsEval(stdout string) (bool, string) {
275+
lower := strings.ToLower(stdout)
276+
hasNorth := strings.Contains(lower, "north.sevenkingdoms.local")
277+
hasEssos := strings.Contains(lower, "essos.local")
278+
if hasNorth && hasEssos {
279+
return true, "north.sevenkingdoms.local + essos.local"
280+
}
281+
var missing []string
282+
if !hasNorth {
283+
missing = append(missing, "north.sevenkingdoms.local")
284+
}
285+
if !hasEssos {
286+
missing = append(missing, "essos.local")
287+
}
288+
return false, "missing: " + strings.Join(missing, ", ")
289+
}
290+
291+
func forestTrustEval(stdout string) (bool, string) {
292+
lower := strings.ToLower(stdout)
293+
if strings.Contains(lower, "sevenkingdoms.local") && strings.Contains(lower, "true") {
294+
return true, "sevenkingdoms.local (forest transitive)"
295+
}
296+
return false, "forest trust to sevenkingdoms.local not found"
297+
}
298+
299+
func dcLocatorEval(stdout string) (bool, string) {
300+
val := strings.TrimSpace(stdout)
301+
if val == "FAIL" || val == "" {
302+
return false, "cannot locate domain controller"
303+
}
304+
return true, val
305+
}

0 commit comments

Comments
 (0)