Skip to content

Commit 5a20fd8

Browse files
committed
feat:(*): Support TLS for mysql server and client.
1. Add a new CR variables tlsSecretName to support TLS. 2. Add there new auto configed mysql variables to support TLS. 3. Add Doc how to use tls
1 parent 2f4dc45 commit 5a20fd8

File tree

12 files changed

+201
-4
lines changed

12 files changed

+201
-4
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,8 @@ bin/
2626

2727
# e2e logs
2828
test/e2e/logs_*
29+
# vscode local
30+
.devcontainer
2931

32+
# vs debug files
33+
__debug_*

api/v1alpha1/mysqlcluster_types.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ type MysqlClusterSpec struct {
9696
// +optional
9797
// +kubebuilder:default:=6
9898
BackupScheduleJobsHistoryLimit *int `json:"backupScheduleJobsHistoryLimit,omitempty"`
99+
// Containing CA (ca.crt) and server cert (tls.crt) ,server private key (tls.key) for SSL
100+
//+optional
101+
TlsSecretName string `json:"tlsSecretName,omitempty"`
99102
}
100103

101104
// MysqlOpts defines the options of MySQL container.

charts/mysql-operator/crds/mysql.radondb.com_mysqlclusters.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,6 +1264,10 @@ spec:
12641264
description: Represents the name of the cluster restore from backup
12651265
path.
12661266
type: string
1267+
tlsSecretName:
1268+
description: containing CA (ca.crt) and server cert (tls.crt) ,server
1269+
private key (tls.key) for SSL
1270+
type: string
12671271
xenonOpts:
12681272
default:
12691273
admitDefeatHearbeatCount: 5

config/crd/bases/mysql.radondb.com_mysqlclusters.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,6 +1264,10 @@ spec:
12641264
description: Represents the name of the cluster restore from backup
12651265
path.
12661266
type: string
1267+
tlsSecretName:
1268+
description: containing CA (ca.crt) and server cert (tls.crt) ,server
1269+
private key (tls.key) for SSL
1270+
type: string
12671271
xenonOpts:
12681272
default:
12691273
admitDefeatHearbeatCount: 5

docs/zh-cn/how_to_use_tls.md

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
[TOC]
2+
3+
# 为MySQL客户端开启加密连接
4+
5+
# `TLS`(传输层加密)简介
6+
7+
RadonDB MySQL Operator 默认采用非加密连接,如果具备网络嗅探及监视的第三方工具可能截获服务端与客户端之间的数据,容易造成信息泄露,因此建议开启加密连接来确保数据安全。
8+
9+
RadonDB MySQL Operator 服务端支持`TLS`,协议为MySQL支持的加密协议,如`5.7`版本支持`TLS 1.0、TLS 1.1、TLS 1.2``8.0`版本支持`TLS 1.0、TLS 1.1、TLS 1.2、TLS 1.3`
10+
11+
使用加密连接需要满足两个条件:
12+
13+
* MySQL Operator 服务端开启加密连接支持
14+
* 客户端使用加密连接
15+
16+
# 配置`MySQL Operator`使用加密连接
17+
18+
## 准备证书
19+
20+
* `ca.crt` - 服务端`CA`证书
21+
* `tls.key` - 服务端证书私钥
22+
* `tls.crt` - 服务端证书
23+
24+
可以用`OpenSSL`生成,也可以用`MySQL`自带的`mysql_ssl_rsa_setup`快捷生成:
25+
26+
`mysql_ssl_rsa_setup --datadir=/tmp/certs`
27+
28+
运行该命令后会生成如下文件:
29+
30+
```shell
31+
certs
32+
├── ca-key.pem
33+
├── ca.pem
34+
├── client-cert.pem
35+
├── client-key.pem
36+
├── private_key.pem
37+
├── public_key.pem
38+
├── server-cert.pem
39+
└── server-key.pem
40+
```
41+
42+
43+
44+
### 根据证书文件创建secret
45+
46+
```shell
47+
kubectl create secret generic sample-ssl --from-file=tls.crt=server.pem --
48+
from-file=tls.key=server-key.pem --from-file=ca.crt=ca.pem --
49+
type=kubernetes.io/tls
50+
```
51+
52+
### 配置RadonDB MySQL 集群使用`TLS`
53+
54+
```shell
55+
kubectl patch mysqlclusters.mysql.radondb.com sample --type=merge -p '{"spec":{"tlsSecretName":"sample-ssl"}}'
56+
```
57+
58+
> 配置之后会触发`rolling update`即集群会重启
59+
60+
### 验证测试
61+
62+
* 不使用`SSL`连接
63+
64+
```shell
65+
kubectl exec -it sample-mysql-0 -c mysql -- mysql -uradondb_usr -p"RadonDB@123" -e "\s"
66+
mysql Ver 14.14 Distrib 5.7.34-37, for Linux (x86_64) using 7.0
67+
Connection id: 7940
68+
Current database:
69+
Current user: radondb_usr@localhost
70+
SSL: Not in use
71+
Current pager: stdout
72+
Using outfile: ''
73+
Using delimiter: ;
74+
Server version: 5.7.34-37-log Percona Server (GPL), Release 37, Revision 7c516e9
75+
Protocol version: 10
76+
Connection: Localhost via UNIX socket
77+
Server characterset: utf8mb4
78+
Db characterset: utf8mb4
79+
Client characterset: latin1
80+
Conn. characterset: latin1
81+
UNIX socket: /var/lib/mysql/mysql.sock
82+
Uptime: 21 hours 49 min 36 sec
83+
84+
Threads: 5 Questions: 181006 Slow queries: 0 Opens: 127 Flush tables: 1 Open tables: 120 Queries per second avg: 2.303
85+
```
86+
87+
88+
89+
* 使用`SSL`连接
90+
91+
```shell
92+
kubectl exec -it sample-mysql-0 -c mysql -- mysql -uradondb_usr -p"RadonDB@123" --ssl-mode=REQUIRED -e "\s"
93+
mysql: [Warning] Using a password on the command line interface can be insecure.
94+
--------------
95+
mysql Ver 14.14 Distrib 5.7.34-37, for Linux (x86_64) using 7.0
96+
97+
Connection id: 7938
98+
Current database:
99+
Current user: radondb_usr@localhost
100+
SSL: Cipher in use is ECDHE-RSA-AES128-GCM-SHA256
101+
Current pager: stdout
102+
Using outfile: ''
103+
Using delimiter: ;
104+
Server version: 5.7.34-37-log Percona Server (GPL), Release 37, Revision 7c516e9
105+
Protocol version: 10
106+
Connection: Localhost via UNIX socket
107+
Server characterset: utf8mb4
108+
Db characterset: utf8mb4
109+
Client characterset: latin1
110+
Conn. characterset: latin1
111+
UNIX socket: /var/lib/mysql/mysql.sock
112+
Uptime: 21 hours 49 min 26 sec
113+
114+
Threads: 5 Questions: 180985 Slow queries: 0 Opens: 127 Flush tables: 1 Open tables: 120 Queries per second avg: 2.303
115+
```

mysqlcluster/container/init_sidecar.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,17 @@ func (c *initSidecar) getVolumeMounts() []corev1.VolumeMount {
200200
MountPath: utils.SysLocalTimeZoneMountPath,
201201
},
202202
}
203-
203+
if c.Spec.TlsSecretName != "" {
204+
volumeMounts = append(volumeMounts,
205+
corev1.VolumeMount{
206+
Name: utils.TlsVolumeName + "-sidecar",
207+
MountPath: "/tmp/mysql-ssl",
208+
}, corev1.VolumeMount{
209+
Name: utils.TlsVolumeName,
210+
MountPath: utils.TlsMountPath,
211+
},
212+
)
213+
}
204214
if c.Spec.MysqlOpts.InitTokuDB {
205215
volumeMounts = append(volumeMounts,
206216
corev1.VolumeMount{

mysqlcluster/container/mysql.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func (c *mysql) getReadinessProbe() *corev1.Probe {
133133

134134
// getVolumeMounts get the container volumeMounts.
135135
func (c *mysql) getVolumeMounts() []corev1.VolumeMount {
136-
return []corev1.VolumeMount{
136+
volumeMounts := []corev1.VolumeMount{
137137
{
138138
Name: utils.MysqlConfVolumeName,
139139
MountPath: utils.MysqlConfVolumeMountPath,
@@ -151,4 +151,13 @@ func (c *mysql) getVolumeMounts() []corev1.VolumeMount {
151151
MountPath: utils.SysLocalTimeZoneMountPath,
152152
},
153153
}
154+
if c.Spec.TlsSecretName != "" {
155+
volumeMounts = append(volumeMounts,
156+
corev1.VolumeMount{
157+
Name: utils.TlsVolumeName,
158+
MountPath: utils.TlsMountPath,
159+
},
160+
)
161+
}
162+
return volumeMounts
154163
}

mysqlcluster/mysqlcluster.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,22 @@ func (c *MysqlCluster) EnsureVolumes() []corev1.Volume {
270270
},
271271
})
272272
}
273+
// Add the ssl secret mounts.
274+
if len(c.Spec.TlsSecretName) != 0 {
275+
volumes = append(volumes, corev1.Volume{
276+
Name: utils.TlsVolumeName + "-sidecar",
277+
VolumeSource: corev1.VolumeSource{
278+
Secret: &corev1.SecretVolumeSource{
279+
SecretName: c.Spec.TlsSecretName,
280+
},
281+
},
282+
}, corev1.Volume{
283+
Name: utils.TlsVolumeName,
284+
VolumeSource: corev1.VolumeSource{
285+
EmptyDir: &corev1.EmptyDirVolumeSource{},
286+
},
287+
})
288+
}
273289
return volumes
274290
}
275291

mysqlcluster/syncer/mysql_cm.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ func buildMysqlConf(c *mysqlcluster.MysqlCluster) (string, error) {
9292
log.Error(err, "failed to add boolean key to config section", "key", key)
9393
}
9494
}
95-
95+
if len(c.Spec.TlsSecretName) != 0 {
96+
addKVConfigsToSection(sec, mysqlSSLConfigs)
97+
}
9698
data, err := writeConfigs(cfg)
9799
if err != nil {
98100
return "", err

mysqlcluster/syncer/mysql_configs.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,10 @@ var mysqlBooleanConfigs = []string{
152152
"log-slave-updates",
153153
"!includedir /etc/mysql/conf.d",
154154
}
155+
156+
// mysqlSSLConfigs is the ist of the mysql ssl configs.
157+
var mysqlSSLConfigs = map[string]string{
158+
"ssl_ca": "/etc/mysql-ssl/ca.crt",
159+
"ssl_cert": "/etc/mysql-ssl/tls.crt",
160+
"ssl_key": "/etc/mysql-ssl/tls.key",
161+
}

0 commit comments

Comments
 (0)