|
| 1 | +/* |
| 2 | +Copyright IBM Corp. All Rights Reserved. |
| 3 | +
|
| 4 | +SPDX-License-Identifier: Apache-2.0 |
| 5 | +*/ |
| 6 | + |
| 7 | +package test |
| 8 | + |
| 9 | +import ( |
| 10 | + "context" |
| 11 | + "testing" |
| 12 | + "time" |
| 13 | + |
| 14 | + "github.com/hyperledger/fabric-x-common/api/committerpb" |
| 15 | + "github.com/onsi/gomega" |
| 16 | + "github.com/stretchr/testify/require" |
| 17 | + "google.golang.org/grpc" |
| 18 | + |
| 19 | + "github.com/hyperledger/fabric-x-committer/integration/runner" |
| 20 | + "github.com/hyperledger/fabric-x-committer/utils/connection" |
| 21 | + "github.com/hyperledger/fabric-x-committer/utils/grpcerror" |
| 22 | + "github.com/hyperledger/fabric-x-committer/utils/test" |
| 23 | + "github.com/hyperledger/fabric-x-committer/utils/testcrypto" |
| 24 | +) |
| 25 | + |
| 26 | +// TestDynamicTLS verifies that the sidecar and query service dynamically update |
| 27 | +// their trusted TLS CA pool when config blocks add or remove peer organizations, |
| 28 | +// while preserving static (YAML-configured) root CAs. |
| 29 | +func TestDynamicTLS(t *testing.T) { |
| 30 | + t.Parallel() |
| 31 | + gomega.RegisterTestingT(t) |
| 32 | + |
| 33 | + c := runner.NewRuntime(t, &runner.Config{ |
| 34 | + TLSMode: connection.MutualTLSMode, |
| 35 | + PeerOrganizationCount: 3, |
| 36 | + BlockTimeout: 2 * time.Second, |
| 37 | + QueryTLSRefreshInterval: 5 * time.Second, |
| 38 | + CrashTest: true, |
| 39 | + }) |
| 40 | + // Start orderer servers in-process so SubmitConfigBlock can write directly |
| 41 | + // to the orderer's block channel (separate-process orderers don't share memory). |
| 42 | + c.OrdererEnv.StartServers(t) |
| 43 | + c.Start(t, runner.CommitterTxPath|runner.QueryService) |
| 44 | + |
| 45 | + serverCACertPaths := c.SystemConfig.ClientTLS.CACertPaths |
| 46 | + sidecarEndpoint := c.SystemConfig.Services.Sidecar.GrpcEndpoint |
| 47 | + queryEndpoint := c.SystemConfig.Services.Query.GrpcEndpoint |
| 48 | + |
| 49 | + // Per-org mTLS configs. Crypto artifacts don't change across config block updates, |
| 50 | + // so these remain valid throughout the test. |
| 51 | + orgTLS := [3]connection.TLSConfig{} |
| 52 | + for i := range orgTLS { |
| 53 | + orgTLS[i] = test.OrgClientTLSConfig(c.OrdererEnv.ArtifactsPath, i, serverCACertPaths) |
| 54 | + } |
| 55 | + |
| 56 | + // Step 1: Assert all three peer orgs can connect to sidecar and query service. |
| 57 | + // The sidecar updates dynamic TLS immediately when processing the genesis config block. |
| 58 | + // The query service polls the DB periodically, so we use Eventually for it. |
| 59 | + t.Log("Step 1: Initial connection - all three orgs should connect") |
| 60 | + for orgIdx, tlsCfg := range orgTLS { |
| 61 | + assertRPCSucceeds(t, sidecarEndpoint, tlsCfg, "sidecar", orgIdx) |
| 62 | + eventuallyRPCSucceeds(t, queryEndpoint, tlsCfg, "query", orgIdx) |
| 63 | + } |
| 64 | + |
| 65 | + // Step 2: Submit config block removing peer-org-2 (keep only peer-org-0 and peer-org-1). |
| 66 | + // CreateOrExtendConfigBlockWithCrypto retains existing crypto on disk, so peer-org-2's |
| 67 | + // certs remain available for reconnection in Step 4. |
| 68 | + t.Log("Step 2: Dynamic removal - submit config with 2 peer orgs") |
| 69 | + c.OrdererEnv.SubmitConfigBlock(t, &testcrypto.ConfigBlock{ |
| 70 | + OrdererEndpoints: c.OrdererEnv.AllEndpoints, |
| 71 | + PeerOrganizationCount: 2, |
| 72 | + }) |
| 73 | + c.ValidateExpectedResultsInCommittedBlock(t, &runner.ExpectedStatusInBlock{ |
| 74 | + Statuses: []committerpb.Status{committerpb.Status_COMMITTED}, |
| 75 | + }) |
| 76 | + |
| 77 | + // Step 3: Verify peer-org-2 is rejected and peer-org-0 remains trusted. |
| 78 | + t.Log("Step 3: Negative assertion - peer-org-2 rejected, peer-org-0 accepted") |
| 79 | + |
| 80 | + // Sidecar updates immediately from config block processing. |
| 81 | + assertRPCSucceeds(t, sidecarEndpoint, orgTLS[0], "sidecar", 0) |
| 82 | + assertRPCFails(t, sidecarEndpoint, orgTLS[2], "sidecar", 2) |
| 83 | + |
| 84 | + // Query service polls the DB; wait for the TLS refresh (up to 15s). |
| 85 | + assertRPCSucceeds(t, queryEndpoint, orgTLS[0], "query", 0) |
| 86 | + gomega.Eventually(func() error { |
| 87 | + return tryRPC(queryEndpoint, orgTLS[2]) |
| 88 | + }, 15*time.Second, time.Second).Should(gomega.HaveOccurred(), |
| 89 | + "query service should reject peer-org-2 after TLS refresh") |
| 90 | + |
| 91 | + // Step 4: Restore peer-org-2. |
| 92 | + t.Log("Step 4: Restoration - add peer-org-2 back") |
| 93 | + c.OrdererEnv.SubmitConfigBlock(t, &testcrypto.ConfigBlock{ |
| 94 | + OrdererEndpoints: c.OrdererEnv.AllEndpoints, |
| 95 | + PeerOrganizationCount: 3, |
| 96 | + }) |
| 97 | + c.ValidateExpectedResultsInCommittedBlock(t, &runner.ExpectedStatusInBlock{ |
| 98 | + Statuses: []committerpb.Status{committerpb.Status_COMMITTED}, |
| 99 | + }) |
| 100 | + |
| 101 | + // Sidecar: peer-org-2 connects immediately. |
| 102 | + assertRPCSucceeds(t, sidecarEndpoint, orgTLS[2], "sidecar", 2) |
| 103 | + |
| 104 | + // Query service: wait for TLS refresh. |
| 105 | + eventuallyRPCSucceeds(t, queryEndpoint, orgTLS[2], "query", 2) |
| 106 | + |
| 107 | + // Step 5: Static persistence - CredentialsFactory client still works. |
| 108 | + t.Log("Step 5: Static persistence - static TLS client still trusted") |
| 109 | + assertRPCSucceeds(t, sidecarEndpoint, c.SystemConfig.ClientTLS, "sidecar (static)", -1) |
| 110 | + assertRPCSucceeds(t, queryEndpoint, c.SystemConfig.ClientTLS, "query (static)", -1) |
| 111 | +} |
| 112 | + |
| 113 | +func assertRPCSucceeds( //nolint:revive // test helper, readability over argument count |
| 114 | + t *testing.T, endpoint connection.WithAddress, |
| 115 | + tlsConfig connection.TLSConfig, service string, orgIdx int, |
| 116 | +) { |
| 117 | + t.Helper() |
| 118 | + err := tryRPC(endpoint, tlsConfig) |
| 119 | + require.NoErrorf(t, err, "%s: peer-org-%d should connect successfully", service, orgIdx) |
| 120 | +} |
| 121 | + |
| 122 | +func assertRPCFails( //nolint:revive // test helper, readability over argument count |
| 123 | + t *testing.T, endpoint connection.WithAddress, |
| 124 | + tlsConfig connection.TLSConfig, service string, orgIdx int, |
| 125 | +) { |
| 126 | + t.Helper() |
| 127 | + err := tryRPC(endpoint, tlsConfig) |
| 128 | + require.Errorf(t, err, "%s: peer-org-%d should be rejected", service, orgIdx) |
| 129 | +} |
| 130 | + |
| 131 | +func eventuallyRPCSucceeds( //nolint:revive // test helper, readability over argument count |
| 132 | + t *testing.T, endpoint connection.WithAddress, |
| 133 | + tlsConfig connection.TLSConfig, service string, orgIdx int, |
| 134 | +) { |
| 135 | + t.Helper() |
| 136 | + gomega.Eventually(func() error { |
| 137 | + return tryRPC(endpoint, tlsConfig) |
| 138 | + }, 15*time.Second, time.Second).ShouldNot(gomega.HaveOccurred(), |
| 139 | + "%s: peer-org-%d should connect successfully", service, orgIdx) |
| 140 | +} |
| 141 | + |
| 142 | +// tryRPC attempts a lightweight gRPC call and returns an error only if the TLS |
| 143 | +// handshake fails. Application-level gRPC errors (InvalidArgument, Unimplemented, |
| 144 | +// etc.) indicate a successful TLS connection and are treated as success. |
| 145 | +func tryRPC(endpoint connection.WithAddress, tlsConfig connection.TLSConfig) error { |
| 146 | + creds, err := tlsConfig.ClientCredentials() |
| 147 | + if err != nil { |
| 148 | + return err |
| 149 | + } |
| 150 | + conn, err := grpc.NewClient(endpoint.Address(), grpc.WithTransportCredentials(creds)) |
| 151 | + if err != nil { |
| 152 | + return err |
| 153 | + } |
| 154 | + defer conn.Close() //nolint:errcheck |
| 155 | + |
| 156 | + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 157 | + defer cancel() |
| 158 | + |
| 159 | + client := committerpb.NewQueryServiceClient(conn) |
| 160 | + _, err = client.GetTransactionStatus(ctx, &committerpb.TxStatusQuery{}) |
| 161 | + // FilterUnavailableErrorCode returns nil for transient connectivity errors |
| 162 | + // (Unavailable, DeadlineExceeded) and passes through application-level errors. |
| 163 | + // An application-level error means TLS succeeded, so we invert: if the filter |
| 164 | + // passes through an error, the connection worked. |
| 165 | + if grpcerror.FilterUnavailableErrorCode(err) != nil { |
| 166 | + return nil |
| 167 | + } |
| 168 | + return err |
| 169 | +} |
0 commit comments