Skip to content

Commit fa17f2d

Browse files
modular-magicianshuyama1
authored andcommitted
fixed permission issues when activating a sub-CA in a different region (#10354) (#17783)
[upstream:8ee3e3e2e9c42bd901178cc409bed2960a691b43] Signed-off-by: Modular Magician <[email protected]>
1 parent 2437b06 commit fa17f2d

File tree

4 files changed

+171
-2
lines changed

4 files changed

+171
-2
lines changed

.changelog/10354.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
privateca: fixed permission issues when activating a sub-CA in a different region
3+
```

google/services/privateca/privateca_ca_utils.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -230,12 +230,14 @@ func activateSubCAWithFirstPartyIssuer(config *transport_tpg.Config, d *schema.R
230230
return fmt.Errorf("Error creating Certificate: %s", err)
231231
}
232232
signedCACert := res["pemCertificate"]
233+
signerCertChain := res["pemCertificateChain"]
233234

234235
// 4. activate sub CA with the signed CA cert.
235236
activateObj := make(map[string]interface{})
236237
activateObj["pemCaCertificate"] = signedCACert
237238
activateObj["subordinateConfig"] = make(map[string]interface{})
238-
activateObj["subordinateConfig"].(map[string]interface{})["certificateAuthority"] = issuer
239+
activateObj["subordinateConfig"].(map[string]interface{})["pemIssuerChain"] = make(map[string]interface{})
240+
activateObj["subordinateConfig"].(map[string]interface{})["pemIssuerChain"].(map[string]interface{})["pemCertificates"] = signerCertChain
239241

240242
activateUrl, err := tpgresource.ReplaceVars(d, config, "{{PrivatecaBasePath}}projects/{{project}}/locations/{{location}}/caPools/{{pool}}/certificateAuthorities/{{certificate_authority_id}}:activate")
241243
if err != nil {

google/services/privateca/resource_privateca_certificate_authority.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,7 @@ and usability purposes only. The resource name is in the format
709709
},
710710
"pem_issuer_chain": {
711711
Type: schema.TypeList,
712+
Computed: true,
712713
Optional: true,
713714
Description: `Contains the PEM certificate chain for the issuers of this CertificateAuthority,
714715
but not pem certificate for this CA itself.`,
@@ -1538,7 +1539,7 @@ func flattenPrivatecaCertificateAuthoritySubordinateConfig(v interface{}, d *sch
15381539
return []interface{}{transformed}
15391540
}
15401541
func flattenPrivatecaCertificateAuthoritySubordinateConfigCertificateAuthority(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
1541-
return v
1542+
return d.Get("subordinate_config.0.certificate_authority")
15421543
}
15431544

15441545
func flattenPrivatecaCertificateAuthoritySubordinateConfigPemIssuerChain(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {

google/services/privateca/resource_privateca_certificate_authority_test.go

+163
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,33 @@ func TestAccPrivatecaCertificateAuthority_rootCaManageDesiredState(t *testing.T)
122122
})
123123
}
124124

125+
func TestAccPrivatecaCertificateAuthority_subordinateCaActivatedByFirstPartyIssuerOnCreation(t *testing.T) {
126+
t.Parallel()
127+
acctest.SkipIfVcr(t)
128+
129+
random_suffix := acctest.RandString(t, 10)
130+
context := map[string]interface{}{
131+
"root_location": "us-central1",
132+
"sub_location": "australia-southeast1",
133+
"random_suffix": random_suffix,
134+
}
135+
136+
resourceName := "google_privateca_certificate_authority.sub-1"
137+
acctest.VcrTest(t, resource.TestCase{
138+
PreCheck: func() { acctest.AccTestPreCheck(t) },
139+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
140+
CheckDestroy: testAccCheckPrivatecaCertificateAuthorityDestroyProducer(t),
141+
Steps: []resource.TestStep{
142+
{
143+
Config: testAccPrivatecaCertificateAuthority_privatecaCertificateAuthoritySubordinateWithFirstPartyIssuer(context),
144+
Check: resource.ComposeTestCheckFunc(
145+
resource.TestCheckResourceAttr(resourceName, "state", "ENABLED"),
146+
),
147+
},
148+
},
149+
})
150+
}
151+
125152
func testAccPrivatecaCertificateAuthority_privatecaCertificateAuthorityBasicRoot(context map[string]interface{}) string {
126153
return acctest.Nprintf(`
127154
resource "google_privateca_certificate_authority" "default" {
@@ -287,3 +314,139 @@ resource "google_privateca_certificate_authority" "default" {
287314
}
288315
`, context)
289316
}
317+
318+
// testAccPrivatecaCertificateAuthority_privatecaCertificateAuthoritySubordinateWithFirstPartyIssuer provides a config
319+
// which contains
320+
// * A CaPool for root CA
321+
// * A root CA
322+
// * A CaPool for sub CA
323+
// * A subordinate CA which should be activated by the above root CA
324+
func testAccPrivatecaCertificateAuthority_privatecaCertificateAuthoritySubordinateWithFirstPartyIssuer(context map[string]interface{}) string {
325+
return acctest.Nprintf(`
326+
resource "google_privateca_ca_pool" "root-pool" {
327+
name = "root-pool-%{random_suffix}"
328+
location = "%{root_location}"
329+
tier = "ENTERPRISE"
330+
publishing_options {
331+
publish_ca_cert = true
332+
publish_crl = true
333+
}
334+
}
335+
336+
resource "google_privateca_certificate_authority" "root-1" {
337+
pool = google_privateca_ca_pool.root-pool.name
338+
certificate_authority_id = "tf-test-my-certificate-authority-root-%{random_suffix}"
339+
location = "%{root_location}"
340+
config {
341+
subject_config {
342+
subject {
343+
organization = "HashiCorp"
344+
common_name = "my-certificate-authority"
345+
}
346+
subject_alt_name {
347+
dns_names = ["hashicorp.com"]
348+
}
349+
}
350+
x509_config {
351+
ca_options {
352+
is_ca = true
353+
max_issuer_path_length = 10
354+
}
355+
key_usage {
356+
base_key_usage {
357+
digital_signature = true
358+
content_commitment = true
359+
key_encipherment = false
360+
data_encipherment = true
361+
key_agreement = true
362+
cert_sign = true
363+
crl_sign = true
364+
decipher_only = true
365+
}
366+
extended_key_usage {
367+
server_auth = true
368+
client_auth = false
369+
email_protection = true
370+
code_signing = true
371+
time_stamping = true
372+
}
373+
}
374+
}
375+
}
376+
lifetime = "86400s"
377+
key_spec {
378+
algorithm = "RSA_PKCS1_4096_SHA256"
379+
}
380+
381+
// Disable CA deletion related safe checks for easier cleanup.
382+
deletion_protection = false
383+
skip_grace_period = true
384+
ignore_active_certificates_on_deletion = true
385+
}
386+
387+
resource "google_privateca_ca_pool" "sub-pool" {
388+
name = "sub-pool-%{random_suffix}"
389+
location = "%{sub_location}"
390+
tier = "ENTERPRISE"
391+
publishing_options {
392+
publish_ca_cert = true
393+
publish_crl = true
394+
}
395+
}
396+
397+
resource "google_privateca_certificate_authority" "sub-1" {
398+
pool = google_privateca_ca_pool.sub-pool.name
399+
certificate_authority_id = "tf-test-my-certificate-authority-sub-%{random_suffix}"
400+
location = "%{sub_location}"
401+
subordinate_config {
402+
certificate_authority = google_privateca_certificate_authority.root-1.name
403+
}
404+
config {
405+
subject_config {
406+
subject {
407+
organization = "HashiCorp"
408+
common_name = "my-certificate-authority"
409+
}
410+
subject_alt_name {
411+
dns_names = ["hashicorp.com"]
412+
}
413+
}
414+
x509_config {
415+
ca_options {
416+
is_ca = true
417+
max_issuer_path_length = 10
418+
}
419+
key_usage {
420+
base_key_usage {
421+
digital_signature = true
422+
content_commitment = true
423+
key_encipherment = false
424+
data_encipherment = true
425+
key_agreement = true
426+
cert_sign = true
427+
crl_sign = true
428+
decipher_only = true
429+
}
430+
extended_key_usage {
431+
server_auth = true
432+
client_auth = false
433+
email_protection = true
434+
code_signing = true
435+
time_stamping = true
436+
}
437+
}
438+
}
439+
}
440+
lifetime = "86400s"
441+
key_spec {
442+
algorithm = "RSA_PKCS1_4096_SHA256"
443+
}
444+
type = "SUBORDINATE"
445+
446+
// Disable CA deletion related safe checks for easier cleanup.
447+
deletion_protection = false
448+
skip_grace_period = true
449+
ignore_active_certificates_on_deletion = true
450+
}
451+
`, context)
452+
}

0 commit comments

Comments
 (0)