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

Fix double-encoding of Kubernetes secrets #6541

Merged
merged 2 commits into from
Oct 20, 2023
Merged
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
7 changes: 3 additions & 4 deletions pkg/controller/reconciler/deployment_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package reconciler

import (
"context"
"encoding/base64"
"fmt"
"strings"
"time"
Expand Down Expand Up @@ -517,6 +516,8 @@ func (r *DeploymentReconciler) updateDeployment(ctx context.Context, deployment
return fmt.Errorf("failed to fetch secret %s: %w", secretName, err)
}

// envtest has some quirky behavior around StringData which makes it hard to test. So we're
// using Data directly.
secret.Data = map[string][]byte{}

for name, source := range annotations.Configuration.Connections {
Expand Down Expand Up @@ -553,10 +554,8 @@ func (r *DeploymentReconciler) updateDeployment(ctx context.Context, deployment
return fmt.Errorf("failed to read values resource %s: %w", id, err)
}

// envtest has some quirky behavior around StringData which makes it hard to test. So we're
// using Data directly.
for k, v := range values {
secret.Data[k] = []byte(base64.RawStdEncoding.EncodeToString([]byte(v)))
secret.Data[k] = []byte(v)
}
}

Expand Down
13 changes: 6 additions & 7 deletions pkg/controller/reconciler/deployment_reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package reconciler

import (
"encoding/base64"
"fmt"
"testing"
"time"
Expand Down Expand Up @@ -366,10 +365,10 @@ func Test_DeploymentReconciler_Connections(t *testing.T) {
require.NoError(t, err)

expectedSecretData := map[string][]byte{
"CONNECTION_A_A-SECRET": []byte(base64.RawStdEncoding.EncodeToString([]byte("a"))),
"CONNECTION_A_A-VALUE": []byte(base64.RawStdEncoding.EncodeToString([]byte("a"))),
"CONNECTION_B_B-SECRET": []byte(base64.RawStdEncoding.EncodeToString([]byte("b"))),
"CONNECTION_B_B-VALUE": []byte(base64.RawStdEncoding.EncodeToString([]byte("b"))),
"CONNECTION_A_A-SECRET": []byte("a"),
"CONNECTION_A_A-VALUE": []byte("a"),
"CONNECTION_B_B-SECRET": []byte("b"),
"CONNECTION_B_B-VALUE": []byte("b"),
}
require.Equal(t, expectedSecretData, secret.Data)

Expand Down Expand Up @@ -412,8 +411,8 @@ func Test_DeploymentReconciler_Connections(t *testing.T) {
require.NoError(t, err)

expectedSecretData = map[string][]byte{
"CONNECTION_B_B-SECRET": []byte(base64.RawStdEncoding.EncodeToString([]byte("b"))),
"CONNECTION_B_B-VALUE": []byte(base64.RawStdEncoding.EncodeToString([]byte("b"))),
"CONNECTION_B_B-SECRET": []byte("b"),
"CONNECTION_B_B-VALUE": []byte("b"),
}
require.Equal(t, expectedSecretData, secret.Data)

Expand Down
7 changes: 2 additions & 5 deletions pkg/controller/reconciler/recipe_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package reconciler

import (
"context"
"encoding/base64"
"fmt"
"strings"
"time"
Expand Down Expand Up @@ -503,8 +502,7 @@ func (r *RecipeReconciler) updateSecret(ctx context.Context, recipe *radappiov1a
}

for k, v := range values {
encoded := base64.RawStdEncoding.EncodeToString([]byte(v))
secret.Data[k] = []byte(encoded)
secret.Data[k] = []byte(v)
}

secrets, err := r.Radius.Resources(recipe.Status.Scope, recipe.Spec.Type).ListSecrets(ctx, recipe.Name)
Expand All @@ -514,8 +512,7 @@ func (r *RecipeReconciler) updateSecret(ctx context.Context, recipe *radappiov1a
return fmt.Errorf("failed to list secrets: %w", err)
} else {
for k, v := range secrets.Value {
encoded := base64.RawStdEncoding.EncodeToString([]byte(*v))
secret.Data[k] = []byte(encoded)
secret.Data[k] = []byte(*v)
}
}

Expand Down
5 changes: 2 additions & 3 deletions pkg/controller/reconciler/recipe_reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package reconciler

import (
"encoding/base64"
"errors"
"testing"
"time"
Expand Down Expand Up @@ -303,8 +302,8 @@ func Test_RecipeReconciler_WithSecret(t *testing.T) {
require.NoError(t, err)

expectedData := map[string][]byte{
"a-value": []byte(base64.RawStdEncoding.EncodeToString([]byte("a"))),
"b-secret": []byte(base64.RawStdEncoding.EncodeToString([]byte("b"))),
"a-value": []byte("a"),
"b-secret": []byte("b"),
}

require.Equal(t, expectedData, secret.Data)
Expand Down
Loading