@@ -17,20 +17,16 @@ limitations under the License.
1717package source
1818
1919import (
20- "bytes"
21- "encoding/pem"
2220 "errors"
2321 "strings"
2422 "testing"
2523
26- jks "github.com/pavlo-v-chernykh/keystore-go/v4"
2724 "github.com/stretchr/testify/assert"
2825 corev1 "k8s.io/api/core/v1"
2926 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3027 "k8s.io/apimachinery/pkg/runtime"
3128 "k8s.io/utils/ptr"
3229 "sigs.k8s.io/controller-runtime/pkg/client/fake"
33- "software.sslmate.com/src/go-pkcs12"
3430
3531 trustapi "github.com/cert-manager/trust-manager/pkg/apis/trust/v1alpha1"
3632 "github.com/cert-manager/trust-manager/pkg/bundle/controller"
@@ -40,26 +36,15 @@ import (
4036 "github.com/cert-manager/trust-manager/test/dummy"
4137)
4238
43- const (
44- jksKey = "trust.jks"
45- pkcs12Key = "trust.p12"
46- data = dummy .TestCertificate1
47- )
48-
4939func Test_BuildBundle (t * testing.T ) {
5040 tests := map [string ]struct {
5141 sources []trustapi.BundleSource
5242 filterExpired bool
53- formats * trustapi.AdditionalFormats
5443 objects []runtime.Object
5544 expData string
5645 expError bool
5746 expNotFoundError bool
5847 expInvalidSecretSourceError bool
59- bool
60- expJKS bool
61- expPKCS12 bool
62- expPassword * string
6348 }{
6449 "if no sources defined, should return NotFoundError" : {
6550 expError : true ,
@@ -333,85 +318,6 @@ func Test_BuildBundle(t *testing.T) {
333318 expError : false ,
334319 expNotFoundError : false ,
335320 },
336-
337- "if has JKS target, return binaryData with encoded JKS" : {
338- sources : []trustapi.BundleSource {
339- {ConfigMap : & trustapi.SourceObjectKeySelector {Name : "configmap" , Key : "key" }},
340- },
341- formats : & trustapi.AdditionalFormats {
342- JKS : & trustapi.JKS {
343- KeySelector : trustapi.KeySelector {
344- Key : jksKey ,
345- },
346- Password : ptr .To (trustapi .DefaultJKSPassword ),
347- },
348- },
349- objects : []runtime.Object {& corev1.ConfigMap {
350- ObjectMeta : metav1.ObjectMeta {Name : "configmap" },
351- Data : map [string ]string {"key" : dummy .TestCertificate1 },
352- }},
353- expData : dummy .JoinCerts (dummy .TestCertificate1 ),
354- expJKS : true ,
355- },
356- "if has JKS target with arbitrary password, return binaryData with encoded JKS" : {
357- sources : []trustapi.BundleSource {
358- {ConfigMap : & trustapi.SourceObjectKeySelector {Name : "configmap" , Key : "key" }},
359- },
360- formats : & trustapi.AdditionalFormats {
361- JKS : & trustapi.JKS {
362- KeySelector : trustapi.KeySelector {
363- Key : jksKey ,
364- },
365- Password : ptr .To ("testPasswd123" ),
366- },
367- },
368- objects : []runtime.Object {& corev1.ConfigMap {
369- ObjectMeta : metav1.ObjectMeta {Name : "configmap" },
370- Data : map [string ]string {"key" : dummy .TestCertificate1 },
371- }},
372- expData : dummy .JoinCerts (dummy .TestCertificate1 ),
373- expJKS : true ,
374- expPassword : ptr .To ("testPasswd123" ),
375- },
376- "if has PKCS12 target, return binaryData with encoded PKCS12" : {
377- sources : []trustapi.BundleSource {
378- {ConfigMap : & trustapi.SourceObjectKeySelector {Name : "configmap" , Key : "key" }},
379- },
380- formats : & trustapi.AdditionalFormats {
381- PKCS12 : & trustapi.PKCS12 {
382- KeySelector : trustapi.KeySelector {
383- Key : pkcs12Key ,
384- },
385- Password : ptr .To (trustapi .DefaultPKCS12Password ),
386- },
387- },
388- objects : []runtime.Object {& corev1.ConfigMap {
389- ObjectMeta : metav1.ObjectMeta {Name : "configmap" },
390- Data : map [string ]string {"key" : dummy .TestCertificate1 },
391- }},
392- expData : dummy .JoinCerts (dummy .TestCertificate1 ),
393- expPKCS12 : true ,
394- },
395- "if has PKCS12 target with arbitrary password, return binaryData with encoded PKCS12" : {
396- sources : []trustapi.BundleSource {
397- {ConfigMap : & trustapi.SourceObjectKeySelector {Name : "configmap" , Key : "key" }},
398- },
399- formats : & trustapi.AdditionalFormats {
400- PKCS12 : & trustapi.PKCS12 {
401- KeySelector : trustapi.KeySelector {
402- Key : pkcs12Key ,
403- },
404- Password : ptr .To ("testPasswd123" ),
405- },
406- },
407- objects : []runtime.Object {& corev1.ConfigMap {
408- ObjectMeta : metav1.ObjectMeta {Name : "configmap" },
409- Data : map [string ]string {"key" : dummy .TestCertificate1 },
410- }},
411- expData : dummy .JoinCerts (dummy .TestCertificate1 ),
412- expPKCS12 : true ,
413- expPassword : ptr .To ("testPasswd123" ),
414- },
415321 }
416322
417323 for name , tt := range tests {
@@ -433,24 +339,7 @@ func Test_BuildBundle(t *testing.T) {
433339 Options : controller.Options {FilterExpiredCerts : tt .filterExpired },
434340 }
435341
436- // for corresponding store if arbitrary password is expected then set it instead of default one
437- var password string
438- if tt .expJKS {
439- if tt .expPassword != nil {
440- password = * tt .expPassword
441- } else {
442- password = trustapi .DefaultJKSPassword
443- }
444- }
445- if tt .expPKCS12 {
446- if tt .expPassword != nil {
447- password = * tt .expPassword
448- } else {
449- password = trustapi .DefaultPKCS12Password
450- }
451- }
452-
453- resolvedBundle , err := b .BuildBundle (t .Context (), tt .sources , tt .formats )
342+ resolvedBundle , err := b .BuildBundle (t .Context (), tt .sources )
454343
455344 if (err != nil ) != tt .expError {
456345 t .Errorf ("unexpected error, exp=%t got=%v" , tt .expError , err )
@@ -462,45 +351,9 @@ func Test_BuildBundle(t *testing.T) {
462351 t .Errorf ("unexpected InvalidSecretError, exp=%t got=%v" , tt .expInvalidSecretSourceError , err )
463352 }
464353
465- if resolvedBundle .Data != tt .expData {
466- t .Errorf ("unexpected data, exp=%q got=%q" , tt .expData , resolvedBundle .Data )
467- }
468-
469- binData , jksExists := resolvedBundle .BinaryData [jksKey ]
470- assert .Equal (t , tt .expJKS , jksExists )
471-
472- if tt .expJKS {
473- reader := bytes .NewReader (binData )
474-
475- ks := jks .New ()
476-
477- err := ks .Load (reader , []byte (password ))
478- assert .Nil (t , err )
479-
480- entryNames := ks .Aliases ()
481-
482- assert .Len (t , entryNames , 1 )
483- assert .True (t , ks .IsTrustedCertificateEntry (entryNames [0 ]))
484-
485- // Safe to ignore errors here, we've tested that it's present and a TrustedCertificateEntry
486- cert , _ := ks .GetTrustedCertificateEntry (entryNames [0 ])
487-
488- // Only one certificate block for this test, so we can safely ignore the `remaining` byte array
489- p , _ := pem .Decode ([]byte (data ))
490- assert .Equal (t , p .Bytes , cert .Certificate .Content )
491- }
492-
493- binData , pkcs12Exists := resolvedBundle .BinaryData [pkcs12Key ]
494- assert .Equal (t , tt .expPKCS12 , pkcs12Exists )
495-
496- if tt .expPKCS12 {
497- cas , err := pkcs12 .DecodeTrustStore (binData , password )
498- assert .Nil (t , err )
499- assert .Len (t , cas , 1 )
500-
501- // Only one certificate block for this test, so we can safely ignore the `remaining` byte array
502- p , _ := pem .Decode ([]byte (data ))
503- assert .Equal (t , p .Bytes , cas [0 ].Raw )
354+ data := resolvedBundle .CertPool .PEM ()
355+ if data != tt .expData {
356+ t .Errorf ("unexpected data, exp=%q got=%q" , tt .expData , data )
504357 }
505358 })
506359 }
0 commit comments