Pawel Kasperkiewicz | 8f0b6f6 | 2020-09-03 10:56:30 +0200 | [diff] [blame] | 1 | CERTS_DIR = resources |
| 2 | CURRENT_DIR := ${CURDIR} |
| 3 | DOCKER_CONTAINER = generate-certs |
| 4 | DOCKER_EXEC = docker exec ${DOCKER_CONTAINER} |
| 5 | |
| 6 | all: 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 |
| 28 | start_docker: |
| 29 | @make stop_docker |
Adam Wudzinski | 11eafc5 | 2020-10-28 11:45:20 +0100 | [diff] [blame] | 30 | $(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 Kasperkiewicz | 8f0b6f6 | 2020-09-03 10:56:30 +0200 | [diff] [blame] | 36 | |
| 37 | # Stops docker container for generating certificates. 'true' is used to return 0 status code, if container is already deleted |
| 38 | stop_docker: |
| 39 | docker rm ${DOCKER_CONTAINER} -f 1>/dev/null || true |
| 40 | |
| 41 | #Clear all files related to certificates |
| 42 | clear_all: |
| 43 | @make clear_existing_certificates |
| 44 | @make clear_unused_files |
| 45 | |
| 46 | #Clear certificates |
| 47 | clear_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 |
| 53 | root_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 |
| 61 | root_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) |
| 67 | root_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 |
| 73 | client_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 |
| 82 | client_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 |
| 88 | client_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 |
| 95 | client_import_root_certificate: |
| 96 | @echo "Import root certificate into intermediate" |
Adam Wudzinski | 11eafc5 | 2020-10-28 11:45:20 +0100 | [diff] [blame] | 97 | ${DOCKER_EXEC} sh -c "cat root.crt >> certServiceClientByRoot.crt" |
Pawel Kasperkiewicz | 8f0b6f6 | 2020-09-03 10:56:30 +0200 | [diff] [blame] | 98 | @echo "####done####" |
| 99 | |
| 100 | #Import signed certificate into certService's client |
| 101 | client_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 |
| 107 | server_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 |
| 116 | server_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 |
| 122 | server_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 |
| 130 | server_import_root_certificate: |
| 131 | @echo "Import root certificate into intermediate(server)" |
Adam Wudzinski | 11eafc5 | 2020-10-28 11:45:20 +0100 | [diff] [blame] | 132 | ${DOCKER_EXEC} sh -c "cat root.crt >> certServiceServerByRoot.crt" |
Pawel Kasperkiewicz | 8f0b6f6 | 2020-09-03 10:56:30 +0200 | [diff] [blame] | 133 | @echo "####done####" |
| 134 | |
| 135 | #Import signed certificate into certService |
| 136 | server_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) |
| 143 | server_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 |
| 150 | clear_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#####" |