@@ -33,6 +33,7 @@ import (
3333
3434 "github.com/ibm-messaging/mq-container/internal/keystore"
3535 "github.com/ibm-messaging/mq-container/internal/mqtemplate"
36+ "github.com/ibm-messaging/mq-container/internal/pathutils"
3637 "github.com/ibm-messaging/mq-container/pkg/logger"
3738)
3839
@@ -175,15 +176,15 @@ func generateAllKeystores(keystoreDir string, p12TruststoreRequired bool) (TLSSt
175176 }
176177
177178 // Create the CMS Keystore
178- cmsKeystore .Keystore = keystore .NewCMSKeyStore (filepath . Join (keystoreDir , cmsKeystoreName ), cmsKeystore .Password )
179+ cmsKeystore .Keystore = keystore .NewCMSKeyStore (pathutils . CleanPath (keystoreDir , cmsKeystoreName ), cmsKeystore .Password )
179180 err = cmsKeystore .Keystore .Create ()
180181 if err != nil {
181182 return TLSStore {cmsKeystore , p12Truststore }, fmt .Errorf ("Failed to create CMS Keystore: %v" , err )
182183 }
183184
184185 // Create the PKCS#12 Truststore (if required)
185186 if p12TruststoreRequired {
186- p12Truststore .Keystore = keystore .NewPKCS12KeyStore (filepath . Join (keystoreDir , p12TruststoreName ), p12Truststore .Password )
187+ p12Truststore .Keystore = keystore .NewPKCS12KeyStore (pathutils . CleanPath (keystoreDir , p12TruststoreName ), p12Truststore .Password )
187188 err = p12Truststore .Keystore .Create ()
188189 if err != nil {
189190 return TLSStore {cmsKeystore , p12Truststore }, fmt .Errorf ("Failed to create PKCS#12 Truststore: %v" , err )
@@ -205,7 +206,7 @@ func processKeys(tlsStore *TLSStore, keystoreDir string, keyDir string) (string,
205206
206207 // Process each set of keys - each set should contain files: *.key & *.crt
207208 for _ , keySet := range keyList {
208- keys , _ := os .ReadDir (filepath . Join (keyDir , keySet .Name ()))
209+ keys , _ := os .ReadDir (pathutils . CleanPath (keyDir , keySet .Name ()))
209210
210211 // Ensure the label of the set of keys does not match the name of the PKCS#12 Truststore
211212 if keySet .Name () == p12TruststoreName [0 :len (p12TruststoreName )- len (filepath .Ext (p12TruststoreName ))] {
@@ -234,16 +235,17 @@ func processKeys(tlsStore *TLSStore, keystoreDir string, keyDir string) (string,
234235 if err != nil {
235236 return "" , fmt .Errorf ("Failed to encode PKCS#12 Keystore %s: %v" , keySet .Name ()+ ".p12" , err )
236237 }
238+ keystorePath := pathutils .CleanPath (keystoreDir , keySet .Name ()+ ".p12" )
237239 // #nosec G306 - this gives permissions to owner/s group only.
238- err = os .WriteFile (filepath . Join ( keystoreDir , keySet . Name () + ".p12" ) , file , 0644 )
240+ err = os .WriteFile (keystorePath , file , 0644 )
239241 if err != nil {
240- return "" , fmt .Errorf ("Failed to write PKCS#12 Keystore %s: %v" , filepath . Join ( keystoreDir , keySet . Name () + ".p12" ) , err )
242+ return "" , fmt .Errorf ("Failed to write PKCS#12 Keystore %s: %v" , keystorePath , err )
241243 }
242244
243245 // Import the new PKCS#12 Keystore into the CMS Keystore
244- err = tlsStore .Keystore .Keystore .Import (filepath . Join ( keystoreDir , keySet . Name () + ".p12" ) , tlsStore .Keystore .Password )
246+ err = tlsStore .Keystore .Keystore .Import (keystorePath , tlsStore .Keystore .Password )
245247 if err != nil {
246- return "" , fmt .Errorf ("Failed to import keys from %s into CMS Keystore: %v" , filepath . Join ( keystoreDir , keySet . Name () + ".p12" ) , err )
248+ return "" , fmt .Errorf ("Failed to import keys from %s into CMS Keystore: %v" , keystorePath , err )
247249 }
248250
249251 // Relabel the certificate in the CMS Keystore
@@ -271,14 +273,15 @@ func processTrustCertificates(tlsStore *TLSStore, trustDir string) error {
271273
272274 // Process each set of keys
273275 for _ , trustSet := range trustList {
274- keys , _ := os .ReadDir (filepath . Join (trustDir , trustSet .Name ()))
276+ keys , _ := os .ReadDir (pathutils . CleanPath (trustDir , trustSet .Name ()))
275277
276278 for _ , key := range keys {
277279 if strings .HasSuffix (key .Name (), ".crt" ) {
280+ trustSetPath := pathutils .CleanPath (trustDir , trustSet .Name (), key .Name ())
278281 // #nosec G304 - filename variable is derived from contents of 'trustDir' which is a defined constant
279- file , err := os .ReadFile (filepath . Join ( trustDir , trustSet . Name (), key . Name ()) )
282+ file , err := os .ReadFile (trustSetPath )
280283 if err != nil {
281- return fmt .Errorf ("Failed to read file %s: %v" , filepath . Join ( trustDir , trustSet . Name (), key . Name ()) , err )
284+ return fmt .Errorf ("Failed to read file %s: %v" , trustSetPath , err )
282285 }
283286
284287 for string (file ) != "" {
@@ -334,15 +337,16 @@ func processPrivateKey(keyDir string, keySetName string, keys []os.DirEntry) (in
334337
335338 for _ , key := range keys {
336339
340+ privateKeyPath := pathutils .CleanPath (keyDir , keySetName , key .Name ())
337341 if strings .HasSuffix (key .Name (), ".key" ) {
338342 // #nosec G304 - filename variable is derived from contents of 'keyDir' which is a defined constant
339- file , err := os .ReadFile (filepath . Join ( keyDir , keySetName , key . Name ()) )
343+ file , err := os .ReadFile (privateKeyPath )
340344 if err != nil {
341- return nil , "" , fmt .Errorf ("Failed to read private key %s: %v" , filepath . Join ( keyDir , keySetName , key . Name ()) , err )
345+ return nil , "" , fmt .Errorf ("Failed to read private key %s: %v" , privateKeyPath , err )
342346 }
343347 block , _ := pem .Decode (file )
344348 if block == nil {
345- return nil , "" , fmt .Errorf ("Failed to decode private key %s: pem.Decode returned nil" , filepath . Join ( keyDir , keySetName , key . Name ()) )
349+ return nil , "" , fmt .Errorf ("Failed to decode private key %s: pem.Decode returned nil" , privateKeyPath )
346350 }
347351
348352 // Check if the private key is PKCS1
@@ -351,7 +355,7 @@ func processPrivateKey(keyDir string, keySetName string, keys []os.DirEntry) (in
351355 // Check if the private key is PKCS8
352356 privateKey , err = x509 .ParsePKCS8PrivateKey (block .Bytes )
353357 if err != nil {
354- return nil , "" , fmt .Errorf ("Failed to parse private key %s: %v" , filepath . Join ( keyDir , keySetName , key . Name ()) , err )
358+ return nil , "" , fmt .Errorf ("Failed to parse private key %s: %v" , privateKeyPath , err )
355359 }
356360 }
357361 keyPrefix = key .Name ()[0 : len (key .Name ())- len (filepath .Ext (key .Name ()))]
@@ -369,19 +373,20 @@ func processCertificates(keyDir string, keySetName, keyPrefix string, keys []os.
369373
370374 for _ , key := range keys {
371375
376+ keystorePath := pathutils .CleanPath (keyDir , keySetName , key .Name ())
372377 if strings .HasPrefix (key .Name (), keyPrefix ) && strings .HasSuffix (key .Name (), ".crt" ) {
373378 // #nosec G304 - filename variable is derived from contents of 'keyDir' which is a defined constant
374- file , err := os .ReadFile (filepath . Join ( keyDir , keySetName , key . Name ()) )
379+ file , err := os .ReadFile (keystorePath )
375380 if err != nil {
376- return nil , nil , fmt .Errorf ("Failed to read public certificate %s: %v" , filepath . Join ( keyDir , keySetName , key . Name ()) , err )
381+ return nil , nil , fmt .Errorf ("Failed to read public certificate %s: %v" , keystorePath , err )
377382 }
378383 block , _ := pem .Decode (file )
379384 if block == nil {
380- return nil , nil , fmt .Errorf ("Failed to decode public certificate %s: pem.Decode returned nil" , filepath . Join ( keyDir , keySetName , key . Name ()) )
385+ return nil , nil , fmt .Errorf ("Failed to decode public certificate %s: pem.Decode returned nil" , keystorePath )
381386 }
382387 publicCertificate , err = x509 .ParseCertificate (block .Bytes )
383388 if err != nil {
384- return nil , nil , fmt .Errorf ("Failed to parse public certificate %s: %v" , filepath . Join ( keyDir , keySetName , key . Name ()) , err )
389+ return nil , nil , fmt .Errorf ("Failed to parse public certificate %s: %v" , keystorePath , err )
385390 }
386391
387392 // Add to known certificates for the CMS Keystore
@@ -392,9 +397,9 @@ func processCertificates(keyDir string, keySetName, keyPrefix string, keys []os.
392397
393398 } else if strings .HasSuffix (key .Name (), ".crt" ) {
394399 // #nosec G304 - filename variable is derived from contents of 'keyDir' which is a defined constant
395- file , err := os .ReadFile (filepath . Join ( keyDir , keySetName , key . Name ()) )
400+ file , err := os .ReadFile (keystorePath )
396401 if err != nil {
397- return nil , nil , fmt .Errorf ("Failed to read CA certificate %s: %v" , filepath . Join ( keyDir , keySetName , key . Name ()) , err )
402+ return nil , nil , fmt .Errorf ("Failed to read CA certificate %s: %v" , keystorePath , err )
398403 }
399404
400405 for string (file ) != "" {
@@ -420,7 +425,7 @@ func processCertificates(keyDir string, keySetName, keyPrefix string, keys []os.
420425
421426 certificate , err := x509 .ParseCertificate (block .Bytes )
422427 if err != nil {
423- return nil , nil , fmt .Errorf ("Failed to parse CA certificate %s: %v" , filepath . Join ( keyDir , keySetName , key . Name ()) , err )
428+ return nil , nil , fmt .Errorf ("Failed to parse CA certificate %s: %v" , keystorePath , err )
424429 }
425430 caCertificate = append (caCertificate , certificate )
426431 }
@@ -467,7 +472,7 @@ func relabelCertificate(newLabel string, cmsKeystore *KeyStoreData) error {
467472// addCertificatesToTruststore adds trust certificates to the PKCS#12 Truststore
468473func addCertificatesToTruststore (p12Truststore * KeyStoreData ) error {
469474
470- temporaryPemFile := filepath . Join ("/tmp" , "trust.pem" )
475+ temporaryPemFile := pathutils . CleanPath ("/tmp" , "trust.pem" )
471476 _ , err := os .Stat (temporaryPemFile )
472477 if err == nil {
473478 err = os .Remove (temporaryPemFile )
@@ -509,7 +514,7 @@ func addCertificatesToTruststore(p12Truststore *KeyStoreData) error {
509514// addCertificatesToCMSKeystore adds trust certificates to the CMS keystore
510515func addCertificatesToCMSKeystore (cmsKeystore * KeyStoreData ) error {
511516
512- temporaryPemFile := filepath . Join ("/tmp" , "cmsTrust.pem" )
517+ temporaryPemFile := pathutils . CleanPath ("/tmp" , "cmsTrust.pem" )
513518 _ , err := os .Stat (temporaryPemFile )
514519 if err == nil {
515520 err = os .Remove (temporaryPemFile )
0 commit comments