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

updated buildkite detectors #3611

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package buildkite
import (
"context"
"fmt"
"io"
"net/http"
"strings"

Expand Down Expand Up @@ -52,18 +53,9 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
}

if verify {
req, err := http.NewRequestWithContext(ctx, "GET", "https://api.buildkite.com/v2/access-token", nil)
if err != nil {
continue
}
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", resMatch))
res, err := client.Do(req)
if err == nil {
defer res.Body.Close()
if res.StatusCode >= 200 && res.StatusCode < 300 {
s1.Verified = true
}
}
isVerified, verificationErr := VerifyBuildKite(ctx, client, resMatch)
s1.Verified = isVerified
s1.SetVerificationError(verificationErr, resMatch)
}

results = append(results, s1)
Expand All @@ -79,3 +71,29 @@ func (s Scanner) Type() detectorspb.DetectorType {
func (s Scanner) Description() string {
return "Buildkite is a platform for running fast, secure, and scalable continuous integration pipelines. Buildkite API tokens can be used to access and modify pipeline data and configurations."
}

func VerifyBuildKite(ctx context.Context, client *http.Client, secret string) (bool, error) {
// create a request
req, err := http.NewRequestWithContext(ctx, "GET", "https://api.buildkite.com/v2/access-token", nil)
if err != nil {
return false, err
}

// add authorization header
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", secret))

res, err := client.Do(req)
if err != nil {
return false, err
}
defer func() {
_, _ = io.Copy(io.Discard, res.Body)
_ = res.Body.Close()
}()

if res.StatusCode == http.StatusOK {
return true, nil
Copy link
Contributor

@rgmz rgmz Nov 18, 2024

Choose a reason for hiding this comment

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

I think it's worth including the scopes in extraData. https://buildkite.com/docs/apis/rest-api/access-token#get-the-current-token

Also, I would explicitly highlight which status code means "bad" and return an error if an unusual one is encountered.

return false, nil, fmt.Errorf("unexpected HTTP response status %d", res.StatusCode)

}

return false, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ package buildkitev2

import (
"context"
"fmt"
"net/http"
"strings"

regexp "github.com/wasilibs/go-re2"

"github.com/trufflesecurity/trufflehog/v3/pkg/common"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
v1 "github.com/trufflesecurity/trufflehog/v3/pkg/detectors/buildkite/v1"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
)

Expand Down Expand Up @@ -52,18 +51,9 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
}

if verify {
req, err := http.NewRequestWithContext(ctx, "GET", "https://api.buildkite.com/v2/access-token", nil)
if err != nil {
continue
}
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", resMatch))
res, err := client.Do(req)
if err == nil {
defer res.Body.Close()
if res.StatusCode >= 200 && res.StatusCode < 300 {
s1.Verified = true
}
}
isVerified, verificationErr := v1.VerifyBuildKite(ctx, client, resMatch)
s1.Verified = isVerified
s1.SetVerificationError(verificationErr, resMatch)
}

results = append(results, s1)
Expand Down
8 changes: 4 additions & 4 deletions pkg/engine/defaults/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ import (
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/budibase"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/bugherd"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/bugsnag"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/buildkite"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/buildkitev2"
buildKitev1 "github.com/trufflesecurity/trufflehog/v3/pkg/detectors/buildkite/v1"
buildKitev2 "github.com/trufflesecurity/trufflehog/v3/pkg/detectors/buildkite/v2"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/bulbul"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/bulksms"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/buttercms"
Expand Down Expand Up @@ -925,8 +925,8 @@ func buildDetectorList() []detectors.Detector {
&budibase.Scanner{},
&bugherd.Scanner{},
&bugsnag.Scanner{},
&buildkite.Scanner{},
&buildkitev2.Scanner{},
&buildKitev1.Scanner{},
&buildKitev2.Scanner{},
&bulbul.Scanner{},
&bulksms.Scanner{},
&buttercms.Scanner{},
Expand Down
Loading