Skip to content

fix: Regenerate non-UTF-8 adopted generated secrets - #300

Open
wnevis-cmyk wants to merge 4 commits into
mainfrom
wnevis/migration-weave-auth-utf8
Open

fix: Regenerate non-UTF-8 adopted generated secrets#300
wnevis-cmyk wants to merge 4 commits into
mainfrom
wnevis/migration-weave-auth-utf8

Conversation

@wnevis-cmyk

@wnevis-cmyk wnevis-cmyk commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

Part of the Operator v2 migration bugfix work from Ajay Pandey's V1 -> V2 migration test.

Problem: the weave-worker-auth generated secret uses useExactName: true, so on migration generateSecrets adopts the pre-existing v1 secret. If v1 stored a raw binary (non-UTF-8) token under key, it is injected as the WANDB_INTERNAL_SERVICE_TOKEN string env var and kubelet fails container creation with grpc: error while marshaling: string field contains invalid UTF-8, leaving weave-trace pods in CreateContainerError.

Fix: generateSecrets now detects an adopted generated secret whose key value is not valid UTF-8 and regenerates it with a UTF-8-safe token (mirrors the accepted manual fix). Valid existing values are left untouched. Applied generically to all generated secrets since the encoding hazard is generic.

  • internal/controller/reconciler/reconcile_v2.go: UTF-8 guard in the existing-secret branch of generateSecrets.
  • Tests: regenerates a seeded non-UTF-8 secret; leaves a valid value untouched; fresh secrets are UTF-8.

Verification: golangci-lint (0 issues) + targeted go test pass.

Summary by CodeRabbit

  • Bug Fixes
    • Improved generated secret handling by rejecting existing values with invalid UTF-8.
    • Preserved invalid values without overwriting them and reported the issue through readiness status and warning events.
    • Preserved valid existing secret values during reconciliation.
    • Ensured newly created secret tokens use valid UTF-8 and the configured length.

Co-authored-by: Cursor <cursoragent@cursor.com>
@coderabbitai

coderabbitai Bot commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Changes

Generated Secret validation

Layer / File(s) Summary
Validate generated Secret values
internal/controller/common/condition.go, internal/controller/reconciler/reconcile_v2.go
Generated Secret reconciliation preserves valid values, creates missing values, and rejects invalid UTF-8 values. Rejection records a warning event and updates readiness.
Validate tests and recorder wiring
internal/controller/reconciler/generate_secrets_test.go, internal/controller/*controller*_test.go
Tests cover invalid, valid, and missing Secret values. Existing reconciliation tests pass fake event recorders to the updated API.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Suggested reviewers: danielpanzella, casey-coreweave

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main change: regenerating adopted generated secrets that contain invalid UTF-8.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch wnevis/migration-weave-auth-utf8

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@wnevis-cmyk
wnevis-cmyk requested a review from a team as a code owner July 23, 2026 23:36

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@internal/controller/reconciler/generate_secrets_test.go`:
- Around line 19-34: Rewrite the tests in generate_secrets_test.go from
testing/Testify and fake.NewClientBuilder to the repository’s Ginkgo/Gomega spec
style, attaching them to the appropriate suite_test.go and using envtest. Remove
the direct testing, Testify, and fake-client dependencies where no longer
needed, then run make lint and make test and report their results before
merging.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 55169eab-b5da-4cc8-84ac-8281f987cb79

📥 Commits

Reviewing files that changed from the base of the PR and between db6e8d9 and eae0610.

📒 Files selected for processing (2)
  • internal/controller/reconciler/generate_secrets_test.go
  • internal/controller/reconciler/reconcile_v2.go

Comment on lines +19 to +34
import (
"context"
"testing"
"unicode/utf8"

"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
ctrlClient "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"

apiv2 "github.com/wandb/operator/api/v2"
serverManifest "github.com/wandb/operator/pkg/wandb/manifest"
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

📐 Maintainability & Code Quality | 🟠 Major | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== locate target file and suite files =="
git ls-files | rg '(^|/)internal/controller/reconciler/generate_secrets_test\.go$|(^|/)suite_test\.go$|(^|/)go\.mod$'

echo
echo "== target file imports and test declarations =="
sed -n '1,220p' internal/controller/reconciler/generate_secrets_test.go

echo
echo "== test framework indicators =="
rg -n 'Describe|Context|It|When|Specify|testing\.|func Test|\bdRequire\(|\bgomega\b|Ginkgo|TestingT|ginkgo|suite_test\.go|envtest|testing\.B|func TestMain' -S .

echo
echo "== lint/test make targets and go.mod deps =="
if [ -f Makefile ]; then sed -n '1,220p' Makefile; fi
echo
[ -f go.mod ] && awk '/module |require \(/,/^)/' go.mod | sed -n '1,220p'

Repository: wandb/operator

Length of output: 50371


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== target file imports/declarations =="
sed -n '1,180p' internal/controller/reconciler/generate_secrets_test.go

echo
echo "== focused test framework indicators excluding vendored/crds =="
rg -n --glob '!pkg/vendored/**' --glob '!**/crds/**' 'func Test[A-Za-z0-9_]+|require\..+|\.Expect\(|Describe\(|Context\(|It\(|GinkgoT|Gomega|envtest|suite_test\.go' --glob '*_test.go' . | sed -n '1,240p'

echo
echo "== reconciler suite files =="
git ls-files 'internal/controller/**suite_test.go' 'internal/controller/**/*_test.go'

echo
echo "== Makefile relevant targets =="
[ -f Makefile ] && (sed -n '/^lint:/,/^test:/p' Makefile; sed -n '/^test:/,/^[a-zA-Z_][A-Za-z0-9_-]*:/p' Makefile)

echo
echo "== go.mod relevant deps =="
[ -f go.mod ] && awk '/^module |^require \(/,/^)/' go.mod | rg 'ginkgo|gomega|envtest|testify' || true

Repository: wandb/operator

Length of output: 42962


Use the required test framework and run validation before merge.

internal/controller/reconciler/generate_secrets_test.go uses testing, Testify, and fake.NewClientBuilder. The repository policy requires Ginkgo/Gomega specs attached to suite_test.go files with envtest.

Also run make lint and make test and include their results before merge.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@internal/controller/reconciler/generate_secrets_test.go` around lines 19 -
34, Rewrite the tests in generate_secrets_test.go from testing/Testify and
fake.NewClientBuilder to the repository’s Ginkgo/Gomega spec style, attaching
them to the appropriate suite_test.go and using envtest. Remove the direct
testing, Testify, and fake-client dependencies where no longer needed, then run
make lint and make test and report their results before merging.

Source: Coding guidelines

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@internal/controller/reconciler/reconcile_v2.go`:
- Around line 1465-1481: Update internal/controller/reconciler/reconcile_v2.go
lines 1465-1481 so invalid adopted Secret values are replaced with a newly
generated UTF-8-safe value, persisted to the Secret, and reconciliation
continues successfully instead of emitting an error and returning. Update
internal/controller/reconciler/generate_secrets_test.go lines 78-114 to assert
successful reconciliation and verify the replaced value is a valid generated
token.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 8760ac0a-3006-4b9e-8bc6-7d22561aeee0

📥 Commits

Reviewing files that changed from the base of the PR and between eae0610 and 50aad4d.

📒 Files selected for processing (5)
  • internal/controller/common/condition.go
  • internal/controller/reconciler/generate_secrets_test.go
  • internal/controller/reconciler/reconcile_v2.go
  • internal/controller/weightsandbiases_controller_networking_test.go
  • internal/controller/weightsandbiases_controller_test.go

Comment on lines +1465 to +1481
// Secret exists; don't overwrite a valid existing value.
existing, hasKey := sec.Data[keyName]
// Non-UTF-8 secretKeyRef env vars break container creation.
if hasKey && !utf8.Valid(existing) {
msg := fmt.Sprintf(
"generated secret %q key %q contains non-UTF-8 bytes; values consumed as container environment variables must be valid UTF-8 — replace it with a UTF-8-safe value",
secretName, keyName,
)
recorder.Event(wandb, corev1.EventTypeWarning, common.InvalidSecretEncodingReason, msg)
if err := updateReadyStatus(ctx, client, wandb, statusBefore, false, common.InvalidSecretEncodingReason, msg); err != nil {
return ctrl.Result{}, err
}
// Generate a value only if missing
return ctrl.Result{}, errors.New(msg)
}
if !hasKey && sec.StringData == nil {
// Secret exists but has no usable key; populate one.
sec.StringData = map[string]string{}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Align the implementation and test with regeneration.

The implementation and test encode rejection. The PR objective requires regeneration of invalid adopted values.

  • internal/controller/reconciler/reconcile_v2.go#L1465-L1481: Generate and persist a UTF-8-safe replacement instead of returning before the Secret is updated.
  • internal/controller/reconciler/generate_secrets_test.go#L78-L114: Assert successful reconciliation and replacement with a valid token.
📍 Affects 2 files
  • internal/controller/reconciler/reconcile_v2.go#L1465-L1481 (this comment)
  • internal/controller/reconciler/generate_secrets_test.go#L78-L114
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@internal/controller/reconciler/reconcile_v2.go` around lines 1465 - 1481,
Update internal/controller/reconciler/reconcile_v2.go lines 1465-1481 so invalid
adopted Secret values are replaced with a newly generated UTF-8-safe value,
persisted to the Secret, and reconciliation continues successfully instead of
emitting an error and returning. Update
internal/controller/reconciler/generate_secrets_test.go lines 78-114 to assert
successful reconciliation and verify the replaced value is a valid generated
token.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant