Skip to content

Commit 22a58a8

Browse files
tf(harbor-standalone): use vsphere-vm module to create vm and add support for pre-existing tls artifacts
1 parent 42d260f commit 22a58a8

12 files changed

+184
-216
lines changed

terraform/modules/harbor-standalone/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ The module deploys Harbor registry in a VM in a vSphere environment.
88

99
| Name | Description | Type | Default | Required |
1010
|------|-------------|------|---------|:--------:|
11-
| domain | n/a | `string` | n/a | yes |
11+
| fqdn | Fully qualified domain name of the VM | `string` | n/a | yes |
1212
| vm\_authorized\_key | Public key authorized to ssh into the VM | `string` | n/a | yes |
1313
| vsphere | vSphere related references to deploy the VM | <pre>object({<br> resource_pool_id = string<br> datastore_id = string<br> network_id = string<br> template_id = string<br> })</pre> | n/a | yes |
14+
| tls | TLS configuration to use. Private key and certificate must be base64 encoded | <pre>object({<br> private_key = string<br> certificate = string<br> ca_chain = optional(string, null)<br> })</pre> | `null` | no |
1415

1516
## Outputs
1617

1718
| Name | Description |
1819
|------|-------------|
1920
| harbor\_admin\_password | n/a |
20-
| harbor\_instance\_ip | n/a |
21+
| instance\_ip | n/a |
2122
<!-- END_TF_DOCS -->
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
harbor_hostname: ${harbor_hostname}
2+
3+
%{ if harbor_base64_tls_key != null ~}
4+
harbor_base64_tls_key: ${harbor_base64_tls_key}
5+
%{ endif ~}
6+
%{ if harbor_base64_tls_cert != null ~}
7+
harbor_base64_tls_cert: ${harbor_base64_tls_cert}
8+
%{ endif ~}
9+
%{ if harbor_base64_tls_ca_chain != null ~}
10+
harbor_base64_tls_ca_chain: ${harbor_base64_tls_ca_chain}
11+
%{ endif ~}

terraform/modules/harbor-standalone/files/harbor-cloud-config.yml.tftpl

Lines changed: 0 additions & 64 deletions
This file was deleted.

terraform/modules/harbor-standalone/files/harbor-config.yml.tftpl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ jobservice:
4343
- FILE
4444
logger_sweeper_duration: 1 #days
4545

46-
4746
notification:
4847
webhook_job_max_retry: 3
4948
webhook_job_http_client_timeout: 3 #seconds

terraform/modules/harbor-standalone/files/harbor-install-playbook.yaml

Lines changed: 109 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
connection: local
1313
become: true
1414

15-
vars:
16-
harbor_hostname: "${harbor_fqdn}"
15+
vars_files:
16+
- ./vars.yaml
1717

1818
tasks:
1919
- name: Create TLS folder
@@ -22,60 +22,85 @@
2222
state: directory
2323
mode: "0700"
2424

25-
# Create self-signed CA
26-
# https://docs.ansible.com/ansible/latest/collections/community/crypto/docsite/guide_ownca.html#set-up-the-ca
27-
- name: Create CA private key
28-
community.crypto.openssl_privatekey:
29-
path: /harbor/tls/ca.key
30-
31-
- name: Create certificate signing request for CA certificate
32-
community.crypto.openssl_csr_pipe:
33-
privatekey_path: /harbor/tls/ca.key
34-
common_name: Harbor CA
35-
use_common_name_for_san: false
36-
basic_constraints:
37-
- "CA:TRUE"
38-
basic_constraints_critical: true
39-
key_usage:
40-
- keyCertSign
41-
key_usage_critical: true
42-
register: ca_csr
43-
44-
- name: Create self-signed CA certificate from CSR
45-
community.crypto.x509_certificate:
46-
path: /harbor/tls/ca.pem
47-
csr_content: "{{ ca_csr.csr }}"
48-
privatekey_path: /harbor/tls/ca.key
49-
provider: selfsigned
50-
# It must be less than 825 days to work on newer MacOs versions
51-
selfsigned_not_after: "+730d"
52-
53-
# Create Harbor certificate
54-
- name: Create Harbor private key
55-
community.crypto.openssl_privatekey:
56-
path: /harbor/tls/harbor.key
57-
58-
# Create CSR and sign it with the self-signed cA
59-
- name: Create Harbor certificate signing request (CSR)
60-
community.crypto.openssl_csr_pipe:
61-
privatekey_path: /harbor/tls/harbor.key
62-
common_name: "{{ harbor_hostname }}"
63-
subject_alt_name:
64-
- "DNS:{{ harbor_hostname }}"
65-
register: harbor_csr
66-
67-
- name: Create Harbor certificate
68-
community.crypto.x509_certificate:
69-
path: /harbor/tls/harbor.pem
70-
csr_content: "{{ harbor_csr.csr }}"
71-
ownca_privatekey_path: /harbor/tls/ca.key
72-
ownca_path: /harbor/tls/ca.pem
73-
# It must be less than 825 days to work on newer MacOs versions
74-
ownca_not_after: "+365d"
75-
provider: ownca
76-
notify:
77-
- Regenerate Harbor configs
78-
- Restart Harbor service
25+
- name: Generate TLS artifacts
26+
when: harbor_base64_tls_key is undefined or harbor_base64_tls_cert is undefined
27+
block:
28+
# Create self-signed CA
29+
# https://docs.ansible.com/ansible/latest/collections/community/crypto/docsite/guide_ownca.html#set-up-the-ca
30+
- name: Create CA private key
31+
community.crypto.openssl_privatekey:
32+
path: /harbor/tls/ca.key
33+
34+
- name: Create certificate signing request for CA certificate
35+
community.crypto.openssl_csr_pipe:
36+
privatekey_path: /harbor/tls/ca.key
37+
common_name: Harbor CA
38+
use_common_name_for_san: false
39+
basic_constraints:
40+
- "CA:TRUE"
41+
basic_constraints_critical: true
42+
key_usage:
43+
- keyCertSign
44+
key_usage_critical: true
45+
register: ca_csr
46+
47+
- name: Create self-signed CA certificate from CSR
48+
community.crypto.x509_certificate:
49+
path: /harbor/tls/ca.pem
50+
csr_content: "{{ ca_csr.csr }}"
51+
privatekey_path: /harbor/tls/ca.key
52+
provider: selfsigned
53+
# It must be less than 825 days to work on newer MacOs versions
54+
selfsigned_not_after: "+730d"
55+
return_content: true
56+
register: harbor_ca_cert_task
57+
58+
# Create Harbor key and certificate
59+
- name: Create Harbor private key
60+
community.crypto.openssl_privatekey:
61+
path: /harbor/tls/harbor.key
62+
format: "pkcs1"
63+
return_content: true
64+
register: harbor_private_key_task
65+
66+
# Create CSR and sign it with the self-signed CA
67+
- name: Create Harbor certificate signing request (CSR)
68+
community.crypto.openssl_csr_pipe:
69+
privatekey_path: /harbor/tls/harbor.key
70+
common_name: "{{ harbor_hostname }}"
71+
subject_alt_name:
72+
- "DNS:{{ harbor_hostname }}"
73+
register: harbor_csr
74+
75+
- name: Create Harbor certificate
76+
community.crypto.x509_certificate:
77+
path: /harbor/tls/harbor.pem
78+
csr_content: "{{ harbor_csr.csr }}"
79+
ownca_privatekey_path: /harbor/tls/ca.key
80+
ownca_path: /harbor/tls/ca.pem
81+
# It must be less than 825 days to work on newer MacOs versions
82+
ownca_not_after: "+365d"
83+
provider: ownca
84+
return_content: true
85+
register: harbor_cert_task
86+
notify:
87+
- Regenerate Harbor configs
88+
- Restart Harbor service
89+
90+
- name: Set TLS facts using variables
91+
ansible.builtin.set_fact:
92+
harbor_tls_key: "{{ harbor_private_key_task.privatekey }}"
93+
harbor_tls_cert: "{{ harbor_cert_task.certificate }}"
94+
harbor_tls_ca_chain: "{{ harbor_ca_cert_task.certificate }}"
95+
96+
- name: Extract TLS artifacts from variables
97+
when: harbor_base64_tls_key is defined and harbor_base64_tls_cert is defined
98+
block:
99+
- name: Set TLS facts using variables
100+
ansible.builtin.set_fact:
101+
harbor_tls_key: "{{ harbor_base64_tls_key | b64decode }}"
102+
harbor_tls_cert: "{{ harbor_base64_tls_cert | b64decode }}"
103+
harbor_tls_ca_chain: '{{ harbor_base64_tls_ca_chain | default(None) | b64decode or "" }}'
79104

80105
# Copy certificate and private key in the all the required folders
81106
- name: Create Docker cert folder
@@ -103,40 +128,50 @@
103128
mode: "0755"
104129

105130
- name: Copy Harbor private key to Docker certs
106-
ansible.builtin.copy:
107-
src: /harbor/tls/harbor.key
131+
ansible.builtin.template:
132+
src: "templates/harbor-tls-key.j2"
108133
dest: "/etc/docker/certs.d/{{ harbor_hostname }}/{{ harbor_hostname}}.key"
109-
remote_src: yes
110134

111135
- name: Copy Harbor certificate to Docker certs
112-
ansible.builtin.copy:
113-
src: /harbor/tls/harbor.pem
136+
ansible.builtin.template:
137+
src: "templates/harbor-tls-certificate.j2"
114138
dest: "/etc/docker/certs.d/{{ harbor_hostname }}/{{ harbor_hostname}}.pem"
115-
remote_src: yes
116139

117140
- name: Copy CA certificate
118-
ansible.builtin.copy:
119-
src: /harbor/tls/ca.pem
141+
ansible.builtin.template:
142+
src: "templates/harbor-tls-ca-chain.j2"
120143
dest: "/etc/docker/certs.d/{{ harbor_hostname }}/ca.pem"
121-
remote_src: yes
122144

123145
- name: Copy Harbor private key to data volume
124-
ansible.builtin.copy:
125-
src: /harbor/tls/harbor.key
146+
ansible.builtin.template:
147+
src: "templates/harbor-tls-key.j2"
126148
dest: "/data/cert/{{ harbor_hostname}}.key"
127-
remote_src: yes
128149

129150
- name: Copy Harbor certificate to data volume
130-
ansible.builtin.copy:
131-
src: /harbor/tls/harbor.pem
151+
ansible.builtin.template:
152+
src: "templates/harbor-tls-certificate.j2"
132153
dest: "/data/cert/{{ harbor_hostname}}.pem"
133-
remote_src: yes
134154

135155
- name: Copy CA to data volume
136-
ansible.builtin.copy:
137-
src: /harbor/tls/ca.pem
156+
ansible.builtin.template:
157+
src: "templates/harbor-tls-ca-chain.j2"
138158
dest: "/data/ca_download/ca.crt"
139-
remote_src: yes
159+
160+
- name: Copy Harbor config
161+
ansible.builtin.copy:
162+
src: "/ansible/harbor-config.yml"
163+
dest: "/harbor/harbor.yml"
164+
notify:
165+
- Regenerate Harbor configs
166+
- Restart Harbor service
167+
168+
- name: Copy Harbor service
169+
ansible.builtin.copy:
170+
src: "templates/harbor-systemd.service"
171+
dest: "/etc/systemd/system/harbor.service"
172+
notify:
173+
- Regenerate Harbor configs
174+
- Restart Harbor service
140175

141176
- name: Check if Harbor is already installed
142177
ansible.builtin.shell:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{ harbor_tls_ca_chain }}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{ harbor_tls_cert }}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{ harbor_tls_key }}

terraform/modules/harbor-standalone/outputs.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
output "harbor_instance_ip" {
2-
value = vsphere_virtual_machine.harbor.default_ip_address
1+
output "instance_ip" {
2+
value = module.vm.instance_ip
33
}
44

55
output "harbor_admin_password" {

terraform/modules/harbor-standalone/variables.tf

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ variable "vm_authorized_key" {
33
type = string
44
}
55

6-
variable "domain" {
6+
variable "fqdn" {
77
type = string
8+
description = "Fully qualified domain name of the VM"
89
}
910

1011
variable "vsphere" {
@@ -17,4 +18,16 @@ variable "vsphere" {
1718
network_id = string
1819
template_id = string
1920
})
21+
}
22+
23+
variable "tls" {
24+
type = object({
25+
private_key = string
26+
certificate = string
27+
ca_chain = optional(string, null)
28+
})
29+
30+
nullable = true
31+
default = null
32+
description = "TLS configuration to use. Private key and certificate must be base64 encoded"
2033
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.0.0
1+
2.0.0

0 commit comments

Comments
 (0)