github.com/IBM-Blockchain/fabric-operator@v1.0.4/pkg/initializer/common/mspparser/mspparser.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 mspparser
    20  
    21  import (
    22  	current "github.com/IBM-Blockchain/fabric-operator/api/v1beta1"
    23  	"github.com/IBM-Blockchain/fabric-operator/pkg/initializer/common/config"
    24  	"github.com/IBM-Blockchain/fabric-operator/pkg/util"
    25  	"github.com/pkg/errors"
    26  
    27  	logf "sigs.k8s.io/controller-runtime/pkg/log"
    28  )
    29  
    30  var log = logf.Log.WithName("peer_init_msp_parser")
    31  
    32  type MSPParser struct {
    33  	Config *current.MSP
    34  }
    35  
    36  func New(cfg *current.MSP) *MSPParser {
    37  	return &MSPParser{
    38  		Config: cfg,
    39  	}
    40  }
    41  
    42  func (m *MSPParser) GetCrypto() (*config.Response, error) {
    43  	return m.Parse()
    44  }
    45  
    46  func (m *MSPParser) Parse() (*config.Response, error) {
    47  	resp := &config.Response{}
    48  
    49  	certBytes, err := util.Base64ToBytes(m.Config.SignCerts)
    50  	if err != nil {
    51  		return nil, errors.Wrap(err, "failed to parse signcert")
    52  	}
    53  	resp.SignCert = certBytes
    54  
    55  	keyBytes, err := util.Base64ToBytes(m.Config.KeyStore)
    56  	if err != nil {
    57  		return nil, errors.Wrap(err, "failed to parse keystore")
    58  	}
    59  	resp.Keystore = keyBytes
    60  
    61  	for _, adminCert := range m.Config.AdminCerts {
    62  		bytes, err := util.Base64ToBytes(adminCert)
    63  		if err != nil {
    64  			return nil, errors.Wrap(err, "failed to parse admin cert")
    65  		}
    66  		resp.AdminCerts = append(resp.AdminCerts, bytes)
    67  	}
    68  
    69  	for _, interCert := range m.Config.IntermediateCerts {
    70  		bytes, err := util.Base64ToBytes(interCert)
    71  		if err != nil {
    72  			return nil, errors.Wrap(err, "failed to parse intermediate cert")
    73  		}
    74  		resp.IntermediateCerts = append(resp.IntermediateCerts, bytes)
    75  	}
    76  
    77  	for _, caCert := range m.Config.CACerts {
    78  		bytes, err := util.Base64ToBytes(caCert)
    79  		if err != nil {
    80  			return nil, errors.Wrap(err, "failed to parse ca cert")
    81  		}
    82  		resp.CACerts = append(resp.CACerts, bytes)
    83  	}
    84  
    85  	return resp, nil
    86  }
    87  
    88  // MSP parser requires no interaction with CA, ping CA is a no-op
    89  func (m *MSPParser) PingCA() error {
    90  	// no-op
    91  	return nil
    92  }
    93  
    94  func (m *MSPParser) Validate() error {
    95  	cfg := m.Config
    96  
    97  	if cfg.KeyStore == "" {
    98  		return errors.New("unable to parse MSP, keystore not specified")
    99  	}
   100  
   101  	if cfg.SignCerts == "" {
   102  		return errors.New("unable to parse MSP, signcert not specified")
   103  	}
   104  
   105  	if len(cfg.CACerts) == 0 {
   106  		return errors.New("unable to parse MSP, ca certs not specified")
   107  	}
   108  
   109  	return nil
   110  }