Skip to content
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

Fix panic when reading unconfigured PKI mount URLs #641

Merged
merged 2 commits into from
Jan 7, 2020
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
4 changes: 0 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,14 @@ go 1.12

require (
github.com/aws/aws-sdk-go v1.22.0
github.com/go-sql-driver/mysql v1.4.1
github.com/google/btree v1.0.0 // indirect
github.com/gosimple/slug v1.4.1
github.com/hashicorp/go-cleanhttp v0.5.1
github.com/hashicorp/go-hclog v0.9.2
github.com/hashicorp/go-multierror v1.0.0
github.com/hashicorp/terraform-plugin-sdk v1.1.1
github.com/hashicorp/vault v1.2.0
github.com/hashicorp/vault/api v1.0.5-0.20190730042357-746c0b111519
github.com/hashicorp/vault/sdk v0.1.14-0.20190730042320-0dc007d98cc8
github.com/mitchellh/go-homedir v1.1.0
github.com/ory/dockertest v3.3.4+incompatible
github.com/rainycape/unidecode v0.0.0-20150907023854-cb7f23ec59be // indirect
github.com/ulikunitz/xz v0.5.6 // indirect
)
Expand Down
8 changes: 6 additions & 2 deletions vault/resource_pki_secret_backend_config_urls.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,13 @@ func pkiSecretBackendConfigUrlsRead(d *schema.ResourceData, meta interface{}) er
config, err := client.Logical().Read(path)

if err != nil {
log.Printf("[WARN] Removing path %q its ID is invalid", path)
return fmt.Errorf("error reading URL config on PKI secret backend %q: %s", path, err)
}

if config == nil {
log.Printf("[WARN] Removing URL config path %q as its ID is invalid", path)
d.SetId("")
return fmt.Errorf("invalid path ID %q: %s", path, err)
return nil
}

d.Set("issuing_certificates", config.Data["issuing_certificates"])
Expand Down
53 changes: 50 additions & 3 deletions vault/resource_pki_secret_backend_config_urls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
"github.com/hashicorp/vault/api"
)
Expand All @@ -24,6 +25,11 @@ func TestPkiSecretBackendConfigUrls_basic(t *testing.T) {
PreCheck: func() { testAccPreCheck(t) },
CheckDestroy: testPkiSecretBackendConfigUrlsDestroy,
Steps: []resource.TestStep{
{
// Test that reading from an unconfigured mount succeeds
Config: testPkiSecretBackendCertConfigUrlsConfig_rootOnly(rootPath),
Check: testPkiSecretBackendConfigUrlsEmptyRead,
},
{
Config: testPkiSecretBackendCertConfigUrlsConfig_basic(rootPath, issuingCertificates, crlDistributionPoints, ocspServers),
Check: resource.ComposeTestCheckFunc(
Expand All @@ -36,12 +42,41 @@ func TestPkiSecretBackendConfigUrls_basic(t *testing.T) {
})
}

func testPkiSecretBackendConfigUrlsEmptyRead(s *terraform.State) error {
paths, err := listPkiPaths(s)
if err != nil {
return err
}
for _, path := range paths {
d := &schema.ResourceData{}
d.SetId(path)
if err := pkiSecretBackendConfigUrlsRead(d, testProvider.Meta()); err != nil {
return err
}
}
return nil
}

func testPkiSecretBackendConfigUrlsDestroy(s *terraform.State) error {
paths, err := listPkiPaths(s)
if err != nil {
return err
}
for _, path := range paths {
return fmt.Errorf("mount %q still exists", path)
}

return nil
}

func listPkiPaths(s *terraform.State) ([]string, error) {
var paths []string

client := testProvider.Meta().(*api.Client)

mounts, err := client.Sys().ListMounts()
if err != nil {
return err
return nil, err
}

for _, rs := range s.RootModule().Resources {
Expand All @@ -52,11 +87,23 @@ func testPkiSecretBackendConfigUrlsDestroy(s *terraform.State) error {
path = strings.Trim(path, "/")
rsPath := strings.Trim(rs.Primary.Attributes["path"], "/")
if mount.Type == "pki" && path == rsPath {
return fmt.Errorf("mount %q still exists", path)
paths = append(paths, path)
}
}
}
return nil

return paths, nil
}

func testPkiSecretBackendCertConfigUrlsConfig_rootOnly(rootPath string) string {
return fmt.Sprintf(`
resource "vault_pki_secret_backend" "test-root" {
path = "%s"
description = "test root"
default_lease_ttl_seconds = "8640000"
max_lease_ttl_seconds = "8640000"
}
`, rootPath)
}

func testPkiSecretBackendCertConfigUrlsConfig_basic(rootPath string, issuingCertificates string, crlDistributionPoints string, ocspServers string) string {
Expand Down