blob: c15fdc7a5199013f18559c2bd38b781afcb134ea [file] [log] [blame]
Pawel Kasperkiewicz8f0b6f62020-09-03 10:56:30 +02001CERTS_DIR = resources
2CURRENT_DIR := ${CURDIR}
3DOCKER_CONTAINER = generate-certs
4DOCKER_EXEC = docker exec ${DOCKER_CONTAINER}
5
6all: start_docker \
7 clear_all \
8 root_generate_keys \
9 root_create_certificate \
10 root_self_sign_certificate \
11 client_generate_keys \
12 client_generate_csr \
13 client_sign_certificate_by_root \
14 client_import_root_certificate \
15 client_convert_certificate_to_jks \
16 server_generate_keys \
17 server_generate_csr \
18 server_sign_certificate_by_root \
19 server_import_root_certificate \
20 server_convert_certificate_to_jks \
21 server_convert_certificate_to_p12 \
22 clear_unused_files \
23 stop_docker
24
25.PHONY: all
26
27# Starts docker container for generating certificates - deletes first, if already running
28start_docker:
29 @make stop_docker
Adam Wudzinski11eafc52020-10-28 11:45:20 +010030 $(eval REPOSITORY := $(shell cat ./values.yaml | grep -i "^[ \t]*repository" -m1 | xargs | cut -d ' ' -f2))
31 $(eval JAVA_IMAGE := $(shell cat ./values.yaml | grep -i "^[ \t]*certificateGenerationImage" -m1 | xargs | cut -d ' ' -f2))
32 $(eval FULL_JAVA_IMAGE := $(REPOSITORY)/$(JAVA_IMAGE))
33 $(eval USER :=$(shell id -u))
34 $(eval GROUP :=$(shell id -g))
35 docker run --rm --name ${DOCKER_CONTAINER} --user "$(USER):$(GROUP)" --mount type=bind,source=${CURRENT_DIR}/${CERTS_DIR},target=/app -w /app --entrypoint "sh" -td $(FULL_JAVA_IMAGE)
Pawel Kasperkiewicz8f0b6f62020-09-03 10:56:30 +020036
37# Stops docker container for generating certificates. 'true' is used to return 0 status code, if container is already deleted
38stop_docker:
39 docker rm ${DOCKER_CONTAINER} -f 1>/dev/null || true
40
41#Clear all files related to certificates
42clear_all:
43 @make clear_existing_certificates
44 @make clear_unused_files
45
46#Clear certificates
47clear_existing_certificates:
48 @echo "Clear certificates"
49 ${DOCKER_EXEC} rm -f certServiceClient-keystore.jks certServiceServer-keystore.jks root.crt truststore.jks certServiceServer-keystore.p12
50 @echo "#####done#####"
51
52#Generate root private and public keys
53root_generate_keys:
54 @echo "Generate root private and public keys"
55 ${DOCKER_EXEC} keytool -genkeypair -v -alias root -keyalg RSA -keysize 4096 -validity 3650 -keystore root-keystore.jks \
56 -dname "CN=root.com, OU=Root Org, O=Root Company, L=Wroclaw, ST=Dolny Slask, C=PL" -keypass secret \
57 -storepass secret -ext BasicConstraints:critical="ca:true"
58 @echo "#####done#####"
59
60#Export public key as certificate
61root_create_certificate:
62 @echo "(Export public key as certificate)"
63 ${DOCKER_EXEC} keytool -exportcert -alias root -keystore root-keystore.jks -storepass secret -file root.crt -rfc
64 @echo "#####done#####"
65
66#Self-signed root (import root certificate into truststore)
67root_self_sign_certificate:
68 @echo "(Self-signed root (import root certificate into truststore))"
69 ${DOCKER_EXEC} keytool -importcert -alias root -keystore truststore.jks -file root.crt -storepass secret -noprompt
70 @echo "#####done#####"
71
72#Generate certService's client private and public keys
73client_generate_keys:
74 @echo "Generate certService's client private and public keys"
75 ${DOCKER_EXEC} keytool -genkeypair -v -alias certServiceClient -keyalg RSA -keysize 2048 -validity 365 \
76 -keystore certServiceClient-keystore.jks -storetype JKS \
77 -dname "CN=certServiceClient.com,OU=certServiceClient company,O=certServiceClient org,L=Wroclaw,ST=Dolny Slask,C=PL" \
78 -keypass secret -storepass secret
79 @echo "####done####"
80
81#Generate certificate signing request for certService's client
82client_generate_csr:
83 @echo "Generate certificate signing request for certService's client"
84 ${DOCKER_EXEC} keytool -certreq -keystore certServiceClient-keystore.jks -alias certServiceClient -storepass secret -file certServiceClient.csr
85 @echo "####done####"
86
87#Sign certService's client certificate by root CA
88client_sign_certificate_by_root:
89 @echo "Sign certService's client certificate by root CA"
90 ${DOCKER_EXEC} keytool -gencert -v -keystore root-keystore.jks -storepass secret -alias root -infile certServiceClient.csr \
91 -outfile certServiceClientByRoot.crt -rfc -ext bc=0 -ext ExtendedkeyUsage="serverAuth,clientAuth"
92 @echo "####done####"
93
94#Import root certificate into client
95client_import_root_certificate:
96 @echo "Import root certificate into intermediate"
Adam Wudzinski11eafc52020-10-28 11:45:20 +010097 ${DOCKER_EXEC} sh -c "cat root.crt >> certServiceClientByRoot.crt"
Pawel Kasperkiewicz8f0b6f62020-09-03 10:56:30 +020098 @echo "####done####"
99
100#Import signed certificate into certService's client
101client_convert_certificate_to_jks:
102 @echo "Import signed certificate into certService's client"
103 ${DOCKER_EXEC} keytool -importcert -file certServiceClientByRoot.crt -destkeystore certServiceClient-keystore.jks -alias certServiceClient -storepass secret -noprompt
104 @echo "####done####"
105
106#Generate certService private and public keys
107server_generate_keys:
108 @echo "Generate certService private and public keys"
109 ${DOCKER_EXEC} keytool -genkeypair -v -alias oom-cert-service -keyalg RSA -keysize 2048 -validity 365 \
110 -keystore certServiceServer-keystore.jks -storetype JKS \
111 -dname "CN=oom-cert-service,OU=certServiceServer company,O=certServiceServer org,L=Wroclaw,ST=Dolny Slask,C=PL" \
112 -keypass secret -storepass secret -ext BasicConstraints:critical="ca:false"
113 @echo "####done####"
114
115#Generate certificate signing request for certService
116server_generate_csr:
117 @echo "Generate certificate signing request for certService"
118 ${DOCKER_EXEC} keytool -certreq -keystore certServiceServer-keystore.jks -alias oom-cert-service -storepass secret -file certServiceServer.csr
119 @echo "####done####"
120
121#Sign certService certificate by root CA
122server_sign_certificate_by_root:
123 @echo "Sign certService certificate by root CA"
124 ${DOCKER_EXEC} keytool -gencert -v -keystore root-keystore.jks -storepass secret -alias root -infile certServiceServer.csr \
125 -outfile certServiceServerByRoot.crt -rfc -ext bc=0 -ext ExtendedkeyUsage="serverAuth,clientAuth" \
126 -ext SubjectAlternativeName:="DNS:oom-cert-service,DNS:localhost"
127 @echo "####done####"
128
129#Import root certificate into server
130server_import_root_certificate:
131 @echo "Import root certificate into intermediate(server)"
Adam Wudzinski11eafc52020-10-28 11:45:20 +0100132 ${DOCKER_EXEC} sh -c "cat root.crt >> certServiceServerByRoot.crt"
Pawel Kasperkiewicz8f0b6f62020-09-03 10:56:30 +0200133 @echo "####done####"
134
135#Import signed certificate into certService
136server_convert_certificate_to_jks:
137 @echo "Import signed certificate into certService"
138 ${DOCKER_EXEC} keytool -importcert -file certServiceServerByRoot.crt -destkeystore certServiceServer-keystore.jks -alias oom-cert-service \
139 -storepass secret -noprompt
140 @echo "####done####"
141
142#Convert certServiceServer-keystore(.jks) to PCKS12 format(.p12)
143server_convert_certificate_to_p12:
144 @echo "Convert certServiceServer-keystore(.jks) to PCKS12 format(.p12)"
145 ${DOCKER_EXEC} keytool -importkeystore -srckeystore certServiceServer-keystore.jks -srcstorepass secret \
146 -destkeystore certServiceServer-keystore.p12 -deststoretype PKCS12 -deststorepass secret
147 @echo "#####done#####"
148
149#Clear unused certificates
150clear_unused_files:
151 @echo "Clear unused certificates"
152 ${DOCKER_EXEC} rm -f certServiceClientByRoot.crt certServiceClient.csr root-keystore.jks certServiceServerByRoot.crt certServiceServer.csr
153 @echo "#####done#####"