Skip to content

Commit 92e1da0

Browse files
committed
Push config update (Integration tests - reconfiguration #247)
Signed-off-by: Genady Gurevich <genadyg@il.ibm.com>
1 parent ce62bd9 commit 92e1da0

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
@@ -697,7 +697,11 @@ func (config *Configuration) CheckIfBatcherNodeExistsInSharedConfig(localSignCer
697697
return fmt.Errorf("batcher in shard%d does not exist for party%d in the shared config", localShardID, localPartyID)
698698
}
699699

700-
if !bytes.Equal(localTLSCert, sharedBatcherConfig.TlsCert) {
700+
equal, err := utils.AreCertificatesEqual(localTLSCert, sharedBatcherConfig.TlsCert)
701+
if err != nil {
702+
return err
703+
}
704+
if !equal {
701705
localTLSCertString, err := utils.CertificateBytesToString(localTLSCert)
702706
if err != nil {
703707
return err
@@ -709,7 +713,11 @@ func (config *Configuration) CheckIfBatcherNodeExistsInSharedConfig(localSignCer
709713
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)
710714
}
711715

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

740-
if !bytes.Equal(localSignCert, sharedPartyConfig.ConsenterConfig.SignCert) {
748+
equal, err := utils.AreCertificatesEqual(localSignCert, sharedPartyConfig.ConsenterConfig.SignCert)
749+
if err != nil {
750+
return err
751+
}
752+
if !equal {
741753
localSignCertString, err := utils.CertificateBytesToString(localSignCert)
742754
if err != nil {
743755
return err
@@ -749,7 +761,11 @@ func (config *Configuration) CheckIfConsenterNodeExistsInSharedConfig(localSignC
749761
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)
750762
}
751763

752-
if !bytes.Equal(localTLSCert, sharedPartyConfig.ConsenterConfig.TlsCert) {
764+
equal, err = utils.AreCertificatesEqual(localTLSCert, sharedPartyConfig.ConsenterConfig.TlsCert)
765+
if err != nil {
766+
return err
767+
}
768+
if !equal {
753769
localTLSCertString, err := utils.CertificateBytesToString(localTLSCert)
754770
if err != nil {
755771
return err
@@ -775,7 +791,11 @@ func (config *Configuration) CheckIfAssemblerNodeExistsInSharedConfig() error {
775791
if sharedPartyConfig.AssemblerConfig == nil {
776792
return fmt.Errorf("assembler configuration of partyID %d is missing from the shared configuration: %+v", localPartyID, sharedPartyConfig)
777793
}
778-
if !bytes.Equal(localTLSCert, sharedPartyConfig.AssemblerConfig.TlsCert) {
794+
equal, err := utils.AreCertificatesEqual(localTLSCert, sharedPartyConfig.AssemblerConfig.TlsCert)
795+
if err != nil {
796+
return err
797+
}
798+
if !equal {
779799
localTLSCertString, err := utils.CertificateBytesToString(localTLSCert)
780800
if err != nil {
781801
return err

node/consensus/consensus_real_reconfig_test.go

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

0 commit comments

Comments
 (0)