|
| 1 | +//go:build detectors |
| 2 | +// +build detectors |
| 3 | + |
| 4 | +package hashicorpvaultauth |
| 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 TestHashiCorpVaultAuth_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 | + roleId := testSecrets.MustGetField("HASHICORPVAULTAUTH_ROLE_ID") |
| 28 | + secretId := testSecrets.MustGetField("HASHICORPVAULTAUTH_SECRET_ID") |
| 29 | + inactiveRoleId := testSecrets.MustGetField("HASHICORPVAULTAUTH_ROLE_ID_INACTIVE") |
| 30 | + inactiveSecretId := testSecrets.MustGetField("HASHICORPVAULTAUTH_SECRET_ID_INACTIVE") |
| 31 | + vaultUrl := testSecrets.MustGetField("HASHICORPVAULTAUTH_URL") |
| 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, unverified - complete set with invalid credentials", |
| 48 | + s: Scanner{}, |
| 49 | + args: args{ |
| 50 | + ctx: context.Background(), |
| 51 | + data: []byte(fmt.Sprintf("hashicorp config:\nrole_id: %s\nsecret_id: %s\nvault_url: %s", inactiveRoleId, inactiveSecretId, vaultUrl)), |
| 52 | + verify: true, |
| 53 | + }, |
| 54 | + want: []detectors.Result{ |
| 55 | + { |
| 56 | + DetectorType: detectorspb.DetectorType_HashiCorpVaultAuth, |
| 57 | + Verified: false, |
| 58 | + VerificationFromCache: false, |
| 59 | + Raw: []byte(inactiveSecretId), |
| 60 | + RawV2: []byte(fmt.Sprintf("%s:%s", inactiveRoleId, inactiveSecretId)), |
| 61 | + ExtraData: map[string]string{ |
| 62 | + "URL": vaultUrl, |
| 63 | + }, |
| 64 | + StructuredData: nil, |
| 65 | + }, |
| 66 | + }, |
| 67 | + wantErr: false, |
| 68 | + wantVerificationErr: false, |
| 69 | + }, |
| 70 | + { |
| 71 | + name: "found, verified - complete set with valid credentials", |
| 72 | + s: Scanner{}, |
| 73 | + args: args{ |
| 74 | + ctx: context.Background(), |
| 75 | + data: []byte(fmt.Sprintf("hashicorp config:\nrole_id: %s\nsecret_id: %s\nvault_url: %s", roleId, secretId, vaultUrl)), |
| 76 | + verify: true, |
| 77 | + }, |
| 78 | + want: []detectors.Result{ |
| 79 | + { |
| 80 | + DetectorType: detectorspb.DetectorType_HashiCorpVaultAuth, |
| 81 | + Verified: true, |
| 82 | + VerificationFromCache: false, |
| 83 | + Raw: []byte(secretId), |
| 84 | + RawV2: []byte(fmt.Sprintf("%s:%s", roleId, secretId)), |
| 85 | + ExtraData: map[string]string{ |
| 86 | + "URL": vaultUrl, |
| 87 | + }, |
| 88 | + StructuredData: nil, |
| 89 | + }, |
| 90 | + }, |
| 91 | + wantErr: false, |
| 92 | + wantVerificationErr: false, |
| 93 | + }, |
| 94 | + { |
| 95 | + name: "found, incomplete set - credentials without vault url", |
| 96 | + s: Scanner{}, |
| 97 | + args: args{ |
| 98 | + ctx: context.Background(), |
| 99 | + data: []byte(fmt.Sprintf("vault config:\nrole_id: %s\nsecret_id: %s", roleId, secretId)), |
| 100 | + verify: true, |
| 101 | + }, |
| 102 | + want: nil, |
| 103 | + wantErr: false, |
| 104 | + wantVerificationErr: false, |
| 105 | + }, |
| 106 | + { |
| 107 | + name: "found, incomplete set - only role_id", |
| 108 | + s: Scanner{}, |
| 109 | + args: args{ |
| 110 | + ctx: context.Background(), |
| 111 | + data: []byte(fmt.Sprintf("vault role_id: %s", roleId)), |
| 112 | + verify: true, |
| 113 | + }, |
| 114 | + want: nil, |
| 115 | + wantErr: false, |
| 116 | + wantVerificationErr: false, |
| 117 | + }, |
| 118 | + { |
| 119 | + name: "found, incomplete set - only secret_id", |
| 120 | + s: Scanner{}, |
| 121 | + args: args{ |
| 122 | + ctx: context.Background(), |
| 123 | + data: []byte(fmt.Sprintf("vault secret_id: %s", secretId)), |
| 124 | + verify: true, |
| 125 | + }, |
| 126 | + want: nil, |
| 127 | + wantErr: false, |
| 128 | + wantVerificationErr: false, |
| 129 | + }, |
| 130 | + { |
| 131 | + name: "not found - no vault context", |
| 132 | + s: Scanner{}, |
| 133 | + args: args{ |
| 134 | + ctx: context.Background(), |
| 135 | + data: []byte("You cannot find the secret within"), |
| 136 | + verify: true, |
| 137 | + }, |
| 138 | + want: nil, |
| 139 | + wantErr: false, |
| 140 | + wantVerificationErr: false, |
| 141 | + }, |
| 142 | + } |
| 143 | + for _, tt := range tests { |
| 144 | + t.Run(tt.name, func(t *testing.T) { |
| 145 | + got, err := tt.s.FromData(tt.args.ctx, tt.args.verify, tt.args.data) |
| 146 | + if (err != nil) != tt.wantErr { |
| 147 | + t.Errorf("HashiCorpVaultAuth.FromData() error = %v, wantErr %v", err, tt.wantErr) |
| 148 | + return |
| 149 | + } |
| 150 | + for i := range got { |
| 151 | + if len(got[i].Raw) == 0 { |
| 152 | + t.Fatalf("no raw secret present: \n %+v", got[i]) |
| 153 | + } |
| 154 | + if (got[i].VerificationError() != nil) != tt.wantVerificationErr { |
| 155 | + t.Fatalf("wantVerificationError = %v, verification error = %v", tt.wantVerificationErr, got[i].VerificationError()) |
| 156 | + } |
| 157 | + } |
| 158 | + // Fix: Ignore ALL unexported fields using cmpopts.IgnoreUnexported |
| 159 | + ignoreOpts := cmpopts.IgnoreUnexported(detectors.Result{}) |
| 160 | + if diff := cmp.Diff(got, tt.want, ignoreOpts); diff != "" { |
| 161 | + t.Errorf("HashiCorpVaultAuth.FromData() %s diff: (-got +want)\n%s", tt.name, diff) |
| 162 | + } |
| 163 | + }) |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +func BenchmarkFromData(benchmark *testing.B) { |
| 168 | + ctx := context.Background() |
| 169 | + s := Scanner{} |
| 170 | + for name, data := range detectors.MustGetBenchmarkData() { |
| 171 | + benchmark.Run(name, func(b *testing.B) { |
| 172 | + b.ResetTimer() |
| 173 | + for n := 0; n < b.N; n++ { |
| 174 | + _, err := s.FromData(ctx, false, data) |
| 175 | + if err != nil { |
| 176 | + b.Fatal(err) |
| 177 | + } |
| 178 | + } |
| 179 | + }) |
| 180 | + } |
| 181 | +} |
0 commit comments