1
1
package gha
2
2
3
3
import (
4
+ "os"
5
+ "path/filepath"
4
6
"testing"
5
7
"time"
6
8
@@ -9,6 +11,8 @@ import (
9
11
slsacommon "github.com/in-toto/in-toto-golang/in_toto/slsa_provenance/common"
10
12
slsa02 "github.com/in-toto/in-toto-golang/in_toto/slsa_provenance/v0.2"
11
13
slsa1 "github.com/in-toto/in-toto-golang/in_toto/slsa_provenance/v1"
14
+ "github.com/slsa-framework/slsa-verifier/v2/options"
15
+ "github.com/slsa-framework/slsa-verifier/v2/verifiers/utils"
12
16
13
17
serrors "github.com/slsa-framework/slsa-verifier/v2/errors"
14
18
"github.com/slsa-framework/slsa-verifier/v2/verifiers/internal/gha/slsaprovenance/common"
@@ -1182,3 +1186,144 @@ func Test_VerifyVersionedTag(t *testing.T) {
1182
1186
})
1183
1187
}
1184
1188
}
1189
+
1190
+ func Test_VerifyProvenance (t * testing.T ) {
1191
+ t .Parallel ()
1192
+ tests := []struct {
1193
+ name string
1194
+ envelopePath string
1195
+ provenanceOpts * options.ProvenanceOpts
1196
+ trustedBuilderIDName string
1197
+ byob bool
1198
+ expectedID * string
1199
+ expected error
1200
+ }{
1201
+ {
1202
+ name : "Verify Trusted (slsa-github-generator) Bazel Builder (v1.8.0)" ,
1203
+ envelopePath : "bazel-trusted-dsseEnvelope.build.slsa" ,
1204
+ provenanceOpts : & options.ProvenanceOpts {
1205
+ ExpectedBranch : nil ,
1206
+ ExpectedTag : nil ,
1207
+ ExpectedVersionedTag : nil ,
1208
+ ExpectedDigest : "caaadba2846905ac477c777e96a636e1c2e067fdf6fed90ec9eeca4df18d6ed9" ,
1209
+ ExpectedSourceURI : "github.com/enteraga6/slsa-lvl3-generic-provenance-with-bazel-example" ,
1210
+ ExpectedBuilderID : "https://github.com/slsa-framework/slsa-github-generator/.github/workflows/delegator_lowperms-generic_slsa3.yml@refs/tags/v1.8.0" ,
1211
+ ExpectedWorkflowInputs : map [string ]string {},
1212
+ },
1213
+ byob : true ,
1214
+ trustedBuilderIDName : "https://github.com/slsa-framework/slsa-github-generator/.github/workflows/delegator_lowperms-generic_slsa3.yml@refs/tags/v1.8.0" ,
1215
+ expectedID : nil ,
1216
+ },
1217
+ {
1218
+ name : "Verify Un-Trusted (slsa-github-generator) Bazel Builder (from enteraga6/slsa-github-generator)" ,
1219
+ envelopePath : "bazel-untrusted-dsseEnvelope.sigstore" ,
1220
+ provenanceOpts : & options.ProvenanceOpts {
1221
+ ExpectedBranch : nil ,
1222
+ ExpectedTag : nil ,
1223
+ ExpectedVersionedTag : nil ,
1224
+ ExpectedDigest : "caaadba2846905ac477c777e96a636e1c2e067fdf6fed90ec9eeca4df18d6ed9" ,
1225
+ ExpectedSourceURI : "github.com/enteraga6/slsa-lvl3-generic-provenance-with-bazel-example" ,
1226
+ ExpectedBuilderID : "https://github.com/slsa-framework/slsa-github-generator/.github/workflows/delegator_lowperms-generic_slsa3.yml@refs/tags/v1.7.0" ,
1227
+ ExpectedWorkflowInputs : map [string ]string {},
1228
+ },
1229
+ byob : true ,
1230
+ trustedBuilderIDName : "https://github.com/slsa-framework/slsa-github-generator/.github/workflows/delegator_lowperms-generic_slsa3.yml@refs/tags/v1.7.0" ,
1231
+ expectedID : nil ,
1232
+ expected : serrors .ErrorInvalidBuilderID ,
1233
+ },
1234
+ {
1235
+ name : "Verify Trusted - Empty ExpectedBuilderID" ,
1236
+ envelopePath : "bazel-trusted-dsseEnvelope.build.slsa" ,
1237
+ provenanceOpts : & options.ProvenanceOpts {
1238
+ ExpectedBranch : nil ,
1239
+ ExpectedTag : nil ,
1240
+ ExpectedVersionedTag : nil ,
1241
+ ExpectedDigest : "caaadba2846905ac477c777e96a636e1c2e067fdf6fed90ec9eeca4df18d6ed9" ,
1242
+ ExpectedSourceURI : "github.com/enteraga6/slsa-lvl3-generic-provenance-with-bazel-example" ,
1243
+ ExpectedBuilderID : "" ,
1244
+ ExpectedWorkflowInputs : map [string ]string {},
1245
+ },
1246
+ byob : true ,
1247
+ trustedBuilderIDName : "https://github.com/slsa-framework/slsa-github-generator/.github/workflows/delegator_lowperms-generic_slsa3.yml@refs/tags/v1.8.0" ,
1248
+ expectedID : nil ,
1249
+ },
1250
+ }
1251
+ for _ , tt := range tests {
1252
+ tt := tt // Re-initializing variable so it is not changed while executing the closure below
1253
+ t .Run (tt .name , func (t * testing.T ) {
1254
+ t .Parallel ()
1255
+ trustedBuilderID , tErr := utils .TrustedBuilderIDNew (tt .trustedBuilderIDName , true )
1256
+ if tErr != nil {
1257
+ t .Errorf ("Provenance Verification FAILED. Error: %v" , tErr )
1258
+ }
1259
+
1260
+ envelopeBytes , err := os .ReadFile (filepath .Join ("testdata" , tt .envelopePath ))
1261
+ if err != nil {
1262
+ t .Errorf ("os.ReadFile: %v" , err )
1263
+ }
1264
+
1265
+ env , err := EnvelopeFromBytes (envelopeBytes )
1266
+ if err != nil {
1267
+ t .Errorf ("unexpected error parsing envelope %v" , err )
1268
+ }
1269
+
1270
+ if err := VerifyProvenance (env , tt .provenanceOpts , trustedBuilderID , tt .byob , tt .expectedID ); ! errCmp (err , tt .expected ) {
1271
+ t .Errorf (cmp .Diff (err , tt .expected ))
1272
+ }
1273
+ })
1274
+ }
1275
+ }
1276
+
1277
+ func Test_VerifyUntrustedProvenance (t * testing.T ) {
1278
+ t .Parallel ()
1279
+ tests := []struct {
1280
+ name string
1281
+ envelopePath string
1282
+ provenanceOpts * options.ProvenanceOpts
1283
+ trustedBuilderIDName string
1284
+ byob bool
1285
+ expectedID * string
1286
+ expected error
1287
+ }{
1288
+ {
1289
+ name : "Verify Un-Trusted (slsa-github-generator) Bazel Builder (from enteraga6/slsa-github-generator)" ,
1290
+ envelopePath : "bazel-untrusted-dsseEnvelope.sigstore" ,
1291
+ provenanceOpts : & options.ProvenanceOpts {
1292
+ ExpectedBranch : nil ,
1293
+ ExpectedTag : nil ,
1294
+ ExpectedVersionedTag : nil ,
1295
+ ExpectedDigest : "caaadba2846905ac477c777e96a636e1c2e067fdf6fed90ec9eeca4df18d6ed9" ,
1296
+ ExpectedSourceURI : "github.com/enteraga6/slsa-lvl3-generic-provenance-with-bazel-example" ,
1297
+ ExpectedBuilderID : "https://github.com/slsa-framework/slsa-github-generator/.github/workflows/delegator_lowperms-generic_slsa3.yml@refs/tags/v1.7.0" ,
1298
+ ExpectedWorkflowInputs : map [string ]string {},
1299
+ },
1300
+ byob : true ,
1301
+ trustedBuilderIDName : "https://github.com/slsa-framework/slsa-github-generator/.github/workflows/delegator_lowperms-generic_slsa3.yml@refs/tags/v1.7.0" ,
1302
+ expectedID : nil ,
1303
+ },
1304
+ }
1305
+ for _ , tt := range tests {
1306
+ tt := tt // Re-initializing variable so it is not changed while executing the closure below
1307
+ t .Run (tt .name , func (t * testing.T ) {
1308
+ t .Parallel ()
1309
+ trustedBuilderID , tErr := utils .TrustedBuilderIDNew (tt .trustedBuilderIDName , true )
1310
+ if tErr != nil {
1311
+ t .Errorf ("Provenance Verification FAILED. Error: %v" , tErr )
1312
+ }
1313
+
1314
+ envelopeBytes , err := os .ReadFile (filepath .Join ("testdata" , tt .envelopePath ))
1315
+ if err != nil {
1316
+ t .Errorf ("os.ReadFile: %v" , err )
1317
+ }
1318
+
1319
+ env , err := EnvelopeFromBytes (envelopeBytes )
1320
+ if err != nil {
1321
+ t .Errorf ("unexpected error parsing envelope %v" , err )
1322
+ }
1323
+
1324
+ if err := VerifyProvenance (env , tt .provenanceOpts , trustedBuilderID , tt .byob , tt .expectedID ); errCmp (err , tt .expected ) {
1325
+ t .Errorf (cmp .Diff (err , tt .expected ))
1326
+ }
1327
+ })
1328
+ }
1329
+ }
0 commit comments