github.com/IBM-Blockchain/fabric-operator@v1.0.4/pkg/initializer/orderer/config/v2/orderer.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  	"strings"
    25  
    26  	"github.com/IBM-Blockchain/fabric-operator/pkg/apis/common"
    27  	commonapi "github.com/IBM-Blockchain/fabric-operator/pkg/apis/common"
    28  	v2 "github.com/IBM-Blockchain/fabric-operator/pkg/apis/orderer/v2"
    29  	"github.com/IBM-Blockchain/fabric-operator/pkg/util/merge"
    30  	"github.com/pkg/errors"
    31  	"sigs.k8s.io/yaml"
    32  )
    33  
    34  type Orderer struct {
    35  	v2.Orderer `json:",inline"`
    36  }
    37  
    38  func (o *Orderer) ToBytes() ([]byte, error) {
    39  	bytes, err := yaml.Marshal(o)
    40  	if err != nil {
    41  		return nil, err
    42  	}
    43  
    44  	return bytes, nil
    45  }
    46  
    47  func (o *Orderer) WriteToFile(path string) error {
    48  	bytes, err := yaml.Marshal(o)
    49  	if err != nil {
    50  		return err
    51  	}
    52  
    53  	err = ioutil.WriteFile(path, bytes, 0600)
    54  	if err != nil {
    55  		return err
    56  	}
    57  
    58  	return nil
    59  }
    60  
    61  func (o *Orderer) MergeWith(newConfig interface{}, usingHSMProxy bool) error {
    62  	newOrderer := newConfig.(*Orderer)
    63  
    64  	if newOrderer != nil {
    65  		err := merge.WithOverwrite(o, newConfig)
    66  		if err != nil {
    67  			return errors.Wrapf(err, "failed to merge orderer configuration overrides")
    68  		}
    69  	}
    70  
    71  	if o.UsingPKCS11() {
    72  		o.SetPKCS11Defaults(usingHSMProxy)
    73  	}
    74  
    75  	return nil
    76  }
    77  
    78  func (o *Orderer) DeepCopyInto(into *Orderer) {
    79  	b, err := json.Marshal(o)
    80  	if err != nil {
    81  		return
    82  	}
    83  
    84  	err = json.Unmarshal(b, into)
    85  	if err != nil {
    86  		return
    87  	}
    88  }
    89  
    90  func (o *Orderer) DeepCopy() *Orderer {
    91  	if o == nil {
    92  		return nil
    93  	}
    94  	out := new(Orderer)
    95  	o.DeepCopyInto(out)
    96  	return out
    97  }
    98  
    99  func (o *Orderer) UsingPKCS11() bool {
   100  	if o.General.BCCSP != nil {
   101  		if strings.ToLower(o.General.BCCSP.ProviderName) == "pkcs11" {
   102  			return true
   103  		}
   104  	}
   105  	return false
   106  }
   107  
   108  func (o *Orderer) SetPKCS11Defaults(usingHSMProxy bool) {
   109  	if o.General.BCCSP.PKCS11 == nil {
   110  		o.General.BCCSP.PKCS11 = &commonapi.PKCS11Opts{}
   111  	}
   112  
   113  	if usingHSMProxy {
   114  		o.General.BCCSP.PKCS11.Library = "/usr/local/lib/libpkcs11-proxy.so"
   115  	}
   116  
   117  	if o.General.BCCSP.PKCS11.HashFamily == "" {
   118  		o.General.BCCSP.PKCS11.HashFamily = "SHA2"
   119  	}
   120  
   121  	if o.General.BCCSP.PKCS11.SecLevel == 0 {
   122  		o.General.BCCSP.PKCS11.SecLevel = 256
   123  	}
   124  }
   125  
   126  func (o *Orderer) SetBCCSPLibrary(library string) {
   127  	if o.General.BCCSP.PKCS11 == nil {
   128  		o.General.BCCSP.PKCS11 = &common.PKCS11Opts{}
   129  	}
   130  
   131  	o.General.BCCSP.PKCS11.Library = library
   132  }
   133  
   134  func (o *Orderer) SetDefaultKeyStore() {
   135  	// No-op
   136  	return
   137  }
   138  
   139  func (o *Orderer) GetBCCSPSection() *commonapi.BCCSP {
   140  	return o.General.BCCSP
   141  }