Skip to content

Commit ca51754

Browse files
heimwehstack72
authored andcommitted
provider/pagerduty: Validate credentials (hashicorp#12854)
* Validate credentials * Add ability to skip validation * Update provider documentation * invalidCredentials -> invalidCreds * Include original error message * Update description for skip_credentials_validation * Add config test * set skip_credentials_validation default to false
1 parent 4759b51 commit ca51754

5 files changed

Lines changed: 72 additions & 1 deletion

File tree

‎builtin/providers/pagerduty/config.go‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,48 @@
11
package pagerduty
22

33
import (
4+
"fmt"
45
"log"
56

67
"github.com/PagerDuty/go-pagerduty"
78
)
89

910
// Config defines the configuration options for the PagerDuty client
1011
type Config struct {
12+
// The PagerDuty API V2 token
1113
Token string
14+
15+
// Skip validation of the token against the PagerDuty API
16+
SkipCredsValidation bool
1217
}
1318

19+
const invalidCreds = `
20+
21+
No valid credentials found for PagerDuty provider.
22+
Please see https://www.terraform.io/docs/providers/pagerduty/index.html
23+
for more information on providing credentials for this provider.
24+
`
25+
1426
// Client returns a new PagerDuty client
1527
func (c *Config) Client() (*pagerduty.Client, error) {
28+
// Validate that the PagerDuty token is set
29+
if c.Token == "" {
30+
return nil, fmt.Errorf(invalidCreds)
31+
}
32+
1633
client := pagerduty.NewClient(c.Token)
1734

35+
if !c.SkipCredsValidation {
36+
// Validate the credentials by calling the abilities endpoint,
37+
// if we get a 401 response back we return an error to the user
38+
if _, err := client.ListAbilities(); err != nil {
39+
if isUnauthorized(err) {
40+
return nil, fmt.Errorf(fmt.Sprintf("%s\n%s", err, invalidCreds))
41+
}
42+
return nil, err
43+
}
44+
}
45+
1846
log.Printf("[INFO] PagerDuty client configured")
1947

2048
return client, nil
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package pagerduty
2+
3+
import (
4+
"testing"
5+
)
6+
7+
// Test config with an empty token
8+
func TestConfigEmptyToken(t *testing.T) {
9+
config := Config{
10+
Token: "",
11+
}
12+
13+
if _, err := config.Client(); err == nil {
14+
t.Fatalf("expected error, but got nil")
15+
}
16+
}
17+
18+
// Test config with invalid token but with SkipCredsValidation
19+
func TestConfigSkipCredsValidation(t *testing.T) {
20+
config := Config{
21+
Token: "foo",
22+
SkipCredsValidation: true,
23+
}
24+
25+
if _, err := config.Client(); err != nil {
26+
t.Fatalf("error: expected the client to not fail: %v", err)
27+
}
28+
}

‎builtin/providers/pagerduty/errors.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,7 @@ func isNotFound(err error) bool {
99

1010
return false
1111
}
12+
13+
func isUnauthorized(err error) bool {
14+
return strings.Contains(err.Error(), "HTTP response code: 401")
15+
}

‎builtin/providers/pagerduty/provider.go‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ func Provider() terraform.ResourceProvider {
1616
Required: true,
1717
DefaultFunc: schema.EnvDefaultFunc("PAGERDUTY_TOKEN", nil),
1818
},
19+
20+
"skip_credentials_validation": {
21+
Type: schema.TypeBool,
22+
Optional: true,
23+
Default: false,
24+
},
1925
},
2026

2127
DataSourcesMap: map[string]*schema.Resource{
@@ -40,7 +46,11 @@ func Provider() terraform.ResourceProvider {
4046
}
4147

4248
func providerConfigure(data *schema.ResourceData) (interface{}, error) {
43-
config := Config{Token: data.Get("token").(string)}
49+
config := Config{
50+
Token: data.Get("token").(string),
51+
SkipCredsValidation: data.Get("skip_credentials_validation").(bool),
52+
}
53+
4454
log.Println("[INFO] Initializing PagerDuty client")
4555
return config.Client()
4656
}

‎website/source/docs/providers/pagerduty/index.html.markdown‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,4 @@ resource "pagerduty_user" "earline" {
3939
The following arguments are supported:
4040

4141
* `token` - (Required) The v2 authorization token. See [API Documentation](https://v2.developer.pagerduty.com/docs/authentication) for more information.
42+
* `skip_credentials_validation` - (Optional) Skip validation of the token against the PagerDuty API.

0 commit comments

Comments
 (0)