@@ -52,7 +52,7 @@ import (
5252 sreconcile "github.com/fluxcd/source-controller/internal/reconcile"
5353)
5454
55- const publicKeyFileName = "pub.pgp "
55+ const publicKeyFileName = "pub.gpg "
5656
5757func TestHelmChartReconciler_Reconcile (t * testing.T ) {
5858 g := NewWithT (t )
@@ -458,14 +458,19 @@ func TestHelmChartReconciler_reconcileSource(t *testing.T) {
458458 }
459459 g .Expect (storage .Archive (gitArtifact , "testdata/charts" , nil )).To (Succeed ())
460460
461+ keyring , err := os .ReadFile ("testdata/charts/pub.gpg" )
462+ g .Expect (err ).ToNot (HaveOccurred ())
463+ g .Expect (keyring ).ToNot (BeEmpty ())
464+
461465 tests := []struct {
462- name string
463- source sourcev1.Source
464- beforeFunc func (obj * sourcev1.HelmChart )
465- want sreconcile.Result
466- wantErr error
467- assertFunc func (g * WithT , build chart.Build , obj sourcev1.HelmChart )
468- cleanFunc func (g * WithT , build * chart.Build )
466+ name string
467+ source sourcev1.Source
468+ keyringSecret * corev1.Secret
469+ beforeFunc func (obj * sourcev1.HelmChart )
470+ want sreconcile.Result
471+ wantErr error
472+ assertFunc func (g * WithT , build chart.Build , obj sourcev1.HelmChart )
473+ cleanFunc func (g * WithT , build * chart.Build )
469474 }{
470475 {
471476 name : "Observes Artifact revision and build result" ,
@@ -501,6 +506,59 @@ func TestHelmChartReconciler_reconcileSource(t *testing.T) {
501506 g .Expect (os .Remove (build .Path )).To (Succeed ())
502507 },
503508 },
509+ {
510+ name : "Observes Artifact revision and build result with valid signature" ,
511+ source : & sourcev1.GitRepository {
512+ ObjectMeta : metav1.ObjectMeta {
513+ Name : "gitrepository" ,
514+ Namespace : "default" ,
515+ },
516+ Status : sourcev1.GitRepositoryStatus {
517+ Artifact : gitArtifact ,
518+ },
519+ },
520+ keyringSecret : & corev1.Secret {
521+ ObjectMeta : metav1.ObjectMeta {
522+ Name : "keyring-secret" ,
523+ Namespace : "default" ,
524+ },
525+ Data : map [string ][]byte {
526+ publicKeyFileName : keyring ,
527+ },
528+ },
529+ beforeFunc : func (obj * sourcev1.HelmChart ) {
530+ obj .Spec .Chart = "testdata/charts/helmchart-0.1.0.tgz"
531+ obj .Spec .SourceRef = sourcev1.LocalHelmChartSourceReference {
532+ Name : "gitrepository" ,
533+ Kind : sourcev1 .GitRepositoryKind ,
534+ }
535+ obj .Spec .VerificationKeyring = & sourcev1.VerificationKeyring {
536+ SecretRef : meta.LocalObjectReference {
537+ Name : "keyring-secret" ,
538+ },
539+ Key : publicKeyFileName ,
540+ }
541+ },
542+ want : sreconcile .ResultSuccess ,
543+ assertFunc : func (g * WithT , build chart.Build , obj sourcev1.HelmChart ) {
544+ g .Expect (build .Complete ()).To (BeTrue ())
545+ g .Expect (build .Name ).To (Equal ("helmchart" ))
546+ g .Expect (build .Version ).To (Equal ("0.1.0" ))
547+ g .Expect (build .Path ).To (BeARegularFile ())
548+ g .Expect (build .VerificationSignature ).ToNot (BeNil ())
549+ g .Expect (build .ProvFilePath ).To (BeARegularFile ())
550+
551+ g .Expect (obj .Status .ObservedSourceArtifactRevision ).To (Equal (gitArtifact .Revision ))
552+ g .Expect (obj .Status .Conditions ).To (conditions .MatchConditions ([]metav1.Condition {
553+ * conditions .TrueCondition (sourcev1 .ArtifactOutdatedCondition , "NewChart" , "pulled 'helmchart' chart with version '0.1.0'" ),
554+ * conditions .TrueCondition (sourcev1 .SourceVerifiedCondition , sourcev1 .ChartVerifiedSucceededReason , "chart signed by: TestUser using key with fingeprint: 943CB5929ECDA2B5B5EC88BC7035BA97D32A87C1 and hash verified: sha256:007c7b7446eebcb18caeffe9898a3356ba1795f54df40ad39cfcc7382874a10a" ),
555+ }))
556+ },
557+ cleanFunc : func (g * WithT , build * chart.Build ) {
558+ g .Expect (os .Remove (build .Path )).To (Succeed ())
559+ g .Expect (os .Remove (build .ProvFilePath )).To (Succeed ())
560+ },
561+ },
504562 {
505563 name : "Error on unavailable source" ,
506564 beforeFunc : func (obj * sourcev1.HelmChart ) {
@@ -605,6 +663,9 @@ func TestHelmChartReconciler_reconcileSource(t *testing.T) {
605663 if tt .source != nil {
606664 clientBuilder .WithRuntimeObjects (tt .source )
607665 }
666+ if tt .keyringSecret != nil {
667+ clientBuilder .WithRuntimeObjects (tt .keyringSecret )
668+ }
608669
609670 r := & HelmChartReconciler {
610671 Client : clientBuilder .Build (),
@@ -1129,7 +1190,7 @@ func TestHelmChartReconciler_reconcileArtifact(t *testing.T) {
11291190 },
11301191 {
11311192 name : "Copying artifact to storage from build makes Ready=True" ,
1132- build : mockChartBuild ("helmchart" , "0.1.0" , "testdata/charts/helmchart-0.1.0.tgz" , "" ),
1193+ build : mockChartBuild ("helmchart" , "0.1.0" , "testdata/charts/helmchart-0.1.0.tgz" ),
11331194 beforeFunc : func (obj * sourcev1.HelmChart ) {
11341195 conditions .MarkTrue (obj , sourcev1 .ArtifactOutdatedCondition , "Foo" , "" )
11351196 },
@@ -1145,24 +1206,6 @@ func TestHelmChartReconciler_reconcileArtifact(t *testing.T) {
11451206 * conditions .TrueCondition (meta .ReadyCondition , sourcev1 .ChartPullSucceededReason , "pulled 'helmchart' chart with version '0.1.0'" ),
11461207 },
11471208 },
1148- {
1149- name : "Build with a verified signature sets SourceVerifiedCondition=Truue" ,
1150- build : mockChartBuild ("helmchart" , "0.1.0" , "testdata/charts/helmchart-0.1.0.tgz" , "testdata/charts/helmchart-0.1.0.tgz.prov" ),
1151- beforeFunc : func (obj * sourcev1.HelmChart ) {
1152- obj .Status .Artifact = & sourcev1.Artifact {
1153- Path : "testdata/charts/helmchart-0.1.0.tgz" ,
1154- }
1155- },
1156- want : sreconcile .ResultSuccess ,
1157- afterFunc : func (t * WithT , obj * sourcev1.HelmChart ) {
1158- provArtifact := testStorage .NewArtifactFor (obj .Kind , obj .GetObjectMeta (), "0.1.0" , "helmchart-0.1.0.tgz.prov" )
1159- t .Expect (provArtifact .Path ).ToNot (BeEmpty ())
1160- },
1161- assertConditions : []metav1.Condition {
1162- * conditions .TrueCondition (meta .ReadyCondition , sourcev1 .ChartPullSucceededReason , "pulled 'helmchart' chart with version '0.1.0'" ),
1163- * conditions .TrueCondition (sourcev1 .SourceVerifiedCondition , sourcev1 .ChartPullSucceededReason , "chart signed by: TestUser1,TestUser2 using key with fingeprint: 0102000000000000000000000000000000000000 and hash verified: 53gntj23r24asnf0" ),
1164- },
1165- },
11661209 {
11671210 name : "Up-to-date chart build does not persist artifact to storage" ,
11681211 build : & chart.Build {
@@ -1208,7 +1251,7 @@ func TestHelmChartReconciler_reconcileArtifact(t *testing.T) {
12081251 },
12091252 {
12101253 name : "Removes ArtifactOutdatedCondition after creating new artifact" ,
1211- build : mockChartBuild ("helmchart" , "0.1.0" , "testdata/charts/helmchart-0.1.0.tgz" , "" ),
1254+ build : mockChartBuild ("helmchart" , "0.1.0" , "testdata/charts/helmchart-0.1.0.tgz" ),
12121255 beforeFunc : func (obj * sourcev1.HelmChart ) {
12131256 conditions .MarkTrue (obj , sourcev1 .ArtifactOutdatedCondition , "Foo" , "" )
12141257 },
@@ -1226,7 +1269,7 @@ func TestHelmChartReconciler_reconcileArtifact(t *testing.T) {
12261269 },
12271270 {
12281271 name : "Creates latest symlink to the created artifact" ,
1229- build : mockChartBuild ("helmchart" , "0.1.0" , "testdata/charts/helmchart-0.1.0.tgz" , "" ),
1272+ build : mockChartBuild ("helmchart" , "0.1.0" , "testdata/charts/helmchart-0.1.0.tgz" ),
12301273 afterFunc : func (t * WithT , obj * sourcev1.HelmChart ) {
12311274 t .Expect (obj .GetArtifact ()).ToNot (BeNil ())
12321275
@@ -1726,10 +1769,8 @@ func TestHelmChartReconciler_reconcileSubRecs(t *testing.T) {
17261769 }
17271770}
17281771
1729- func mockChartBuild (name , version , path , provFilePath string ) * chart.Build {
1772+ func mockChartBuild (name , version , path string ) * chart.Build {
17301773 var copyP string
1731- var copyPP string
1732- var verSig * chart.VerificationSignature
17331774 if path != "" {
17341775 f , err := os .Open (path )
17351776 if err == nil {
@@ -1743,29 +1784,9 @@ func mockChartBuild(name, version, path, provFilePath string) *chart.Build {
17431784 }
17441785 }
17451786 }
1746- if provFilePath != "" {
1747- f , err := os .Open (provFilePath )
1748- if err == nil {
1749- defer f .Close ()
1750- ff , err := os .CreateTemp ("" , "chart-mock-*.tgz.prov" )
1751- if err == nil {
1752- defer ff .Close ()
1753- if _ , err = io .Copy (ff , f ); err == nil {
1754- copyPP = ff .Name ()
1755- }
1756- }
1757- verSig = & chart.VerificationSignature {
1758- FileHash : "53gntj23r24asnf0" ,
1759- Identities : []string {"TestUser1" , "TestUser2" },
1760- KeyFingerprint : [20 ]byte {1 , 2 },
1761- }
1762- }
1763- }
17641787 return & chart.Build {
1765- Name : name ,
1766- Version : version ,
1767- Path : copyP ,
1768- ProvFilePath : copyPP ,
1769- VerificationSignature : verSig ,
1788+ Name : name ,
1789+ Version : version ,
1790+ Path : copyP ,
17701791 }
17711792}
0 commit comments