github.com/IBM-Blockchain/fabric-operator@v1.0.4/pkg/initializer/peer/config/v1/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 v1
    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  	v1 "github.com/IBM-Blockchain/fabric-operator/pkg/apis/peer/v1"
    29  	"github.com/IBM-Blockchain/fabric-operator/pkg/util/merge"
    30  	"github.com/pkg/errors"
    31  	"sigs.k8s.io/yaml"
    32  )
    33  
    34  type Core struct {
    35  	v1.Core       `json:",inline"`
    36  	addrOverrides []AddressOverride
    37  }
    38  
    39  func (c *Core) ToBytes() ([]byte, error) {
    40  	bytes, err := yaml.Marshal(c)
    41  	if err != nil {
    42  		return nil, err
    43  	}
    44  
    45  	return bytes, nil
    46  }
    47  
    48  func (c *Core) WriteToFile(path string) error {
    49  	bytes, err := yaml.Marshal(c)
    50  	if err != nil {
    51  		return err
    52  	}
    53  
    54  	err = ioutil.WriteFile(filepath.Clean(path), bytes, 0600)
    55  	if err != nil {
    56  		return err
    57  	}
    58  
    59  	return nil
    60  }
    61  
    62  func (c *Core) MergeWith(newConfig interface{}, UsingHSMProxy bool) error {
    63  	newCore := newConfig.(*Core)
    64  
    65  	if newCore != nil {
    66  		err := merge.WithOverwrite(c, newCore)
    67  		if err != nil {
    68  			return errors.Wrapf(err, "failed to merge peer configuration overrides")
    69  		}
    70  	}
    71  
    72  	if c.UsingPKCS11() {
    73  		c.SetPKCS11Defaults(UsingHSMProxy)
    74  	}
    75  
    76  	dc := DeliveryClient{DeliveryClient: c.Peer.DeliveryClient}
    77  	addrOverrides, err := dc.HandleCAcertsFiles()
    78  	if err != nil {
    79  		return errors.Wrapf(err, "failed to convert base64 certs to filepath")
    80  	}
    81  	c.Peer.DeliveryClient = dc.DeliveryClient
    82  	c.addrOverrides = addrOverrides
    83  
    84  	return nil
    85  }
    86  
    87  func (c *Core) DeepCopyInto(into *Core) {
    88  	b, err := json.Marshal(c)
    89  	if err != nil {
    90  		return
    91  	}
    92  
    93  	err = json.Unmarshal(b, into)
    94  	if err != nil {
    95  		return
    96  	}
    97  }
    98  
    99  func (c *Core) DeepCopy() *Core {
   100  	if c == nil {
   101  		return nil
   102  	}
   103  	out := new(Core)
   104  	c.DeepCopyInto(out)
   105  	return out
   106  }
   107  
   108  func (c *Core) UsingPKCS11() bool {
   109  	if c.Peer.BCCSP != nil {
   110  		if strings.ToLower(c.Peer.BCCSP.ProviderName) == "pkcs11" {
   111  			return true
   112  		}
   113  	}
   114  	return false
   115  }
   116  
   117  func (c *Core) SetPKCS11Defaults(usingHSMProxy bool) {
   118  	if c.Peer.BCCSP.PKCS11 == nil {
   119  		c.Peer.BCCSP.PKCS11 = &common.PKCS11Opts{}
   120  	}
   121  
   122  	if usingHSMProxy {
   123  		c.Peer.BCCSP.PKCS11.Library = "/usr/local/lib/libpkcs11-proxy.so"
   124  	}
   125  
   126  	if c.Peer.BCCSP.PKCS11.HashFamily == "" {
   127  		c.Peer.BCCSP.PKCS11.HashFamily = "SHA2"
   128  	}
   129  
   130  	if c.Peer.BCCSP.PKCS11.SecLevel == 0 {
   131  		c.Peer.BCCSP.PKCS11.SecLevel = 256
   132  	}
   133  
   134  	c.Peer.BCCSP.PKCS11.SoftVerify = true
   135  }
   136  
   137  func (c *Core) SetDefaultKeyStore() {
   138  	if c.Peer.BCCSP.PKCS11 != nil {
   139  		c.Peer.BCCSP.PKCS11.FileKeyStore = &common.FileKeyStoreOpts{
   140  			KeyStorePath: "msp/keystore",
   141  		}
   142  	}
   143  }
   144  
   145  func (c *Core) GetAddressOverrides() []AddressOverride {
   146  	return c.addrOverrides
   147  }
   148  
   149  func (c *Core) GetBCCSPSection() *common.BCCSP {
   150  	return c.Peer.BCCSP
   151  }
   152  
   153  func (c *Core) GetMaxNameLength() *int {
   154  	return c.MaxNameLength
   155  }
   156  
   157  func (c *Core) SetBCCSPLibrary(library string) {
   158  	if c.Peer.BCCSP.PKCS11 == nil {
   159  		c.Peer.BCCSP.PKCS11 = &common.PKCS11Opts{}
   160  	}
   161  
   162  	c.Peer.BCCSP.PKCS11.Library = library
   163  }