From 409815315cf9b7a4a29b67e99fa1414307b61b75 Mon Sep 17 00:00:00 2001 From: Alexander Hellbom Date: Sat, 18 Mar 2017 17:27:00 +0100 Subject: [PATCH 1/8] Validate credentials --- builtin/providers/pagerduty/config.go | 22 ++++++++++++++++++++++ builtin/providers/pagerduty/errors.go | 4 ++++ 2 files changed, 26 insertions(+) diff --git a/builtin/providers/pagerduty/config.go b/builtin/providers/pagerduty/config.go index 00f55e71cba5..f88ed1c22908 100644 --- a/builtin/providers/pagerduty/config.go +++ b/builtin/providers/pagerduty/config.go @@ -1,6 +1,7 @@ package pagerduty import ( + "fmt" "log" "github.com/PagerDuty/go-pagerduty" @@ -11,10 +12,31 @@ type Config struct { Token string } +const invalidCredentials = ` + +No valid credentials found for PagerDuty provider. +Please see https://www.terraform.io/docs/providers/pagerduty/index.html +for more information on providing credentials for this provider. +` + // Client returns a new PagerDuty client func (c *Config) Client() (*pagerduty.Client, error) { + // Validate that the PagerDuty token is set + if c.Token == "" { + return nil, fmt.Errorf(invalidCredentials) + } + client := pagerduty.NewClient(c.Token) + // Validate the credentials by calling the abilities endpoint, + // if we get a 401 response back we return an error to the user + if _, err := client.ListAbilities(); err != nil { + if isUnauthorized(err) { + return nil, fmt.Errorf(invalidCredentials) + } + return nil, err + } + log.Printf("[INFO] PagerDuty client configured") return client, nil diff --git a/builtin/providers/pagerduty/errors.go b/builtin/providers/pagerduty/errors.go index 2e8efee5b5d0..fc9d579d666c 100644 --- a/builtin/providers/pagerduty/errors.go +++ b/builtin/providers/pagerduty/errors.go @@ -9,3 +9,7 @@ func isNotFound(err error) bool { return false } + +func isUnauthorized(err error) bool { + return strings.Contains(err.Error(), "HTTP response code: 401") +} From 3da6fb418d98ea0629727a39c70a7491b35d7f43 Mon Sep 17 00:00:00 2001 From: Alexander Hellbom Date: Sun, 19 Mar 2017 11:29:00 +0100 Subject: [PATCH 2/8] Add ability to skip validation --- builtin/providers/pagerduty/config.go | 18 ++++++++++++------ builtin/providers/pagerduty/provider.go | 11 ++++++++++- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/builtin/providers/pagerduty/config.go b/builtin/providers/pagerduty/config.go index f88ed1c22908..4dc90099474e 100644 --- a/builtin/providers/pagerduty/config.go +++ b/builtin/providers/pagerduty/config.go @@ -9,7 +9,11 @@ import ( // Config defines the configuration options for the PagerDuty client type Config struct { + // The PagerDuty API V2 token Token string + + // Skip validation of the token against the PagerDuty API + SkipCredsValidation bool } const invalidCredentials = ` @@ -28,13 +32,15 @@ func (c *Config) Client() (*pagerduty.Client, error) { client := pagerduty.NewClient(c.Token) - // Validate the credentials by calling the abilities endpoint, - // if we get a 401 response back we return an error to the user - if _, err := client.ListAbilities(); err != nil { - if isUnauthorized(err) { - return nil, fmt.Errorf(invalidCredentials) + if !c.SkipCredsValidation { + // Validate the credentials by calling the abilities endpoint, + // if we get a 401 response back we return an error to the user + if _, err := client.ListAbilities(); err != nil { + if isUnauthorized(err) { + return nil, fmt.Errorf(invalidCredentials) + } + return nil, err } - return nil, err } log.Printf("[INFO] PagerDuty client configured") diff --git a/builtin/providers/pagerduty/provider.go b/builtin/providers/pagerduty/provider.go index 55dc01819865..c8fe629ab64d 100644 --- a/builtin/providers/pagerduty/provider.go +++ b/builtin/providers/pagerduty/provider.go @@ -16,6 +16,11 @@ func Provider() terraform.ResourceProvider { Required: true, DefaultFunc: schema.EnvDefaultFunc("PAGERDUTY_TOKEN", nil), }, + + "skip_credentials_validation": { + Type: schema.TypeBool, + Optional: true, + }, }, DataSourcesMap: map[string]*schema.Resource{ @@ -40,7 +45,11 @@ func Provider() terraform.ResourceProvider { } func providerConfigure(data *schema.ResourceData) (interface{}, error) { - config := Config{Token: data.Get("token").(string)} + config := Config{ + Token: data.Get("token").(string), + SkipCredsValidation: data.Get("skip_credentials_validation").(bool), + } + log.Println("[INFO] Initializing PagerDuty client") return config.Client() } From f5299b4a64b6dbb9efe440b9531d863fdc5073b2 Mon Sep 17 00:00:00 2001 From: Alexander Hellbom Date: Sun, 19 Mar 2017 11:45:46 +0100 Subject: [PATCH 3/8] Update provider documentation --- website/source/docs/providers/pagerduty/index.html.markdown | 1 + 1 file changed, 1 insertion(+) diff --git a/website/source/docs/providers/pagerduty/index.html.markdown b/website/source/docs/providers/pagerduty/index.html.markdown index ca834d6e0737..1620ee06caec 100644 --- a/website/source/docs/providers/pagerduty/index.html.markdown +++ b/website/source/docs/providers/pagerduty/index.html.markdown @@ -39,3 +39,4 @@ resource "pagerduty_user" "earline" { The following arguments are supported: * `token` - (Required) The v2 authorization token. See [API Documentation](https://v2.developer.pagerduty.com/docs/authentication) for more information. +* `skip_credentials_validation` - (Optional) Skip the credentials validation via the PagerDuty API. From 72df2ae8da75b90000f8d80054d3813a402ebdad Mon Sep 17 00:00:00 2001 From: Alexander Hellbom Date: Sun, 19 Mar 2017 12:10:43 +0100 Subject: [PATCH 4/8] invalidCredentials -> invalidCreds --- builtin/providers/pagerduty/config.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/builtin/providers/pagerduty/config.go b/builtin/providers/pagerduty/config.go index 4dc90099474e..3295f50447c1 100644 --- a/builtin/providers/pagerduty/config.go +++ b/builtin/providers/pagerduty/config.go @@ -16,7 +16,7 @@ type Config struct { SkipCredsValidation bool } -const invalidCredentials = ` +const invalidCreds = ` No valid credentials found for PagerDuty provider. Please see https://www.terraform.io/docs/providers/pagerduty/index.html @@ -27,7 +27,7 @@ for more information on providing credentials for this provider. func (c *Config) Client() (*pagerduty.Client, error) { // Validate that the PagerDuty token is set if c.Token == "" { - return nil, fmt.Errorf(invalidCredentials) + return nil, fmt.Errorf(invalidCreds) } client := pagerduty.NewClient(c.Token) @@ -37,7 +37,7 @@ func (c *Config) Client() (*pagerduty.Client, error) { // if we get a 401 response back we return an error to the user if _, err := client.ListAbilities(); err != nil { if isUnauthorized(err) { - return nil, fmt.Errorf(invalidCredentials) + return nil, fmt.Errorf(invalidCreds) } return nil, err } From 984190144d93a7c5bda54d722d1c17dc0b563caf Mon Sep 17 00:00:00 2001 From: Alexander Hellbom Date: Sun, 19 Mar 2017 14:10:38 +0100 Subject: [PATCH 5/8] Include original error message --- builtin/providers/pagerduty/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/builtin/providers/pagerduty/config.go b/builtin/providers/pagerduty/config.go index 3295f50447c1..d8b083bfc99c 100644 --- a/builtin/providers/pagerduty/config.go +++ b/builtin/providers/pagerduty/config.go @@ -37,7 +37,7 @@ func (c *Config) Client() (*pagerduty.Client, error) { // if we get a 401 response back we return an error to the user if _, err := client.ListAbilities(); err != nil { if isUnauthorized(err) { - return nil, fmt.Errorf(invalidCreds) + return nil, fmt.Errorf(fmt.Sprintf("%s\n%s", err, invalidCreds)) } return nil, err } From 14e4f9b413fb7e087e00a60f3c561feb513db497 Mon Sep 17 00:00:00 2001 From: Alexander Hellbom Date: Sun, 19 Mar 2017 14:13:33 +0100 Subject: [PATCH 6/8] Update description for skip_credentials_validation --- website/source/docs/providers/pagerduty/index.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/source/docs/providers/pagerduty/index.html.markdown b/website/source/docs/providers/pagerduty/index.html.markdown index 1620ee06caec..5b34c7c2fd8f 100644 --- a/website/source/docs/providers/pagerduty/index.html.markdown +++ b/website/source/docs/providers/pagerduty/index.html.markdown @@ -39,4 +39,4 @@ resource "pagerduty_user" "earline" { The following arguments are supported: * `token` - (Required) The v2 authorization token. See [API Documentation](https://v2.developer.pagerduty.com/docs/authentication) for more information. -* `skip_credentials_validation` - (Optional) Skip the credentials validation via the PagerDuty API. +* `skip_credentials_validation` - (Optional) Skip validation of the token against the PagerDuty API. From d3d75921c01ecbb8ae91b07e6814ce94ad3e0bd6 Mon Sep 17 00:00:00 2001 From: Alexander Hellbom Date: Sun, 19 Mar 2017 14:45:41 +0100 Subject: [PATCH 7/8] Add config test --- builtin/providers/pagerduty/config_test.go | 28 ++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 builtin/providers/pagerduty/config_test.go diff --git a/builtin/providers/pagerduty/config_test.go b/builtin/providers/pagerduty/config_test.go new file mode 100644 index 000000000000..4f43d736cd11 --- /dev/null +++ b/builtin/providers/pagerduty/config_test.go @@ -0,0 +1,28 @@ +package pagerduty + +import ( + "testing" +) + +// Test config with an empty token +func TestConfigEmptyToken(t *testing.T) { + config := Config{ + Token: "", + } + + if _, err := config.Client(); err == nil { + t.Fatalf("expected error, but got nil") + } +} + +// Test config with invalid token but with SkipCredsValidation +func TestConfigSkipCredsValidation(t *testing.T) { + config := Config{ + Token: "foo", + SkipCredsValidation: true, + } + + if _, err := config.Client(); err != nil { + t.Fatalf("error: expected the client to not fail: %v", err) + } +} From 3dbdbc613ab0c0de579c4daf62e3fbd2fd38a05a Mon Sep 17 00:00:00 2001 From: Alexander Hellbom Date: Sun, 19 Mar 2017 18:21:59 +0100 Subject: [PATCH 8/8] set skip_credentials_validation default to false --- builtin/providers/pagerduty/provider.go | 1 + 1 file changed, 1 insertion(+) diff --git a/builtin/providers/pagerduty/provider.go b/builtin/providers/pagerduty/provider.go index c8fe629ab64d..96a2f338ebbf 100644 --- a/builtin/providers/pagerduty/provider.go +++ b/builtin/providers/pagerduty/provider.go @@ -20,6 +20,7 @@ func Provider() terraform.ResourceProvider { "skip_credentials_validation": { Type: schema.TypeBool, Optional: true, + Default: false, }, },