Skip to content

Commit b111c98

Browse files
Fix an issue which cause failure when updating a sub-CA (#12495) (#8872)
[upstream:172e79f94b241da6b7aac980bbbe189d53722836] Signed-off-by: Modular Magician <[email protected]>
1 parent 124e2a0 commit b111c98

File tree

3 files changed

+67
-3
lines changed

3 files changed

+67
-3
lines changed

.changelog/12495.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note: bug
2+
privateca: fixed an issue which causes error when updating labels for activated sub-CA
3+
```

google-beta/services/privateca/resource_privateca_certificate_authority.go

+15
Original file line numberDiff line numberDiff line change
@@ -1217,6 +1217,21 @@ func resourcePrivatecaCertificateAuthorityUpdate(d *schema.ResourceData, meta in
12171217
}
12181218
}
12191219

1220+
// `subordinateConfig.certificateAuthority` is not directly passed
1221+
// to the backend when activating a sub-CA. Instead, it is used to sign CA cert
1222+
// and activate the sub-CA at client side. See b/332548736 for details.
1223+
// Drop this field to avoid both `subordinateConfig.certificateAuthority`
1224+
// and `subordinateConfig.pemIssuerChain` to be passed to the backend.
1225+
if _, ok := obj["subordinateConfig"]; ok {
1226+
subConfig := obj["subordinateConfig"].(map[string]interface{})
1227+
// There could be case which a sub-CA was activated via `subordinateConfig.certificateAuthority`
1228+
// directly by older version of providers.
1229+
// For backward compatibility, delete `certificateAuthority` only if `pemIssuerChain` is presented.
1230+
if _, ok := subConfig["pemIssuerChain"]; ok {
1231+
delete(subConfig, "certificateAuthority")
1232+
}
1233+
}
1234+
12201235
// err == nil indicates that the billing_project value was found
12211236
if bp, err := tpgresource.GetBillingProject(d, config); err == nil {
12221237
billingProject = bp

google-beta/services/privateca/resource_privateca_certificate_authority_test.go

+49-3
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,10 @@ func TestAccPrivatecaCertificateAuthority_subordinateCaActivatedByFirstPartyIssu
128128

129129
random_suffix := acctest.RandString(t, 10)
130130
context := map[string]interface{}{
131-
"root_location": "us-central1",
132-
"sub_location": "australia-southeast1",
133-
"random_suffix": random_suffix,
131+
"root_location": "us-central1",
132+
"sub_location": "australia-southeast1",
133+
"random_suffix": random_suffix,
134+
"first_label_value": "bar",
134135
}
135136

136137
resourceName := "google_privateca_certificate_authority.sub-1"
@@ -176,6 +177,47 @@ func TestAccPrivatecaCertificateAuthority_subordinateCaActivatedByFirstPartyIssu
176177
})
177178
}
178179

180+
func TestAccPrivatecaCertificateAuthority_subordinateCaCanUpdateLabel(t *testing.T) {
181+
t.Parallel()
182+
acctest.SkipIfVcr(t)
183+
184+
random_suffix := acctest.RandString(t, 10)
185+
context1 := map[string]interface{}{
186+
"root_location": "us-central1",
187+
"sub_location": "australia-southeast1",
188+
"random_suffix": random_suffix,
189+
"first_label_value": "bar-1",
190+
}
191+
192+
context2 := map[string]interface{}{
193+
"root_location": "us-central1",
194+
"sub_location": "australia-southeast1",
195+
"random_suffix": random_suffix,
196+
"first_label_value": "bar-2",
197+
}
198+
199+
resourceName := "google_privateca_certificate_authority.sub-1"
200+
acctest.VcrTest(t, resource.TestCase{
201+
PreCheck: func() { acctest.AccTestPreCheck(t) },
202+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
203+
CheckDestroy: testAccCheckPrivatecaCertificateAuthorityDestroyProducer(t),
204+
Steps: []resource.TestStep{
205+
{
206+
Config: testAccPrivatecaCertificateAuthority_privatecaCertificateAuthoritySubordinateWithFirstPartyIssuer(context1),
207+
Check: resource.ComposeTestCheckFunc(
208+
resource.TestCheckResourceAttr(resourceName, "labels.first_label", context1["first_label_value"].(string)),
209+
),
210+
},
211+
{
212+
Config: testAccPrivatecaCertificateAuthority_privatecaCertificateAuthoritySubordinateWithFirstPartyIssuer(context2),
213+
Check: resource.ComposeTestCheckFunc(
214+
resource.TestCheckResourceAttr(resourceName, "labels.first_label", context2["first_label_value"].(string)),
215+
),
216+
},
217+
},
218+
})
219+
}
220+
179221
func testAccPrivatecaCertificateAuthority_privatecaCertificateAuthorityBasicRoot(context map[string]interface{}) string {
180222
return acctest.Nprintf(`
181223
resource "google_privateca_certificate_authority" "default" {
@@ -470,6 +512,10 @@ resource "google_privateca_certificate_authority" "sub-1" {
470512
}
471513
type = "SUBORDINATE"
472514
515+
labels = {
516+
first_label = "%{first_label_value}"
517+
}
518+
473519
// Disable CA deletion related safe checks for easier cleanup.
474520
deletion_protection = false
475521
skip_grace_period = true

0 commit comments

Comments
 (0)