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

provider/pagerduty: Validate credentials #12854

Merged
merged 8 commits into from
Mar 19, 2017
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
28 changes: 28 additions & 0 deletions builtin/providers/pagerduty/config.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,48 @@
package pagerduty

import (
"fmt"
"log"

"github.com/PagerDuty/go-pagerduty"
)

// 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 invalidCreds = `

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(invalidCreds)
}

client := pagerduty.NewClient(c.Token)

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(fmt.Sprintf("%s\n%s", err, invalidCreds))
}
return nil, err
}
}

log.Printf("[INFO] PagerDuty client configured")

return client, nil
Expand Down
28 changes: 28 additions & 0 deletions builtin/providers/pagerduty/config_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
4 changes: 4 additions & 0 deletions builtin/providers/pagerduty/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
12 changes: 11 additions & 1 deletion builtin/providers/pagerduty/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ func Provider() terraform.ResourceProvider {
Required: true,
DefaultFunc: schema.EnvDefaultFunc("PAGERDUTY_TOKEN", nil),
},

"skip_credentials_validation": {
Type: schema.TypeBool,
Optional: true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think we need to define a default here? We are using d.Get - what if the value isn't found?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch :) I'll push a fix for that

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

skip_credentials_validation should now default to false

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! thanks :)

Default: false,
},
},

DataSourcesMap: map[string]*schema.Resource{
Expand All @@ -40,7 +46,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()
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 validation of the token against the PagerDuty API.