Files
trufflehog/pkg/detectors/humioapitoken/humioapitoken_integration_test.go
Dave HubbardandCursor 080c22fb8e [INT-942] Add HumioAPIToken detector (#5258)
* [INT-942] Add HumioAPIToken detector

Add a TruffleHog detector for Humio/CrowdStrike Falcon LogScale API
tokens. These are personal and repository-scoped tokens in a
tilde-delimited base62 format ({24 or 32}~{44}) that grant read/write
access to LogScale repositories via the REST and GraphQL APIs.

Verification uses the read-only GET /api/v1/health-json endpoint.
Known SaaS regions (US and EU) are tried automatically; self-hosted or
CrowdStrike regional instances are supported via explicit endpoint
configuration. ExtraData surfaces token type (Personal API Token vs
Repository API Token) based on prefix length.

Feature-flag gated behind HumioAPITokenDetectorEnabled.

Co-authored-by: Cursor <[email protected]>

* Use realistic test inputs for pattern matching

Replace synthetic one-liner inputs with multi-line snippets that
mirror real-world contexts: Python client config, .env file, Docker
Compose environment block. Adopt require.NoError from testify to
match the convention in solarwindsobservability_test.go.

Co-authored-by: Cursor <[email protected]>

* Use url.JoinPath for verification endpoint URLs

Replace string concatenation with url.JoinPath to safely construct
verification URLs from base endpoints.

Co-authored-by: Cursor <[email protected]>

---------

Co-authored-by: Cursor <[email protected]>
2026-09-08 09:22:21 -04:00

179 lines
4.9 KiB
Go

//go:build detectors
// +build detectors
package humioapitoken
import (
"context"
"fmt"
"net/http"
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detector_typepb"
)
// newTestScanner returns a Scanner with the cloud endpoint configured, mirroring
// what DefaultDetectors() does at startup. Without this, Endpoints() returns
// nothing and the verification loop never runs.
func newTestScanner(client *http.Client) Scanner {
s := Scanner{client: client}
s.SetCloudEndpoint("https://cloud.us.humio.com")
s.UseCloudEndpoint(true)
return s
}
func TestHumioAPIToken_FromChunk(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
testSecrets, err := common.GetSecret(ctx, "trufflehog-testing", "detectors7")
if err != nil {
t.Fatalf("could not get test secrets from GCP: %s", err)
}
secret := testSecrets.MustGetField("HUMIOAPITOKEN")
inactiveSecret := testSecrets.MustGetField("HUMIOAPITOKEN_INACTIVE")
type args struct {
ctx context.Context
data []byte
verify bool
}
tests := []struct {
name string
s Scanner
args args
want []detectors.Result
wantErr bool
}{
{
name: "found, verified",
s: newTestScanner(nil),
args: args{
ctx: context.Background(),
data: []byte(fmt.Sprintf("You can find a humio api token %s within", secret)),
verify: true,
},
want: []detectors.Result{
{
DetectorType: detector_typepb.DetectorType_HumioAPIToken,
Verified: true,
},
},
wantErr: false,
},
{
name: "found, real secrets, verification error due to timeout",
s: newTestScanner(common.SaneHttpClientTimeOut(1 * time.Microsecond)),
args: args{
ctx: context.Background(),
data: []byte(fmt.Sprintf("You can find a humio api token %s within", secret)),
verify: true,
},
want: func() []detectors.Result {
r := detectors.Result{
DetectorType: detector_typepb.DetectorType_HumioAPIToken,
Verified: false,
}
r.SetVerificationError(context.DeadlineExceeded)
return []detectors.Result{r}
}(),
wantErr: false,
},
{
name: "found, real secrets, verification error due to unexpected api surface",
s: newTestScanner(common.ConstantResponseHttpClient(500, "{}")),
args: args{
ctx: context.Background(),
data: []byte(fmt.Sprintf("You can find a humio api token %s within", secret)),
verify: true,
},
want: func() []detectors.Result {
r := detectors.Result{
DetectorType: detector_typepb.DetectorType_HumioAPIToken,
Verified: false,
}
r.SetVerificationError(fmt.Errorf("unexpected HTTP response status 500"))
return []detectors.Result{r}
}(),
wantErr: false,
},
{
name: "found, unverified",
s: newTestScanner(nil),
args: args{
ctx: context.Background(),
data: []byte(fmt.Sprintf("You can find a humio api token %s within but not valid", inactiveSecret)),
verify: true,
},
want: []detectors.Result{
{
DetectorType: detector_typepb.DetectorType_HumioAPIToken,
Verified: false,
},
},
wantErr: false,
},
{
name: "not found",
s: newTestScanner(nil),
args: args{
ctx: context.Background(),
data: []byte("You cannot find the secret within"),
verify: true,
},
want: nil,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.s.FromData(tt.args.ctx, tt.args.verify, tt.args.data)
if (err != nil) != tt.wantErr {
t.Errorf("HumioAPIToken.FromData() error = %v, wantErr %v", err, tt.wantErr)
return
}
for i := range got {
if len(got[i].Raw) == 0 {
t.Fatalf("no raw secret present: \n %+v", got[i])
}
gotErr := ""
if got[i].VerificationError() != nil {
gotErr = got[i].VerificationError().Error()
}
wantErr := ""
if tt.want[i].VerificationError() != nil {
wantErr = tt.want[i].VerificationError().Error()
}
if gotErr != wantErr {
t.Fatalf("wantVerificationError = %v, verification error = %v", tt.want[i].VerificationError(), got[i].VerificationError())
}
}
ignoreOpts := cmpopts.IgnoreFields(detectors.Result{}, "Raw", "RawV2", "verificationError", "SecretParts", "ExtraData")
if diff := cmp.Diff(got, tt.want, ignoreOpts); diff != "" {
t.Errorf("HumioAPIToken.FromData() %s diff: (-got +want)\n%s", tt.name, diff)
}
})
}
}
func BenchmarkFromData(benchmark *testing.B) {
ctx := context.Background()
s := Scanner{}
for name, data := range detectors.MustGetBenchmarkData() {
benchmark.Run(name, func(b *testing.B) {
b.ResetTimer()
for n := 0; n < b.N; n++ {
_, err := s.FromData(ctx, false, data)
if err != nil {
b.Fatal(err)
}
}
})
}
}