Skip to content

Commit b7ffb32

Browse files
committed
K8SPS-265: Fix escaping passwords
1 parent 49be220 commit b7ffb32

File tree

6 files changed

+18
-14
lines changed

6 files changed

+18
-14
lines changed

build/ps-pre-stop.sh

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@ fi
88

99
LOG_FILE=/var/lib/mysql/pre-stop.log
1010
NAMESPACE=$(</var/run/secrets/kubernetes.io/serviceaccount/namespace)
11-
OPERATOR_PASSWORD=$(</etc/mysql/mysql-users-secret/operator)
11+
OPERATOR_PASSWORD=$(jq -rn --arg x "$(</etc/mysql/mysql-users-secret/operator)" '$x|@uri')
1212
FQDN="${HOSTNAME}.${SERVICE_NAME}.${NAMESPACE}"
1313
POD_IP=$(hostname -I | awk '{print $1}')
1414

1515
echo "$(date +%Y-%m-%dT%H:%M:%S%Z): Removing ${FQDN} from cluster" >>${LOG_FILE}
16-
mysqlsh --js -i -h "${POD_IP}" -P 33062 -u operator -p"${OPERATOR_PASSWORD}" -e "dba.getCluster().removeInstance('${FQDN}:3306')" >>${LOG_FILE} 2>&1
16+
17+
mysqlsh --js -i \
18+
-h "${POD_IP}" -P 33062 \
19+
-u operator -p"${OPERATOR_PASSWORD}" \
20+
-e "dba.getCluster().removeInstance('${FQDN}:3306')" >>${LOG_FILE} 2>&1

cmd/bootstrap/group_replication.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,8 @@ func (m *mysqlsh) getURI() string {
5555
if err != nil {
5656
return ""
5757
}
58-
escapedPass := url.QueryEscape(operatorPass)
5958

60-
return fmt.Sprintf("%s:%s@%s", apiv1alpha1.UserOperator, escapedPass, m.host)
59+
return fmt.Sprintf("%s:%s@%s", apiv1alpha1.UserOperator, url.QueryEscape(operatorPass), m.host)
6160
}
6261

6362
func (m *mysqlsh) run(ctx context.Context, cmd string) (bytes.Buffer, bytes.Buffer, error) {

pkg/controller/ps/controller.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"crypto/md5"
2222
"encoding/json"
2323
"fmt"
24-
"net/url"
2524
"reflect"
2625
"slices"
2726
"strconv"
@@ -221,7 +220,7 @@ func (r *PerconaServerMySQLReconciler) deleteMySQLPods(ctx context.Context, cr *
221220
return errors.Wrap(err, "get operator password")
222221
}
223222

224-
firstPodUri := fmt.Sprintf("%s:%s@%s", apiv1alpha1.UserOperator, url.QueryEscape(operatorPass), mysql.PodFQDN(cr, &firstPod))
223+
firstPodUri := getMySQLURI(apiv1alpha1.UserOperator, operatorPass, mysql.PodFQDN(cr, &firstPod))
225224

226225
um := database.NewReplicationManager(&firstPod, r.ClientCmd, apiv1alpha1.UserOperator, operatorPass, mysql.PodFQDN(cr, &firstPod))
227226

@@ -268,7 +267,7 @@ func (r *PerconaServerMySQLReconciler) deleteMySQLPods(ctx context.Context, cr *
268267
continue
269268
}
270269

271-
podUri := fmt.Sprintf("%s:%s@%s", apiv1alpha1.UserOperator, operatorPass, podFQDN)
270+
podUri := getMySQLURI(apiv1alpha1.UserOperator, operatorPass, podFQDN)
272271

273272
log.Info("Removing member from GR", "member", pod.Name, "memberState", state)
274273
err = mysh.RemoveInstanceWithExec(ctx, cr.InnoDBClusterName(), podUri)
@@ -579,7 +578,6 @@ func (r *PerconaServerMySQLReconciler) reconcileDatabase(ctx context.Context, cr
579578
}
580579

581580
if cr.Spec.UpdateStrategy == apiv1alpha1.SmartUpdateStatefulSetStrategyType {
582-
log.Info("Performing smart update for StatefulSet")
583581
return r.smartUpdate(ctx, sts, cr)
584582
}
585583

@@ -1102,7 +1100,7 @@ func (r *PerconaServerMySQLReconciler) rescanClusterIfNeeded(ctx context.Context
11021100
return errors.Wrap(err, "get operator password")
11031101
}
11041102

1105-
uri := fmt.Sprintf("%s:%s@%s", apiv1alpha1.UserOperator, operatorPass, mysql.PodFQDN(cr, pod))
1103+
uri := getMySQLURI(apiv1alpha1.UserOperator, operatorPass, mysql.PodFQDN(cr, pod))
11061104

11071105
msh, err := mysqlsh.NewWithExec(r.ClientCmd, pod, uri)
11081106
if err != nil {

pkg/controller/ps/crash_recovery.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"context"
66
"fmt"
7-
"net/url"
87
"strings"
98

109
"github.com/pkg/errors"
@@ -74,8 +73,8 @@ func (r *PerconaServerMySQLReconciler) reconcileFullClusterCrash(ctx context.Con
7473
return errors.Wrap(err, "get operator password")
7574
}
7675

77-
podFQDN := fmt.Sprintf("%s.%s.%s", pod.Name, mysql.ServiceName(cr), cr.Namespace)
78-
podUri := fmt.Sprintf("%s:%s@%s", apiv1alpha1.UserOperator, url.QueryEscape(operatorPass), podFQDN)
76+
podFQDN := mysql.PodFQDN(cr, &pod)
77+
podUri := getMySQLURI(apiv1alpha1.UserOperator, operatorPass, podFQDN)
7978

8079
mysh, err := mysqlsh.NewWithExec(r.ClientCmd, &pod, podUri)
8180
if err != nil {

pkg/controller/ps/status.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"context"
66
"fmt"
7-
"net/url"
87
"strings"
98

109
"github.com/pkg/errors"
@@ -252,7 +251,7 @@ func (r *PerconaServerMySQLReconciler) isGRReady(ctx context.Context, cr *apiv1a
252251
return false, nil
253252
}
254253

255-
uri := fmt.Sprintf("%s:%s@%s", apiv1alpha1.UserOperator, url.QueryEscape(operatorPass), mysql.PodFQDN(cr, pod))
254+
uri := getMySQLURI(apiv1alpha1.UserOperator, operatorPass, mysql.PodFQDN(cr, pod))
256255

257256
msh, err := mysqlsh.NewWithExec(r.ClientCmd, pod, uri)
258257
if err != nil {

pkg/controller/ps/user.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"context"
66
"fmt"
7+
"net/url"
78
"strings"
89

910
"github.com/pkg/errors"
@@ -448,3 +449,7 @@ func (r *PerconaServerMySQLReconciler) passwordsPropagated(ctx context.Context,
448449
log.Info("Updated password propagated")
449450
return nil
450451
}
452+
453+
func getMySQLURI(user apiv1alpha1.SystemUser, password, host string) string {
454+
return fmt.Sprintf("%s:%s@%s", user, url.QueryEscape(password), host)
455+
}

0 commit comments

Comments
 (0)