1import re
2
3import salt.utils.files
4import salt.utils.stringutils
5from salt.exceptions import CommandExecutionError
6
7
8def key_is_encrypted(key):
9    # NOTE: this is a temporary workaround until we can get salt/modules/ssh.py
10    # working on Windows.
11    try:
12        with salt.utils.files.fopen(key, "r") as fp_:
13            key_data = salt.utils.stringutils.to_unicode(fp_.read())
14    except OSError as exc:
15        # Raise a CommandExecutionError
16        salt.utils.files.process_read_exception(exc, key)
17
18    is_private_key = re.search(r"BEGIN (?:\w+\s)*PRIVATE KEY", key_data)
19    is_encrypted = "ENCRYPTED" in key_data
20    del key_data
21
22    if not is_private_key:
23        raise CommandExecutionError("{} is not a private key".format(key))
24
25    return is_encrypted
26