From 8d0374378f8cbbf0ac84ca7ce376a475cf5b404f Mon Sep 17 00:00:00 2001 From: MrLuje Date: Mon, 15 Aug 2022 21:35:48 +0200 Subject: [PATCH] fix(cluster): crash on empty namespace (#198) --- argocd/resource_argocd_cluster_test.go | 55 ++++++++++++++++++++++++++ argocd/structure_cluster.go | 5 +++ 2 files changed, 60 insertions(+) diff --git a/argocd/resource_argocd_cluster_test.go b/argocd/resource_argocd_cluster_test.go index 849db05a..1cce408d 100644 --- a/argocd/resource_argocd_cluster_test.go +++ b/argocd/resource_argocd_cluster_test.go @@ -370,6 +370,25 @@ func TestAccArgoCDCluster_uniqueByServerTrimmed(t *testing.T) { }) } +func TestAccArgoCDCluster_namespacesErrorWhenEmpty(t *testing.T) { + name := acctest.RandString(10) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t); testAccPreCheckFeatureSupported(t, featureProjectScopedClusters) }, + ProviderFactories: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccArgoCDClusterNamespacesContainsEmptyString(name), + ExpectError: regexp.MustCompile("namespaces: must contain non-empty strings"), + }, + { + Config: testAccArgoCDClusterNamespacesContainsEmptyString_MultipleItems(name), + ExpectError: regexp.MustCompile("namespaces: must contain non-empty strings"), + }, + }, + }) +} + func testAccArgoCDClusterBearerToken(clusterName string) string { return fmt.Sprintf(` resource "argocd_cluster" "simple" { @@ -610,6 +629,42 @@ resource "argocd_cluster" "cluster_metadata" { `, clusterName) } +func testAccArgoCDClusterNamespacesContainsEmptyString(clusterName string) string { + return fmt.Sprintf(` +resource "argocd_cluster" "simple" { + server = "https://kubernetes.default.svc.cluster.local" + name = "%s" + shard = "1" + namespaces = [""] + config { + # Uses Kind's bootstrap token whose ttl is 24 hours after cluster bootstrap. + bearer_token = "abcdef.0123456789abcdef" + tls_client_config { + insecure = true + } + } +} +`, clusterName) +} + +func testAccArgoCDClusterNamespacesContainsEmptyString_MultipleItems(clusterName string) string { + return fmt.Sprintf(` +resource "argocd_cluster" "simple" { + server = "https://kubernetes.default.svc.cluster.local" + name = "%s" + shard = "1" + namespaces = ["default", ""] + config { + # Uses Kind's bootstrap token whose ttl is 24 hours after cluster bootstrap. + bearer_token = "abcdef.0123456789abcdef" + tls_client_config { + insecure = true + } + } +} +`, clusterName) +} + // getInternalRestConfig returns the internal Kubernetes cluster REST config. func getInternalRestConfig() (*rest.Config, error) { rc := &rest.Config{} diff --git a/argocd/structure_cluster.go b/argocd/structure_cluster.go index 3c9878cb..e1ef9a6a 100644 --- a/argocd/structure_cluster.go +++ b/argocd/structure_cluster.go @@ -1,6 +1,8 @@ package argocd import ( + "fmt" + application "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) @@ -24,6 +26,9 @@ func expandCluster(d *schema.ResourceData) (*application.Cluster, error) { } if ns, ok := d.GetOk("namespaces"); ok { for _, n := range ns.([]interface{}) { + if n == nil { + return nil, fmt.Errorf("namespaces: must contain non-empty strings") + } cluster.Namespaces = append(cluster.Namespaces, n.(string)) } }