From e08816b295e69c825e2abca9b8f47995fe9c6e7c Mon Sep 17 00:00:00 2001 From: Ramgopal Nagaboina Date: Wed, 2 Sep 2026 17:49:26 -0400 Subject: [PATCH 1/2] fix: prevent data source filter panics on unknown or non-string fields The data source filter helpers indexed the resource JSON map and asserted the result to string: [updatedName].(string). This panics when a filter name matches no field (the lookup returns nil) or when the field is not a string (numbers unmarshal to float64, booleans to bool). Format the value with fmt.Sprintf("%v", ...) instead, matching applyIpAddressFilters. Covers the instance, network_offering, physical_network, service_offering, ssh_keypair, template, user, volume, vpc, vpn_connection and zone data sources. Adds a unit test. Signed-off-by: Ramgopal Nagaboina --- cloudstack/data_source_cloudstack_instance.go | 2 +- ...data_source_cloudstack_network_offering.go | 2 +- ...data_source_cloudstack_physical_network.go | 2 +- ...data_source_cloudstack_service_offering.go | 2 +- .../data_source_cloudstack_ssh_keypair.go | 2 +- cloudstack/data_source_cloudstack_template.go | 2 +- cloudstack/data_source_cloudstack_user.go | 2 +- cloudstack/data_source_cloudstack_volume.go | 2 +- cloudstack/data_source_cloudstack_vpc.go | 2 +- .../data_source_cloudstack_vpn_connection.go | 2 +- cloudstack/data_source_cloudstack_zone.go | 2 +- .../data_source_filter_panic_unit_test.go | 54 +++++++++++++++++++ 12 files changed, 65 insertions(+), 11 deletions(-) create mode 100644 cloudstack/data_source_filter_panic_unit_test.go diff --git a/cloudstack/data_source_cloudstack_instance.go b/cloudstack/data_source_cloudstack_instance.go index a6017225..1da44e07 100644 --- a/cloudstack/data_source_cloudstack_instance.go +++ b/cloudstack/data_source_cloudstack_instance.go @@ -207,7 +207,7 @@ func applyInstanceFilters(instance *cloudstack.VirtualMachine, filters *schema.S return false, fmt.Errorf("Invalid regex: %s", err) } updatedName := strings.ReplaceAll(m["name"].(string), "_", "") - instanceField := instanceJSON[updatedName].(string) + instanceField := fmt.Sprintf("%v", instanceJSON[updatedName]) if !r.MatchString(instanceField) { return false, nil } diff --git a/cloudstack/data_source_cloudstack_network_offering.go b/cloudstack/data_source_cloudstack_network_offering.go index fe82ebe3..441937fe 100644 --- a/cloudstack/data_source_cloudstack_network_offering.go +++ b/cloudstack/data_source_cloudstack_network_offering.go @@ -245,7 +245,7 @@ func applyNetworkOfferingFilters(networkOffering *cloudstack.NetworkOffering, fi return false, fmt.Errorf("Invalid regex: %s", err) } updatedName := strings.ReplaceAll(m["name"].(string), "_", "") - networkOfferingField := networkOfferingJSON[updatedName].(string) + networkOfferingField := fmt.Sprintf("%v", networkOfferingJSON[updatedName]) if !r.MatchString(networkOfferingField) { return false, nil } diff --git a/cloudstack/data_source_cloudstack_physical_network.go b/cloudstack/data_source_cloudstack_physical_network.go index 6a436051..bf7090f0 100644 --- a/cloudstack/data_source_cloudstack_physical_network.go +++ b/cloudstack/data_source_cloudstack_physical_network.go @@ -131,7 +131,7 @@ func applyPhysicalNetworkFilters(physicalNetwork *cloudstack.PhysicalNetwork, fi return false, fmt.Errorf("Invalid regex: %s", err) } updatedName := strings.ReplaceAll(m["name"].(string), "_", "") - physicalNetworkField := physicalNetworkJSON[updatedName].(string) + physicalNetworkField := fmt.Sprintf("%v", physicalNetworkJSON[updatedName]) if !r.MatchString(physicalNetworkField) { return false, nil } diff --git a/cloudstack/data_source_cloudstack_service_offering.go b/cloudstack/data_source_cloudstack_service_offering.go index 8c1272a3..507a40a9 100644 --- a/cloudstack/data_source_cloudstack_service_offering.go +++ b/cloudstack/data_source_cloudstack_service_offering.go @@ -128,7 +128,7 @@ func applyServiceOfferingFilters(serviceOffering *cloudstack.ServiceOffering, fi return false, fmt.Errorf("Invalid regex: %s", err) } updatedName := strings.ReplaceAll(m["name"].(string), "_", "") - serviceOfferingField := serviceOfferingJSON[updatedName].(string) + serviceOfferingField := fmt.Sprintf("%v", serviceOfferingJSON[updatedName]) if !r.MatchString(serviceOfferingField) { return false, nil } diff --git a/cloudstack/data_source_cloudstack_ssh_keypair.go b/cloudstack/data_source_cloudstack_ssh_keypair.go index 1cb2734f..f67e49b0 100644 --- a/cloudstack/data_source_cloudstack_ssh_keypair.go +++ b/cloudstack/data_source_cloudstack_ssh_keypair.go @@ -103,7 +103,7 @@ func applySshKeyPairsFilters(sshKeyPair *cloudstack.SSHKeyPair, filters *schema. return false, fmt.Errorf("Invalid regex: %s", err) } updatedName := strings.ReplaceAll(m["name"].(string), "_", "") - sshKeyPairField := sshKeyPairJSON[updatedName].(string) + sshKeyPairField := fmt.Sprintf("%v", sshKeyPairJSON[updatedName]) if !r.MatchString(sshKeyPairField) { return false, nil } diff --git a/cloudstack/data_source_cloudstack_template.go b/cloudstack/data_source_cloudstack_template.go index 5040f9bb..f5bef526 100644 --- a/cloudstack/data_source_cloudstack_template.go +++ b/cloudstack/data_source_cloudstack_template.go @@ -182,7 +182,7 @@ func applyFilters(template *cloudstack.Template, filters *schema.Set) (bool, err return false, fmt.Errorf("Invalid regex: %s", err) } updatedName := strings.ReplaceAll(m["name"].(string), "_", "") - templateField := templateJSON[updatedName].(string) + templateField := fmt.Sprintf("%v", templateJSON[updatedName]) if !r.MatchString(templateField) { return false, nil } diff --git a/cloudstack/data_source_cloudstack_user.go b/cloudstack/data_source_cloudstack_user.go index 23516f1c..11e557a9 100644 --- a/cloudstack/data_source_cloudstack_user.go +++ b/cloudstack/data_source_cloudstack_user.go @@ -145,7 +145,7 @@ func applyUserFilters(user *cloudstack.User, filters *schema.Set) (bool, error) } updatedName := strings.ReplaceAll(m["name"].(string), "_", "") log.Print(updatedName) - userField := userJSON[updatedName].(string) + userField := fmt.Sprintf("%v", userJSON[updatedName]) if !r.MatchString(userField) { return false, nil } diff --git a/cloudstack/data_source_cloudstack_volume.go b/cloudstack/data_source_cloudstack_volume.go index 2e548eb0..90a3b0c9 100644 --- a/cloudstack/data_source_cloudstack_volume.go +++ b/cloudstack/data_source_cloudstack_volume.go @@ -133,7 +133,7 @@ func applyVolumeFilters(volume *cloudstack.Volume, filters *schema.Set) (bool, e return false, fmt.Errorf("Invalid regex: %s", err) } updatedName := strings.ReplaceAll(m["name"].(string), "_", "") - volume := volumeJSON[updatedName].(string) + volume := fmt.Sprintf("%v", volumeJSON[updatedName]) if !r.MatchString(volume) { return false, nil } diff --git a/cloudstack/data_source_cloudstack_vpc.go b/cloudstack/data_source_cloudstack_vpc.go index 499ef373..f282ff94 100644 --- a/cloudstack/data_source_cloudstack_vpc.go +++ b/cloudstack/data_source_cloudstack_vpc.go @@ -171,7 +171,7 @@ func applyVPCFilters(vpc *cloudstack.VPC, filters *schema.Set) (bool, error) { } updatedName := strings.ReplaceAll(m["name"].(string), "_", "") log.Print(updatedName) - vpcField := vpcJSON[updatedName].(string) + vpcField := fmt.Sprintf("%v", vpcJSON[updatedName]) if !r.MatchString(vpcField) { return false, nil } diff --git a/cloudstack/data_source_cloudstack_vpn_connection.go b/cloudstack/data_source_cloudstack_vpn_connection.go index 90bbeedc..4a21368e 100644 --- a/cloudstack/data_source_cloudstack_vpn_connection.go +++ b/cloudstack/data_source_cloudstack_vpn_connection.go @@ -131,7 +131,7 @@ func applyVPNConnectionFilters(vpnConnection *cloudstack.VpnConnection, filters } updatedName := strings.ReplaceAll(m["name"].(string), "_", "") log.Print(updatedName) - vpnConnectionField := vpnConnectionJSON[updatedName].(string) + vpnConnectionField := fmt.Sprintf("%v", vpnConnectionJSON[updatedName]) if !r.MatchString(vpnConnectionField) { return false, nil } diff --git a/cloudstack/data_source_cloudstack_zone.go b/cloudstack/data_source_cloudstack_zone.go index 9fb211b2..f5a38503 100644 --- a/cloudstack/data_source_cloudstack_zone.go +++ b/cloudstack/data_source_cloudstack_zone.go @@ -111,7 +111,7 @@ func applyZoneFilters(zone *cloudstack.Zone, filters *schema.Set) (bool, error) return false, fmt.Errorf("Invalid regex: %s", err) } updatedName := strings.ReplaceAll(m["name"].(string), "_", "") - zoneField := zoneJSON[updatedName].(string) + zoneField := fmt.Sprintf("%v", zoneJSON[updatedName]) if !r.MatchString(zoneField) { return false, nil } diff --git a/cloudstack/data_source_filter_panic_unit_test.go b/cloudstack/data_source_filter_panic_unit_test.go new file mode 100644 index 00000000..aa535f44 --- /dev/null +++ b/cloudstack/data_source_filter_panic_unit_test.go @@ -0,0 +1,54 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package cloudstack + +import ( + "testing" + + "github.com/apache/cloudstack-go/v2/cloudstack" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func dsPanicFilterSet(pairs ...[2]string) *schema.Set { + hash := func(i interface{}) int { + m := i.(map[string]interface{}) + return schema.HashString(m["name"].(string) + "|" + m["value"].(string)) + } + items := make([]interface{}, 0, len(pairs)) + for _, p := range pairs { + items = append(items, map[string]interface{}{"name": p[0], "value": p[1]}) + } + return schema.NewSet(hash, items) +} + +// A data-source filter referencing an unknown field name, or a non-string field, must not panic. +func TestApplyVolumeFiltersDoesNotPanicOnUnknownOrNonStringField(t *testing.T) { + vol := &cloudstack.Volume{Name: "vol-a", Size: 5368709120} + + // Unknown filter field: the JSON lookup returns nil; must not panic and must not match. + if match, err := applyVolumeFilters(vol, dsPanicFilterSet([2]string{"no_such_field", "x"})); err != nil { + t.Fatalf("unexpected error: %s", err) + } else if match { + t.Errorf("an unknown filter field should not match") + } + + // Non-string (numeric) field: the JSON value is a float64; must not panic. + if _, err := applyVolumeFilters(vol, dsPanicFilterSet([2]string{"size", "anything"})); err != nil { + t.Fatalf("unexpected error: %s", err) + } +} From d5f1178e6cb8d45960b26f3c1f127ce79fa4c358 Mon Sep 17 00:00:00 2001 From: Ramgopal Nagaboina Date: Mon, 7 Sep 2026 14:59:00 -0400 Subject: [PATCH 2/2] fix: return no match for missing or nil data source filter fields fmt.Sprintf("%v", nil) yields the literal "", which a permissive regex such as ".*" would match, so a filter on a field that does not exist could match. Look the field up explicitly and return no match when it is absent or nil, in every data source filter (including ipaddress and vpc_offering, which already used fmt.Sprintf). Extend the unit test to cover a permissive regex on a missing field and a positive match. --- cloudstack/data_source_cloudstack_instance.go | 6 ++++- .../data_source_cloudstack_ipaddress.go | 6 ++++- ...data_source_cloudstack_network_offering.go | 6 ++++- ...data_source_cloudstack_physical_network.go | 6 ++++- ...data_source_cloudstack_service_offering.go | 6 ++++- .../data_source_cloudstack_ssh_keypair.go | 6 ++++- cloudstack/data_source_cloudstack_template.go | 6 ++++- cloudstack/data_source_cloudstack_user.go | 6 ++++- cloudstack/data_source_cloudstack_volume.go | 6 ++++- cloudstack/data_source_cloudstack_vpc.go | 6 ++++- .../data_source_cloudstack_vpc_offering.go | 6 ++++- .../data_source_cloudstack_vpn_connection.go | 6 ++++- cloudstack/data_source_cloudstack_zone.go | 6 ++++- .../data_source_filter_panic_unit_test.go | 22 ++++++++++++++++--- 14 files changed, 84 insertions(+), 16 deletions(-) diff --git a/cloudstack/data_source_cloudstack_instance.go b/cloudstack/data_source_cloudstack_instance.go index 1da44e07..8073e2d0 100644 --- a/cloudstack/data_source_cloudstack_instance.go +++ b/cloudstack/data_source_cloudstack_instance.go @@ -207,7 +207,11 @@ func applyInstanceFilters(instance *cloudstack.VirtualMachine, filters *schema.S return false, fmt.Errorf("Invalid regex: %s", err) } updatedName := strings.ReplaceAll(m["name"].(string), "_", "") - instanceField := fmt.Sprintf("%v", instanceJSON[updatedName]) + instanceFieldValue, instanceFieldFound := instanceJSON[updatedName] + if !instanceFieldFound || instanceFieldValue == nil { + return false, nil + } + instanceField := fmt.Sprintf("%v", instanceFieldValue) if !r.MatchString(instanceField) { return false, nil } diff --git a/cloudstack/data_source_cloudstack_ipaddress.go b/cloudstack/data_source_cloudstack_ipaddress.go index 997a3a26..367d62c2 100644 --- a/cloudstack/data_source_cloudstack_ipaddress.go +++ b/cloudstack/data_source_cloudstack_ipaddress.go @@ -165,7 +165,11 @@ func applyIPAddressFilters(publicIpAddress *cloudstack.PublicIpAddress, filters return false, fmt.Errorf("Invalid regex: %s", err) } updatedName := strings.ReplaceAll(m["name"].(string), "_", "") - publicIPAdressField := fmt.Sprintf("%v", publicIPAdressJSON[updatedName]) + publicIPAdressFieldValue, publicIPAdressFieldFound := publicIPAdressJSON[updatedName] + if !publicIPAdressFieldFound || publicIPAdressFieldValue == nil { + return false, nil + } + publicIPAdressField := fmt.Sprintf("%v", publicIPAdressFieldValue) if !r.MatchString(publicIPAdressField) { return false, nil } diff --git a/cloudstack/data_source_cloudstack_network_offering.go b/cloudstack/data_source_cloudstack_network_offering.go index 441937fe..b68cce37 100644 --- a/cloudstack/data_source_cloudstack_network_offering.go +++ b/cloudstack/data_source_cloudstack_network_offering.go @@ -245,7 +245,11 @@ func applyNetworkOfferingFilters(networkOffering *cloudstack.NetworkOffering, fi return false, fmt.Errorf("Invalid regex: %s", err) } updatedName := strings.ReplaceAll(m["name"].(string), "_", "") - networkOfferingField := fmt.Sprintf("%v", networkOfferingJSON[updatedName]) + networkOfferingFieldValue, networkOfferingFieldFound := networkOfferingJSON[updatedName] + if !networkOfferingFieldFound || networkOfferingFieldValue == nil { + return false, nil + } + networkOfferingField := fmt.Sprintf("%v", networkOfferingFieldValue) if !r.MatchString(networkOfferingField) { return false, nil } diff --git a/cloudstack/data_source_cloudstack_physical_network.go b/cloudstack/data_source_cloudstack_physical_network.go index bf7090f0..7e37644f 100644 --- a/cloudstack/data_source_cloudstack_physical_network.go +++ b/cloudstack/data_source_cloudstack_physical_network.go @@ -131,7 +131,11 @@ func applyPhysicalNetworkFilters(physicalNetwork *cloudstack.PhysicalNetwork, fi return false, fmt.Errorf("Invalid regex: %s", err) } updatedName := strings.ReplaceAll(m["name"].(string), "_", "") - physicalNetworkField := fmt.Sprintf("%v", physicalNetworkJSON[updatedName]) + physicalNetworkFieldValue, physicalNetworkFieldFound := physicalNetworkJSON[updatedName] + if !physicalNetworkFieldFound || physicalNetworkFieldValue == nil { + return false, nil + } + physicalNetworkField := fmt.Sprintf("%v", physicalNetworkFieldValue) if !r.MatchString(physicalNetworkField) { return false, nil } diff --git a/cloudstack/data_source_cloudstack_service_offering.go b/cloudstack/data_source_cloudstack_service_offering.go index 507a40a9..3ca2ad26 100644 --- a/cloudstack/data_source_cloudstack_service_offering.go +++ b/cloudstack/data_source_cloudstack_service_offering.go @@ -128,7 +128,11 @@ func applyServiceOfferingFilters(serviceOffering *cloudstack.ServiceOffering, fi return false, fmt.Errorf("Invalid regex: %s", err) } updatedName := strings.ReplaceAll(m["name"].(string), "_", "") - serviceOfferingField := fmt.Sprintf("%v", serviceOfferingJSON[updatedName]) + serviceOfferingFieldValue, serviceOfferingFieldFound := serviceOfferingJSON[updatedName] + if !serviceOfferingFieldFound || serviceOfferingFieldValue == nil { + return false, nil + } + serviceOfferingField := fmt.Sprintf("%v", serviceOfferingFieldValue) if !r.MatchString(serviceOfferingField) { return false, nil } diff --git a/cloudstack/data_source_cloudstack_ssh_keypair.go b/cloudstack/data_source_cloudstack_ssh_keypair.go index f67e49b0..5cfc8d86 100644 --- a/cloudstack/data_source_cloudstack_ssh_keypair.go +++ b/cloudstack/data_source_cloudstack_ssh_keypair.go @@ -103,7 +103,11 @@ func applySshKeyPairsFilters(sshKeyPair *cloudstack.SSHKeyPair, filters *schema. return false, fmt.Errorf("Invalid regex: %s", err) } updatedName := strings.ReplaceAll(m["name"].(string), "_", "") - sshKeyPairField := fmt.Sprintf("%v", sshKeyPairJSON[updatedName]) + sshKeyPairFieldValue, sshKeyPairFieldFound := sshKeyPairJSON[updatedName] + if !sshKeyPairFieldFound || sshKeyPairFieldValue == nil { + return false, nil + } + sshKeyPairField := fmt.Sprintf("%v", sshKeyPairFieldValue) if !r.MatchString(sshKeyPairField) { return false, nil } diff --git a/cloudstack/data_source_cloudstack_template.go b/cloudstack/data_source_cloudstack_template.go index f5bef526..48183f56 100644 --- a/cloudstack/data_source_cloudstack_template.go +++ b/cloudstack/data_source_cloudstack_template.go @@ -182,7 +182,11 @@ func applyFilters(template *cloudstack.Template, filters *schema.Set) (bool, err return false, fmt.Errorf("Invalid regex: %s", err) } updatedName := strings.ReplaceAll(m["name"].(string), "_", "") - templateField := fmt.Sprintf("%v", templateJSON[updatedName]) + templateFieldValue, templateFieldFound := templateJSON[updatedName] + if !templateFieldFound || templateFieldValue == nil { + return false, nil + } + templateField := fmt.Sprintf("%v", templateFieldValue) if !r.MatchString(templateField) { return false, nil } diff --git a/cloudstack/data_source_cloudstack_user.go b/cloudstack/data_source_cloudstack_user.go index 11e557a9..8a2285ac 100644 --- a/cloudstack/data_source_cloudstack_user.go +++ b/cloudstack/data_source_cloudstack_user.go @@ -145,7 +145,11 @@ func applyUserFilters(user *cloudstack.User, filters *schema.Set) (bool, error) } updatedName := strings.ReplaceAll(m["name"].(string), "_", "") log.Print(updatedName) - userField := fmt.Sprintf("%v", userJSON[updatedName]) + userFieldValue, userFieldFound := userJSON[updatedName] + if !userFieldFound || userFieldValue == nil { + return false, nil + } + userField := fmt.Sprintf("%v", userFieldValue) if !r.MatchString(userField) { return false, nil } diff --git a/cloudstack/data_source_cloudstack_volume.go b/cloudstack/data_source_cloudstack_volume.go index 90a3b0c9..09bb5892 100644 --- a/cloudstack/data_source_cloudstack_volume.go +++ b/cloudstack/data_source_cloudstack_volume.go @@ -133,7 +133,11 @@ func applyVolumeFilters(volume *cloudstack.Volume, filters *schema.Set) (bool, e return false, fmt.Errorf("Invalid regex: %s", err) } updatedName := strings.ReplaceAll(m["name"].(string), "_", "") - volume := fmt.Sprintf("%v", volumeJSON[updatedName]) + volumeValue, volumeFound := volumeJSON[updatedName] + if !volumeFound || volumeValue == nil { + return false, nil + } + volume := fmt.Sprintf("%v", volumeValue) if !r.MatchString(volume) { return false, nil } diff --git a/cloudstack/data_source_cloudstack_vpc.go b/cloudstack/data_source_cloudstack_vpc.go index f282ff94..92a4513e 100644 --- a/cloudstack/data_source_cloudstack_vpc.go +++ b/cloudstack/data_source_cloudstack_vpc.go @@ -171,7 +171,11 @@ func applyVPCFilters(vpc *cloudstack.VPC, filters *schema.Set) (bool, error) { } updatedName := strings.ReplaceAll(m["name"].(string), "_", "") log.Print(updatedName) - vpcField := fmt.Sprintf("%v", vpcJSON[updatedName]) + vpcFieldValue, vpcFieldFound := vpcJSON[updatedName] + if !vpcFieldFound || vpcFieldValue == nil { + return false, nil + } + vpcField := fmt.Sprintf("%v", vpcFieldValue) if !r.MatchString(vpcField) { return false, nil } diff --git a/cloudstack/data_source_cloudstack_vpc_offering.go b/cloudstack/data_source_cloudstack_vpc_offering.go index d97d0c53..75834a9d 100644 --- a/cloudstack/data_source_cloudstack_vpc_offering.go +++ b/cloudstack/data_source_cloudstack_vpc_offering.go @@ -220,7 +220,11 @@ func applyVPCOfferingFilters(vpcOffering *cloudstack.VPCOffering, filters *schem return false, fmt.Errorf("Invalid regex: %s", err) } updatedName := strings.ReplaceAll(m["name"].(string), "_", "") - vpcOfferingField := fmt.Sprintf("%v", vpcOfferingJSON[updatedName]) + vpcOfferingFieldValue, vpcOfferingFieldFound := vpcOfferingJSON[updatedName] + if !vpcOfferingFieldFound || vpcOfferingFieldValue == nil { + return false, nil + } + vpcOfferingField := fmt.Sprintf("%v", vpcOfferingFieldValue) if !r.MatchString(vpcOfferingField) { return false, nil } diff --git a/cloudstack/data_source_cloudstack_vpn_connection.go b/cloudstack/data_source_cloudstack_vpn_connection.go index 4a21368e..5062746c 100644 --- a/cloudstack/data_source_cloudstack_vpn_connection.go +++ b/cloudstack/data_source_cloudstack_vpn_connection.go @@ -131,7 +131,11 @@ func applyVPNConnectionFilters(vpnConnection *cloudstack.VpnConnection, filters } updatedName := strings.ReplaceAll(m["name"].(string), "_", "") log.Print(updatedName) - vpnConnectionField := fmt.Sprintf("%v", vpnConnectionJSON[updatedName]) + vpnConnectionFieldValue, vpnConnectionFieldFound := vpnConnectionJSON[updatedName] + if !vpnConnectionFieldFound || vpnConnectionFieldValue == nil { + return false, nil + } + vpnConnectionField := fmt.Sprintf("%v", vpnConnectionFieldValue) if !r.MatchString(vpnConnectionField) { return false, nil } diff --git a/cloudstack/data_source_cloudstack_zone.go b/cloudstack/data_source_cloudstack_zone.go index f5a38503..5bdb320a 100644 --- a/cloudstack/data_source_cloudstack_zone.go +++ b/cloudstack/data_source_cloudstack_zone.go @@ -111,7 +111,11 @@ func applyZoneFilters(zone *cloudstack.Zone, filters *schema.Set) (bool, error) return false, fmt.Errorf("Invalid regex: %s", err) } updatedName := strings.ReplaceAll(m["name"].(string), "_", "") - zoneField := fmt.Sprintf("%v", zoneJSON[updatedName]) + zoneFieldValue, zoneFieldFound := zoneJSON[updatedName] + if !zoneFieldFound || zoneFieldValue == nil { + return false, nil + } + zoneField := fmt.Sprintf("%v", zoneFieldValue) if !r.MatchString(zoneField) { return false, nil } diff --git a/cloudstack/data_source_filter_panic_unit_test.go b/cloudstack/data_source_filter_panic_unit_test.go index aa535f44..fcc465ef 100644 --- a/cloudstack/data_source_filter_panic_unit_test.go +++ b/cloudstack/data_source_filter_panic_unit_test.go @@ -38,7 +38,7 @@ func dsPanicFilterSet(pairs ...[2]string) *schema.Set { // A data-source filter referencing an unknown field name, or a non-string field, must not panic. func TestApplyVolumeFiltersDoesNotPanicOnUnknownOrNonStringField(t *testing.T) { - vol := &cloudstack.Volume{Name: "vol-a", Size: 5368709120} + vol := &cloudstack.Volume{Name: "vol-a", Size: 5} // Unknown filter field: the JSON lookup returns nil; must not panic and must not match. if match, err := applyVolumeFilters(vol, dsPanicFilterSet([2]string{"no_such_field", "x"})); err != nil { @@ -47,8 +47,24 @@ func TestApplyVolumeFiltersDoesNotPanicOnUnknownOrNonStringField(t *testing.T) { t.Errorf("an unknown filter field should not match") } - // Non-string (numeric) field: the JSON value is a float64; must not panic. - if _, err := applyVolumeFilters(vol, dsPanicFilterSet([2]string{"size", "anything"})); err != nil { + // A permissive regex must not match a field that does not exist. + if match, err := applyVolumeFilters(vol, dsPanicFilterSet([2]string{"no_such_field", ".*"})); err != nil { t.Fatalf("unexpected error: %s", err) + } else if match { + t.Errorf("a nonexistent field should not match even a permissive regex") + } + + // A numeric field is stringified and matched against the regex. + if match, err := applyVolumeFilters(vol, dsPanicFilterSet([2]string{"size", "^5$"})); err != nil { + t.Fatalf("unexpected error: %s", err) + } else if !match { + t.Errorf("a numeric field should match its string representation") + } + + // An existing field must still match. + if match, err := applyVolumeFilters(vol, dsPanicFilterSet([2]string{"name", "vol-a"})); err != nil { + t.Fatalf("unexpected error: %s", err) + } else if !match { + t.Errorf("an existing field that matches the regex should match") } }