forked from OpenVPN/easy-rsa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
373 lines (349 loc) · 13.4 KB
/
Makefile
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# Requires:
# - make (to run this)
# - openssh (for easyrsa and expiry checks)
# - aws cli (to push CRL certificate)
ifndef VERBOSE
.SILENT:
endif
define USAGE
make debug-server
Check the status of the current VPN configuration
make current
Shows the current connection
make available
Lists the available connections to restore
make new-server name=NAME
Initializes a new client VPN certificate
name: A readable name for the endpoint. Will be used as the common name for the certificate. Is saved to pki/vpn_name. [.a-z_-] e.g. vpn.buy.nsw.gov.au
make renew-server
Renews an existing client VPN certificate
make archive
Backs up the current configuration up to a zip file named NAME.DATE.zip and removes its pki directory
make restore name=NAME [date=YYYYMMDD] [proceed=1]
Restores an archive for that VPN to pki. If `proceed=1` is not included, this only lists the archive that would be restored.
make debug-client [name=NAME]
Check the status of one or all client VPN configurations
make new-client name=NAME
Creates a client certificate for connecting to the VPN
name: Typically the name of the developer. [.a-z_-] e.g. blair, blair_mckenzie
make renew-client name=NAME
Renews a client certificate
name: The name of the client certificate
make generate-crl
Regenerates the certificate revocation list certificate
make push-crl profile=AWSPROFILE [proceed=1]
Regenerates the certificate revocation list certificate
make cat-crl
Outputs the current CRL certificate
make purge-client name=NAME [proceed=1]
Removes the files relevant to a client. If `proceed=1` is not included, this only lists the files that would be removed.
name: The name of the client certificate
make purge-server [proceed=1]
Removes the pki directory. If `proceed=1` is not included, this only lists the number of files that would be removed.
make purge-backups [proceed=1]
Removes backups. If `proceed=1` is not included, this only lists the number of files that would be removed.
endef
export USAGE
default:
@echo "$$USAGE"
debug-server:
cd easyrsa3;\
if [ ! -d "pki" ]; then\
echo "./easyrsa3/pki directory: Does not exist. This must contain a current easyrsa configuration.";\
else\
echo "./easyrsa3/pki directory: Exists";\
if [ ! -f "pki/vpn_id" ]; then\
echo "./easyrsa3/pki/vpn_id file: Does not exist. This must contain the Client VPN endpoint ID.";\
else\
VPN_ID=$$(cat pki/vpn_id);\
echo "./easyrsa3/pki/vpn_id file: $$VPN_ID";\
fi;\
if [ ! -f "pki/vpn_region" ]; then\
echo "./easyrsa3/pki/vpn_region file: Does not exist. This must should contain the region of the endpoint, for pushing CRL certficiates.";\
else\
VPN_REGION=$$(cat pki/vpn_region);\
echo "./easyrsa3/pki/vpn_region file: $$VPN_REGION";\
fi;\
if [ ! -f "pki/vpn_name" ]; then\
echo "./easyrsa3/pki/vpn_name file: Does not exist. This must contain the name of the VPN.";\
else\
VPN_NAME=$$(cat pki/vpn_name);\
echo "./easyrsa3/pki/vpn_name file: $$VPN_NAME";\
echo "Server: $$(openssl x509 -enddate -noout -in ./pki/issued/$$VPN_NAME.crt)";\
fi;\
echo "CRL: $$(openssl crl -in .//pki/crl.pem -text | grep 'Next Update')";\
fi
current:
cd easyrsa3;\
if [ ! -f "pki/vpn_name" ]; then\
echo "There is no current server";\
else\
VPN_NAME=$$(cat pki/vpn_name);\
echo "The current server is $$VPN_NAME";\
fi
available:
cd easyrsa3;\
FILES=$$(find . -maxdepth 1 -type f -name '*.zip' | grep -oP '^\./\K.+(?=\.[0-9]{8}\.zip)');\
UNIQUE_CODES=$$(echo "$$FILES" | sort -u);\
for code in $$UNIQUE_CODES; do\
LATEST_FILE=$$(ls $$code.*.zip | sort -r | head -n 1);\
CLIENT_CODE=$${LATEST_FILE%.[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].zip};\
DATE=$${LATEST_FILE%.zip};\
DATE=$${DATE##*.};\
echo "$$CLIENT_CODE:$$DATE ('make restore name=$$CLIENT_CODE date=$$DATE')";\
done;\
if [ -f "pki/vpn_name" ]; then\
echo "There is an existing server open (see 'make current'). You will need to archive it before restoring another (see 'make archive').";\
fi;
new-server:
cd easyrsa3;\
if [ -d "pki" ]; then\
echo "There is an existing ./easyrsa3/pki directory. Run 'make archive' or 'make purge-server' first.";\
elif [ "$(name)" = "" ]; then\
echo "name is not specified";\
else\
./easyrsa init-pki;\
EASYRSA_REQ_CN="$(name)";\
./easyrsa --batch build-ca nopass;\
./easyrsa build-server-full $(name) nopass;\
echo "$(name)" >pki/vpn_name;\
echo "Created certificate for $(name). Find the following files for importing into AWS Certificate Manager:";\
echo "- Certificate body: easyrsa3/pki/issued/$(name).crt";\
echo "- Certificate private key: easyrsa3/pki/private/$(name).key";\
echo "- Certificate chain: easyrsa3/pki/ca.crt";\
echo "Once the endpoint has been set up, download the client config ('downloaded-client-config.ovpn')";\
echo "to the pki directory. You will then be able to create client certificates with 'make new-client name=bob'.";\
fi
renew-server:
cd easyrsa3;\
if [ ! -f "pki/vpn_name" ]; then\
echo "There is no ./easyrsa3/pki directory";\
else\
VPN_NAME=$$(cat pki/vpn_name);\
./easyrsa renew $$VPN_NAME nopass;\
echo "Updated certificate for $$VPN_NAME. Find the following files for importing into AWS Certificate Manager:";\
echo "- Certificate body: easyrsa3/pki/issued/$$VPN_NAME.crt";\
echo "- Certificate private key: easyrsa3/pki/private/$$VPN_NAME.key";\
echo "- Certificate chain: easyrsa3/pki/ca.crt";\
echo "Note that you should not need to recreate the client certficiates, but if you choose to then download the";\
echo "updated client config ('downloaded-client-config.ovpn') to the pki directory first.";\
fi
# TODO: in theory it should be possible to push the new certificates using the CLI:
# aws acm import-certificate --certificate fileb://Certificate.pem \
# --certificate-chain fileb://CertificateChain.pem \
# --private-key fileb://PrivateKey.pem \
# --certificate-arn arn:aws:acm:region:123456789012:certificate/12345678-1234-1234-1234-12345678901
# Need to test this though.
# Would work the same way as push-crl. profile+proceed paramaters; certificate id as a file in pki.
# I guess the initial cert creation could be a command too.
archive:
cd easyrsa3;\
if [ ! -d "pki" ]; then\
echo "There is no ./easyrsa3/pki directory";\
else\
VPN_NAME=$$(cat pki/vpn_name);\
DT=$$(date +"%Y%m%d");\
zip -r $$VPN_NAME.$$DT.zip ./pki;\
echo "Archived to easyrsa3/$$VPN_NAME.$$DT.zip";\
rm -rf pki;\
echo "Removed pki directory";\
fi
restore:
cd easyrsa3;\
if [ -f "pki/vpn_name" ]; then\
echo "There is an existing ./easyrsa3/pki directory";\
elif [ "$(name)" = "" ]; then\
echo "name is not specified";\
elif [ "$(date)" = "" ]; then\
if [ "$$(ls -r $(name).*.zip | head -1)" = "" ]; then\
echo "Backup not found";\
elif [ "$(proceed)" != "1" ]; then\
echo "Found backup: $$(ls -r $(name).*.zip | head -1). Run 'make restore name=$(name) proceed=1' to continue.";\
else\
unzip -uo $$(ls -r $(name).*.zip | head -1);\
fi;\
elif [ "$$(ls -r $(name).$(date).zip | head -1)" = "" ]; then\
echo "Backup not found";\
elif [ "$(proceed)" != "1" ]; then\
echo "Found backup: $$(ls -r $(name).$(date).zip | head -1). Run 'make restore name=$(name) date=$(date) proceed=1' to continue.";\
else\
unzip -uo $$(ls -r $(name).$(date).zip | head -1);\
fi;
debug-client:
cd easyrsa3;\
if [ ! -d "pki" ]; then\
echo "There is no ./easyrsa3/pki directory. This must contain a current easyrsa configuration.";\
elif [ ! -f "pki/vpn_name" ]; then\
echo "The ./easyrsa3/pki/vpn_name file does not exist";\
else\
VPN_NAME=$$(cat pki/vpn_name);\
\
if [ "$(name)" = "" ]; then\
FILES=$$(ls pki/*.$${VPN_NAME}.ovpn | sed "s/\.$${VPN_NAME}\.ovpn$$//" | sed "s/^pki\///");\
echo "Client certificate status:";\
for file in $$FILES; do\
echo "- $$file: $$(openssl x509 -enddate -noout -in ./pki/issued/$${file}.$${VPN_NAME}.crt)";\
done;\
else\
echo "Client certificate: $$(openssl x509 -enddate -noout -in ./pki/issued/$(name).$${VPN_NAME}.crt)";\
fi;\
fi
list-clients:
cd easyrsa3;\
if [ ! -d "pki" ]; then\
echo "There is no ./easyrsa3/pki directory. This must contain a current easyrsa configuration.";\
elif [ ! -f "pki/vpn_name" ]; then\
echo "The ./easyrsa3/pki/vpn_name file does not exist";\
else\
VPN_NAME=$$(cat pki/vpn_name);\
FILES=$$(ls pki/*.$${VPN_NAME}.ovpn | sed "s/\.$${VPN_NAME}\.ovpn$$//" | sed "s/^pki\///");\
echo "Clients:";\
for file in $$FILES; do\
echo "- $$file";\
done;\
fi
new-client:
cd easyrsa3;\
if [ ! -f "pki/vpn_name" ]; then\
echo "There is no ./easyrsa3/pki directory";\
elif [ "$(name)" = "" ]; then\
echo "name is not specified";\
elif [ ! -f "pki/downloaded-client-config.ovpn" = "" ]; then\
echo "downloaded-client-config.ovpn missing from pki directory";\
else\
VPN_NAME=$$(cat pki/vpn_name);\
\
echo "Building new client certificate for $${VPN_NAME}";\
./easyrsa build-client-full $(name).$${VPN_NAME} nopass;\
\
echo "Generating OVPN file";\
OVPN_FILE=pki/$(name).$${VPN_NAME}.ovpn;\
cp pki/downloaded-client-config.ovpn $$OVPN_FILE;\
echo "" >>$$OVPN_FILE;\
echo "<cert>" >>$$OVPN_FILE;\
awk '/-----BEGIN CERTIFICATE-----/,/-----END CERTIFICATE-----/' pki/issued/$(name).$${VPN_NAME}.crt >>$$OVPN_FILE;\
echo "</cert>" >>$$OVPN_FILE;\
echo "<key>" >>$$OVPN_FILE;\
cat pki/private/$(name).$${VPN_NAME}.key >>$$OVPN_FILE;\
echo "</key>" >>$$OVPN_FILE;\
\
echo "Creating client archive";\
DT=$$(date +"%Y%m%d");\
zip -j pki/$(name).$$VPN_NAME.$$DT.zip $$OVPN_FILE;\
fi
renew-client:
cd easyrsa3;\
if [ ! -f "pki/vpn_name" ]; then\
echo "There is no ./easyrsa3/pki directory";\
elif [ "$(name)" = "" ]; then\
echo "name is not specified";\
else\
VPN_NAME=$$(cat pki/vpn_name);\
\
echo "Renewing client certificate for $${VPN_NAME}";\
./easyrsa expire $(name).$${VPN_NAME};\
./easyrsa sign-req client $(name).$${VPN_NAME} nopass;\
\
echo "Generating OVPN file";\
OVPN_FILE=pki/$(name).$${VPN_NAME}.ovpn;\
cp pki/downloaded-client-config.ovpn $$OVPN_FILE;\
echo "" >>$$OVPN_FILE;\
echo "<cert>" >>$$OVPN_FILE;\
awk '/-----BEGIN CERTIFICATE-----/,/-----END CERTIFICATE-----/' pki/issued/$(name).$${VPN_NAME}.crt >>$$OVPN_FILE;\
echo "</cert>" >>$$OVPN_FILE;\
echo "<key>" >>$$OVPN_FILE;\
cat pki/private/$(name).$${VPN_NAME}.key >>$$OVPN_FILE;\
echo "</key>" >>$$OVPN_FILE;\
\
echo "Creating client archive";\
DT=$$(date +"%Y%m%d");\
zip -j pki/$(name).$$VPN_NAME.$$DT.zip $$OVPN_FILE;\
fi
revoke-client:
cd easyrsa3;\
if [ ! -f "pki/vpn_name" ]; then\
echo "There is no ./easyrsa3/pki directory";\
elif [ "$(name)" = "" ]; then\
echo "name is not specified";\
else\
VPN_NAME=$$(cat pki/vpn_name);\
\
./easyrsa revoke $(name).$${VPN_NAME};\
\
echo "Client revoked. See `make generate-crl` and `make push-crl`.";\
fi;
generate-crl:
cd easyrsa3;\
./easyrsa gen-crl;\
echo "Upload the content of the above file as the new client revocation list. See 'make push-crl'.";
push-crl:
cd easyrsa3;\
if [ ! -f "pki/vpn_id" ]; then\
echo "There is no ./easyrsa3/pki/vpn_id file. This must contain the Client VPN endpoint ID";\
elif [ ! -f "pki/vpn_region" ]; then\
echo "There is no ./easyrsa3/pki/vpn_region file. This must contain the region of the endpoint";\
elif [ "$(profile)" = "" ]; then\
echo "profile is not specified";\
else\
VPN_ID=$$(cat pki/vpn_id);\
VPN_REGION=$$(cat pki/vpn_region);\
\
if [ "$(proceed)" = "" ]; then\
echo "CRL would be pushed to $${VPN_ID} in $${VPN_REGION}";\
echo "Run 'make push-crl profile=$(profile) proceed=1' to continue";\
else\
aws --profile "${profile}" ec2 import-client-vpn-client-certificate-revocation-list --certificate-revocation-list file://pki/crl.pem --client-vpn-endpoint-id $${VPN_ID} --region $${VPN_REGION};\
\
echo "CRL is updated. It may take a minute or two for connections to reflect the change.";\
fi;\
fi
cat-crl:
cat easyrsa3/pki/crl.pem
purge-client:
cd easyrsa3;\
if [ ! -f "pki/vpn_name" ]; then\
echo "There is no ./easyrsa3/pki directory";\
elif [ "$(name)" = "" ]; then\
echo "name is not specified";\
elif [ "$(proceed)" != "1" ]; then\
VPN_NAME=$$(cat pki/vpn_name);\
FILES=$$(ls pki/reqs/$(name).$$VPN_NAME.req pki/private/$(name).$$VPN_NAME.key pki/issued/$(name).$$VPN_NAME.crt pki/$(name).$$VPN_NAME.*);\
echo $$FILES;\
if [ -z "$$FILES" ]; then\
echo "No client files found";\
else\
echo "The following files will be removed:";\
for file in $$FILES; do\
echo "- $$file";\
done;\
echo "Run 'make purge-client name=$(name) proceed=1' to continue";\
fi;\
else\
VPN_NAME=$$(cat pki/vpn_name);\
FILES=$$(ls pki/reqs/$(name).$$VPN_NAME.req pki/private/$(name).$$VPN_NAME.key pki/issued/$(name).$$VPN_NAME.crt pki/$(name).$$VPN_NAME.*);\
rm pki/reqs/$(name).$$VPN_NAME.req pki/private/$(name).$$VPN_NAME.key pki/issued/$(name).$$VPN_NAME.crt pki/$(name).$$VPN_NAME.*;\
echo "Removed:";\
for file in $$FILES; do\
echo "- $$file";\
done;\
fi;
purge-server:
if [ ! -d "easyrsa3/pki" ]; then\
echo "There is no ./easyrsa3/pki directory";\
elif [ "$(proceed)" != "1" ]; then\
FILES=$$(ls -R easyrsa3/pki/);\
echo "$${#FILES[@]} files will be removed";\
echo "Run 'make purge-server proceed=1' to continue";\
else\
rm -rf easyrsa3/pki;\
echo "Removed pki directory";\
fi
purge-backups:
if [ "$(proceed)" != "1" ]; then\
FILES=$$(ls easyrsa3/pki/*.zip);\
echo "$${#FILES[@]} files will be removed";\
echo "Run 'make purge-backups proceed=1' to continue";\
else\
rm -rf easyrsa3/*.zip;\
echo "Removed backups";\
fi