Skip to content

Commit fc460cd

Browse files
gengurtock-ibm
authored andcommitted
Push config update (Integration tests - reconfiguration #247)
Signed-off-by: Genady Gurevich <genadyg@il.ibm.com>
1 parent 6c9e8d8 commit fc460cd

File tree

5 files changed

+297
-11
lines changed

5 files changed

+297
-11
lines changed

common/tools/armageddon/cryptogen.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,19 @@ func copyFile(src, dst string) error {
552552
return err
553553
}
554554

555-
func CreateNewCertificateFromCA(caCertPath string, caPrivateKeyPath string, pathToNewTLSCert string, pathToNewTLSKey string, nodesIPs []string) ([]byte, error) {
555+
func CreateNewCertificateFromCA(caCertPath string, caPrivateKeyPath string, certType string, pathToNewCert string, pathToNewPrivateKey string, nodesIPs []string) ([]byte, error) {
556+
var ku x509.KeyUsage
557+
switch certType {
558+
case "tls":
559+
certType = "tls"
560+
ku = x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature
561+
case "sign":
562+
certType = "sign"
563+
ku = x509.KeyUsageDigitalSignature
564+
default:
565+
return nil, fmt.Errorf("unsupported cert type: %s", certType)
566+
}
567+
556568
caCertBytes, err := utils.ReadPem(caCertPath)
557569
if err != nil {
558570
return nil, err
@@ -595,20 +607,20 @@ func CreateNewCertificateFromCA(caCertPath string, caPrivateKeyPath string, path
595607
return nil, fmt.Errorf("failed marshaling private key, err: %s", err)
596608
}
597609

598-
_, err = ca.SignCertificate(pathToNewTLSCert, "tls", nil, nodesIPs, GetPublicKey(privateKey), x509.KeyUsageCertSign|x509.KeyUsageCRLSign, []x509.ExtKeyUsage{
610+
_, err = ca.SignCertificate(pathToNewCert, certType, nil, nodesIPs, GetPublicKey(privateKey), ku, []x509.ExtKeyUsage{
599611
x509.ExtKeyUsageClientAuth,
600612
x509.ExtKeyUsageServerAuth,
601613
})
602614
if err != nil {
603615
return nil, err
604616
}
605617

606-
err = utils.WritePEMToFile(pathToNewTLSKey, "PRIVATE KEY", privateKeyBytes)
618+
err = utils.WritePEMToFile(pathToNewPrivateKey, "PRIVATE KEY", privateKeyBytes)
607619
if err != nil {
608620
return nil, err
609621
}
610622

611-
newCertBytes, err := os.ReadFile(filepath.Join(pathToNewTLSCert, "tls-cert.pem"))
623+
newCertBytes, err := os.ReadFile(filepath.Join(pathToNewCert, fmt.Sprintf("%s-cert.pem", certType)))
612624
if err != nil {
613625
return nil, err
614626
}

common/utils/net.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ SPDX-License-Identifier: Apache-2.0
77
package utils
88

99
import (
10+
"bytes"
1011
"context"
1112
"crypto/x509"
1213
"fmt"
@@ -64,6 +65,20 @@ func CertificateBytesToString(cert []byte) (string, error) {
6465
return CertificateToString(x509Cert), nil
6566
}
6667

68+
func AreCertificatesEqual(cert1, cert2 []byte) (bool, error) {
69+
x509Cert1, err := Parsex509Cert(cert1)
70+
if err != nil {
71+
return false, err
72+
}
73+
x509Cert2, err := Parsex509Cert(cert2)
74+
if err != nil {
75+
return false, err
76+
}
77+
78+
// Compare RawTBSCertificate fields
79+
return bytes.Equal(x509Cert1.RawTBSCertificate, x509Cert2.RawTBSCertificate), nil
80+
}
81+
6782
func CertificateToString(cert *x509.Certificate) string {
6883
var sb strings.Builder
6984
fmt.Fprintf(&sb, "Certificate:\n")

config/config.go

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,11 @@ func (config *Configuration) CheckIfBatcherNodeExistsInSharedConfig(localSignCer
698698
return fmt.Errorf("batcher in shard%d does not exist for party%d in the shared config", localShardID, localPartyID)
699699
}
700700

701-
if !bytes.Equal(localTLSCert, sharedBatcherConfig.TlsCert) {
701+
equal, err := utils.AreCertificatesEqual(localTLSCert, sharedBatcherConfig.TlsCert)
702+
if err != nil {
703+
return err
704+
}
705+
if !equal {
702706
localTLSCertString, err := utils.CertificateBytesToString(localTLSCert)
703707
if err != nil {
704708
return err
@@ -710,7 +714,11 @@ func (config *Configuration) CheckIfBatcherNodeExistsInSharedConfig(localSignCer
710714
return fmt.Errorf("certificate mismatch: the batcher of party %d shard %d is attempting to load with TLS certificate: %v that differs from the shared configuration TLS certificate: %v", localPartyID, localShardID, localTLSCertString, sharedTLSCertString)
711715
}
712716

713-
if !bytes.Equal(localSignCert, sharedBatcherConfig.SignCert) {
717+
equal, err = utils.AreCertificatesEqual(localSignCert, sharedBatcherConfig.SignCert)
718+
if err != nil {
719+
return err
720+
}
721+
if !equal {
714722
localSignCertString, err := utils.CertificateBytesToString(localSignCert)
715723
if err != nil {
716724
return err
@@ -738,7 +746,11 @@ func (config *Configuration) CheckIfConsenterNodeExistsInSharedConfig(localSignC
738746
return fmt.Errorf("consenter configuration of partyID %d is missing from the shared configuration: %+v", localPartyID, sharedPartyConfig)
739747
}
740748

741-
if !bytes.Equal(localSignCert, sharedPartyConfig.ConsenterConfig.SignCert) {
749+
equal, err := utils.AreCertificatesEqual(localSignCert, sharedPartyConfig.ConsenterConfig.SignCert)
750+
if err != nil {
751+
return err
752+
}
753+
if !equal {
742754
localSignCertString, err := utils.CertificateBytesToString(localSignCert)
743755
if err != nil {
744756
return err
@@ -750,7 +762,11 @@ func (config *Configuration) CheckIfConsenterNodeExistsInSharedConfig(localSignC
750762
return fmt.Errorf("sign certificate mismatch: Consenter%d is attempting to load with sign certificate: %v that differs from the shared configuration sign certificate: %v", localPartyID, localSignCertString, sharedSignCertString)
751763
}
752764

753-
if !bytes.Equal(localTLSCert, sharedPartyConfig.ConsenterConfig.TlsCert) {
765+
equal, err = utils.AreCertificatesEqual(localTLSCert, sharedPartyConfig.ConsenterConfig.TlsCert)
766+
if err != nil {
767+
return err
768+
}
769+
if !equal {
754770
localTLSCertString, err := utils.CertificateBytesToString(localTLSCert)
755771
if err != nil {
756772
return err
@@ -776,7 +792,11 @@ func (config *Configuration) CheckIfAssemblerNodeExistsInSharedConfig() error {
776792
if sharedPartyConfig.AssemblerConfig == nil {
777793
return fmt.Errorf("assembler configuration of partyID %d is missing from the shared configuration: %+v", localPartyID, sharedPartyConfig)
778794
}
779-
if !bytes.Equal(localTLSCert, sharedPartyConfig.AssemblerConfig.TlsCert) {
795+
equal, err := utils.AreCertificatesEqual(localTLSCert, sharedPartyConfig.AssemblerConfig.TlsCert)
796+
if err != nil {
797+
return err
798+
}
799+
if !equal {
780800
localTLSCertString, err := utils.CertificateBytesToString(localTLSCert)
781801
if err != nil {
782802
return err

node/consensus/consensus_real_reconfig_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ func TestConsensusWithRealConfigUpdate(t *testing.T) {
165165
caPrivKeyPath := filepath.Join(dir, "crypto", "ordererOrganizations", fmt.Sprintf("org%d", consenterToUpdate), "tlsca", "priv_sk")
166166
newCertPath := filepath.Join(dir, "crypto", "ordererOrganizations", fmt.Sprintf("org%d", consenterToUpdate), "orderers", fmt.Sprintf("party%d", consenterToUpdate), "consenter", "tls")
167167
newKeyPath := filepath.Join(dir, "crypto", "ordererOrganizations", fmt.Sprintf("org%d", consenterToUpdate), "orderers", fmt.Sprintf("party%d", consenterToUpdate), "consenter", "tls", "key.pem")
168-
newCert, err := armageddon.CreateNewCertificateFromCA(caCertPath, caPrivKeyPath, newCertPath, newKeyPath, nodesIPs)
168+
newCert, err := armageddon.CreateNewCertificateFromCA(caCertPath, caPrivKeyPath, "tls", newCertPath, newKeyPath, nodesIPs)
169169
require.NoError(t, err)
170170
configUpdatePbData := configUpdateBuilder.UpdateConsensusTLSCert(t, consenterToUpdate, newCert)
171171
env := configutil.CreateConfigTX(t, dir, parties, 1, configUpdatePbData)

0 commit comments

Comments
 (0)