Skip to content
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

K8SPSMDB-780: Unsafe improvements #1504

Merged
merged 14 commits into from
Apr 17, 2024
Merged

K8SPSMDB-780: Unsafe improvements #1504

merged 14 commits into from
Apr 17, 2024

Conversation

egegunes
Copy link
Contributor

@egegunes egegunes commented Apr 6, 2024

K8SPSMDB-780 Powered by Pull Request Badge

CHANGE DESCRIPTION

These changes attempt to fix the overloaded allowUnsafeConfigurations
flag.

In previous implementation, allowUnsafeConfigurations wasn't just
allow unsafe configuration but make everything unsafe by disabling TLS,
allowing backups in unhealthy clusters, etc... without user's explicit
intent.

With these changes, we decouple those things from the unsafe flag and
remove all implicit behaviors. We introduce a new section called
unsafeFlags:

unsafeFlags:
  tls: false
  replsetSize: false
  mongosSize: false
  terminationGracePeriod: false
  backupIfUnhealthy: false

Starting from v1.16.0, allowUnsafeConfigurations is deprecated and
won't have any affect.

TLS Mode

This decoupling required a special attention to the TLS configuration.
Before these changes only way to disable TLS is setting
allowUnsafeConfigurations to true. Now, we introduce a new field:

spec:
  tls:
    mode: disabled

This field accepts the following values: disabled, allowTLS,
preferTLS and requireTLS.

If user sets mode to disabled, the operator will throw an error: TLS must be enabled. Set spec.unsafeFlags.tls to true to disable this check.

Since the use of TLS flags and reconciling TLS secrets depends on
tls.mode field, we need to block users to set net.tls.mode in custom
MongoDB configuration. If user sets a custom configuration like:

spec:
  replsets:
  - name: rs0
    size: 3
    configuration: |
      net:
        tls:
          mode: allowTLS

the operator will throw an error: tlsMode must be set using spec.tls.mode.

CHECKLIST

Jira

  • Is the Jira ticket created and referenced properly?
  • Does the Jira ticket have the proper statuses for documentation (Needs Doc) and QA (Needs QA)?
  • Does the Jira ticket link to the proper milestone (Fix Version field)?

Tests

  • Is an E2E test/test case added for the new feature/change?
  • Are unit tests added where appropriate?
  • Are OpenShift compare files changed for E2E tests (compare/*-oc.yml)?

Config/Logging/Testability

  • Are all needed new/changed options added to default YAML files?
  • Did we add proper logging messages for operator actions?
  • Did we ensure compatibility with the previous version or cluster upgrade process?
  • Does the change support oldest and newest supported MongoDB version?
  • Does the change support oldest and newest supported Kubernetes version?

@pull-request-size pull-request-size bot added the size/XL 500-999 lines label Apr 6, 2024
Comment on lines +72 to +73
"$checkArg" | "$checkArg"=*)
return 0
;;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[shfmt] reported by reviewdog 🐶

Suggested change
"$checkArg" | "$checkArg"=*)
return 0
;;
"$checkArg" | "$checkArg"=*)
return 0
;;

Comment on lines +87 to +93
"$checkArg")
echo "$1"
return 0
;;
"$checkArg"=*)
echo "${arg#"$checkArg"=}"
return 0
;;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[shfmt] reported by reviewdog 🐶

Suggested change
"$checkArg")
echo "$1"
return 0
;;
"$checkArg"=*)
echo "${arg#"$checkArg"=}"
return 0
;;
"$checkArg")
echo "$1"
return 0
;;
"$checkArg"=*)
echo "${arg#"$checkArg"=}"
return 0
;;

Comment on lines +135 to +141
"$ensureNoArg")
shift # also skip the value
continue
;;
"$ensureNoArg"=*)
# value is already included
continue
;;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[shfmt] reported by reviewdog 🐶

Suggested change
"$ensureNoArg")
shift # also skip the value
continue
;;
"$ensureNoArg"=*)
# value is already included
continue
;;
"$ensureNoArg")
shift # also skip the value
continue
;;
"$ensureNoArg"=*)
# value is already included
continue
;;

Comment on lines +286 to +288
*.sh | *.js) # this should match the set of files we check for below
shouldPerformInitdb="$f"
break
;;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[shfmt] reported by reviewdog 🐶

Suggested change
*.sh | *.js) # this should match the set of files we check for below
shouldPerformInitdb="$f"
break
;;
*.sh | *.js) # this should match the set of files we check for below
shouldPerformInitdb="$f"
break
;;

Comment on lines +386 to +395
*.sh)
echo "$0: running $f"
# shellcheck source=/dev/null
. "$f"
;;
*.js)
echo "$0: running $f"
"${mongo[@]}" "$MONGO_INITDB_DATABASE" "$f"
echo
;;
*) echo "$0: ignoring $f" ;;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[shfmt] reported by reviewdog 🐶

Suggested change
*.sh)
echo "$0: running $f"
# shellcheck source=/dev/null
. "$f"
;;
*.js)
echo "$0: running $f"
"${mongo[@]}" "$MONGO_INITDB_DATABASE" "$f"
echo
;;
*) echo "$0: ignoring $f" ;;
*.sh)
echo "$0: running $f"
# shellcheck source=/dev/null
. "$f"
;;
*.js)
echo "$0: running $f"
"${mongo[@]}" "$MONGO_INITDB_DATABASE" "$f"
echo
;;
*) echo "$0: ignoring $f" ;;

@egegunes egegunes force-pushed the K8SPSMDB-780 branch 2 times, most recently from 10ecc42 to 52ddcce Compare April 8, 2024 09:36
These changes attempt to fix the overloaded `allowUnsafeConfigurations`
flag.

In previous implementation, `allowUnsafeConfigurations` wasn't just
allow unsafe configuration but make everything unsafe by disabling TLS,
allowing backups in unhealthy clusters, etc... without user's explicit
intent.

With these changes, we decouple those things from the unsafe flag and
remove all implicit behaviors. We introduce a new section called
`unsafeFlags`:

```
unsafeFlags:
  tls: false
  replsetSize: false
  mongosSize: false
  terminationGracePeriod: false
  backupIfUnhealthy: false
```

Starting from `v1.16.0`, `allowUnsafeConfigurations` is deprecated and
won't have any affect.

**TLS Mode**

This decoupling required a special attention to the TLS configuration.
Before these changes only way to disable TLS is setting
`allowUnsafeConfigurations` to true. Now, we introduce a new field:

```
spec:
  tls:
    mode: disabled
```

This field accepts the following values: `disabled`, `allowTLS`,
`preferTLS` and `requireTLS`.

If user sets mode to `disabled`, the operator will throw an error: `TLS
must be enabled. Set spec.unsafeFlags.tls to true to disable this
check.`

Since the use of TLS flags and reconciling TLS secrets depends on
`tls.mode` field, we need to block users to set `net.tls.mode` in custom
MongoDB configuration. If user sets a custom configuration like:

```
spec:
  replsets:
  - name: rs0
    size: 3
    configuration: |
      net:
        tls:
          mode: allowTLS
```

the operator will throw an error: `tlsMode must be set using spec.tls.mode`.
@pull-request-size pull-request-size bot added size/XXL 1000+ lines and removed size/XL 500-999 lines labels Apr 8, 2024
Comment on lines 70 to 73
cat_config "$conf_dir/$cluster.yml" |
yq eval '.spec.replsets[0].expose.enabled=true' |
yq eval '.spec.replsets[0].expose.exposeType="ClusterIP"' |
kubectl_bin apply -f -
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[shfmt] reported by reviewdog 🐶

Suggested change
cat_config "$conf_dir/$cluster.yml" |
yq eval '.spec.replsets[0].expose.enabled=true' |
yq eval '.spec.replsets[0].expose.exposeType="ClusterIP"' |
kubectl_bin apply -f -
cat_config "$conf_dir/$cluster.yml" \
| yq eval '.spec.replsets[0].expose.enabled=true' \
| yq eval '.spec.replsets[0].expose.exposeType="ClusterIP"' \
| kubectl_bin apply -f -

Comment on lines +70 to +74
cat_config "$conf_dir/$cluster.yml" |
yq eval '.spec.unsafeFlags.replsetSize=true' |
yq eval '.spec.replsets[0].expose.enabled=true' |
yq eval '.spec.replsets[0].expose.exposeType="ClusterIP"' |
kubectl_bin apply -f -
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[shfmt] reported by reviewdog 🐶

Suggested change
cat_config "$conf_dir/$cluster.yml" |
yq eval '.spec.unsafeFlags.replsetSize=true' |
yq eval '.spec.replsets[0].expose.enabled=true' |
yq eval '.spec.replsets[0].expose.exposeType="ClusterIP"' |
kubectl_bin apply -f -
cat_config "$conf_dir/$cluster.yml" \
| yq eval '.spec.unsafeFlags.replsetSize=true' \
| yq eval '.spec.replsets[0].expose.enabled=true' \
| yq eval '.spec.replsets[0].expose.exposeType="ClusterIP"' \
| kubectl_bin apply -f -

@egegunes egegunes added this to the v1.16.0 milestone Apr 15, 2024
@egegunes egegunes marked this pull request as ready for review April 16, 2024 15:55
} else {
args = append(args, "--clusterAuthMode=x509")
}
} else if (cr.CompareVersion("1.16.0") >= 0 && cr.Spec.Unsafe.TLS) || (cr.CompareVersion("1.16.0") < 0 && cr.Spec.UnsafeConf) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about creating a cr.UnsafeTLSDisabled() method to replace this long condition check?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pkg/psmdb/pmm.go Outdated
Comment on lines 300 to 302
if cr.CompareVersion("1.13.0") >= 0 && !cr.Spec.UnsafeConf {
if cr.CompareVersion("1.13.0") >= 0 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why it was removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch, we need to check if tls enabled there: 6badccf

@egegunes egegunes requested a review from pooknull April 17, 2024 08:05
@@ -226,7 +226,7 @@ func TestSetSafeDefault(t *testing.T) {
cr := &api.PerconaServerMongoDB{
ObjectMeta: metav1.ObjectMeta{Name: "psmdb-mock", Namespace: "psmdb"},
Spec: api.PerconaServerMongoDBSpec{
CRVersion: version.Version,
CRVersion: "1.15.0",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should add or update this unit tests to cover these new unsafe flags for 1.16.0.

Copy link
Collaborator

@hors hors left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JNKPercona
Copy link
Collaborator

Test name Status
arbiter passed
balancer passed
custom-replset-name passed
cross-site-sharded passed
data-at-rest-encryption passed
data-sharded passed
demand-backup passed
demand-backup-eks-credentials passed
demand-backup-physical passed
demand-backup-physical-sharded passed
demand-backup-sharded passed
expose-sharded passed
ignore-labels-annotations passed
init-deploy passed
finalizer passed
ldap passed
ldap-tls passed
limits passed
liveness passed
mongod-major-upgrade passed
mongod-major-upgrade-sharded passed
monitoring-2-0 passed
multi-cluster-service passed
non-voting passed
one-pod passed
operator-self-healing-chaos passed
pitr passed
pitr-sharded passed
pitr-physical passed
pvc-resize passed
recover-no-primary passed
rs-shard-migration passed
scaling passed
scheduled-backup passed
security-context passed
self-healing-chaos passed
service-per-pod passed
serviceless-external-nodes passed
smart-update passed
split-horizon passed
storage passed
tls-issue-cert-manager passed
upgrade passed
upgrade-consistency passed
upgrade-consistency-sharded-tls passed
upgrade-sharded passed
users passed
version-service passed
We run 48 out of 48

commit: 660460a
image: perconalab/percona-server-mongodb-operator:PR-1504-660460a7

@hors hors self-requested a review April 17, 2024 15:49
@hors hors merged commit c325d88 into main Apr 17, 2024
16 checks passed
@hors hors deleted the K8SPSMDB-780 branch April 17, 2024 15:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size/XXL 1000+ lines
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants