Skip to content

Commit 2f8bad2

Browse files
gfxccniharika-98
authored andcommitted
docs: update code sample to use a sub-CA to issue leaf certificates (terraform-google-modules#819)
1 parent fbc148d commit 2f8bad2

File tree

1 file changed

+82
-27
lines changed

1 file changed

+82
-27
lines changed

privateca/quickstart/main.tf

Lines changed: 82 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,30 +23,66 @@ resource "google_project_service" "privateca_api" {
2323
disable_on_destroy = false
2424
}
2525

26-
resource "tls_private_key" "example" {
27-
algorithm = "RSA"
28-
}
26+
# Root CaPool & CA
2927

30-
resource "tls_cert_request" "example" {
31-
private_key_pem = tls_private_key.example.private_key_pem
28+
resource "google_privateca_ca_pool" "root" {
29+
name = "root-pool"
30+
location = "us-central1"
31+
tier = "ENTERPRISE"
32+
publishing_options {
33+
publish_ca_cert = true
34+
publish_crl = true
35+
}
36+
}
3237

33-
subject {
34-
common_name = "example.com"
35-
organization = "ACME Examples, Inc"
38+
resource "google_privateca_certificate_authority" "root-ca" {
39+
certificate_authority_id = "my-root-ca"
40+
location = "us-central1"
41+
pool = google_privateca_ca_pool.root.name
42+
config {
43+
subject_config {
44+
subject {
45+
organization = "google"
46+
common_name = "my-certificate-authority"
47+
}
48+
}
49+
x509_config {
50+
ca_options {
51+
is_ca = true
52+
}
53+
key_usage {
54+
base_key_usage {
55+
cert_sign = true
56+
crl_sign = true
57+
}
58+
extended_key_usage {
59+
server_auth = true
60+
}
61+
}
62+
}
63+
}
64+
type = "SELF_SIGNED"
65+
key_spec {
66+
algorithm = "RSA_PKCS1_4096_SHA256"
3667
}
68+
69+
// Disable CA deletion related safe checks for easier cleanup.
70+
deletion_protection = false
71+
skip_grace_period = true
72+
ignore_active_certificates_on_deletion = true
3773
}
3874

39-
resource "google_privateca_ca_pool" "default" {
40-
name = "my-ca-pool"
75+
# Sub CaPool & CA
76+
77+
resource "google_privateca_ca_pool" "subordinate" {
78+
name = "sub-pool"
4179
location = "us-central1"
4280
tier = "ENTERPRISE"
4381
publishing_options {
4482
publish_ca_cert = true
4583
publish_crl = true
4684
}
47-
labels = {
48-
foo = "bar"
49-
}
85+
5086
issuance_policy {
5187
baseline_values {
5288
ca_options {
@@ -65,26 +101,28 @@ resource "google_privateca_ca_pool" "default" {
65101
}
66102
}
67103

68-
resource "google_privateca_certificate_authority" "test_ca" {
69-
certificate_authority_id = "my-authority"
104+
resource "google_privateca_certificate_authority" "sub-ca" {
105+
pool = google_privateca_ca_pool.subordinate.name
106+
certificate_authority_id = "my-sub-ca"
70107
location = "us-central1"
71-
pool = google_privateca_ca_pool.default.name
108+
subordinate_config {
109+
certificate_authority = google_privateca_certificate_authority.root-ca.name
110+
}
72111
config {
73112
subject_config {
74113
subject {
75-
country_code = "us"
76-
organization = "google"
77-
organizational_unit = "enterprise"
78-
locality = "mountain view"
79-
province = "california"
80-
street_address = "1600 amphitheatre parkway"
81-
postal_code = "94109"
82-
common_name = "my-certificate-authority"
114+
organization = "HashiCorp"
115+
common_name = "my-subordinate-authority"
116+
}
117+
subject_alt_name {
118+
dns_names = ["hashicorp.com"]
83119
}
84120
}
85121
x509_config {
86122
ca_options {
87123
is_ca = true
124+
# Force the sub CA to only issue leaf certs
125+
max_issuer_path_length = 0
88126
}
89127
key_usage {
90128
base_key_usage {
@@ -97,20 +135,37 @@ resource "google_privateca_certificate_authority" "test_ca" {
97135
}
98136
}
99137
}
100-
type = "SELF_SIGNED"
138+
lifetime = "31536000s"
101139
key_spec {
102140
algorithm = "RSA_PKCS1_4096_SHA256"
103141
}
142+
type = "SUBORDINATE"
104143

105144
// Disable CA deletion related safe checks for easier cleanup.
106145
deletion_protection = false
107146
skip_grace_period = true
108147
ignore_active_certificates_on_deletion = true
109148
}
110149

150+
# Leaf cert
151+
152+
resource "tls_private_key" "example" {
153+
algorithm = "RSA"
154+
}
155+
156+
resource "tls_cert_request" "example" {
157+
private_key_pem = tls_private_key.example.private_key_pem
158+
159+
subject {
160+
common_name = "example.com"
161+
organization = "ACME Examples, Inc"
162+
}
163+
}
164+
111165
resource "google_privateca_certificate" "default" {
112-
pool = google_privateca_ca_pool.default.name
113-
certificate_authority = google_privateca_certificate_authority.test_ca.certificate_authority_id
166+
pool = google_privateca_ca_pool.subordinate.name
167+
# Explicitly refer the sub-CA so that the certificate creation will wait for the CA creation.
168+
certificate_authority = google_privateca_certificate_authority.sub-ca.certificate_authority_id
114169
location = "us-central1"
115170
lifetime = "860s"
116171
name = "my-certificate"

0 commit comments

Comments
 (0)