github.com/IBM-Blockchain/fabric-operator@v1.0.4/pkg/initializer/ca/config/ca_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 v1 "github.com/IBM-Blockchain/fabric-operator/pkg/apis/ca/v1" 26 "github.com/IBM-Blockchain/fabric-operator/pkg/initializer/ca/config" 27 . "github.com/onsi/ginkgo/v2" 28 . "github.com/onsi/gomega" 29 ) 30 31 var _ = Describe("CA config", func() { 32 var ( 33 cfg *config.Config 34 homeDir = "caconfigtest" 35 ) 36 37 BeforeEach(func() { 38 os.Mkdir(homeDir, 0777) 39 }) 40 41 AfterEach(func() { 42 err := os.RemoveAll(homeDir) 43 Expect(err).NotTo(HaveOccurred()) 44 }) 45 46 Context("parses CA configuration", func() { 47 BeforeEach(func() { 48 cfg = &config.Config{ 49 ServerConfig: &v1.ServerConfig{ 50 CAConfig: v1.CAConfig{ 51 CA: v1.CAInfo{ 52 Certfile: certFile, 53 Keyfile: keyFile, 54 Chainfile: certFile, 55 }, 56 }, 57 }, 58 HomeDir: homeDir, 59 } 60 }) 61 62 It("returns an error if unexpected type passed for keyfile and no key found in keystore", func() { 63 cfg.HomeDir = "fake" 64 cfg.ServerConfig.CAConfig.CA.Keyfile = "invalidType" 65 _, err := cfg.ParseCABlock() 66 Expect(err).To(HaveOccurred()) 67 Expect(err.Error()).To(ContainSubstring("no such file or directory")) 68 os.RemoveAll(cfg.HomeDir) 69 }) 70 71 It("if key is unexpected type look in keystore folder for key", func() { 72 cfg.HomeDir = "../../../../testdata" 73 cfg.ServerConfig.CAConfig.CA.Keyfile = "invalidType" 74 crypto, err := cfg.ParseCABlock() 75 Expect(err).NotTo(HaveOccurred()) 76 77 keyData, keyKeyExists := crypto["key.pem"] 78 Expect(keyKeyExists).To(Equal(true)) 79 Expect(keyData).NotTo(BeNil()) 80 Expect(cfg.ServerConfig.CAConfig.CA.Keyfile).To(Equal(filepath.Join(cfg.HomeDir, "key.pem"))) 81 82 os.Remove(filepath.Join(cfg.HomeDir, "cert.pem")) 83 os.Remove(filepath.Join(cfg.HomeDir, "key.pem")) 84 os.Remove(filepath.Join(cfg.HomeDir, "chain.pem")) 85 }) 86 87 It("returns if unexpected type passed for trusted root cert files", func() { 88 cfg.ServerConfig.CAConfig.CA.Chainfile = "invalidType" 89 c, err := cfg.ParseCABlock() 90 Expect(err).NotTo(HaveOccurred()) 91 Expect(c).NotTo(BeNil()) 92 }) 93 94 It("parses config and returns a map containing all crypto and updated paths to crypto material", func() { 95 crypto, err := cfg.ParseCABlock() 96 Expect(err).NotTo(HaveOccurred()) 97 98 certData, certKeyExists := crypto["cert.pem"] 99 Expect(certKeyExists).To(Equal(true)) 100 Expect(certData).NotTo(BeNil()) 101 Expect(cfg.ServerConfig.CAConfig.CA.Certfile).To(Equal(filepath.Join(cfg.HomeDir, "cert.pem"))) 102 103 keyData, keyKeyExists := crypto["key.pem"] 104 Expect(keyKeyExists).To(Equal(true)) 105 Expect(keyData).NotTo(BeNil()) 106 Expect(cfg.ServerConfig.CAConfig.CA.Keyfile).To(Equal(filepath.Join(cfg.HomeDir, "key.pem"))) 107 108 chainData, chainKeyExists := crypto["chain.pem"] 109 Expect(chainKeyExists).To(Equal(true)) 110 Expect(chainData).NotTo(BeNil()) 111 Expect(cfg.ServerConfig.CAConfig.CA.Chainfile).To(Equal(filepath.Join(cfg.HomeDir, "chain.pem"))) 112 }) 113 }) 114 })