Skip to content

Commit

Permalink
Allow a user to disable Reaper auth by specifically setting the UiUse…
Browse files Browse the repository at this point in the history
…rSecretRef.Name to "".
  • Loading branch information
Miles-Garnsey committed Jan 10, 2024
1 parent b02da26 commit c33ee08
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 14 deletions.
2 changes: 1 addition & 1 deletion apis/reaper/v1alpha1/reaper_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ type ReaperTemplate struct {

// Defines the secret which contains the username and password for the Reaper UI and REST API authentication.
// +optional
UiUserSecretRef corev1.LocalObjectReference `json:"uiUserSecretRef,omitempty"`
UiUserSecretRef *corev1.LocalObjectReference `json:"uiUserSecretRef,omitempty"`

// SecretsProvider defines whether the secrets used for credentials and certs will be backed
// by an external secret backend. This moves the responsibility of generating and storing
Expand Down
6 changes: 5 additions & 1 deletion apis/reaper/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 9 additions & 5 deletions controllers/k8ssandra/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,22 +66,26 @@ func (r *K8ssandraClusterReconciler) reconcileReaperSecrets(ctx context.Context,
var uiUserSecretRef corev1.LocalObjectReference
if kc.Spec.Reaper != nil {
cassandraUserSecretRef = kc.Spec.Reaper.CassandraUserSecretRef
uiUserSecretRef = kc.Spec.Reaper.UiUserSecretRef
if kc.Spec.Reaper.UiUserSecretRef != nil {
uiUserSecretRef = *kc.Spec.Reaper.UiUserSecretRef
}

Check warning on line 71 in controllers/k8ssandra/secrets.go

View check run for this annotation

Codecov / codecov/patch

controllers/k8ssandra/secrets.go#L70-L71

Added lines #L70 - L71 were not covered by tests
}
if cassandraUserSecretRef.Name == "" {
cassandraUserSecretRef.Name = reaper.DefaultUserSecretName(kc.SanitizedName())
}
if uiUserSecretRef.Name == "" {
if kc.Spec.Reaper.UiUserSecretRef == nil {
uiUserSecretRef.Name = reaper.DefaultUiSecretName(kc.SanitizedName())
}
kcKey := utils.GetKey(kc)
if err := secret.ReconcileSecret(ctx, r.Client, cassandraUserSecretRef.Name, kcKey); err != nil {
logger.Error(err, "Failed to reconcile Reaper CQL user secret", "ReaperCassandraUserSecretRef", cassandraUserSecretRef)
return result.Error(err)
}
if err := secret.ReconcileSecret(ctx, r.Client, uiUserSecretRef.Name, kcKey); err != nil {
logger.Error(err, "Failed to reconcile Reaper UI secret", "ReaperUiUserSecretRef", uiUserSecretRef)
return result.Error(err)
if kc.Spec.Reaper.UiUserSecretRef != nil {
if err := secret.ReconcileSecret(ctx, r.Client, uiUserSecretRef.Name, kcKey); err != nil {
logger.Error(err, "Failed to reconcile Reaper UI secret", "ReaperUiUserSecretRef", uiUserSecretRef)
return result.Error(err)
}

Check warning on line 88 in controllers/k8ssandra/secrets.go

View check run for this annotation

Codecov / codecov/patch

controllers/k8ssandra/secrets.go#L85-L88

Added lines #L85 - L88 were not covered by tests
}
logger.Info("Reaper user secrets successfully reconciled")

Expand Down
7 changes: 4 additions & 3 deletions controllers/reaper/reaper_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,8 @@ func (r *ReaperReconciler) configureReaper(ctx context.Context, actualReaper *re
}

func (r *ReaperReconciler) getReaperUICredentials(ctx context.Context, actualReaper *reaperapi.Reaper, logger logr.Logger) (string, string, error) {
if actualReaper.Spec.UiUserSecretRef.Name == "" {

if actualReaper.Spec.UiUserSecretRef == nil || actualReaper.Spec.UiUserSecretRef.Name == "" {
// The UI user secret doesn't exist, meaning auth is disabled
return "", "", nil
}
Expand Down Expand Up @@ -383,11 +384,11 @@ func (r *ReaperReconciler) collectAuthVarsForType(ctx context.Context, actualRea
secretRef = &actualReaper.Spec.CassandraUserSecretRef
envVars = []*corev1.EnvVar{}
case "ui":
secretRef = &actualReaper.Spec.UiUserSecretRef
secretRef = actualReaper.Spec.UiUserSecretRef
envVars = []*corev1.EnvVar{reaper.EnableAuthVar}
}

if len(secretRef.Name) > 0 && !actualReaper.Spec.UseExternalSecrets() {
if secretRef != nil && len(secretRef.Name) > 0 && !actualReaper.Spec.UseExternalSecrets() {
secretKey := types.NamespacedName{Namespace: actualReaper.Namespace, Name: secretRef.Name}
if secret, err := r.getSecret(ctx, secretKey); err != nil {
logger.Error(err, "Failed to get Cassandra authentication secret", authType, secretKey)
Expand Down
4 changes: 2 additions & 2 deletions controllers/reaper/reaper_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ func testCreateReaperWithAuthEnabled(t *testing.T, ctx context.Context, k8sClien
t.Log("create the Reaper object and modify it")
rpr := newReaper(testNamespace)
rpr.Spec.CassandraUserSecretRef.Name = "top-secret-cass"
rpr.Spec.UiUserSecretRef.Name = "top-secret-ui"
rpr.Spec.UiUserSecretRef = &corev1.LocalObjectReference{Name: "top-secret-ui"}
err = k8sClient.Create(ctx, rpr)
require.NoError(t, err)

Expand Down Expand Up @@ -477,7 +477,7 @@ func testCreateReaperWithAuthEnabledExternalSecret(t *testing.T, ctx context.Con
//lint:ignore SA1019 Verify deprecated method is ineffective
rpr.Spec.JmxUserSecretRef.Name = "top-secret-jmx" //nolint:staticcheck

rpr.Spec.UiUserSecretRef.Name = "top-secret-ui"
rpr.Spec.UiUserSecretRef = &corev1.LocalObjectReference{Name: "top-secret-ui"}
err = k8sClient.Create(ctx, rpr)
require.NoError(t, err)

Expand Down
4 changes: 2 additions & 2 deletions pkg/reaper/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ func NewReaper(
desiredReaper.Spec.CassandraUserSecretRef.Name = DefaultUserSecretName(kc.SanitizedName())
}
// Note: deliberately skip JmxUserSecretRef, which is deprecated.
if desiredReaper.Spec.UiUserSecretRef.Name == "" {
desiredReaper.Spec.UiUserSecretRef.Name = DefaultUiSecretName(kc.SanitizedName())
if desiredReaper.Spec.UiUserSecretRef == nil {
desiredReaper.Spec.UiUserSecretRef = &corev1.LocalObjectReference{Name: DefaultUiSecretName(kc.SanitizedName())}

Check warning on line 71 in pkg/reaper/resource.go

View check run for this annotation

Codecov / codecov/patch

pkg/reaper/resource.go#L70-L71

Added lines #L70 - L71 were not covered by tests
}

if desiredReaper.Spec.ResourceMeta == nil {
Expand Down

0 comments on commit c33ee08

Please sign in to comment.