|
2 | 2 | Unit tests for saltext.nebula.modules.nebula |
3 | 3 | """ |
4 | 4 |
|
| 5 | +import os |
5 | 6 | from datetime import datetime |
6 | 7 | from datetime import timedelta |
7 | 8 | from unittest.mock import MagicMock |
@@ -413,6 +414,117 @@ def test_static_host_map(self): |
413 | 414 | assert "172.25.0.1" in config["static_host_map"] |
414 | 415 | assert config["static_host_map"]["172.25.0.1"] == ["1.2.3.4:4242"] |
415 | 416 |
|
| 417 | + def test_lighthouse_serve_dns(self): |
| 418 | + """Lighthouse with serve_dns emits serve_dns and dns block inside lighthouse config.""" |
| 419 | + nebula_mod.__pillar__["nebula"]["hosts"]["testhost"]["is_lighthouse"] = True |
| 420 | + nebula_mod.__pillar__["nebula"]["hosts"]["testhost"]["serve_dns"] = True |
| 421 | + nebula_mod.__pillar__["nebula"]["hosts"]["testhost"]["dns"] = { |
| 422 | + "host": "172.25.0.2", |
| 423 | + "port": 5353, |
| 424 | + } |
| 425 | + with patch.object( |
| 426 | + nebula_mod, |
| 427 | + "detect_paths", |
| 428 | + return_value={ |
| 429 | + "ca_file": "/etc/nebula/ca.crt", |
| 430 | + "cert_file": "/etc/nebula/testhost.crt", |
| 431 | + "key_file": "/etc/nebula/testhost.key", |
| 432 | + "config_dir": "/etc/nebula", |
| 433 | + }, |
| 434 | + ): |
| 435 | + config = nebula_mod.build_config() |
| 436 | + |
| 437 | + assert config["lighthouse"]["serve_dns"] is True |
| 438 | + assert config["lighthouse"]["dns"]["host"] == "172.25.0.2" |
| 439 | + assert config["lighthouse"]["dns"]["port"] == 5353 |
| 440 | + |
| 441 | + def test_serve_dns_omitted_on_non_lighthouse(self): |
| 442 | + """serve_dns is not emitted for regular nodes even if set in pillar.""" |
| 443 | + nebula_mod.__pillar__["nebula"]["hosts"]["testhost"]["serve_dns"] = True |
| 444 | + with patch.object( |
| 445 | + nebula_mod, |
| 446 | + "detect_paths", |
| 447 | + return_value={ |
| 448 | + "ca_file": "/etc/nebula/ca.crt", |
| 449 | + "cert_file": "/etc/nebula/testhost.crt", |
| 450 | + "key_file": "/etc/nebula/testhost.key", |
| 451 | + "config_dir": "/etc/nebula", |
| 452 | + }, |
| 453 | + ): |
| 454 | + config = nebula_mod.build_config() |
| 455 | + |
| 456 | + assert "serve_dns" not in config["lighthouse"] |
| 457 | + assert "dns" not in config["lighthouse"] |
| 458 | + |
| 459 | + def test_sshd_enabled(self): |
| 460 | + """sshd block is built correctly from pillar.""" |
| 461 | + nebula_mod.__pillar__["nebula"]["hosts"]["testhost"]["sshd"] = { |
| 462 | + "enabled": True, |
| 463 | + "listen": "127.0.0.1:20022", |
| 464 | + "host_key": "/etc/nebula/testhost_host", |
| 465 | + "authorized_users": [{"user": "alice", "keys": ["ssh-ed25519 AAAA..."]}], |
| 466 | + } |
| 467 | + with patch.object( |
| 468 | + nebula_mod, |
| 469 | + "detect_paths", |
| 470 | + return_value={ |
| 471 | + "ca_file": "/etc/nebula/ca.crt", |
| 472 | + "cert_file": "/etc/nebula/testhost.crt", |
| 473 | + "key_file": "/etc/nebula/testhost.key", |
| 474 | + "config_dir": "/etc/nebula", |
| 475 | + }, |
| 476 | + ): |
| 477 | + config = nebula_mod.build_config() |
| 478 | + |
| 479 | + assert "sshd" in config |
| 480 | + assert config["sshd"]["enabled"] is True |
| 481 | + assert config["sshd"]["listen"] == "127.0.0.1:20022" |
| 482 | + assert config["sshd"]["host_key"] == "/etc/nebula/testhost_host" |
| 483 | + assert config["sshd"]["authorized_users"][0]["user"] == "alice" |
| 484 | + |
| 485 | + @pytest.mark.parametrize( |
| 486 | + "config_dir", |
| 487 | + [ |
| 488 | + "/etc/nebula", |
| 489 | + "C:\\ProgramData\\Nebula", |
| 490 | + ], |
| 491 | + ) |
| 492 | + def test_sshd_default_host_key(self, config_dir): |
| 493 | + """sshd host_key defaults to <config_dir>/ssh_host_ed25519_key on both Unix and Windows.""" |
| 494 | + nebula_mod.__pillar__["nebula"]["hosts"]["testhost"]["sshd"] = { |
| 495 | + "enabled": True, |
| 496 | + "listen": "127.0.0.1:22", |
| 497 | + } |
| 498 | + with patch.object( |
| 499 | + nebula_mod, |
| 500 | + "detect_paths", |
| 501 | + return_value={ |
| 502 | + "ca_file": f"{config_dir}/ca.crt", |
| 503 | + "cert_file": f"{config_dir}/testhost.crt", |
| 504 | + "key_file": f"{config_dir}/testhost.key", |
| 505 | + "config_dir": config_dir, |
| 506 | + }, |
| 507 | + ): |
| 508 | + config = nebula_mod.build_config() |
| 509 | + |
| 510 | + assert config["sshd"]["host_key"] == os.path.join(config_dir, "ssh_host_ed25519_key") |
| 511 | + |
| 512 | + def test_sshd_absent_when_not_configured(self): |
| 513 | + """sshd key is not emitted when not in pillar.""" |
| 514 | + with patch.object( |
| 515 | + nebula_mod, |
| 516 | + "detect_paths", |
| 517 | + return_value={ |
| 518 | + "ca_file": "/etc/nebula/ca.crt", |
| 519 | + "cert_file": "/etc/nebula/testhost.crt", |
| 520 | + "key_file": "/etc/nebula/testhost.key", |
| 521 | + "config_dir": "/etc/nebula", |
| 522 | + }, |
| 523 | + ): |
| 524 | + config = nebula_mod.build_config() |
| 525 | + |
| 526 | + assert "sshd" not in config |
| 527 | + |
416 | 528 |
|
417 | 529 | # --------------------------------------------------------------------------- |
418 | 530 | # backup_config / rollback_config |
|
0 commit comments