github.com/IBM-Blockchain/fabric-operator@v1.0.4/pkg/migrator/initsecret/migrator.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 initsecret
    20  
    21  import (
    22  	"errors"
    23  
    24  	commonconfig "github.com/IBM-Blockchain/fabric-operator/pkg/initializer/common/config"
    25  	"github.com/IBM-Blockchain/fabric-operator/pkg/util"
    26  )
    27  
    28  type Secret struct {
    29  	Component *MSP `json:"component,omitempty"`
    30  	TLS       *MSP `json:"tls,omitempty"`
    31  }
    32  
    33  type MSP struct {
    34  	Keystore          []string `json:"keystore,omitempty"`
    35  	SignCerts         []string `json:"signcerts,omitempty"`
    36  	CACerts           []string `json:"cacerts,omitempty"`
    37  	IntermediateCerts []string `json:"intermediatecerts,omitempty"`
    38  	AdminCerts        []string `json:"admincerts,omitempty"`
    39  }
    40  
    41  type Migrator struct {
    42  	Secret *Secret
    43  }
    44  
    45  func (m *Migrator) ParseComponentCrypto() (*commonconfig.Response, error) {
    46  	crypto := m.Secret.Component
    47  	if crypto == nil {
    48  		return nil, errors.New("init secret missing component crypto")
    49  	}
    50  	return m.ParseCrypto(crypto)
    51  }
    52  
    53  func (m *Migrator) ParseTLSCrypto() (*commonconfig.Response, error) {
    54  	crypto := m.Secret.TLS
    55  	if crypto == nil {
    56  		return nil, errors.New("init secret missing TLS crypto")
    57  	}
    58  	return m.ParseCrypto(crypto)
    59  }
    60  
    61  func (m *Migrator) ParseCrypto(crypto *MSP) (*commonconfig.Response, error) {
    62  	signcert := crypto.SignCerts[0] // When would there ever be more then 1 signed cert? Assuming only one as of right now. However, the MSP secret json has this defined as an array
    63  	keystore := crypto.Keystore[0]
    64  
    65  	signcertBytes, err := util.Base64ToBytes(signcert)
    66  	if err != nil {
    67  		return nil, err
    68  	}
    69  
    70  	keystoreBytes, err := util.Base64ToBytes(keystore)
    71  	if err != nil {
    72  		return nil, err
    73  	}
    74  
    75  	adminCerts := [][]byte{}
    76  	for _, cert := range crypto.AdminCerts {
    77  		certBytes, err := util.Base64ToBytes(cert)
    78  		if err != nil {
    79  			return nil, err
    80  		}
    81  
    82  		adminCerts = append(adminCerts, certBytes)
    83  	}
    84  
    85  	caCerts := [][]byte{}
    86  	for _, cert := range crypto.CACerts {
    87  		certBytes, err := util.Base64ToBytes(cert)
    88  		if err != nil {
    89  			return nil, err
    90  		}
    91  
    92  		caCerts = append(caCerts, certBytes)
    93  	}
    94  
    95  	interCerts := [][]byte{}
    96  	for _, cert := range crypto.IntermediateCerts {
    97  		certBytes, err := util.Base64ToBytes(cert)
    98  		if err != nil {
    99  			return nil, err
   100  		}
   101  
   102  		interCerts = append(interCerts, certBytes)
   103  	}
   104  
   105  	return &commonconfig.Response{
   106  		SignCert:          signcertBytes,
   107  		Keystore:          keystoreBytes,
   108  		CACerts:           caCerts,
   109  		AdminCerts:        adminCerts,
   110  		IntermediateCerts: interCerts,
   111  	}, nil
   112  
   113  }