2015-10-04 23:02:04 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# some internal functions are copied from salt.states.file
|
|
|
|
|
2016-09-24 21:51:02 +02:00
|
|
|
from cryptography.hazmat.backends import default_backend
|
|
|
|
from cryptography.hazmat.primitives import serialization
|
|
|
|
from cryptography.hazmat.primitives.asymmetric import rsa
|
2015-10-04 23:02:04 +02:00
|
|
|
import os
|
|
|
|
|
|
|
|
|
|
|
|
def _check_user(user, group):
|
2020-03-04 19:33:23 +01:00
|
|
|
"""
|
2015-10-04 23:02:04 +02:00
|
|
|
Checks if the named user and group are present on the minion
|
2020-03-04 19:33:23 +01:00
|
|
|
"""
|
|
|
|
err = ""
|
2015-10-04 23:02:04 +02:00
|
|
|
if user:
|
2020-03-04 19:33:23 +01:00
|
|
|
uid = __salt__["file.user_to_uid"](user)
|
|
|
|
if uid == "":
|
|
|
|
err += "User {0} is not available ".format(user)
|
2015-10-04 23:02:04 +02:00
|
|
|
if group:
|
2020-03-04 19:33:23 +01:00
|
|
|
gid = __salt__["file.group_to_gid"](group)
|
|
|
|
if gid == "":
|
|
|
|
err += "Group {0} is not available".format(group)
|
2015-10-04 23:02:04 +02:00
|
|
|
return err
|
|
|
|
|
|
|
|
|
|
|
|
def _error(ret, err_msg):
|
2020-03-04 19:33:23 +01:00
|
|
|
ret["result"] = False
|
|
|
|
ret["comment"] = err_msg
|
2015-10-04 23:02:04 +02:00
|
|
|
return ret
|
|
|
|
|
|
|
|
|
|
|
|
def _calculate_umask(mode):
|
2020-03-04 19:33:23 +01:00
|
|
|
mode = str(mode).lstrip("0")
|
2015-10-04 23:02:04 +02:00
|
|
|
if not mode:
|
2020-03-04 19:33:23 +01:00
|
|
|
mode = "0"
|
2015-10-04 23:02:04 +02:00
|
|
|
modeint = int(mode, 8)
|
2020-03-04 19:33:23 +01:00
|
|
|
return modeint ^ 0o777
|
2015-10-04 23:02:04 +02:00
|
|
|
|
|
|
|
|
2020-03-04 19:33:23 +01:00
|
|
|
def valid_key(name, bits=2048, user=None, group=None, mode="0700"):
|
2015-10-04 23:02:04 +02:00
|
|
|
"""
|
|
|
|
Make sure that the given key file exists and contains a valid RSA key.
|
|
|
|
|
|
|
|
name
|
|
|
|
The name of the key file to check
|
|
|
|
|
|
|
|
bits
|
|
|
|
Minimum bits for the RSA key
|
|
|
|
|
|
|
|
user
|
|
|
|
The user to own the file, this defaults to the user salt is running as
|
|
|
|
on the minion
|
|
|
|
|
|
|
|
group
|
|
|
|
The group ownership set for the file, this defaults to the group salt
|
|
|
|
is running on the minion
|
|
|
|
|
|
|
|
mode
|
|
|
|
The permissions set on the file, this defaults to 0600
|
|
|
|
"""
|
|
|
|
|
2020-03-04 19:33:23 +01:00
|
|
|
mode = __salt__["config.manage_mode"](mode)
|
2015-10-04 23:02:04 +02:00
|
|
|
|
2020-03-04 19:33:23 +01:00
|
|
|
ret = {"name": name, "changes": {}, "result": None, "comment": ""}
|
|
|
|
if not os.path.isfile(name) and __opts__["test"]:
|
|
|
|
ret["comment"] = "would create RSA key in file {0}".format(name)
|
2015-10-04 23:02:04 +02:00
|
|
|
return ret
|
|
|
|
|
|
|
|
u_check = _check_user(user, group)
|
|
|
|
if u_check:
|
|
|
|
return _error(ret, u_check)
|
|
|
|
if not os.path.isabs(name):
|
2020-03-04 19:33:23 +01:00
|
|
|
return _error(ret, "Specified file {0} is not an absolute path".format(name))
|
2015-10-04 23:02:04 +02:00
|
|
|
if os.path.isdir(name):
|
2020-03-04 19:33:23 +01:00
|
|
|
return _error(ret, "Specified target {0} is a directory".format(name))
|
2015-10-04 23:02:04 +02:00
|
|
|
if os.path.exists(name):
|
2020-03-04 19:33:23 +01:00
|
|
|
ret, perms = __salt__["file.check_perms"](name, ret, user, group, mode)
|
|
|
|
if __opts__["test"]:
|
|
|
|
ret["comment"] = "File {0} not updated".format(name)
|
2015-10-04 23:02:04 +02:00
|
|
|
return ret
|
|
|
|
|
|
|
|
if not os.path.isfile(name):
|
2016-09-24 21:51:02 +02:00
|
|
|
rsakey = rsa.generate_private_key(
|
2020-03-04 19:33:23 +01:00
|
|
|
public_exponent=65537, key_size=bits, backend=default_backend()
|
|
|
|
)
|
2015-10-04 23:02:04 +02:00
|
|
|
oldumask = os.umask(_calculate_umask(mode))
|
2020-03-04 19:33:23 +01:00
|
|
|
with open(name, "wb") as rsafile:
|
|
|
|
rsafile.write(
|
|
|
|
rsakey.private_bytes(
|
|
|
|
encoding=serialization.Encoding.PEM,
|
|
|
|
format=serialization.PrivateFormat.PKCS8,
|
|
|
|
encryption_algorithm=serialization.NoEncryption(),
|
|
|
|
)
|
|
|
|
)
|
2015-10-04 23:02:04 +02:00
|
|
|
os.umask(oldumask)
|
2020-03-04 19:33:23 +01:00
|
|
|
ret["comment"] = "created new RSA key and saved PEM file {0}".format(name)
|
|
|
|
ret["changes"]["created"] = name
|
|
|
|
ret["result"] = True
|
2015-10-04 23:02:04 +02:00
|
|
|
return ret
|
|
|
|
try:
|
2020-03-04 19:33:23 +01:00
|
|
|
with open(name, "rb") as rsafile:
|
2016-09-24 21:51:02 +02:00
|
|
|
rsakey = serialization.load_pem_private_key(
|
2020-03-04 19:33:23 +01:00
|
|
|
rsafile.read(), password=None, backend=default_backend()
|
|
|
|
)
|
2015-10-04 23:02:04 +02:00
|
|
|
except Exception as e:
|
2020-03-04 19:33:23 +01:00
|
|
|
ret["comment"] = "error loading RSA key from file {0}: {1}".format(name, e)
|
|
|
|
ret["result"] = False
|
2015-10-04 23:02:04 +02:00
|
|
|
return ret
|
2016-09-24 21:51:02 +02:00
|
|
|
if rsakey.key_size < bits:
|
2020-03-04 19:33:23 +01:00
|
|
|
ret["comment"] = (
|
|
|
|
"RSA key in {0} is only {1} bits, which is less than the "
|
|
|
|
"required {2} bits".format(name, rsakey.key_size, bits)
|
|
|
|
)
|
|
|
|
ret["result"] = False
|
2015-10-04 23:02:04 +02:00
|
|
|
else:
|
2020-03-04 19:33:23 +01:00
|
|
|
ret["comment"] = "RSA key in file {0} is ok ({1} bits)".format(
|
|
|
|
name, rsakey.key_size
|
|
|
|
)
|
|
|
|
ret["result"] = True
|
2015-10-04 23:02:04 +02:00
|
|
|
return ret
|