|
14 | 14 | package server |
15 | 15 |
|
16 | 16 | import ( |
17 | | - "encoding/json" |
| 17 | + "strings" |
18 | 18 | "testing" |
19 | 19 |
|
20 | | - tiflowConfig "github.com/pingcap/tiflow/pkg/config" |
21 | 20 | "github.com/stretchr/testify/require" |
22 | 21 | ) |
23 | 22 |
|
24 | | -func TestRunTiFlowServerPopulatesSecurityConfig(t *testing.T) { |
25 | | - // This test verifies that TLS credentials from CLI flags are properly |
26 | | - // transferred to serverConfig.Security before being passed to tiflow. |
27 | | - // See https://github.com/pingcap/ticdc/issues/3718 |
28 | | - |
| 23 | +func TestBuildTiFlowServerOptionsPropagatesTLSFlags(t *testing.T) { |
| 24 | + // Scenario: TiCDC runs in old architecture mode and delegates to tiflow's |
| 25 | + // server command, but reuses TiCDC's cobra.Command (flags are bound to TiCDC's |
| 26 | + // options, not tiflow's). If we don't copy TLS flags to tiflowServer.Options, |
| 27 | + // tiflow will see TLS flags as visited but with empty values, clear the |
| 28 | + // security config, and fail when PD endpoints are https. |
29 | 29 | o := newOptions() |
30 | 30 | o.caPath = "/path/to/ca.crt" |
31 | 31 | o.certPath = "/path/to/server.crt" |
32 | 32 | o.keyPath = "/path/to/server.key" |
33 | 33 | o.allowedCertCN = "cn1,cn2" |
| 34 | + o.pdEndpoints = []string{"https://127.0.0.1:2379"} |
34 | 35 |
|
35 | | - // Verify that before calling getCredential, serverConfig.Security is empty |
36 | | - require.Empty(t, o.serverConfig.Security.CAPath) |
37 | | - require.Empty(t, o.serverConfig.Security.CertPath) |
38 | | - require.Empty(t, o.serverConfig.Security.KeyPath) |
39 | | - |
40 | | - // Simulate what runTiFlowServer does: populate Security from CLI flags |
41 | | - o.serverConfig.Security = o.getCredential() |
42 | | - |
43 | | - // Verify Security is now populated |
44 | | - require.Equal(t, "/path/to/ca.crt", o.serverConfig.Security.CAPath) |
45 | | - require.Equal(t, "/path/to/server.crt", o.serverConfig.Security.CertPath) |
46 | | - require.Equal(t, "/path/to/server.key", o.serverConfig.Security.KeyPath) |
47 | | - require.Equal(t, []string{"cn1", "cn2"}, o.serverConfig.Security.CertAllowedCN) |
48 | | - |
49 | | - // Verify that JSON marshaling preserves the Security config |
50 | | - cfgData, err := json.Marshal(o.serverConfig) |
51 | | - require.NoError(t, err) |
52 | | - |
53 | | - var oldCfg tiflowConfig.ServerConfig |
54 | | - err = json.Unmarshal(cfgData, &oldCfg) |
| 36 | + oldOptions, err := buildTiFlowServerOptions(o) |
55 | 37 | require.NoError(t, err) |
56 | 38 |
|
57 | | - // This is the critical assertion: tiflow's ServerConfig.Security |
58 | | - // should have the TLS credentials after unmarshaling |
59 | | - require.Equal(t, "/path/to/ca.crt", oldCfg.Security.CAPath) |
60 | | - require.Equal(t, "/path/to/server.crt", oldCfg.Security.CertPath) |
61 | | - require.Equal(t, "/path/to/server.key", oldCfg.Security.KeyPath) |
62 | | - require.Equal(t, []string{"cn1", "cn2"}, oldCfg.Security.CertAllowedCN) |
| 39 | + // Ensure tiflow options carry TLS flags, so tiflow can rebuild credentials |
| 40 | + // regardless of cobra flag binding. |
| 41 | + require.Equal(t, "/path/to/ca.crt", oldOptions.CaPath) |
| 42 | + require.Equal(t, "/path/to/server.crt", oldOptions.CertPath) |
| 43 | + require.Equal(t, "/path/to/server.key", oldOptions.KeyPath) |
| 44 | + require.Equal(t, "cn1,cn2", oldOptions.AllowedCertCN) |
| 45 | + |
| 46 | + // Ensure the converted tiflow ServerConfig also contains the TLS credential, |
| 47 | + // which is used for logging and downstream config consumers. |
| 48 | + require.Equal(t, "/path/to/ca.crt", oldOptions.ServerConfig.Security.CAPath) |
| 49 | + require.Equal(t, "/path/to/server.crt", oldOptions.ServerConfig.Security.CertPath) |
| 50 | + require.Equal(t, "/path/to/server.key", oldOptions.ServerConfig.Security.KeyPath) |
| 51 | + require.Equal(t, []string{"cn1", "cn2"}, oldOptions.ServerConfig.Security.CertAllowedCN) |
| 52 | + |
| 53 | + require.Equal(t, strings.Join(o.pdEndpoints, ","), oldOptions.ServerPdAddr) |
63 | 54 | } |
64 | 55 |
|
65 | 56 | func TestGetCredential(t *testing.T) { |
|
0 commit comments