Skip to content

Instantly share code, notes, and snippets.

@hadi2f244
Last active July 26, 2023 08:44
Show Gist options
  • Save hadi2f244/e24c27eb10cd83af68cc925ae85445ff to your computer and use it in GitHub Desktop.
Save hadi2f244/e24c27eb10cd83af68cc925ae85445ff to your computer and use it in GitHub Desktop.
A simple guidance for installing CA with step-ca

Prerequisites:

CA Server

  1. Install step and step-ca commands. Follow official document or simply :
curl -L https://dl.smallstep.com/gh-release/cli/docs-ca-install/v0.23.2/step_linux_0.23.2_amd64.tar.gz \
   -o step.tar.gz
tar -xf step.tar.gz
sudo cp step_0.23.2/bin/step /usr/bin

curl -L https://dl.smallstep.com/gh-release/cli/docs-ca-install/v0.23.2/step_linux_0.23.2_amd64.tar.gz \
   -o step.tar.gz
tar -xf step.tar.gz
sudo cp step_0.23.2/bin/step /usr/bin
  1. Better to do all the task with root user.
  2. Initilize : step ca init 3.1. You can add password 3.2. Add proper DNS rescord (here I used ca.hadiazad.local)
  3. Add the password to this path: /root/.step/.ca-pw
  4. Make sure /root/.step/config/ca.json exists
  5. To support ACME protocol, use this command step ca provisioner add acme --type ACME
  6. Edit the /root/config/ca.json file and in the ACME section add some lifetimes:
	{
		"type": "ACME",
		"name": "domain",
		"forceCN": true,
		"claims": {
			...
		       "maxTLSCertDuration": "2160h",
		       "defaultTLSCertDuration": "2160h"
		       ...
		}
	}
  1. Add this service to /etc/systemd/system/step-ca-server.service:
[Unit]
Description=step-ca-server
After=network-online.target
Wants=network-online.target

[Service]
TimeoutStartSec=0
ExecStart=/usr/bin/step-ca --password-file=/root/.step/.ca-pw /root/.step/config/ca.json

ExecReload=kill -s sighup $(ps aux | grep 'step-ca' | grep json | tr -s ' ' | cut -f 2 -d ' ')

ExecStop=kill -9 $(ps aux | grep 'step-ca' | grep json | tr -s ' ' | cut -f 2 -d ' ')

Restart=always
RestartSec=30

[Install]
WantedBy=multi-user.target

  1. sudo systemctl daemon-reload
  2. sudo systemctl enable --now step-ca-server && sudo systemctl restart step-ca-server

Now CA server should run on port 443.

On client side:

  1. Check You can acces CA server by curl -k https://ca.hadiazad.local/acme/acme/directory 1.1. You could not access without -k option.
  2. Download /root/.step/certs/root_ca.crt on Client.
  3. Copy crt to ca-certificates: sudo cp root_ca.crt /usr/local/share/ca-certificates/
  4. sudo update-ca-certificates
  5. curl https://ca.hadiazad.local/acme/acme/directory without -k

Note: On every client you should do the same thing!

Using Certbot to create new certificate

Use this command to create certificate for test.hadiazad.local domain:

sudo certbot certonly -d test.hadiazad.local --server https://ca.hadiazad.local/acme/acme/directory

Config Nginx

Download these files, Move to /etc/letsencrypt

https://raw.githubusercontent.com/certbot/certbot/ddd4b31b1c0bc397f04a9c96176157ab5ae639ee/certbot/certbot/ssl-dhparams.pem
https://raw.githubusercontent.com/certbot/certbot/master/certbot-nginx/certbot_nginx/_internal/tls_configs/options-ssl-nginx.conf

Nginx config example:

upstream nexus-apt {
	server 127.0.0.1:8081;
}

server {
	server_name nexus.hadiazad.local;
	
    	listen [::]:443 ssl ipv6only=on;	
   	listen 443 ssl;
   	ssl_certificate /etc/letsencrypt/live/nexus.hadiazad.local/fullchain.pem; # managed by Certbot
   	ssl_certificate_key /etc/letsencrypt/live/nexus.hadiazad.local/privkey.pem; # managed by Certbot
	include /etc/letsencrypt/options-ssl-nginx.conf;
  	ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
  	client_max_body_size      1G;

	location / {
		proxy_pass          http://nexus-apt;
	    	proxy_set_header 	X-Forwarded-For $proxy_add_x_forwarded_for;
	    	proxy_set_header 	X-Real-IP $remote_addr;
	}
}

server {
    	if ($host = nexus.hadiazad.local) {
        	return 301 https://$host$request_uri;
    	}

	listen 80 default_server;
	listen [::]:80 default_server;

	server_name nexus.hadiazad.local;
    	return 404;
}

References:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment