Skip to content

fix: app display name validation #344

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 13, 2025
Merged
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
13 changes: 13 additions & 0 deletions provider/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ var (
appSlugRegex = regexp.MustCompile(`^[a-z0-9](-?[a-z0-9])*$`)
)

const appDisplayNameMaxLength = 64 // database column limit

func appResource() *schema.Resource {
return &schema.Resource{
SchemaVersion: 1,
Expand Down Expand Up @@ -124,6 +126,17 @@ func appResource() *schema.Resource {
Description: "A display name to identify the app. Defaults to the slug.",
ForceNew: true,
Optional: true,
ValidateDiagFunc: func(val interface{}, c cty.Path) diag.Diagnostics {
valStr, ok := val.(string)
if !ok {
return diag.Errorf("expected string, got %T", val)
}

if len(valStr) > appDisplayNameMaxLength {
return diag.Errorf("display name is too long (max %d characters)", appDisplayNameMaxLength)
}
return nil
},
},
"subdomain": {
Type: schema.TypeBool,
Expand Down
62 changes: 62 additions & 0 deletions provider/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,4 +415,66 @@ func TestApp(t *testing.T) {
}
})

t.Run("DisplayName", func(t *testing.T) {
t.Parallel()

cases := []struct {
name string
displayName string
expectValue string
expectError *regexp.Regexp
}{
{
name: "Empty",
displayName: "",
},
{
name: "Regular",
displayName: "Regular Application",
},
{
name: "DisplayNameStillOK",
displayName: "0123456789012345678901234567890123456789012345678901234567890123",
},
{
name: "DisplayNameTooLong",
displayName: "01234567890123456789012345678901234567890123456789012345678901234",
expectError: regexp.MustCompile("display name is too long"),
},
}

for _, c := range cases {
c := c

t.Run(c.name, func(t *testing.T) {
t.Parallel()

config := fmt.Sprintf(`
provider "coder" {
}
resource "coder_agent" "dev" {
os = "linux"
arch = "amd64"
}
resource "coder_app" "code-server" {
agent_id = coder_agent.dev.id
slug = "code-server"
display_name = "%s"
url = "http://localhost:13337"
open_in = "slim-window"
}
`, c.displayName)

resource.Test(t, resource.TestCase{
ProviderFactories: coderFactory(),
IsUnitTest: true,
Steps: []resource.TestStep{{
Config: config,
ExpectError: c.expectError,
}},
})
})
}
})

}
Loading