Skip to content

K8SPS-421: Add keyring vault support #938

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions api/v1alpha1/perconaservermysql_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ type MySQLSpec struct {
SidecarVolumes []corev1.Volume `json:"sidecarVolumes,omitempty"`
SidecarPVCs []SidecarPVC `json:"sidecarPVCs,omitempty"`

VaultSecretName string `json:"vaultSecretName,omitempty"`

PodSpec `json:",inline"`
}

Expand Down Expand Up @@ -572,6 +574,16 @@ func (cr *PerconaServerMySQL) SetVersion() {
cr.Spec.CRVersion = version.Version()
}

func (cr *PerconaServerMySQL) Version() *v.Version {
return v.Must(v.NewVersion(cr.Spec.CRVersion))
}

// CompareVersion compares given version to current version.
// Returns -1, 0, or 1 if given version is smaller, equal, or larger than the current version, respectively.
func (cr *PerconaServerMySQL) CompareVersion(ver string) int {
return cr.Version().Compare(v.Must(v.NewVersion(ver)))
}

// CheckNSetDefaults validates and sets default values for the PerconaServerMySQL custom resource.
func (cr *PerconaServerMySQL) CheckNSetDefaults(_ context.Context, serverVersion *platform.ServerVersion) error {
if len(cr.Spec.MySQL.ClusterType) == 0 {
Expand Down Expand Up @@ -862,6 +874,10 @@ func (cr *PerconaServerMySQL) CheckNSetDefaults(_ context.Context, serverVersion
cr.Spec.SSLSecretName = cr.Name + "-ssl"
}

if cr.Spec.MySQL.VaultSecretName == "" {
cr.Spec.MySQL.VaultSecretName = cr.Name + "-vault"
}

return nil
}

Expand Down
20 changes: 20 additions & 0 deletions build/ps-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
case "$f" in
*.sh)
echo "$0: running $f"
. "$f"

Check warning on line 61 in build/ps-entrypoint.sh

View workflow job for this annotation

GitHub Actions / shellcheck

[shellcheck] build/ps-entrypoint.sh#L61 <ShellCheck.SC1090>

ShellCheck can't follow non-constant source. Use a directive to specify location.
Raw output
./build/ps-entrypoint.sh:61:6: warning: ShellCheck can't follow non-constant source. Use a directive to specify location. (ShellCheck.SC1090)
;;
*.sql)
echo "$0: running $f"
Expand Down Expand Up @@ -167,6 +167,26 @@
sed -i "/\[mysqld\]/a ssl_key=${TLS_DIR}/tls.key" $CFG
fi

# if vault secret file exists we assume we need to turn on encryption
vault_secret="/etc/mysql/vault-keyring-secret/keyring_vault.conf"
if [[ -f "${vault_secret}" ]]; then
sed -i "/\[mysqld\]/a early-plugin-load=keyring_vault.so" $CFG
sed -i "/\[mysqld\]/a keyring_vault_config=${vault_secret}" $CFG

if [[ ${MYSQL_VERSION} =~ ^(8\.0|8\.4)$ ]]; then
sed -i "/\[mysqld\]/a default_table_encryption=ON" $CFG
sed -i "/\[mysqld\]/a table_encryption_privilege_check=ON" $CFG
sed -i "/\[mysqld\]/a innodb_undo_log_encrypt=ON" $CFG
sed -i "/\[mysqld\]/a innodb_redo_log_encrypt=ON" $CFG
sed -i "/\[mysqld\]/a binlog_encryption=ON" $CFG
sed -i "/\[mysqld\]/a binlog_rotate_encryption_master_key_at_startup=ON" $CFG
sed -i "/\[mysqld\]/a innodb_temp_tablespace_encrypt=ON" $CFG
sed -i "/\[mysqld\]/a innodb_parallel_dblwr_encrypt=ON" $CFG
sed -i "/\[mysqld\]/a innodb_encrypt_online_alter_logs=ON" $CFG
sed -i "/\[mysqld\]/a encrypt_tmp_files=ON" $CFG
fi
fi

for f in "${CUSTOM_CONFIG_FILES[@]}"; do
echo "${f}"
if [ -f "${f}" ]; then
Expand Down Expand Up @@ -197,7 +217,7 @@
exit 1
fi

if [ "$1" = 'mysqld' -a -z "$wantHelp" ]; then

Check warning on line 220 in build/ps-entrypoint.sh

View workflow job for this annotation

GitHub Actions / shellcheck

[shellcheck] build/ps-entrypoint.sh#L220 <ShellCheck.SC2166>

Prefer [ p ] && [ q ] as [ p -a q ] is not well defined.
Raw output
./build/ps-entrypoint.sh:220:22: warning: Prefer [ p ] && [ q ] as [ p -a q ] is not well defined. (ShellCheck.SC2166)
# still need to check config, container may have started with --user
_check_config "$@"

Expand All @@ -213,7 +233,7 @@
touch /var/lib/mysql/bootstrap.lock
file_env 'MYSQL_ROOT_PASSWORD' '' 'root'
{ set +x; } 2>/dev/null
if [ -z "$MYSQL_ROOT_PASSWORD" -a -z "$MYSQL_ALLOW_EMPTY_PASSWORD" -a -z "$MYSQL_RANDOM_ROOT_PASSWORD" ]; then

Check warning on line 236 in build/ps-entrypoint.sh

View workflow job for this annotation

GitHub Actions / shellcheck

[shellcheck] build/ps-entrypoint.sh#L236 <ShellCheck.SC2166>

Prefer [ p ] && [ q ] as [ p -a q ] is not well defined.
Raw output
./build/ps-entrypoint.sh:236:34: warning: Prefer [ p ] && [ q ] as [ p -a q ] is not well defined. (ShellCheck.SC2166)

Check warning on line 236 in build/ps-entrypoint.sh

View workflow job for this annotation

GitHub Actions / shellcheck

[shellcheck] build/ps-entrypoint.sh#L236 <ShellCheck.SC2166>

Prefer [ p ] && [ q ] as [ p -a q ] is not well defined.
Raw output
./build/ps-entrypoint.sh:236:70: warning: Prefer [ p ] && [ q ] as [ p -a q ] is not well defined. (ShellCheck.SC2166)
echo >&2 'error: database is uninitialized and password option is not specified '
echo >&2 ' You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD'
exit 1
Expand Down Expand Up @@ -271,7 +291,7 @@
rootCreate=
# default root to listen for connections from anywhere
file_env 'MYSQL_ROOT_HOST' '%'
if [ -n "$MYSQL_ROOT_HOST" -a "$MYSQL_ROOT_HOST" != 'localhost' ]; then

Check warning on line 294 in build/ps-entrypoint.sh

View workflow job for this annotation

GitHub Actions / shellcheck

[shellcheck] build/ps-entrypoint.sh#L294 <ShellCheck.SC2166>

Prefer [ p ] && [ q ] as [ p -a q ] is not well defined.
Raw output
./build/ps-entrypoint.sh:294:30: warning: Prefer [ p ] && [ q ] as [ p -a q ] is not well defined. (ShellCheck.SC2166)
# no, we don't care if read finds a terminating character in this heredoc
# https://unix.stackexchange.com/questions/265149/why-is-set-o-errexit-breaking-this-read-heredoc-expression/265151#265151
read -r -d '' rootCreate <<-EOSQL || true
Expand Down Expand Up @@ -353,7 +373,7 @@
file_env 'MYSQL_USER'
file_env 'MYSQL_PASSWORD'
{ set +x; } 2>/dev/null
if [ "$MYSQL_USER" -a "$MYSQL_PASSWORD" ]; then

Check warning on line 376 in build/ps-entrypoint.sh

View workflow job for this annotation

GitHub Actions / shellcheck

[shellcheck] build/ps-entrypoint.sh#L376 <ShellCheck.SC2166>

Prefer [ p ] && [ q ] as [ p -a q ] is not well defined.
Raw output
./build/ps-entrypoint.sh:376:22: warning: Prefer [ p ] && [ q ] as [ p -a q ] is not well defined. (ShellCheck.SC2166)
echo "CREATE USER '$MYSQL_USER'@'%' IDENTIFIED BY '$MYSQL_PASSWORD' ;" | "${mysql[@]}"

if [ "$MYSQL_DATABASE" ]; then
Expand Down Expand Up @@ -401,7 +421,7 @@
if [[ -f /var/lib/mysql/full-cluster-crash ]]; then
set +o xtrace
node_name=$(hostname -f)
cluster_name=$(hostname | cut -d '-' -f1) # TODO: This won't work if CR has `-` in its name.

Check warning on line 424 in build/ps-entrypoint.sh

View workflow job for this annotation

GitHub Actions / shellcheck

[shellcheck] build/ps-entrypoint.sh#L424 <ShellCheck.SC2034>

cluster_name appears unused. Verify use (or export if used externally).
Raw output
./build/ps-entrypoint.sh:424:2: warning: cluster_name appears unused. Verify use (or export if used externally). (ShellCheck.SC2034)
gtid_executed=$(</var/lib/mysql/full-cluster-crash)
namespace=$(</var/run/secrets/kubernetes.io/serviceaccount/namespace)

Expand Down
8 changes: 7 additions & 1 deletion build/run-restore.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,13 @@
"azure") run_azure | extract "${tmpdir}" ;;
esac

xtrabackup --prepare --rollback-prepared-trx --target-dir="${tmpdir}"
local keyring=""
if [[ -f ${KEYRING_VAULT_PATH} ]]; then
echo "Using keyring vault config: ${KEYRING_VAULT_PATH}"
keyring="--keyring-vault-config=${KEYRING_VAULT_PATH}"
fi

xtrabackup --prepare --rollback-prepared-trx --target-dir="${tmpdir}" ${keyring}

Check notice on line 50 in build/run-restore.sh

View workflow job for this annotation

GitHub Actions / shellcheck

[shellcheck] build/run-restore.sh#L50 <ShellCheck.SC2086>

Double quote to prevent globbing and word splitting.
Raw output
./build/run-restore.sh:50:72: info: Double quote to prevent globbing and word splitting. (ShellCheck.SC2086)
xtrabackup --datadir="${DATADIR}" --move-back --force-non-empty-directories --target-dir="${tmpdir}"

rm -rf "${tmpdir}"
Expand Down
2 changes: 2 additions & 0 deletions config/crd/bases/ps.percona.com_perconaservermysqls.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5001,6 +5001,8 @@ spec:
- whenUnsatisfiable
type: object
type: array
vaultSecretName:
type: string
volumeSpec:
properties:
emptyDir:
Expand Down
2 changes: 2 additions & 0 deletions deploy/bundle.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6924,6 +6924,8 @@ spec:
- whenUnsatisfiable
type: object
type: array
vaultSecretName:
type: string
volumeSpec:
properties:
emptyDir:
Expand Down
1 change: 1 addition & 0 deletions deploy/cr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ spec:
image: perconalab/percona-server-mysql-operator:main-psmysql
imagePullPolicy: Always
# initImage: perconalab/percona-server-mysql-operator:main
# vaultSecretName: cluster1-vault
size: 3

# env:
Expand Down
2 changes: 2 additions & 0 deletions deploy/crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6924,6 +6924,8 @@ spec:
- whenUnsatisfiable
type: object
type: array
vaultSecretName:
type: string
volumeSpec:
properties:
emptyDir:
Expand Down
2 changes: 2 additions & 0 deletions deploy/cw-bundle.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6924,6 +6924,8 @@ spec:
- whenUnsatisfiable
type: object
type: array
vaultSecretName:
type: string
volumeSpec:
properties:
emptyDir:
Expand Down
10 changes: 10 additions & 0 deletions deploy/vault-secret.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: v1
kind: Secret
metadata:
name: cluster1-vault
type: Opaque
stringData:
keyring_vault.conf: |-
token = <secret>
vault_url = http://vault-service.vault-service.svc.cluster.local:8200
secret_mount_point = secret
13 changes: 13 additions & 0 deletions e2e-tests/conf/vault-secret.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: v1
kind: Secret
metadata:
name: vault-keyring-secret
type: Opaque
stringData:
keyring_vault.conf: |-
token = #token
vault_url = #vault_url
secret_mount_point = #secret
#vault_ca = /etc/mysql/vault-keyring-secret/ca.cert
ca.cert: |-
#certVal
Loading
Loading