Skip to content

Commit e5a1c5f

Browse files
modular-magicianrileykarson
authored andcommitted
Fix issue with google_compute_backend_service IAP client secret @ update (#2978)
<!-- This change is generated by MagicModules. --> /cc @rileykarson
1 parent 3d43932 commit e5a1c5f

4 files changed

+29
-19
lines changed

google/resource_compute_backend_service.go

+11-14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package google
22

33
import (
4-
"crypto/sha256"
54
"errors"
65
"fmt"
76
"log"
@@ -53,12 +52,11 @@ func resourceComputeBackendService() *schema.Resource {
5352
Type: schema.TypeString,
5453
Required: true,
5554
Sensitive: true,
56-
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
57-
if old == fmt.Sprintf("%x", sha256.Sum256([]byte(new))) {
58-
return true
59-
}
60-
return false
61-
},
55+
},
56+
"oauth2_client_secret_sha256": {
57+
Type: schema.TypeString,
58+
Computed: true,
59+
Sensitive: true,
6260
},
6361
},
6462
},
@@ -321,7 +319,7 @@ func resourceComputeBackendServiceRead(d *schema.ResourceData, meta interface{})
321319
d.Set("self_link", ConvertSelfLinkToV1(service.SelfLink))
322320
d.Set("backend", flattenBackends(service.Backends))
323321
d.Set("connection_draining_timeout_sec", service.ConnectionDraining.DrainingTimeoutSec)
324-
d.Set("iap", flattenIap(service.Iap))
322+
d.Set("iap", flattenIap(d, service.Iap))
325323
d.Set("project", project)
326324
guardedHealthChecks := make([]string, len(service.HealthChecks))
327325
for i, v := range service.HealthChecks {
@@ -423,18 +421,17 @@ func expandIap(configured []interface{}) *computeBeta.BackendServiceIAP {
423421
}
424422
}
425423

426-
func flattenIap(iap *computeBeta.BackendServiceIAP) []map[string]interface{} {
424+
func flattenIap(d *schema.ResourceData, iap *computeBeta.BackendServiceIAP) []map[string]interface{} {
427425
result := make([]map[string]interface{}, 0, 1)
428426
if iap == nil || !iap.Enabled {
429427
return result
430428
}
431429

432-
result = append(result, map[string]interface{}{
433-
"oauth2_client_id": iap.Oauth2ClientId,
434-
"oauth2_client_secret": iap.Oauth2ClientSecretSha256,
430+
return append(result, map[string]interface{}{
431+
"oauth2_client_id": iap.Oauth2ClientId,
432+
"oauth2_client_secret": d.Get("iap.0.oauth2_client_secret"),
433+
"oauth2_client_secret_sha256": iap.Oauth2ClientSecretSha256,
435434
})
436-
437-
return result
438435
}
439436

440437
func expandBackends(configured []interface{}) ([]*computeBeta.Backend, error) {

google/resource_compute_backend_service_test.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,15 @@ func TestAccComputeBackendService_withBackendAndIAP(t *testing.T) {
111111
Config: testAccComputeBackendService_withBackendAndIAP(
112112
serviceName, igName, itName, checkName, 10),
113113
Check: resource.ComposeTestCheckFunc(
114-
testAccCheckComputeBackendServiceExistsWithIAP(
115-
"google_compute_backend_service.lipsum", &svc),
114+
testAccCheckComputeBackendServiceExistsWithIAP("google_compute_backend_service.lipsum", &svc),
115+
resource.TestCheckResourceAttr("google_compute_backend_service.lipsum", "iap.0.oauth2_client_secret", "test"),
116116
),
117117
},
118118
{
119-
ResourceName: "google_compute_backend_service.lipsum",
120-
ImportState: true,
121-
ImportStateVerify: true,
119+
ResourceName: "google_compute_backend_service.lipsum",
120+
ImportState: true,
121+
ImportStateVerify: true,
122+
ImportStateVerifyIgnore: []string{"iap.0.oauth2_client_secret"},
122123
},
123124
{
124125
Config: testAccComputeBackendService_withBackend(

website/docs/r/compute_backend_service.html.markdown

+4
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,16 @@ The `iap` block supports:
179179
* `oauth2_client_id` - (Required) The client ID for use with OAuth 2.0.
180180

181181
* `oauth2_client_secret` - (Required) The client secret for use with OAuth 2.0.
182+
Out of band changes to this field will not be detected by Terraform, and it may
183+
perform spurious no-op updates when imported, or upgraded from pre-`2.0.0`.
182184

183185
## Attributes Reference
184186

185187
In addition to the arguments listed above, the following computed attributes are
186188
exported:
187189

190+
* `iap.0.oauth2_client_secret_sha256` - The SHA256 hash of the OAuth 2.0 client secret value.
191+
188192
* `fingerprint` - The fingerprint of the backend service.
189193

190194
* `self_link` - The URI of the created resource.

website/docs/version_2_upgrade.html.markdown

+8
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,14 @@ for more details.
289289

290290
Use the [`google-beta` provider](#google-beta-provider) to set this field.
291291

292+
### `iap` may cause spurious updates
293+
294+
Due to technical limitations around how Terraform can diff fields, you may see a
295+
spurious update where the client secret in your config replaces an incorrect
296+
value that was recorded in state, the SHA256 hash of the secret's value.
297+
298+
You may also encounter the same behaviour on import.
299+
292300
## Resource: `google_compute_disk`
293301

294302
### `disk_encryption_key_raw` and `disk_encryption_key_sha256` have been removed.

0 commit comments

Comments
 (0)