Skip to content

Commit 4c06280

Browse files
authored
[cherry-pick] Adding TLS support on MariaDB plugin (#62) (#82)
/cherry-pick Signed-off-by: Alif Biswas <alif@appscode.com>
1 parent d6d9a6e commit 4c06280

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

pkg/backup.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ package pkg
1919
import (
2020
"context"
2121
"fmt"
22+
"io/ioutil"
23+
"os"
2224
"path/filepath"
2325
"strings"
2426

@@ -38,6 +40,10 @@ import (
3840
v1 "kmodules.xyz/offshoot-api/api/v1"
3941
)
4042

43+
const (
44+
MariaDBTLSRootCA = "ca.crt"
45+
)
46+
4147
func NewCmdBackup() *cobra.Command {
4248
var (
4349
masterURL string
@@ -221,8 +227,20 @@ func (opt *mariadbOptions) backupMariaDB(targetRef api_v1beta1.TargetRef) (*rest
221227
backupCmd.Args = append(backupCmd.Args, arg)
222228
}
223229

230+
// if ssl enabled, add ca.crt in the arguments
231+
if appBinding.Spec.ClientConfig.CABundle != nil {
232+
if err := ioutil.WriteFile(filepath.Join(opt.setupOptions.ScratchDir, MariaDBTLSRootCA), appBinding.Spec.ClientConfig.CABundle, os.ModePerm); err != nil {
233+
return nil, err
234+
}
235+
tlsCreds := []interface{}{
236+
fmt.Sprintf("--ssl-ca=%v", filepath.Join(opt.setupOptions.ScratchDir, MariaDBTLSRootCA)),
237+
}
238+
239+
backupCmd.Args = append(backupCmd.Args, tlsCreds...)
240+
}
241+
224242
// wait for DB ready
225-
err = waitForDBReady(appBinding, appBindingSecret, opt.waitTimeout)
243+
err = opt.waitForDBReady(appBinding, appBindingSecret)
226244
if err != nil {
227245
return nil, err
228246
}

pkg/restore.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ package pkg
1919
import (
2020
"context"
2121
"fmt"
22+
"io/ioutil"
23+
"os"
2224
"path/filepath"
2325
"strings"
2426

@@ -186,12 +188,24 @@ func (opt *mariadbOptions) restoreMariaDB(targetRef api_v1beta1.TargetRef) (*res
186188
if appBinding.Spec.ClientConfig.Service.Port != 0 {
187189
restoreCmd.Args = append(restoreCmd.Args, fmt.Sprintf("--port=%d", appBinding.Spec.ClientConfig.Service.Port))
188190
}
191+
// if ssl enabled, add ca.crt in the arguments
192+
if appBinding.Spec.ClientConfig.CABundle != nil {
193+
if err := ioutil.WriteFile(filepath.Join(opt.setupOptions.ScratchDir, MariaDBTLSRootCA), appBinding.Spec.ClientConfig.CABundle, os.ModePerm); err != nil {
194+
return nil, err
195+
}
196+
tlsCreds := []interface{}{
197+
fmt.Sprintf("--ssl-ca=%v", filepath.Join(opt.setupOptions.ScratchDir, MariaDBTLSRootCA)),
198+
}
199+
200+
restoreCmd.Args = append(restoreCmd.Args, tlsCreds...)
201+
}
202+
189203
for _, arg := range strings.Fields(opt.myArgs) {
190204
restoreCmd.Args = append(restoreCmd.Args, arg)
191205
}
192206

193207
// wait for DB ready
194-
err = waitForDBReady(appBinding, appBindingSecret, opt.waitTimeout)
208+
err = opt.waitForDBReady(appBinding, appBindingSecret)
195209
if err != nil {
196210
return nil, err
197211
}

pkg/utils.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package pkg
1818

1919
import (
2020
"fmt"
21+
"path/filepath"
2122

2223
stash "stash.appscode.dev/apimachinery/client/clientset/versioned"
2324
"stash.appscode.dev/apimachinery/pkg/restic"
@@ -56,18 +57,22 @@ type mariadbOptions struct {
5657
dumpOptions restic.DumpOptions
5758
}
5859

59-
func waitForDBReady(appBinding *v1alpha1.AppBinding, secret *core.Secret, waitTimeout int32) error {
60+
func (opt *mariadbOptions) waitForDBReady(appBinding *v1alpha1.AppBinding, secret *core.Secret) error {
6061
log.Infoln("Waiting for the database to be ready.....")
6162
shell := sh.NewSession()
6263
shell.SetEnv(EnvMariaDBPassword, string(secret.Data[MariaDBPassword]))
6364
args := []interface{}{
6465
"ping",
6566
"--host", appBinding.Spec.ClientConfig.Service.Name,
66-
"--user=root",
67-
fmt.Sprintf("--wait=%d", waitTimeout),
67+
"--user", string(secret.Data[MariaDBUser]),
6868
}
6969
if appBinding.Spec.ClientConfig.Service.Port != 0 {
7070
args = append(args, fmt.Sprintf("--port=%d", appBinding.Spec.ClientConfig.Service.Port))
7171
}
72+
73+
if appBinding.Spec.ClientConfig.CABundle != nil {
74+
args = append(args, fmt.Sprintf("--ssl-ca=%v", filepath.Join(opt.setupOptions.ScratchDir, MariaDBTLSRootCA)))
75+
}
76+
7277
return shell.Command("mysqladmin", args...).Run()
7378
}

0 commit comments

Comments
 (0)