Skip to content

Commit

Permalink
fix(server): panic when passing empty string as ssh key (#736)
Browse files Browse the repository at this point in the history
  • Loading branch information
apricote committed Aug 14, 2023
1 parent d51ee4a commit d57b386
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
31 changes: 31 additions & 0 deletions internal/e2etests/server/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -916,6 +916,37 @@ func TestServerResource_Protection(t *testing.T) {
})
}

func TestServerResource_EmptySSHKey(t *testing.T) {
// Regression Test for https://github.com/hetznercloud/terraform-provider-hcloud/issues/727
var srv hcloud.Server

srvRes := &server.RData{
Name: "server-empty-ssh-key",
Type: e2etests.TestServerType,
Image: e2etests.TestImage,
SSHKeys: []string{"\"\""},
}
srvRes.SetRName("server-empty-ssh-key")

tmplMan := testtemplate.Manager{}

resource.ParallelTest(t, resource.TestCase{
PreCheck: e2etests.PreCheck(t),
ProviderFactories: e2etests.ProviderFactories(),
CheckDestroy: testsupport.CheckResourcesDestroyed(server.ResourceType, server.ByID(t, &srv)),
Steps: []resource.TestStep{
{
// Create a new Server using the required values
// only.
Config: tmplMan.Render(t,
"testdata/r/hcloud_server", srvRes,
),
ExpectError: regexp.MustCompile("Invalid ssh key passed"),
},
},
})
}

func isRecreated(new, old *hcloud.Server) func() error {
return func() error {
if new.ID == old.ID {
Expand Down
10 changes: 9 additions & 1 deletion internal/server/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,15 @@ func Resource() *schema.Resource {
"ssh_keys": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Elem: &schema.Schema{Type: schema.TypeString,
ValidateDiagFunc: func(i interface{}, _ cty.Path) diag.Diagnostics {
s := i.(string)
if len(s) == 0 {
return diag.Errorf("Invalid ssh key passed. You need to pass a string with at least 1 character")
}

return nil
}},
ForceNew: true,
},
"keep_disk": {
Expand Down

0 comments on commit d57b386

Please sign in to comment.