Verify that the named directories are in place and that the environment can shake the salt
(path, dirs, permissive=False, pki_dir="", skip_extra=False)
| 622 | |
| 623 | |
| 624 | def win_verify_env(path, dirs, permissive=False, pki_dir="", skip_extra=False): |
| 625 | """ |
| 626 | Verify that the named directories are in place and that the environment |
| 627 | can shake the salt |
| 628 | """ |
| 629 | import salt.utils.path |
| 630 | import salt.utils.win_dacl |
| 631 | import salt.utils.win_functions |
| 632 | |
| 633 | # Make sure the file_roots is not set to something unsafe since permissions |
| 634 | # on that directory are reset |
| 635 | # `salt.utils.path.safe_path` will consider anything inside `C:\Windows` to |
| 636 | # be unsafe. In some instances the test suite uses |
| 637 | # `C:\Windows\Temp\salt-tests-tmpdir\rootdir` as the file_roots. So, we need |
| 638 | # to consider anything in `C:\Windows\Temp` to be safe |
| 639 | system_root = os.environ.get("SystemRoot", r"C:\Windows") |
| 640 | allow_path = "\\".join([system_root, "TEMP"]) |
| 641 | if not salt.utils.path.safe_path(path=path, allow_path=allow_path): |
| 642 | raise CommandExecutionError( |
| 643 | f"`file_roots` set to a possibly unsafe location: {path}" |
| 644 | ) |
| 645 | |
| 646 | # Create the root path directory if missing |
| 647 | if not os.path.isdir(path): |
| 648 | os.makedirs(path) |
| 649 | |
| 650 | current_user = salt.utils.win_functions.get_current_user() |
| 651 | # Set permissions to the registry key |
| 652 | if salt.utils.win_functions.is_elevated(): |
| 653 | reg_path = "HKLM\\SOFTWARE\\Salt Project\\salt" |
| 654 | if not salt.utils.win_reg.key_exists( |
| 655 | hive="HKLM", key="SOFTWARE\\Salt Project\\salt" |
| 656 | ): |
| 657 | salt.utils.win_reg.set_value( |
| 658 | hive="HKLM", key="SOFTWARE\\Salt Project\\salt" |
| 659 | ) |
| 660 | try: |
| 661 | # Make the Administrators group owner |
| 662 | # Use the SID to be locale agnostic |
| 663 | salt.utils.win_dacl.set_owner( |
| 664 | obj_name=reg_path, principal="S-1-5-32-544", obj_type="registry" |
| 665 | ) |
| 666 | except CommandExecutionError: |
| 667 | log.critical("Unable to securely set the owner of '%s'.", reg_path) |
| 668 | |
| 669 | try: |
| 670 | # Get a clean dacl by not passing an obj_name |
| 671 | dacl = salt.utils.win_dacl.dacl(obj_type="registry") |
| 672 | |
| 673 | # Add aces to the dacl, use the GUID (locale non-specific) |
| 674 | # Administrators Group |
| 675 | dacl.add_ace( |
| 676 | principal="S-1-5-32-544", |
| 677 | access_mode="grant", |
| 678 | permissions="full_control", |
| 679 | applies_to="this_key_subkeys", |
| 680 | ) |
| 681 | # System |
no test coverage detected