Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/1622.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
`helm_release`: Include validation of `name` during plan phase to catch invalid Helm release names early
```
17 changes: 17 additions & 0 deletions helm/resource_helm_release.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"net/url"
"os"
pathpkg "path"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -1665,6 +1666,8 @@ func checkChartDependencies(ctx context.Context, model *HelmReleaseModel, c *cha
return false, diags
}

var helmReleaseNameRegexp = regexp.MustCompile(`^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$`)

func (r *HelmRelease) ModifyPlan(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) {
if req.Plan.Raw.IsNull() {
// resource is being destroyed
Expand All @@ -1675,6 +1678,20 @@ func (r *HelmRelease) ModifyPlan(ctx context.Context, req resource.ModifyPlanReq
if resp.Diagnostics.HasError() {
return
}
if !plan.Name.IsNull() {
name := plan.Name.ValueString()
if !helmReleaseNameRegexp.MatchString(name) {
resp.Diagnostics.AddError(
"Invalid Helm Release Name",
fmt.Sprintf(
"Release name %q is invalid. Must match regex %s",
name,
helmReleaseNameRegexp.String(),
),
)
return
}
}
var state *HelmReleaseModel
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
Expand Down
61 changes: 34 additions & 27 deletions helm/resource_helm_release_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -767,33 +767,6 @@ func TestAccResourceRelease_namespaceDoesNotExist(t *testing.T) {
},
})
}

func TestAccResourceRelease_invalidName(t *testing.T) {
namespace := createRandomNamespace(t)
defer deleteNamespace(t, namespace)

broken := fmt.Sprintf(`
resource "helm_release" "test" {
name = "1nva&lidname$"
namespace = %q
repository = %q
chart = "test-chart"
}`, namespace, testRepositoryURL)

resource.Test(t, resource.TestCase{
// PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: protoV6ProviderFactories(),
// CheckDestroy: testAccCheckHelmReleaseDestroy(namespace),
Steps: []resource.TestStep{
{
Config: broken,
ExpectError: regexp.MustCompile("invalid release name"),
ExpectNonEmptyPlan: true,
},
},
})
}

func TestAccResourceRelease_createNamespace(t *testing.T) {
name := randName("create-namespace")
namespace := randName("helm-created-namespace")
Expand Down Expand Up @@ -823,6 +796,29 @@ func TestAccResourceRelease_createNamespace(t *testing.T) {
},
})
}
func TestAccResourceRelease_planValidationInvalidName(t *testing.T) {
invalidNames := []string{
"invalid_helm_release_name",
"ThisHelmRelease",
}
namespace := createRandomNamespace(t)
defer deleteNamespace(t, namespace)

for index, name := range invalidNames {
t.Run(fmt.Sprintf("invalid_name_%d", index), func(t *testing.T) {
resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: protoV6ProviderFactories(),
Steps: []resource.TestStep{
{
Config: testAccHelmReleaseConfigInvalidNamePlan(testResourceName, namespace, name),
PlanOnly: true,
ExpectError: regexp.MustCompile(`Invalid Helm Release Name`),
},
},
})
})
}
}

func TestAccResourceRelease_LocalVersion(t *testing.T) {
// NOTE this test confirms that the user is warned if their configured
Expand Down Expand Up @@ -894,6 +890,17 @@ func testAccHelmReleaseConfigBasic(resource, ns, name, version string) string {
`, resource, name, ns, testRepositoryURL, version)
}

func testAccHelmReleaseConfigInvalidNamePlan(resource, ns, name string) string {
return fmt.Sprintf(`
resource "helm_release" "%s" {
name = %q
namespace = %q
repository = %q
chart = "test-chart"
}
`, resource, name, ns, testRepositoryURL)
}

func testAccHelmReleaseConfig_set_wo(resource, ns, name, version string) string {
return fmt.Sprintf(`
resource "helm_release" "%s" {
Expand Down
Loading