Skip to content

Commit 1ad167c

Browse files
authored
feat: support mtls for discovery (#6781)
1 parent 0ebc31e commit 1ad167c

34 files changed

+842
-72
lines changed

cmd/discovery/main.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ func main() {
9393
if tlsEnabled == strconv.FormatBool(true) {
9494
tcTls = true
9595
}
96+
discoveryMTLS := os.Getenv("DISCOVERY_MTLS_ENABLED") == strconv.FormatBool(true)
9697
// informers
9798
options := []kubeinformers.SharedInformerOption{
9899
kubeinformers.WithNamespace(os.Getenv("MY_POD_NAMESPACE")),
@@ -113,7 +114,16 @@ func main() {
113114
klog.Infof("starting TiDB Discovery server, listening on %s", addr)
114115
lister := kubeInformerFactory.Core().V1().Secrets().Lister()
115116
discoveryServer := server.NewServer(pdapi.NewDefaultPDControl(lister), dmapi.NewDefaultMasterControl(lister), cli, kubeCli)
116-
discoveryServer.ListenAndServe(addr)
117+
if discoveryMTLS {
118+
klog.Infof("mTLS enabled for discovery server")
119+
discoveryServer.ListenAndServeTLS(addr,
120+
"/var/lib/discovery-tls/tls.crt",
121+
"/var/lib/discovery-tls/tls.key",
122+
"/var/lib/discovery-tls/ca.crt",
123+
)
124+
} else {
125+
discoveryServer.ListenAndServe(addr)
126+
}
117127
}, 5*time.Second)
118128
go wait.Forever(func() {
119129
addr := fmt.Sprintf("0.0.0.0:%d", proxyPort)

docs/api-references/docs.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17020,6 +17020,23 @@ For Client: kubectl create secret generic <clusterName>-cluster-client-secret &n
1702017020
Same for other components.</p>
1702117021
</td>
1702217022
</tr>
17023+
<tr>
17024+
<td>
17025+
<code>enableDiscoveryMTLS</code></br>
17026+
<em>
17027+
bool
17028+
</em>
17029+
</td>
17030+
<td>
17031+
<em>(Optional)</em>
17032+
<p>EnableDiscoveryMTLS indicates whether to enable mutual TLS on the discovery server (port 10261).
17033+
When enabled, the discovery server presents its own certificate, and all components must present
17034+
a client certificate when calling the discovery service.
17035+
A single secret named <clusterName>-discovery-cluster-secret must be created containing ca.crt,
17036+
tls.crt and tls.key, and will be mounted to both the discovery server pod and all component pods.
17037+
This field only takes effect when Enabled is true.</p>
17038+
</td>
17039+
</tr>
1702317040
</tbody>
1702417041
</table>
1702517042
<h3 id="tlsconfig">TLSConfig</h3>

manifests/crd.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19118,6 +19118,8 @@ spec:
1911819118
type: array
1911919119
tlsCluster:
1912019120
properties:
19121+
enableDiscoveryMTLS:
19122+
type: boolean
1912119123
enabled:
1912219124
type: boolean
1912319125
type: object
@@ -58528,6 +58530,8 @@ spec:
5852858530
type: object
5852958531
tlsCluster:
5853058532
properties:
58533+
enableDiscoveryMTLS:
58534+
type: boolean
5853158535
enabled:
5853258536
type: boolean
5853358537
type: object

manifests/crd/v1/pingcap.com_dmclusters.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6321,6 +6321,8 @@ spec:
63216321
type: array
63226322
tlsCluster:
63236323
properties:
6324+
enableDiscoveryMTLS:
6325+
type: boolean
63246326
enabled:
63256327
type: boolean
63266328
type: object

manifests/crd/v1/pingcap.com_tidbclusters.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32874,6 +32874,8 @@ spec:
3287432874
type: object
3287532875
tlsCluster:
3287632876
properties:
32877+
enableDiscoveryMTLS:
32878+
type: boolean
3287732879
enabled:
3287832880
type: boolean
3287932881
type: object

pkg/apis/pingcap/v1alpha1/tidbcluster.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,6 +1135,10 @@ func (tc *TidbCluster) IsTLSClusterEnabled() bool {
11351135
return tc.Spec.TLSCluster != nil && tc.Spec.TLSCluster.Enabled
11361136
}
11371137

1138+
func (tc *TidbCluster) IsDiscoveryMTLSEnabled() bool {
1139+
return tc.IsTLSClusterEnabled() && tc.Spec.TLSCluster.EnableDiscoveryMTLS
1140+
}
1141+
11381142
func (tc *TidbCluster) IsRecoveryMode() bool {
11391143
return tc.Spec.RecoveryMode
11401144
}

pkg/apis/pingcap/v1alpha1/types.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2212,6 +2212,15 @@ type TLSCluster struct {
22122212
// Same for other components.
22132213
// +optional
22142214
Enabled bool `json:"enabled,omitempty"`
2215+
2216+
// EnableDiscoveryMTLS indicates whether to enable mutual TLS on the discovery server (port 10261).
2217+
// When enabled, the discovery server presents its own certificate, and all components must present
2218+
// a client certificate when calling the discovery service.
2219+
// A single secret named <clusterName>-discovery-cluster-secret must be created containing ca.crt,
2220+
// tls.crt and tls.key, and will be mounted to both the discovery server pod and all component pods.
2221+
// This field only takes effect when Enabled is true.
2222+
// +optional
2223+
EnableDiscoveryMTLS bool `json:"enableDiscoveryMTLS,omitempty"`
22152224
}
22162225

22172226
// +genclient

pkg/controller/tidbcluster/pod_control_test.go

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,9 @@ func TestTiKVPodSyncForEviction(t *testing.T) {
9393
})
9494

9595
stop := make(chan struct{})
96-
go func() {
97-
deps.KubeInformerFactory.Start(stop)
98-
}()
96+
deps.KubeInformerFactory.Start(stop)
9997
deps.KubeInformerFactory.WaitForCacheSync(stop)
100-
go func() {
101-
deps.InformerFactory.Start(stop)
102-
}()
98+
deps.InformerFactory.Start(stop)
10399
deps.InformerFactory.WaitForCacheSync(stop)
104100

105101
defer close(stop)
@@ -577,13 +573,9 @@ func TestPDPodSyncForLeaderTransfer(t *testing.T) {
577573
podController.testPDClient = pdClient
578574

579575
stop := make(chan struct{})
580-
go func() {
581-
deps.KubeInformerFactory.Start(stop)
582-
}()
576+
deps.KubeInformerFactory.Start(stop)
583577
deps.KubeInformerFactory.WaitForCacheSync(stop)
584-
go func() {
585-
deps.InformerFactory.Start(stop)
586-
}()
578+
deps.InformerFactory.Start(stop)
587579
deps.InformerFactory.WaitForCacheSync(stop)
588580

589581
defer close(stop)
@@ -753,13 +745,9 @@ func TestTiDBPodSyncForGracefulShutdown(t *testing.T) {
753745
podController := NewPodController(deps)
754746

755747
stop := make(chan struct{})
756-
go func() {
757-
deps.KubeInformerFactory.Start(stop)
758-
}()
748+
deps.KubeInformerFactory.Start(stop)
759749
deps.KubeInformerFactory.WaitForCacheSync(stop)
760-
go func() {
761-
deps.InformerFactory.Start(stop)
762-
}()
750+
deps.InformerFactory.Start(stop)
763751
deps.InformerFactory.WaitForCacheSync(stop)
764752

765753
defer close(stop)

pkg/discovery/server/interface.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ package server
1515

1616
type Server interface {
1717
ListenAndServe(addr string)
18+
ListenAndServeTLS(addr, certFile, keyFile, caFile string)
1819
}

pkg/discovery/server/proxy.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,7 @@ func (p *proxyServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
101101
func (p *proxyServer) ListenAndServe(addr string) {
102102
klog.Fatal(http.ListenAndServe(addr, p))
103103
}
104+
105+
func (p *proxyServer) ListenAndServeTLS(addr, certFile, keyFile, caFile string) {
106+
klog.Fatal("proxy server does not support mTLS")
107+
}

0 commit comments

Comments
 (0)