github.com/IBM-Blockchain/fabric-operator@v1.0.4/pkg/initializer/peer/config/v2/config.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 v2
    20  
    21  import (
    22  	"encoding/json"
    23  	"io/ioutil"
    24  	"path/filepath"
    25  	"strings"
    26  
    27  	"github.com/IBM-Blockchain/fabric-operator/pkg/apis/common"
    28  	v2 "github.com/IBM-Blockchain/fabric-operator/pkg/apis/peer/v2"
    29  	"github.com/IBM-Blockchain/fabric-operator/pkg/initializer/peer/config/commoncore"
    30  	v1config "github.com/IBM-Blockchain/fabric-operator/pkg/initializer/peer/config/v1"
    31  	"github.com/IBM-Blockchain/fabric-operator/pkg/util/merge"
    32  	"github.com/pkg/errors"
    33  	"sigs.k8s.io/yaml"
    34  )
    35  
    36  type Core struct {
    37  	v2.Core       `json:",inline"`
    38  	addrOverrides []v1config.AddressOverride
    39  }
    40  
    41  func (c *Core) ToBytes() ([]byte, error) {
    42  	bytes, err := yaml.Marshal(c)
    43  	if err != nil {
    44  		return nil, err
    45  	}
    46  
    47  	return bytes, nil
    48  }
    49  
    50  func (c *Core) WriteToFile(path string) error {
    51  	bytes, err := yaml.Marshal(c)
    52  	if err != nil {
    53  		return err
    54  	}
    55  
    56  	err = ioutil.WriteFile(filepath.Clean(path), bytes, 0600)
    57  	if err != nil {
    58  		return err
    59  	}
    60  
    61  	return nil
    62  }
    63  
    64  func (c *Core) MergeWith(newConfig interface{}, usingHSMProxy bool) error {
    65  	newCore := newConfig.(*Core)
    66  
    67  	if newCore != nil {
    68  		err := merge.WithOverwrite(c, newCore)
    69  		if err != nil {
    70  			return errors.Wrapf(err, "failed to merge peer configuration overrides")
    71  		}
    72  	}
    73  
    74  	if c.UsingPKCS11() {
    75  		c.SetPKCS11Defaults(usingHSMProxy)
    76  	}
    77  
    78  	dc := v1config.DeliveryClient{DeliveryClient: c.Peer.DeliveryClient}
    79  	addrOverrides, err := dc.HandleCAcertsFiles()
    80  	if err != nil {
    81  		return errors.Wrapf(err, "failed to convert base64 certs to filepath")
    82  	}
    83  	c.Peer.DeliveryClient = dc.DeliveryClient
    84  	c.addrOverrides = addrOverrides
    85  
    86  	return nil
    87  }
    88  
    89  func (c *Core) DeepCopyInto(into *Core) {
    90  	b, err := json.Marshal(c)
    91  	if err != nil {
    92  		return
    93  	}
    94  
    95  	err = json.Unmarshal(b, into)
    96  	if err != nil {
    97  		return
    98  	}
    99  }
   100  
   101  func (c *Core) DeepCopy() *Core {
   102  	if c == nil {
   103  		return nil
   104  	}
   105  	out := new(Core)
   106  	c.DeepCopyInto(out)
   107  	return out
   108  }
   109  
   110  func (c *Core) UsingPKCS11() bool {
   111  	if c.Peer.BCCSP != nil {
   112  		if strings.ToLower(c.Peer.BCCSP.ProviderName) == "pkcs11" {
   113  			return true
   114  		}
   115  	}
   116  	return false
   117  }
   118  
   119  func (c *Core) SetPKCS11Defaults(usingHSMProxy bool) {
   120  	if c.Peer.BCCSP.PKCS11 == nil {
   121  		c.Peer.BCCSP.PKCS11 = &common.PKCS11Opts{}
   122  	}
   123  
   124  	if usingHSMProxy {
   125  		c.Peer.BCCSP.PKCS11.Library = "/usr/local/lib/libpkcs11-proxy.so"
   126  	}
   127  
   128  	if c.Peer.BCCSP.PKCS11.HashFamily == "" {
   129  		c.Peer.BCCSP.PKCS11.HashFamily = "SHA2"
   130  	}
   131  
   132  	if c.Peer.BCCSP.PKCS11.SecLevel == 0 {
   133  		c.Peer.BCCSP.PKCS11.SecLevel = 256
   134  	}
   135  
   136  	c.Peer.BCCSP.PKCS11.SoftVerify = true
   137  }
   138  
   139  func (c *Core) SetDefaultKeyStore() {
   140  	// No-op
   141  	return
   142  }
   143  
   144  func (c *Core) GetMaxNameLength() *int {
   145  	return c.MaxNameLength
   146  }
   147  
   148  func (c *Core) GetAddressOverrides() []v1config.AddressOverride {
   149  	return c.addrOverrides
   150  }
   151  
   152  func (c *Core) GetBCCSPSection() *common.BCCSP {
   153  	return c.Peer.BCCSP
   154  }
   155  
   156  func (c *Core) SetBCCSPLibrary(library string) {
   157  	if c.Peer.BCCSP.PKCS11 == nil {
   158  		c.Peer.BCCSP.PKCS11 = &common.PKCS11Opts{}
   159  	}
   160  
   161  	c.Peer.BCCSP.PKCS11.Library = library
   162  }
   163  
   164  func ReadCoreFile(path string) (*Core, error) {
   165  	core, err := ioutil.ReadFile(filepath.Clean(path))
   166  	if err != nil {
   167  		return nil, err
   168  	}
   169  
   170  	return coreFromBytes(core)
   171  }
   172  
   173  func ReadCoreFromBytes(core []byte) (*Core, error) {
   174  	return coreFromBytes(core)
   175  }
   176  
   177  func ReadFrom(from *[]byte) (*Core, error) {
   178  	return coreFromBytes(*from)
   179  }
   180  
   181  func coreFromBytes(coreBytes []byte) (*Core, error) {
   182  	coreConfig := &Core{}
   183  	err := yaml.Unmarshal(coreBytes, coreConfig)
   184  	if err != nil {
   185  		// Check if peer.gossip.bootstrap needs to be converted
   186  		updatedCore, err := commoncore.ConvertBootstrapToArray(coreBytes)
   187  		if err != nil {
   188  			return nil, errors.Wrap(err, "failed to convert peer.gossip.bootstrap to string array")
   189  		}
   190  		err = yaml.Unmarshal(updatedCore, coreConfig)
   191  		if err != nil {
   192  			return nil, err
   193  		}
   194  	}
   195  
   196  	return coreConfig, nil
   197  }