Skip to content
Open
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
15 changes: 15 additions & 0 deletions pkg/detectors/pagerdutyapikey/pagerdutyapikey.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pagerdutyapikey
import (
"context"
"fmt"
"io"
"net/http"
"strings"

Expand Down Expand Up @@ -84,6 +85,20 @@ func verifyPagerdutyapikey(ctx context.Context, client *http.Client, token strin
switch res.StatusCode {
case http.StatusOK:
return true, nil
case http.StatusForbidden:
bodyBytes, err := io.ReadAll(res.Body)
if err != nil {
return false, err
}
bodyStr := string(bodyBytes)

// 403 with "Access Denied" and "2010" means valid credentials with insufficient permissions.
// Ref: https://developer.pagerduty.com/docs/errors
if strings.Contains(bodyStr, "Access Denied") && strings.Contains(bodyStr, "2010") {
Copy link
Contributor

Choose a reason for hiding this comment

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

I wish there was unit test coverage for this. Also, does it make sense to actually parse the JSON, rather than to just do a string contains?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the review @martinlocklear, an additional unit test would be beneficial, yes.

Generally in detectors I've seen us rely on string contains, unless we need to extract something from the response. If our matching string is specific enough -- which I feel it is, in our case -- I lean towards string contains. IMO this would would also be slightly more adaptive to any potential change in the response shape (e.g. if keys get renamed but the message still contains these keywords)

return true, nil
}

return false, fmt.Errorf("response status forbidden 403: %s", bodyStr)
case http.StatusUnauthorized:
return false, nil
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func TestPagerDutyApiKey_FromChunk(t *testing.T) {
t.Errorf("PagerDutyApiKey.FromData() verificationError = %v, wantVerificationErr %v", got[i].VerificationError(), tt.wantVerificationErr)
}
}
ignoreOpts := cmpopts.IgnoreFields(detectors.Result{}, "Raw", "verificationError")
ignoreOpts := cmpopts.IgnoreFields(detectors.Result{}, "Raw", "verificationError", "primarySecret")
if diff := cmp.Diff(got, tt.want, ignoreOpts); diff != "" {
t.Errorf("PagerDutyApiKey.FromData() %s diff: (-got +want)\n%s", tt.name, diff)
}
Expand Down
Loading