def test_issue_56769_unix_line_endings(): """ Test that it handles a gpt.ini file with Unix-style line endings. Should create a gpt.ini with Windows-style line endings. """ data_to_write = b"[\x00d\x00u\x00m\x00m\x00y\x00\\\x00d\x00a\x00t\x00a]\x00" gpt_extension = "gPCMachineExtensionNames" gpt_extension_guid = ( "[{35378EAC-683F-11D2-A89A-00C04FBBCFA2}{D02B1F72-3407-48AE-BA88-E8213C6761F1}]" ) gpt_ini = "\n".join( ["[General]", "gPCMachineExtensionNames=", "Version=8", ""]) expected = "\r\n".join([ "[General]", "gPCMachineExtensionNames=[{35378EAC-683F-11D2-A89A-00C04FBBCFA2}{D02B1F72-3407-48AE-BA88-E8213C6761F1}]", "Version=9", "", ]) with pytest.helpers.temp_file( "Registry.pol") as reg_pol_file, pytest.helpers.temp_file( "gpt.ini") as gpt_ini_file: # We're using salt.utils.file.fopen here because the temp_file helper # doesn't preserve line endings when writing the test file with salt.utils.files.fopen(str(gpt_ini_file), "w") as fp: fp.write(gpt_ini) win_lgpo._write_regpol_data( data_to_write=data_to_write, policy_file_path=str(reg_pol_file), gpt_ini_path=str(gpt_ini_file), gpt_extension=gpt_extension, gpt_extension_guid=gpt_extension_guid, ) # We're using salt.utils.file.fopen here because the temp_file helper # doesn't preserve line endings when reading the test file with salt.utils.files.fopen(str(gpt_ini_file)) as fp: result = fp.read() assert result == expected
def apply_policies(policies, overwrite_regpol=False): r""" Apply a policy that manages Local Group Policy Objects. :param policies: A policy dictionary, or a list of policy dictionaries. Each policy dictionary must be of one of the forms below: { 'policy_type' : 'regpol', 'key' : '<hive>\path\to\registry\key\value_name', 'value' : 'value of the registry key', 'vtype' : 'DWORD' | 'SZ' } -OR- { 'policy_type' : 'secedit', 'name' : 'name of the secedit inf setting', 'value' : 'value to apply to the setting' } Policy dictionaries support the same aliases as the individual policy parameters. See ``ash_lgpo.set_registry_value`` for the aliases. :param overwrite_regpol: When ``False`` (the default), read the registry.pol if it exists and update it with the specified policies. When ``True``, specified policies will wholly overwrite an existing registry.pol file. CLI Examples: .. code-block:: bash policies="[{'policy_type':'regpol', \ 'key':'HKLM\Software\Salt\Policies\Foo', \ 'value':'0', \ 'vtype':'DWORD'}]" salt '*' ash_lgpo.apply_policies policies="${policies}" """ valid_policies, reason, policy = validate_policies(policies) if not valid_policies: raise SaltInvocationError('{0}; policy={1}'.format(reason, policy)) policy_objects = _get_policy_objects(valid_policies, overwrite_regpol=overwrite_regpol) # Apply regpol policies has_regpol = False for regclass, regpol in policy_objects.get('regpol', {}).items(): _write_regpol_data( regpol, POLICY_INFO.admx_registry_classes[regclass]['policy_path'], POLICY_INFO.gpt_ini_path, POLICY_INFO.admx_registry_classes[regclass] ['gpt_extension_location'], POLICY_INFO.admx_registry_classes[regclass]['gpt_extension_guid']) has_regpol = True if regpol else has_regpol # Apply secedit policies __salt__['lgpo.set']( computer_policy=policy_objects.get('secedit', {}), cumulative_rights_assignments=False, ) # Trigger gpupdate to create registry entries from regpol if has_regpol: _ = __salt__['cmd.retcode']('gpupdate') return valid_policies