4
4
"fmt"
5
5
"os"
6
6
"regexp"
7
+ "strconv"
7
8
"strings"
8
9
"testing"
9
10
@@ -225,21 +226,34 @@ func TestAccComputeInstance_deprecated_disksWithAutodelete(t *testing.T) {
225
226
func TestAccComputeInstance_diskEncryption (t * testing.T ) {
226
227
var instance compute.Instance
227
228
var instanceName = fmt .Sprintf ("instance-test-%s" , acctest .RandString (10 ))
228
- var diskName = fmt .Sprintf ("instance-testd-%s" , acctest .RandString (10 ))
229
+ bootEncryptionKey := "SGVsbG8gZnJvbSBHb29nbGUgQ2xvdWQgUGxhdGZvcm0="
230
+ bootEncryptionKeyHash := "esTuF7d4eatX4cnc4JsiEiaI+Rff78JgPhA/v1zxX9E="
231
+ diskNameToEncryptionKey := map [string ]* compute.CustomerEncryptionKey {
232
+ fmt .Sprintf ("instance-testd-%s" , acctest .RandString (10 )): {
233
+ RawKey : "Ym9vdDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI=" ,
234
+ Sha256 : "awJ7p57H+uVZ9axhJjl1D3lfC2MgA/wnt/z88Ltfvss=" ,
235
+ },
236
+ fmt .Sprintf ("instance-testd-%s" , acctest .RandString (10 )): {
237
+ RawKey : "c2Vjb25kNzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI=" ,
238
+ Sha256 : "7TpIwUdtCOJpq2m+3nt8GFgppu6a2Xsj1t0Gexk13Yc=" ,
239
+ },
240
+ fmt .Sprintf ("instance-testd-%s" , acctest .RandString (10 )): {
241
+ RawKey : "dGhpcmQ2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI=" ,
242
+ Sha256 : "b3pvaS7BjDbCKeLPPTx7yXBuQtxyMobCHN1QJR43xeM=" ,
243
+ },
244
+ }
229
245
230
246
resource .Test (t , resource.TestCase {
231
247
PreCheck : func () { testAccPreCheck (t ) },
232
248
Providers : testAccProviders ,
233
249
CheckDestroy : testAccCheckComputeInstanceDestroy ,
234
250
Steps : []resource.TestStep {
235
251
resource.TestStep {
236
- Config : testAccComputeInstance_disks_encryption (diskName , instanceName ),
252
+ Config : testAccComputeInstance_disks_encryption (bootEncryptionKey , diskNameToEncryptionKey , instanceName ),
237
253
Check : resource .ComposeTestCheckFunc (
238
254
testAccCheckComputeInstanceExists (
239
255
"google_compute_instance.foobar" , & instance ),
240
- testAccCheckComputeInstanceDisk (& instance , instanceName , true , true ),
241
- testAccCheckComputeInstanceDisk (& instance , diskName , true , false ),
242
- testAccCheckComputeInstanceDiskEncryptionKey ("google_compute_instance.foobar" , & instance ),
256
+ testAccCheckComputeInstanceDiskEncryptionKey ("google_compute_instance.foobar" , & instance , bootEncryptionKeyHash , diskNameToEncryptionKey ),
243
257
),
244
258
},
245
259
},
@@ -982,24 +996,66 @@ func testAccCheckComputeInstanceScratchDisk(instance *compute.Instance, interfac
982
996
}
983
997
}
984
998
985
- func testAccCheckComputeInstanceDiskEncryptionKey (n string , instance * compute.Instance ) resource.TestCheckFunc {
999
+ func testAccCheckComputeInstanceDiskEncryptionKey (n string , instance * compute.Instance , bootDiskEncryptionKey string , diskNameToEncryptionKey map [ string ] * compute. CustomerEncryptionKey ) resource.TestCheckFunc {
986
1000
return func (s * terraform.State ) error {
987
1001
rs , ok := s .RootModule ().Resources [n ]
988
1002
if ! ok {
989
1003
return fmt .Errorf ("Not found: %s" , n )
990
1004
}
991
1005
992
1006
for i , disk := range instance .Disks {
993
- attr := rs .Primary .Attributes [fmt .Sprintf ("disk.%d.disk_encryption_key_sha256" , i )]
994
- if attr == "" && disk .Boot {
995
- attr = rs .Primary .Attributes ["boot_disk.0.disk_encryption_key_sha256" ]
1007
+ if disk .Boot {
1008
+ attr := rs .Primary .Attributes ["boot_disk.0.disk_encryption_key_sha256" ]
1009
+ if attr == "" {
1010
+ attr = rs .Primary .Attributes [fmt .Sprintf ("disk.%d.disk_encryption_key_sha256" , i )]
1011
+ }
1012
+ if attr != bootDiskEncryptionKey {
1013
+ return fmt .Errorf ("Boot disk has wrong encryption key in state.\n Expected: %s\n Actual: %s" , bootDiskEncryptionKey , attr )
1014
+ }
1015
+ if disk .DiskEncryptionKey == nil && attr != "" {
1016
+ return fmt .Errorf ("Disk %d has mismatched encryption key.\n TF State: %+v\n GCP State: <empty>" , i , attr )
1017
+ }
1018
+ if disk .DiskEncryptionKey != nil && attr != disk .DiskEncryptionKey .Sha256 {
1019
+ return fmt .Errorf ("Disk %d has mismatched encryption key.\n TF State: %+v\n GCP State: %+v" ,
1020
+ i , attr , disk .DiskEncryptionKey .Sha256 )
1021
+ }
1022
+ } else {
1023
+ if disk .DiskEncryptionKey != nil {
1024
+ sourceUrl := strings .Split (disk .Source , "/" )
1025
+ expectedKey := diskNameToEncryptionKey [sourceUrl [len (sourceUrl )- 1 ]].Sha256
1026
+ if disk .DiskEncryptionKey .Sha256 != expectedKey {
1027
+ return fmt .Errorf ("Disk %d has unexpected encryption key in GCP.\n Expected: %s\n Actual: %s" , i , expectedKey , disk .DiskEncryptionKey .Sha256 )
1028
+ }
1029
+ }
996
1030
}
997
- if disk .DiskEncryptionKey == nil && attr != "" {
998
- return fmt .Errorf ("Disk %d has mismatched encryption key.\n TF State: %+v\n GCP State: <empty>" , i , attr )
1031
+ }
1032
+
1033
+ numDisks , err := strconv .Atoi (rs .Primary .Attributes ["disk.#" ])
1034
+ if err != nil {
1035
+ return fmt .Errorf ("Error converting value of disk.#" )
1036
+ }
1037
+ for i := 0 ; i < numDisks ; i ++ {
1038
+ diskName := rs .Primary .Attributes [fmt .Sprintf ("disk.%d.disk" , i )]
1039
+ encryptionKey := rs .Primary .Attributes [fmt .Sprintf ("disk.%d.disk_encryption_key_sha256" , i )]
1040
+ expectedEncryptionKey := diskNameToEncryptionKey [diskName ].Sha256
1041
+ if encryptionKey != expectedEncryptionKey {
1042
+ return fmt .Errorf ("Disk %d has unexpected encryption key in state.\n Expected: %s\n Actual: %s" , i , expectedEncryptionKey , encryptionKey )
999
1043
}
1000
- if disk .DiskEncryptionKey != nil && attr != disk .DiskEncryptionKey .Sha256 {
1001
- return fmt .Errorf ("Disk %d has mismatched encryption key.\n TF State: %+v\n GCP State: %+v" ,
1002
- i , attr , disk .DiskEncryptionKey .Sha256 )
1044
+ }
1045
+
1046
+ numAttachedDisks , err := strconv .Atoi (rs .Primary .Attributes ["attached_disk.#" ])
1047
+ if err != nil {
1048
+ return fmt .Errorf ("Error converting value of attached_disk.#" )
1049
+ }
1050
+ for i := 0 ; i < numAttachedDisks ; i ++ {
1051
+ diskSourceUrl := strings .Split (rs .Primary .Attributes [fmt .Sprintf ("attached_disk.%d.source" , i )], "/" )
1052
+ diskName := diskSourceUrl [len (diskSourceUrl )- 1 ]
1053
+ encryptionKey := rs .Primary .Attributes [fmt .Sprintf ("attached_disk.%d.disk_encryption_key_sha256" , i )]
1054
+ if key , ok := diskNameToEncryptionKey [diskName ]; ok {
1055
+ expectedEncryptionKey := key .Sha256
1056
+ if encryptionKey != expectedEncryptionKey {
1057
+ return fmt .Errorf ("Attached disk %d has unexpected encryption key in state.\n Expected: %s\n Actual: %s" , i , expectedEncryptionKey , encryptionKey )
1058
+ }
1003
1059
}
1004
1060
}
1005
1061
return nil
@@ -1452,13 +1508,44 @@ resource "google_compute_instance" "foobar" {
1452
1508
` , disk , instance , autodelete )
1453
1509
}
1454
1510
1455
- func testAccComputeInstance_disks_encryption (disk , instance string ) string {
1511
+ func testAccComputeInstance_disks_encryption (bootEncryptionKey string , diskNameToEncryptionKey map [string ]* compute.CustomerEncryptionKey , instance string ) string {
1512
+ diskNames := []string {}
1513
+ for k , _ := range diskNameToEncryptionKey {
1514
+ diskNames = append (diskNames , k )
1515
+ }
1456
1516
return fmt .Sprintf (`
1457
1517
resource "google_compute_disk" "foobar" {
1458
1518
name = "%s"
1459
1519
size = 10
1460
1520
type = "pd-ssd"
1461
1521
zone = "us-central1-a"
1522
+
1523
+ disk_encryption_key_raw = "%s"
1524
+ }
1525
+
1526
+ resource "google_compute_disk" "foobar2" {
1527
+ name = "%s"
1528
+ size = 10
1529
+ type = "pd-ssd"
1530
+ zone = "us-central1-a"
1531
+
1532
+ disk_encryption_key_raw = "%s"
1533
+ }
1534
+
1535
+ resource "google_compute_disk" "foobar3" {
1536
+ name = "%s"
1537
+ size = 10
1538
+ type = "pd-ssd"
1539
+ zone = "us-central1-a"
1540
+
1541
+ disk_encryption_key_raw = "%s"
1542
+ }
1543
+
1544
+ resource "google_compute_disk" "foobar4" {
1545
+ name = "%s"
1546
+ size = 10
1547
+ type = "pd-ssd"
1548
+ zone = "us-central1-a"
1462
1549
}
1463
1550
1464
1551
resource "google_compute_instance" "foobar" {
@@ -1470,11 +1557,26 @@ resource "google_compute_instance" "foobar" {
1470
1557
initialize_params{
1471
1558
image = "debian-8-jessie-v20160803"
1472
1559
}
1473
- disk_encryption_key_raw = "SGVsbG8gZnJvbSBHb29nbGUgQ2xvdWQgUGxhdGZvcm0= "
1560
+ disk_encryption_key_raw = "%s "
1474
1561
}
1475
1562
1476
1563
disk {
1477
1564
disk = "${google_compute_disk.foobar.name}"
1565
+ disk_encryption_key_raw = "%s"
1566
+ }
1567
+
1568
+ attached_disk {
1569
+ source = "${google_compute_disk.foobar2.self_link}"
1570
+ disk_encryption_key_raw = "%s"
1571
+ }
1572
+
1573
+ attached_disk {
1574
+ source = "${google_compute_disk.foobar4.self_link}"
1575
+ }
1576
+
1577
+ attached_disk {
1578
+ source = "${google_compute_disk.foobar3.self_link}"
1579
+ disk_encryption_key_raw = "%s"
1478
1580
}
1479
1581
1480
1582
network_interface {
@@ -1485,7 +1587,12 @@ resource "google_compute_instance" "foobar" {
1485
1587
foo = "bar"
1486
1588
}
1487
1589
}
1488
- ` , disk , instance )
1590
+ ` , diskNames [0 ], diskNameToEncryptionKey [diskNames [0 ]].RawKey ,
1591
+ diskNames [1 ], diskNameToEncryptionKey [diskNames [1 ]].RawKey ,
1592
+ diskNames [2 ], diskNameToEncryptionKey [diskNames [2 ]].RawKey ,
1593
+ "instance-testd-" + acctest .RandString (10 ),
1594
+ instance , bootEncryptionKey ,
1595
+ diskNameToEncryptionKey [diskNames [0 ]].RawKey , diskNameToEncryptionKey [diskNames [1 ]].RawKey , diskNameToEncryptionKey [diskNames [2 ]].RawKey )
1489
1596
}
1490
1597
1491
1598
func testAccComputeInstance_attachedDisk (disk , instance string ) string {
0 commit comments