refactor configure_https.md

This commit is contained in:
xiahaoshawn 2016-03-29 11:59:52 +08:00
parent 45929455e4
commit 40aff95c0d

View File

@ -1,35 +1,56 @@
#Configure Harbor with HTTPS Access
Because Harbor does not ship with any certificates, it uses HTTP by default to serve registry requests. This makes it relative simple to configure. However, it is highly recommended that security be enabled for any production environment. Harbor has an Nginx instance as a reverse proxy for all services, you can configure Nginx to enable https.
Because Harbor does not ship with any certificates, it uses HTTP by default to serve registry requests. This makes it relatively simple to configure. However, it is highly recommended that security be enabled for any production environment. Harbor has an Nginx instance as a reverse proxy for all services, you can configure Nginx to enable https.
##Get a certificate
Assuming that your registrys domain is harbordomain.com, and that its DNS record points to the host where you are running Harbor, you first should get a certificate from a CA. The certificate usually contains a .crt file and a .key file, for example, harbordomain.crt and harbordomain.key.
Assuming that your registrys **hostname** is **reg.yourdomain.com**, and that its DNS record points to the host where you are running Harbor, you first should get a certificate from a CA. The certificate usually contains a .crt file and a .key file, for example, **yourdomain.com.crt** and **yourdomain.com.key**.
In a test or development environment, you may choose to use a self-signed certificate instead of the one from a CA. The below command generates your own certificate:
In a test or development environment, you may choose to use a self-signed certificate instead of the one from a CA. The below commands generate your own certificate:
1) Create your own CA certificate:
```
openssl req \
-newkey rsa:4096 -nodes -sha256 -keyout harbordomain.key \
-x509 -days 365 -out harbordomain.crt
openssl req \
-newkey rsa:4096 -nodes -sha256 -keyout ca.key \
-x509 -days 365 -out ca.crt
```
2) Generate a Certificate Signing Request, be sure to use **reg.yourdomain.com** as the CN (Common Name):
```
openssl req \
-newkey rsa:4096 -nodes -sha256 -keyout yourdomain.com.key \
-out yourdomain.com.csr
```
3) Generate the certificate of your registry host
You need to configure openssl first. On Ubuntu, the config file locates at /etc/ssl/openssl.cnf. Refer to openssl document for more information. The default CA directory of openssl is called demoCA. Lets creates necessary directories and files:
```
mkdir demoCA
cd demoCA
touch index.txt
echo '01' > serial
cd ..
```
Then run this command to generate the certificate of your registry host:
```
openssl ca -in yourdomain.com.csr -out yourdomain.com.crt -cert ca.crt -keyfile ca.key outdir .
```
Be sure to use harbordomain as a CN.
##Configuration of Nginx
After obtaining the .crt and .key files, change the directory to Deploy/config/nginx in Harbor project.
After obtaining the **yourdomain.com.crt** and **yourdomain.com.key** files, change the directory to Deploy/config/nginx in Harbor project.
```
cd Deploy/config/nginx
```
Create a new directory “cert/” if it does not exist. Then copy harbordomain.crt and harbordomain.key to cert/.
Create a new directory “cert/” if it does not exist. Then copy **yourdomain.com.crt** and **yourdomain.com.key** to cert/.
Rename the existing configuration file of Nginx:
```
mv nginx.conf nginx.conf.bak
```
Copy the template nginx.https.conf as the new configuration file:
Copy the template **nginx.https.conf** as the new configuration file:
```
cp nginx.https.conf nginx.conf
```
Edit the file nginx.conf and replace two occurrences of harbordomain.com to your own domain name.
Edit the file nginx.conf and replace two occurrences of **server name** harbordomain.com to your own host name: reg.yourdomain.com .
```
server {
listen 443 ssl;
@ -41,15 +62,16 @@ Edit the file nginx.conf and replace two occurrences of harbordomain.com to your
listen 80;
server_name harbordomain.com;
rewrite ^/(.*) https://$server_name$1 permanent;
```
Then look for the SSL section to make sure the files of your certificates match the names in the config file.
Then look for the SSL section to make sure the files of your certificates match the names in the config file. Do not change the path of the files.
```
# SSL
ssl_certificate /etc/nginx/cert/harbordomain.crt;
ssl_certificate_key /etc/nginx/cert/harbordomain.key;
ssl_certificate /etc/nginx/cert/yourdomain.com.crt;
ssl_certificate_key /etc/nginx/cert/yourdomain.com.key;
```
Save your changes in nginx.conf.
@ -57,24 +79,45 @@ Save your changes in nginx.conf.
Next, edit the file Deploy/harbor.cfg , update the hostname and the protocol:
```
#set hostname
hostname = habordomain.com
hostname = reg.yourdomain.com
#set ui_url_protocol
ui_url_protocol = https
```
Generate configuration files for Harbor:
```
./prepare
./prepare
```
If Harbor is already running, stop and remove the existing instance:
If Harbor is already running, stop and remove the existing instance. Your image data remain in the file system
```
docker-compose stop
docker-compose stop
docker-compose rm
```
Finally, restart Harbor:
```
docker-compose up d
```
After setting up HTTPS for Harbor, you can verify it by the follow steps:
1. Open a browser and enter the address: https://reg.yourdomain.com . It should display the user interface of Harbor.
2. On a machine with Docker daemon, make sure the option “--insecure-registry” does not present, run any docker command to verify the setup, e.g.
```
docker login reg.yourdomain.com
```
##Troubleshooting
1.` `You may get an intermediate certificate from a certificate issuer. In this case, you should merge the intermediate certificate with your own certificate to create a certificate bundle. You can achieve this by the below command:
```
cat intermediate-certificate.pem >> yourdomain.com.crt
```
2.` `On some systems where docker daemon runs, you may need to trust the certificate at OS level.
On Ubuntu, this can be done by below commands:
```
cp youdomain.com.crt /usr/local/share/ca-certificates/reg.yourdomain.com.crt
update-ca-certificates
```
On Red Hat (CentOS etc), the commands are:
```
cp yourdomain.com.crt /etc/pki/ca-trust/source/anchors/reg.yourdomain.com.crt
update-ca-trust