Skip to content

Commit a6a4aed

Browse files
Fixed Grafana detector (#4166)
* fixed grafana detector * tighten the regex * simplified
1 parent ab76fe4 commit a6a4aed

File tree

3 files changed

+52
-28
lines changed

3 files changed

+52
-28
lines changed

pkg/detectors/grafana/grafana.go

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package grafana
33
import (
44
"context"
55
"fmt"
6+
"io"
67
"net/http"
78
"strings"
89

@@ -23,13 +24,22 @@ var _ detectors.Detector = (*Scanner)(nil)
2324
var (
2425
defaultClient = common.SaneHttpClient()
2526
// Make sure that your group is surrounded in boundary characters such as below to reduce false positives.
26-
keyPat = regexp.MustCompile(`\b(glc_[A-Za-z0-9+\/]{50,150}\={0,2})`)
27+
keyPat = regexp.MustCompile(`\b(glc_eyJ[A-Za-z0-9+\/=]{60,160})`)
2728
)
2829

30+
func (s Scanner) getClient() *http.Client {
31+
client := s.client
32+
if client == nil {
33+
client = defaultClient
34+
}
35+
36+
return client
37+
}
38+
2939
// Keywords are used for efficiently pre-filtering chunks.
3040
// Use identifiers in the secret preferably, or the provider name.
3141
func (s Scanner) Keywords() []string {
32-
return []string{"glc_"}
42+
return []string{"glc_eyJ"}
3343
}
3444

3545
// FromData will find and optionally verify Grafana secrets in a given set of bytes.
@@ -47,29 +57,9 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
4757
}
4858

4959
if verify {
50-
client := s.client
51-
if client == nil {
52-
client = defaultClient
53-
}
54-
req, err := http.NewRequestWithContext(ctx, "GET", "https://grafana.com/api/v1/tokens?region=us", nil)
55-
if err != nil {
56-
continue
57-
}
58-
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", resMatch))
59-
res, err := client.Do(req)
60-
if err == nil {
61-
defer res.Body.Close()
62-
if res.StatusCode >= 200 && res.StatusCode < 300 || res.StatusCode == 403 {
63-
s1.Verified = true
64-
} else if res.StatusCode == 401 {
65-
// The secret is determinately not verified (nothing to do)
66-
} else {
67-
err = fmt.Errorf("unexpected HTTP response status %d", res.StatusCode)
68-
s1.SetVerificationError(err, resMatch)
69-
}
70-
} else {
71-
s1.SetVerificationError(err, resMatch)
72-
}
60+
isVerified, verificationErr := verifyGrafanaKey(ctx, s.getClient(), resMatch)
61+
s1.Verified = isVerified
62+
s1.SetVerificationError(verificationErr, resMatch)
7363
}
7464

7565
results = append(results, s1)
@@ -85,3 +75,37 @@ func (s Scanner) Type() detectorspb.DetectorType {
8575
func (s Scanner) Description() string {
8676
return "Grafana is an open-source platform for monitoring and observability. Grafana API keys can be used to access and manage Grafana resources."
8777
}
78+
79+
func verifyGrafanaKey(ctx context.Context, client *http.Client, token string) (bool, error) {
80+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://grafana.com/api/v1/tokens?region=us", http.NoBody)
81+
if err != nil {
82+
return false, err
83+
}
84+
85+
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
86+
87+
resp, err := client.Do(req)
88+
if err != nil {
89+
return false, err
90+
}
91+
92+
defer func() {
93+
_, _ = io.Copy(io.Discard, resp.Body)
94+
_ = resp.Body.Close()
95+
}()
96+
97+
switch resp.StatusCode {
98+
case http.StatusOK:
99+
return true, nil
100+
case http.StatusUnauthorized:
101+
bodyBytes, err := io.ReadAll(resp.Body)
102+
if err != nil {
103+
return false, err
104+
}
105+
106+
// token is valid but has restricted permissions
107+
return strings.Contains(string(bodyBytes), "Unauthorized"), nil
108+
default:
109+
return false, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
110+
}
111+
}

pkg/detectors/grafana/grafana_integration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func TestGrafana_FromChunk(t *testing.T) {
136136
t.Fatalf("wantVerificationError = %v, verification error = %v", tt.wantVerificationErr, got[i].VerificationError())
137137
}
138138
}
139-
ignoreOpts := cmpopts.IgnoreFields(detectors.Result{}, "Raw", "verificationError")
139+
ignoreOpts := cmpopts.IgnoreFields(detectors.Result{}, "Raw", "verificationError", "primarySecret")
140140
if diff := cmp.Diff(got, tt.want, ignoreOpts); diff != "" {
141141
t.Errorf("Grafana.FromData() %s diff: (-got +want)\n%s", tt.name, diff)
142142
}

pkg/detectors/grafana/grafana_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ var (
1919
"authentication_type": "",
2020
"verification_url": "https://api.example.com/example",
2121
"test_secrets": {
22-
"grafana_secret": "glc_gnVF057+C0x9J+QwzC5JXb5uQ/WSzn98X/iIrZXKaA3Hh+lum0XBRcu56qMlW7ZaxXrNt33XoI3CXz7IRPci="
22+
"grafana_secret": "glc_eyJF057+C0x9J+QwzC5JXb5uQ/WSzn98X/iIrZXKaA3Hh+lum0XBRcu56qMlW7ZaxXrNt33XoI3CXz7IRPci="
2323
},
2424
"expected_response": "200",
2525
"method": "GET",
2626
"deprecated": false
2727
}]`
28-
secret = "glc_gnVF057+C0x9J+QwzC5JXb5uQ/WSzn98X/iIrZXKaA3Hh+lum0XBRcu56qMlW7ZaxXrNt33XoI3CXz7IRPci="
28+
secret = "glc_eyJF057+C0x9J+QwzC5JXb5uQ/WSzn98X/iIrZXKaA3Hh+lum0XBRcu56qMlW7ZaxXrNt33XoI3CXz7IRPci="
2929
)
3030

3131
func TestGrafana_Pattern(t *testing.T) {

0 commit comments

Comments
 (0)