|
| 1 | +//go:build detectors |
| 2 | +// +build detectors |
| 3 | + |
| 4 | +package gitlaboauth2 |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + "testing" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/google/go-cmp/cmp" |
| 13 | + "github.com/google/go-cmp/cmp/cmpopts" |
| 14 | + |
| 15 | + "github.com/trufflesecurity/trufflehog/v3/pkg/common" |
| 16 | + "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" |
| 17 | + "github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb" |
| 18 | +) |
| 19 | + |
| 20 | +func TestGitlabOauth_FromChunk(t *testing.T) { |
| 21 | + ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) |
| 22 | + defer cancel() |
| 23 | + testSecrets, err := common.GetSecret(ctx, "trufflehog-testing", "detectors6") |
| 24 | + if err != nil { |
| 25 | + t.Fatalf("could not get test secrets from GCP: %s", err) |
| 26 | + } |
| 27 | + |
| 28 | + clientId := testSecrets.MustGetField("GITLABOAUTH_CLIENT_ID") |
| 29 | + clientSecret := testSecrets.MustGetField("GITLABOAUTH_CLIENT_SECRET") |
| 30 | + inactiveClientId := testSecrets.MustGetField("GITLABOAUTH_CLIENT_ID_INACTIVE") |
| 31 | + inactiveClientSecret := testSecrets.MustGetField("GITLABOAUTH_CLIENT_SECRET_INACTIVE") |
| 32 | + |
| 33 | + type args struct { |
| 34 | + ctx context.Context |
| 35 | + data []byte |
| 36 | + verify bool |
| 37 | + } |
| 38 | + tests := []struct { |
| 39 | + name string |
| 40 | + s Scanner |
| 41 | + args args |
| 42 | + want []detectors.Result |
| 43 | + wantErr bool |
| 44 | + wantVerificationErr bool |
| 45 | + }{ |
| 46 | + { |
| 47 | + name: "found, verified", |
| 48 | + s: Scanner{}, |
| 49 | + args: args{ |
| 50 | + ctx: context.Background(), |
| 51 | + data: []byte(fmt.Sprintf(` |
| 52 | + gitlab: |
| 53 | + client_id: %s |
| 54 | + client_secret: %s |
| 55 | + `, clientId, clientSecret)), |
| 56 | + verify: true, |
| 57 | + }, |
| 58 | + want: []detectors.Result{ |
| 59 | + { |
| 60 | + DetectorType: detectorspb.DetectorType_GitLabOauth2, |
| 61 | + Verified: true, |
| 62 | + }, |
| 63 | + }, |
| 64 | + wantErr: false, |
| 65 | + wantVerificationErr: false, |
| 66 | + }, |
| 67 | + { |
| 68 | + name: "found, unverified", |
| 69 | + s: Scanner{}, |
| 70 | + args: args{ |
| 71 | + ctx: context.Background(), |
| 72 | + data: []byte(fmt.Sprintf(` |
| 73 | + gitlab: |
| 74 | + client_id: %s |
| 75 | + client_secret: %s |
| 76 | + `, inactiveClientId, inactiveClientSecret)), |
| 77 | + verify: true, |
| 78 | + }, |
| 79 | + want: []detectors.Result{ |
| 80 | + { |
| 81 | + DetectorType: detectorspb.DetectorType_GitLabOauth2, |
| 82 | + Verified: false, |
| 83 | + }, |
| 84 | + }, |
| 85 | + wantErr: false, |
| 86 | + wantVerificationErr: false, |
| 87 | + }, |
| 88 | + { |
| 89 | + name: "not found", |
| 90 | + s: Scanner{}, |
| 91 | + args: args{ |
| 92 | + ctx: context.Background(), |
| 93 | + data: []byte("You cannot find the secret within"), |
| 94 | + verify: true, |
| 95 | + }, |
| 96 | + want: nil, |
| 97 | + wantErr: false, |
| 98 | + wantVerificationErr: false, |
| 99 | + }, |
| 100 | + { |
| 101 | + name: "found, would be verified if not for timeout", |
| 102 | + s: Scanner{client: common.SaneHttpClientTimeOut(1 * time.Microsecond)}, |
| 103 | + args: args{ |
| 104 | + ctx: context.Background(), |
| 105 | + data: []byte(fmt.Sprintf(` |
| 106 | + gitlab: |
| 107 | + client_id: %s |
| 108 | + client_secret: %s |
| 109 | + `, clientId, clientSecret)), |
| 110 | + verify: true, |
| 111 | + }, |
| 112 | + want: []detectors.Result{ |
| 113 | + { |
| 114 | + DetectorType: detectorspb.DetectorType_GitLabOauth2, |
| 115 | + Verified: false, |
| 116 | + }, |
| 117 | + }, |
| 118 | + wantErr: false, |
| 119 | + wantVerificationErr: true, |
| 120 | + }, |
| 121 | + { |
| 122 | + name: "found, verified but unexpected api surface", |
| 123 | + s: Scanner{client: common.ConstantResponseHttpClient(404, "")}, |
| 124 | + args: args{ |
| 125 | + ctx: context.Background(), |
| 126 | + data: []byte(fmt.Sprintf(` |
| 127 | + gitlab: |
| 128 | + client_id: %s |
| 129 | + client_secret: %s |
| 130 | + `, clientId, clientSecret)), |
| 131 | + verify: true, |
| 132 | + }, |
| 133 | + want: []detectors.Result{ |
| 134 | + { |
| 135 | + DetectorType: detectorspb.DetectorType_GitLabOauth2, |
| 136 | + Verified: false, |
| 137 | + }, |
| 138 | + }, |
| 139 | + wantErr: false, |
| 140 | + wantVerificationErr: true, |
| 141 | + }, |
| 142 | + } |
| 143 | + for _, tt := range tests { |
| 144 | + t.Run(tt.name, func(t *testing.T) { |
| 145 | + tt.s.SetCloudEndpoint(tt.s.CloudEndpoint()) |
| 146 | + tt.s.UseCloudEndpoint(true) |
| 147 | + got, err := tt.s.FromData(tt.args.ctx, tt.args.verify, tt.args.data) |
| 148 | + if (err != nil) != tt.wantErr { |
| 149 | + t.Errorf("GitlabOauth.FromData() error = %v, wantErr %v", err, tt.wantErr) |
| 150 | + return |
| 151 | + } |
| 152 | + for i := range got { |
| 153 | + if len(got[i].Raw) == 0 { |
| 154 | + t.Fatalf("no raw secret present: \n %+v", got[i]) |
| 155 | + } |
| 156 | + if (got[i].VerificationError() != nil) != tt.wantVerificationErr { |
| 157 | + t.Fatalf("wantVerificationError = %v, verification error = %v", tt.wantVerificationErr, got[i].VerificationError()) |
| 158 | + } |
| 159 | + } |
| 160 | + ignoreOpts := cmpopts.IgnoreFields(detectors.Result{}, |
| 161 | + "Raw", "RawV2", "verificationError", "ExtraData", "primarySecret") |
| 162 | + if diff := cmp.Diff(got, tt.want, ignoreOpts); diff != "" { |
| 163 | + t.Errorf("GitlabOauth.FromData() %s diff: (-got +want)\n%s", tt.name, diff) |
| 164 | + } |
| 165 | + }) |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +func BenchmarkFromData(benchmark *testing.B) { |
| 170 | + ctx := context.Background() |
| 171 | + s := Scanner{} |
| 172 | + s.SetCloudEndpoint(s.CloudEndpoint()) |
| 173 | + s.UseCloudEndpoint(true) |
| 174 | + for name, data := range detectors.MustGetBenchmarkData() { |
| 175 | + benchmark.Run(name, func(b *testing.B) { |
| 176 | + b.ResetTimer() |
| 177 | + for n := 0; n < b.N; n++ { |
| 178 | + _, err := s.FromData(ctx, false, data) |
| 179 | + if err != nil { |
| 180 | + b.Fatal(err) |
| 181 | + } |
| 182 | + } |
| 183 | + }) |
| 184 | + } |
| 185 | +} |
0 commit comments