github.com/IBM-Blockchain/fabric-operator@v1.0.4/pkg/initializer/ca/initializer.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 initializer 20 21 import ( 22 "github.com/hyperledger/fabric-ca/lib" 23 "k8s.io/apimachinery/pkg/runtime" 24 25 current "github.com/IBM-Blockchain/fabric-operator/api/v1beta1" 26 v1 "github.com/IBM-Blockchain/fabric-operator/pkg/apis/ca/v1" 27 "github.com/IBM-Blockchain/fabric-operator/pkg/initializer/ca/config" 28 commonconfig "github.com/IBM-Blockchain/fabric-operator/pkg/initializer/common/config" 29 k8sclient "github.com/IBM-Blockchain/fabric-operator/pkg/k8s/controllerclient" 30 31 logf "sigs.k8s.io/controller-runtime/pkg/log" 32 ) 33 34 var log = logf.Log.WithName("ca_initializer") 35 36 type Config struct { 37 SharedPath string `json:"sharedPath"` 38 CADefaultConfigPath string `json:"cadefaultconfigpath"` 39 TLSCADefaultConfigPath string `json:"tlscadefaultconfigpath"` 40 CAOverrideConfigPath string `json:"caoverrideconfigpath"` 41 TLSCAOverrideConfigPath string `json:"tlscaoverrideconfigpath"` 42 DeploymentFile string 43 PVCFile string 44 ServiceFile string 45 RoleFile string 46 ServiceAccountFile string 47 RoleBindingFile string 48 ConfigMapFile string 49 IngressFile string 50 Ingressv1beta1File string 51 RouteFile string 52 } 53 54 type ConfigOptions struct { 55 DefaultPath string `json:"defaultpath"` 56 OverridePath string `json:"overridepath"` 57 } 58 59 type Response struct { 60 Config *v1.ServerConfig 61 CryptoMap map[string][]byte 62 } 63 64 //go:generate counterfeiter -o mocks/ibpca.go -fake-name IBPCA . IBPCA 65 66 type IBPCA interface { 67 OverrideServerConfig(newConfig *v1.ServerConfig) (err error) 68 ViperUnmarshal(configFile string) (*lib.ServerConfig, error) 69 ParseCrypto() (map[string][]byte, error) 70 ParseCABlock() (map[string][]byte, error) 71 GetServerConfig() *v1.ServerConfig 72 WriteConfig() (err error) 73 RemoveHomeDir() error 74 IsBeingUpdated() 75 ConfigToBytes() ([]byte, error) 76 GetHomeDir() string 77 Init() (err error) 78 SetMountPaths() 79 GetType() config.Type 80 } 81 82 type Initializer struct { 83 Timeouts HSMInitJobTimeouts 84 Client k8sclient.Client 85 Scheme *runtime.Scheme 86 } 87 88 func (i *Initializer) Create(instance *current.IBPCA, overrides *v1.ServerConfig, ca IBPCA) (*Response, error) { 89 type Create interface { 90 Create(instance *current.IBPCA, overrides *v1.ServerConfig, ca IBPCA) (*Response, error) 91 } 92 93 var initializer Create 94 if instance.IsHSMEnabledForType(ca.GetType()) { 95 if instance.UsingHSMProxy() { 96 // If Using HSM Proxy, currently sticking with old way of initialization which is within the operator process 97 // and not a kuberenetes job 98 initializer = &SW{} 99 } else { 100 hsmConfig, err := commonconfig.ReadHSMConfig(i.Client, instance) 101 if err != nil { 102 return nil, err 103 } 104 105 if hsmConfig.Daemon != nil { 106 initializer = &HSMDaemon{Client: i.Client, Timeouts: i.Timeouts, Config: hsmConfig} 107 } else { 108 initializer = &HSM{Client: i.Client, Timeouts: i.Timeouts, Config: hsmConfig} 109 } 110 } 111 } else { 112 initializer = &SW{} 113 } 114 115 return initializer.Create(instance, overrides, ca) 116 } 117 118 func (i *Initializer) Update(instance *current.IBPCA, overrides *v1.ServerConfig, ca IBPCA) (*Response, error) { 119 ca.IsBeingUpdated() 120 121 err := ca.OverrideServerConfig(overrides) 122 if err != nil { 123 return nil, err 124 } 125 126 crypto, err := ca.ParseCrypto() 127 if err != nil { 128 return nil, err 129 } 130 131 ca.SetMountPaths() 132 133 return &Response{ 134 Config: ca.GetServerConfig(), 135 CryptoMap: crypto, 136 }, nil 137 }