|
1 | | -import { generateXCCDF } from '@shell/utils/xccdf'; |
| 1 | +import { generateXCCDF, generateXCCDFPerNode } from '@shell/utils/xccdf'; |
2 | 2 |
|
3 | 3 | describe('xccdf util: generateXCCDF', () => { |
4 | 4 | const baseReport = { |
@@ -193,3 +193,199 @@ describe('xccdf util: generateXCCDF', () => { |
193 | 193 | expect(xml).toContain('id="xccdf_compliance-operator_rule_5.1.1"'); |
194 | 194 | }); |
195 | 195 | }); |
| 196 | + |
| 197 | +describe('xccdf util: generateXCCDFPerNode', () => { |
| 198 | + const multiNodeReport = { |
| 199 | + version: '1.0', |
| 200 | + total: 3, |
| 201 | + pass: 1, |
| 202 | + nodes: { master: ['m-1'], node: ['w-1', 'w-2'] }, |
| 203 | + results: [{ |
| 204 | + id: '1.1', |
| 205 | + description: 'Master Node Configuration', |
| 206 | + checks: [{ |
| 207 | + id: '1.1.1', |
| 208 | + description: 'master check', |
| 209 | + scored: true, |
| 210 | + state: 'pass' as const, |
| 211 | + }], |
| 212 | + }, { |
| 213 | + id: '4.1', |
| 214 | + description: 'Worker Node Configuration', |
| 215 | + checks: [{ |
| 216 | + id: '4.1.1', |
| 217 | + description: 'mixed check', |
| 218 | + scored: true, |
| 219 | + state: 'mixed' as const, |
| 220 | + nodes: ['w-2'], |
| 221 | + }, { |
| 222 | + id: '4.1.2', |
| 223 | + description: 'failing check', |
| 224 | + scored: false, |
| 225 | + state: 'fail' as const, |
| 226 | + }], |
| 227 | + }], |
| 228 | + }; |
| 229 | + |
| 230 | + it('emits a single <target> equal to the hostname', () => { |
| 231 | + const xml = generateXCCDFPerNode({ |
| 232 | + report: multiNodeReport, benchmarkVersion: 'cis-1.7', hostname: 'w-1', role: 'node', |
| 233 | + }); |
| 234 | + |
| 235 | + expect(xml).toContain('<target>w-1</target>'); |
| 236 | + expect(xml).not.toContain('<target>w-2</target>'); |
| 237 | + expect(xml).not.toContain('<target>m-1</target>'); |
| 238 | + }); |
| 239 | + |
| 240 | + it('assigns each per-node document a TestResult id suffixed with the hostname so co-loaded files do not collide', () => { |
| 241 | + const a = generateXCCDFPerNode({ |
| 242 | + report: multiNodeReport, benchmarkVersion: 'cis-1.7', hostname: 'w-1', role: 'node', |
| 243 | + }); |
| 244 | + const b = generateXCCDFPerNode({ |
| 245 | + report: multiNodeReport, benchmarkVersion: 'cis-1.7', hostname: 'w-2', role: 'node', |
| 246 | + }); |
| 247 | + |
| 248 | + expect(a).toContain('<TestResult id="xccdf_compliance-operator_testresult_1_w-1"'); |
| 249 | + expect(b).toContain('<TestResult id="xccdf_compliance-operator_testresult_1_w-2"'); |
| 250 | + expect(a).not.toContain('id="xccdf_compliance-operator_testresult_1_w-2"'); |
| 251 | + }); |
| 252 | + |
| 253 | + it('emits every rule from the cluster report regardless of role', () => { |
| 254 | + const workerXml = generateXCCDFPerNode({ |
| 255 | + report: multiNodeReport, benchmarkVersion: 'cis-1.7', hostname: 'w-1', role: 'node', |
| 256 | + }); |
| 257 | + const masterXml = generateXCCDFPerNode({ |
| 258 | + report: multiNodeReport, benchmarkVersion: 'cis-1.7', hostname: 'm-1', role: 'master', |
| 259 | + }); |
| 260 | + |
| 261 | + [workerXml, masterXml].forEach((xml) => { |
| 262 | + expect(xml).toContain('xccdf_compliance-operator_rule_1.1.1'); |
| 263 | + expect(xml).toContain('xccdf_compliance-operator_rule_4.1.1'); |
| 264 | + expect(xml).toContain('xccdf_compliance-operator_rule_4.1.2'); |
| 265 | + }); |
| 266 | + }); |
| 267 | + |
| 268 | + it('maps mixed-state checks to fail for dissenting hosts and pass for the rest', () => { |
| 269 | + const dissenter = generateXCCDFPerNode({ |
| 270 | + report: multiNodeReport, benchmarkVersion: 'cis-1.7', hostname: 'w-2', role: 'node', |
| 271 | + }); |
| 272 | + const compliant = generateXCCDFPerNode({ |
| 273 | + report: multiNodeReport, benchmarkVersion: 'cis-1.7', hostname: 'w-1', role: 'node', |
| 274 | + }); |
| 275 | + |
| 276 | + expect(dissenter).toMatch(/idref="xccdf_compliance-operator_rule_4\.1\.1"[\s\S]*?<result>fail<\/result>/); |
| 277 | + expect(compliant).toMatch(/idref="xccdf_compliance-operator_rule_4\.1\.1"[\s\S]*?<result>pass<\/result>/); |
| 278 | + }); |
| 279 | + |
| 280 | + it('treats mixed-state checks with no dissent list as pass for all nodes', () => { |
| 281 | + const report = { |
| 282 | + ...multiNodeReport, |
| 283 | + results: [{ |
| 284 | + id: '4.1', |
| 285 | + description: 'g', |
| 286 | + checks: [{ |
| 287 | + id: '4.1.9', description: 'm', state: 'mixed' as const, |
| 288 | + }], |
| 289 | + }], |
| 290 | + }; |
| 291 | + const xml = generateXCCDFPerNode({ |
| 292 | + report, benchmarkVersion: 'cis-1.7', hostname: 'w-1', role: 'node', |
| 293 | + }); |
| 294 | + |
| 295 | + expect(xml).toMatch(/idref="xccdf_compliance-operator_rule_4\.1\.9"[\s\S]*?<result>pass<\/result>/); |
| 296 | + }); |
| 297 | + |
| 298 | + it('recomputes pass count per node while preserving cluster total as the scoring denominator', () => { |
| 299 | + const report = { |
| 300 | + version: '1.0', |
| 301 | + total: 2, |
| 302 | + pass: 1, |
| 303 | + nodes: { node: ['w-1', 'w-2'] }, |
| 304 | + results: [{ |
| 305 | + id: '1', |
| 306 | + description: 'g', |
| 307 | + checks: [ |
| 308 | + { |
| 309 | + id: 'a', description: 'a', state: 'pass' as const |
| 310 | + }, |
| 311 | + { |
| 312 | + id: 'b', description: 'b', state: 'mixed' as const, nodes: ['w-2'], |
| 313 | + }, |
| 314 | + ], |
| 315 | + }], |
| 316 | + }; |
| 317 | + const compliant = generateXCCDFPerNode({ |
| 318 | + report, benchmarkVersion: 'cis-1.7', hostname: 'w-1', role: 'node', |
| 319 | + }); |
| 320 | + const dissenter = generateXCCDFPerNode({ |
| 321 | + report, benchmarkVersion: 'cis-1.7', hostname: 'w-2', role: 'node', |
| 322 | + }); |
| 323 | + |
| 324 | + expect(compliant).toMatch(/<score[^>]*>100\.0<\/score>/); |
| 325 | + expect(dissenter).toMatch(/<score[^>]*>50\.0<\/score>/); |
| 326 | + }); |
| 327 | + |
| 328 | + it('preserves full rule metadata (title, fixtext, idents, check) from the cluster report', () => { |
| 329 | + const report = { |
| 330 | + version: '1.0', |
| 331 | + total: 1, |
| 332 | + pass: 1, |
| 333 | + nodes: { node: ['w-1'] }, |
| 334 | + results: [{ |
| 335 | + id: 'V-254554', |
| 336 | + description: 'controller manager group', |
| 337 | + checks: [{ |
| 338 | + id: 'V-254554', |
| 339 | + description: 'use-service-account-credentials', |
| 340 | + audit: '/bin/ps -fC kube-controller-manager', |
| 341 | + remediation: 'set use-service-account-credentials=true', |
| 342 | + scored: true, |
| 343 | + state: 'pass' as const, |
| 344 | + }], |
| 345 | + }], |
| 346 | + }; |
| 347 | + const xml = generateXCCDFPerNode({ |
| 348 | + report, benchmarkVersion: 'rke2-stig-1.31-rgs', hostname: 'w-1', role: 'node', |
| 349 | + }); |
| 350 | + |
| 351 | + expect(xml).toContain('<Group id="V-254554">'); |
| 352 | + expect(xml).toContain('<check-content>/bin/ps -fC kube-controller-manager</check-content>'); |
| 353 | + expect(xml).toContain('set use-service-account-credentials=true'); |
| 354 | + }); |
| 355 | + |
| 356 | + it('passes through non-mixed states unchanged', () => { |
| 357 | + const report = { |
| 358 | + ...multiNodeReport, |
| 359 | + results: [{ |
| 360 | + id: '4.1', |
| 361 | + description: 'g', |
| 362 | + checks: [ |
| 363 | + { |
| 364 | + id: 'a', description: 'a', state: 'pass' as const |
| 365 | + }, |
| 366 | + { |
| 367 | + id: 'b', description: 'b', state: 'fail' as const |
| 368 | + }, |
| 369 | + { |
| 370 | + id: 'c', description: 'c', state: 'skip' as const |
| 371 | + }, |
| 372 | + { |
| 373 | + id: 'd', description: 'd', state: 'warn' as const |
| 374 | + }, |
| 375 | + { |
| 376 | + id: 'e', description: 'e', state: 'notApplicable' as const |
| 377 | + }, |
| 378 | + ], |
| 379 | + }], |
| 380 | + }; |
| 381 | + const xml = generateXCCDFPerNode({ |
| 382 | + report, benchmarkVersion: 'cis-1.7', hostname: 'w-1', role: 'node', |
| 383 | + }); |
| 384 | + |
| 385 | + expect(xml).toContain('<result>pass</result>'); |
| 386 | + expect(xml).toContain('<result>fail</result>'); |
| 387 | + expect(xml).toContain('<result>notselected</result>'); |
| 388 | + expect(xml).toContain('<result>informational</result>'); |
| 389 | + expect(xml).toContain('<result>notapplicable</result>'); |
| 390 | + }); |
| 391 | +}); |
0 commit comments