Certificate Generator Dockerfile
This Dockerfile allows you to generate client-server certificates using CFSSL and export them to a local volume.
Prerequisites
- Docker should be installed on your machine.
Usage
-
Clone the repository and navigate to the project directory.
-
Place your configuration files (
ca-csr.json
,ca-config.json
,peer.json
,server.json
,client.json
) in the project directory. -
Build the Docker image:
docker build -t certificate-generator .
- Run a container from the image, mapping the
/certs_volume
directory to a local volume on your host machine:
docker run -v "$(pwd):/certs_volume" certificate-generator
-
After running the container, the generated certificates will be available in the specified local volume.
-
At the end verify the generated pem files
openssl x509 -in ca.pem -text -noout
openssl x509 -in server.pem -text -noout
openssl x509 -in client.pem -text -noout
- Extract public key from a certificate
openssl x509 -in certificate.pem -pubkey -noout > public.pem
FROM cfssl/cfssl AS cfssl
LABEL author="Dipjyoti Metia"
LABEL version="1.0"
WORKDIR /app
COPY *.json /app/
# Generate the certificates
RUN cfssl gencert -initca ca-csr.json | cfssljson -bare ca -
RUN cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=peer peer.json | cfssljson -bare peer
RUN cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=server server.json | cfssljson -bare server
RUN cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=client client.json | cfssljson -bare client
FROM debian:11-slim
COPY --from=cfssl /app/*.pem /app/*.csr /certs/
WORKDIR /certs
CMD ["cp", "-R", "/certs", "/certs_volume"]
{
"signing": {
"default": {
"expiry": "43800h"
},
"profiles": {
"server": {
"expiry": "43800h",
"usages": [
"signing",
"key encipherment",
"server auth"
]
},
"client": {
"expiry": "43800h",
"usages": [
"signing",
"key encipherment",
"client auth"
]
},
"peer": {
"expiry": "43800h",
"usages": [
"signing",
"key encipherment",
"server auth",
"client auth"
]
}
}
}
}
ca-csr
{
"CN": "docker.event",
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "Australia",
"L": "Melbourne",
"O": "Open Source",
"OU": "IT Department",
"ST": "VIC"
}
]
}