github.com/IBM-Blockchain/fabric-operator@v1.0.4/pkg/initializer/ca/config/tls_test.go (about)

     1  /*
     2   * Copyright contributors to the Hyperledger Fabric Operator project
     3   *
     4   * SPDX-License-Identifier: Apache-2.0
     5   *
     6   * Licensed under the Apache License, Version 2.0 (the "License");
     7   * you may not use this file except in compliance with the License.
     8   * You may obtain a copy of the License at:
     9   *
    10   * 	  http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   * Unless required by applicable law or agreed to in writing, software
    13   * distributed under the License is distributed on an "AS IS" BASIS,
    14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15   * See the License for the specific language governing permissions and
    16   * limitations under the License.
    17   */
    18  
    19  package config_test
    20  
    21  import (
    22  	"os"
    23  	"path/filepath"
    24  
    25  	. "github.com/onsi/ginkgo/v2"
    26  	. "github.com/onsi/gomega"
    27  
    28  	v1 "github.com/IBM-Blockchain/fabric-operator/pkg/apis/ca/v1"
    29  	"github.com/IBM-Blockchain/fabric-operator/pkg/initializer/ca/config"
    30  	"github.com/IBM-Blockchain/fabric-operator/pkg/util/pointer"
    31  )
    32  
    33  var _ = Describe("TLS Config", func() {
    34  	const (
    35  		homeDir = "configtest"
    36  	)
    37  
    38  	Context("parses TLS configuration", func() {
    39  		var cfg *config.Config
    40  
    41  		BeforeEach(func() {
    42  			cfg = &config.Config{
    43  				ServerConfig: &v1.ServerConfig{
    44  					TLS: v1.ServerTLSConfig{
    45  						Enabled:  pointer.True(),
    46  						CertFile: certFile,
    47  						KeyFile:  keyFile,
    48  						ClientAuth: v1.ClientAuth{
    49  							CertFiles: []string{"../../../../testdata/tls/tls.crt"},
    50  						},
    51  					},
    52  				},
    53  				HomeDir: homeDir,
    54  			}
    55  
    56  			os.Mkdir(homeDir, 0777)
    57  		})
    58  
    59  		AfterEach(func() {
    60  			err := os.RemoveAll(homeDir)
    61  			Expect(err).NotTo(HaveOccurred())
    62  		})
    63  
    64  		It("returns no error and an empty map if TLS disabled", func() {
    65  			cfg.ServerConfig.TLS.Enabled = pointer.False()
    66  			crypto, err := cfg.ParseTLSBlock()
    67  			Expect(err).NotTo(HaveOccurred())
    68  			Expect(crypto).To(BeNil())
    69  		})
    70  
    71  		It("parses config and returns a map containing all crypto and updated paths to crypto material", func() {
    72  			crypto, err := cfg.ParseTLSBlock()
    73  			Expect(err).NotTo(HaveOccurred())
    74  
    75  			certData, certKeyExists := crypto["tls-cert.pem"]
    76  			Expect(certKeyExists).To(Equal(true))
    77  			Expect(certData).NotTo(BeNil())
    78  			Expect(cfg.ServerConfig.TLS.CertFile).To(Equal(filepath.Join(cfg.HomeDir, "tls-cert.pem")))
    79  
    80  			keyData, keyKeyExists := crypto["tls-key.pem"]
    81  			Expect(keyKeyExists).To(Equal(true))
    82  			Expect(keyData).NotTo(BeNil())
    83  			Expect(cfg.ServerConfig.TLS.KeyFile).To(Equal(filepath.Join(cfg.HomeDir, "tls-key.pem")))
    84  
    85  			clientAuthData, clientAuthCertKeyExists := crypto["tls-certfile0.pem"]
    86  			Expect(clientAuthCertKeyExists).To(Equal(true))
    87  			Expect(clientAuthData).NotTo(BeNil())
    88  			Expect(cfg.ServerConfig.TLS.ClientAuth.CertFiles[0]).To(Equal(filepath.Join(cfg.HomeDir, "tls-certfile0.pem")))
    89  		})
    90  	})
    91  })