Skip to content

Commit 956e280

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.
1 parent b3e4120 commit 956e280

File tree

12 files changed

+88
-6
lines changed

12 files changed

+88
-6
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
@@ -85,6 +85,9 @@ type MysqlClusterSpec struct {
8585
// Represents NFS ip address where cluster restore from.
8686
// +optional
8787
NFSServerAddress string `json:"nfsServerAddress,omitempty"`
88+
//containing CA (ca.pem) and optional CRL (crl.pem) ,server certificate and private key for SSL
89+
//+optional
90+
TlsSecretName string `json:"tlsSecretName,omitempty"`
8891
}
8992

9093
// MysqlOpts defines the options of MySQL container.

charts/mysql-operator/values.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ tolerationSeconds: 30
2121

2222
manager:
2323
image: radondb/mysql-operator
24-
tag: v2.2.0
24+
tag: v2.2.0-beta.1
2525
enabledWebhooks: true
2626
resources: {}
2727
# We usually recommend not to specify default resources and to leave this as a conscious

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,6 +1256,10 @@ spec:
12561256
description: Represents the name of the cluster restore from backup
12571257
path.
12581258
type: string
1259+
tlsSecretName:
1260+
description: containing CA (ca.pem) and optional CRL (crl.pem) ,server
1261+
certificate and private key for SSL
1262+
type: string
12591263
xenonOpts:
12601264
default:
12611265
admitDefeatHearbeatCount: 5

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+
}

sidecar/init.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,10 @@ func runInitCommand(cfg *Config) error {
140140
if err = copyFile(path.Join(mysqlCMPath, "my.cnf"), path.Join(mysqlConfigPath, "my.cnf")); err != nil {
141141
return fmt.Errorf("failed to copy my.cnf: %s", err)
142142
}
143-
143+
//ssl settins
144+
if exists, _ := checkIfPathExists(utils.TlsMountPath); exists {
145+
buildSSLdata(uid, gid)
146+
}
144147
buildDefaultXenonMeta(uid, gid)
145148

146149
// build client.conf.
@@ -163,7 +166,6 @@ func runInitCommand(cfg *Config) error {
163166
if err = os.Chown(extraConfPath, uid, gid); err != nil {
164167
return fmt.Errorf("failed to chown %s: %s", dataPath, err)
165168
}
166-
167169
// Run reset master in init-mysql container.
168170
if err = ioutil.WriteFile(initFilePath+"/reset.sql", []byte("reset master;"), 0644); err != nil {
169171
return fmt.Errorf("failed to write reset.sql: %s", err)
@@ -315,3 +317,18 @@ func buildDefaultXenonMeta(uid, gid int) error {
315317
}
316318
return nil
317319
}
320+
func buildSSLdata(uid, gid int) error {
321+
// cp -rp /tmp/myssl/* /etc/mysql/ssl/
322+
//refer https://stackoverflow.com/questions/31467153/golang-failed-exec-command-that-works-in-terminal
323+
shellCmd := "cp /tmp/mysql-ssl/* " + utils.TlsMountPath
324+
cmd := exec.Command("sh", "-c", shellCmd)
325+
if err := cmd.Run(); err != nil {
326+
return fmt.Errorf("failed to copy ssl: %s", err)
327+
}
328+
cronCmd := "chown -R mysql.mysql " + utils.TlsMountPath
329+
cmd = exec.Command("sh", "-c", cronCmd)
330+
if err := cmd.Run(); err != nil {
331+
return fmt.Errorf("failed to copy ssl: %s", err)
332+
}
333+
return nil
334+
}

0 commit comments

Comments
 (0)