forked from jgraichen/salt-pki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pki.py
186 lines (141 loc) · 5.36 KB
/
pki.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# -*- coding: utf-8 -*-
# pylint: disable=missing-module-docstring
import logging
import os
try:
from salt.utils.files import backup_minion as _backup_minion
except ImportError:
from salt.utils import backup_minion as _backup_minion
from salt.exceptions import SaltInvocationError
def __virtual__():
"""
Depend on corresponding execution module
"""
if "pki.create_private_key" not in __salt__:
return False, "Execution module unavailable"
return True
def private_key(
name, new=False, type="ec", size=4096, curve="secp256r1", backup=True
): # pylint: disable=R0913
"""
Manage a private key.
name:
Path to private key
new:
Always create a new key. Default to ``False``.
Combining new with :mod:`prereq <salt.states.requsities.preqreq>` can
allow key rotation whenever a new certificate is generated.
type:
Key type to generate. Can be 'ec' (default) or 'rsa'.
size:
Length of private RSA key in bits. Defaults to ``4096``.
curve:
Curve name to use for EC keys. Defaults to ``secp256r1``.
backup:
When replacing an existing file, backup the old file on the minion.
Default is ``True``.
"""
# pylint: disable=redefined-builtin
ret = {"name": name, "changes": {}, "result": False, "comment": ""}
if type == "ec":
target = {"type": "ec", "curve": curve}
elif type == "rsa":
target = {"type": "rsa", "size": size}
else:
raise SaltInvocationError(f"Invalid key type: {type}")
if os.path.isfile(name):
try:
current = __salt__["pki.read_private_key"](name)
except SaltInvocationError as e:
current = f"The private key is not valid: {e}"
else:
current = "The private key does not exist"
if not new and current == target:
ret["result"] = True
ret["comment"] = "The private key is already in the correct state"
return ret
ret["changes"] = {"old": current, "new": target}
if __opts__["test"] is True:
ret["result"] = None
ret["comment"] = "A new private key would be generated"
return ret
if os.path.isfile(name) and backup:
bkroot = os.path.join(__opts__["cachedir"], "file_backup")
_backup_minion(name, bkroot)
__salt__["pki.create_private_key"](path=name, type=type, size=size, curve=curve)
ret["result"] = True
ret["comment"] = "New private key generated"
return ret
def certificate(
name, csr=None, days_remaining=28, backup=True, **kwargs
): # pylint: disable=R0912
"""
Manage a x509 certificate.
name:
Path to store the certificate.
path:
Path to store the certificate is ``name`` is not a file path.
csr:
Path to CSR. If no CSR is given all additional arguments will be passed
to ``pki.create_csr`` to create a CSR on demand.
See ``pki.create_csr`` for available arguments.
days_remaining:
The minimum number of days remaining when the certificate should be
renewed. Defaults to 28 days.
backup:
When replacing an existing certificate, backup the old file on the
minion. Default is ``True``.
"""
ret = {"name": name, "changes": {}, "result": False, "comment": ""}
renewal_needed = False
cert_changed = False
if "path" in kwargs:
name = kwargs.pop("path")
if os.path.isfile(name):
try:
current = __salt__["pki.read_certificate"](name)
except SaltInvocationError as e:
current = f"Certificate is not valid: {e}"
else:
current = "Certificate does not exist"
if not csr:
if "key" not in kwargs:
raise SaltInvocationError("CSR or private key required")
if not os.path.exists(kwargs["key"]) and __opts__["test"]:
new = "Private key does not yet exist, cannot preview changes"
else:
csr = __salt__["pki.create_csr"](text=True, **kwargs)
new = __salt__["pki.read_csr"](csr)
else:
new = __salt__["pki.read_csr"](csr)
if os.path.exists(name):
renewal_needed = __salt__["pki.renewal_needed"](
name, days_remaining=days_remaining
)
if isinstance(current, dict) and isinstance(new, dict):
for key in ("subject", "extensions", "public_key"):
if current[key] != new[key]:
logging.debug("[%s] Certificate %s has changed", name, key)
cert_changed = True
else:
cert_changed = True
if not renewal_needed and not cert_changed:
ret["result"] = True
ret["comment"] = "Certificate is already in correct state"
return ret
ret["changes"] = {"old": current, "new": new}
if __opts__["test"]:
ret["result"] = None
if cert_changed:
ret["comment"] = "The certificate has changed and will be updated"
if renewal_needed:
ret["comment"] = "The certificate expires soon and will be updated"
return ret
if os.path.isfile(name) and backup:
bkroot = os.path.join(__opts__["cachedir"], "file_backup")
_backup_minion(name, bkroot)
result = __salt__["pki.create_certificate"](path=name, csr=csr, **kwargs)
ret["changes"] = {"old": current, "new": result}
ret["comment"] = "The certificate has been updated"
ret["result"] = True
return ret