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

VC-36545: Collect the secret's labels #581

Merged
merged 2 commits into from
Oct 3, 2024
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
1 change: 1 addition & 0 deletions pkg/datagatherer/k8s/fieldfilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var SecretSelectedFields = []string{
"kind",
"apiVersion",
"metadata.annotations",
"metadata.labels",
"metadata.name",
"metadata.namespace",
"metadata.ownerReferences",
Expand Down
272 changes: 117 additions & 155 deletions pkg/datagatherer/k8s/fieldfilter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import (
"encoding/json"
"testing"

"github.com/jetstack/preflight/pkg/testutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

func TestSelect(t *testing.T) {
// secret objects
secretResource := &unstructured.Unstructured{
Object: map[string]interface{}{
t.Run("secret", run_TestSelect(
map[string]interface{}{
"apiVersion": "v1",
"kind": "Secret",
"metadata": map[string]interface{}{
Expand All @@ -19,46 +21,52 @@ func TestSelect(t *testing.T) {
"annotations": map[string]string{
"kubectl.kubernetes.io/last-applied-configuration": "secret",
},
"labels": map[string]string{
"foo": "bar",
},
},
"type": "kubernetes.io/tls",
"data": map[string]interface{}{
"tls.crt": "cert data",
"tls.key": "secret",
},
},
}

secretFieldsToSelect := []string{
"apiVersion",
"kind",
"metadata.name",
"metadata.namespace",
"type",
"/data/tls.crt",
}
SecretSelectedFields,
map[string]interface{}{
"apiVersion": "v1",
"kind": "Secret",
"metadata": map[string]interface{}{
"name": "example",
"namespace": "example",
"annotations": map[string]interface{}{
// The "last-applied-configuration" isn't ignored in
// "Select". "Redact" removes it.
"kubectl.kubernetes.io/last-applied-configuration": "secret",
},
"labels": map[string]interface{}{
"foo": "bar",
},
},
"type": "kubernetes.io/tls",
"data": map[string]interface{}{
// The "tls.key" is ignored.
"tls.crt": "cert data",
},
},
))

secretExpectedJSON := `{
"apiVersion": "v1",
"data": {
"tls.crt": "cert data"
},
"kind": "Secret",
"metadata": {
"name": "example",
"namespace": "example"
},
"type": "kubernetes.io/tls"
}`
// route objects
routeResource := &unstructured.Unstructured{
Object: map[string]interface{}{
t.Run("route", run_TestSelect(
map[string]interface{}{
"apiVersion": "v1",
"kind": "Route",
"metadata": map[string]interface{}{
"name": "example",
"annotations": map[string]string{
"kubectl.kubernetes.io/last-applied-configuration": "secret",
},
"labels": map[string]string{
"foo": "bar",
},
},
"spec": map[string]interface{}{
"host": "www.example.com",
Expand All @@ -74,68 +82,44 @@ func TestSelect(t *testing.T) {
"destinationCACertificate": "destinationCaCert data",
},
},
}, RouteSelectedFields,
map[string]interface{}{
"apiVersion": "v1",
"kind": "Route",
"metadata": map[string]interface{}{
"name": "example",
"annotations": map[string]interface{}{
// The "last-applied-configuration" isn't ignored in
// "Select". "Redact" removes it.
"kubectl.kubernetes.io/last-applied-configuration": "secret",
},
tfadeyi marked this conversation as resolved.
Show resolved Hide resolved
},
"spec": map[string]interface{}{
"host": "www.example.com",
"to": map[string]interface{}{
"kind": "Service",
"name": "frontend",
},
"tls": map[string]interface{}{
"termination": "reencrypt",
// The "key" field is ignored.
"certificate": "cert data",
"caCertificate": "caCert data",
"destinationCACertificate": "destinationCaCert data",
},
},
},
}

routeFieldsToSelect := []string{
"apiVersion",
"kind",
"metadata.name",
"spec.host",
"spec.to.kind",
"spec.to.name",
"spec.tls.termination",
"spec.tls.certificate",
"spec.tls.caCertificate",
"spec.tls.destinationCACertificate",
}

routeExpectedJSON := `{
"apiVersion": "v1",
"kind": "Route",
"metadata": {
"name": "example"
},
"spec": {
"host": "www.example.com",
"tls": {
"caCertificate": "caCert data",
"certificate": "cert data",
"destinationCACertificate": "destinationCaCert data",
"termination": "reencrypt"
},
"to": {
"kind": "Service",
"name": "frontend"
}
}
}`

tests := map[string]struct {
resource *unstructured.Unstructured
fieldsToSelect []string
expectedJSON string
}{
"secret": {secretResource, secretFieldsToSelect, secretExpectedJSON},
"route": {routeResource, routeFieldsToSelect, routeExpectedJSON},
}

for name, test := range tests {
err := Select(test.fieldsToSelect, test.resource)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
))
}

bytes, err := json.MarshalIndent(test.resource, "", " ")
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
func run_TestSelect(given map[string]interface{}, givenSelect []string, expect map[string]interface{}) func(*testing.T) {
return func(t *testing.T) {
t.Helper()
givenPtr := unstructured.Unstructured{Object: given}
err := Select(givenSelect, &givenPtr)
require.NoError(t, err)

t.Run(name, func(t *testing.T) {
if string(bytes) != test.expectedJSON {
t.Fatalf("unexpected JSON: \ngot \n%s\nwant\n%s", string(bytes), test.expectedJSON)
}
})
assert.Equal(t, expect, givenPtr.Object)
}
}

Expand All @@ -153,21 +137,15 @@ func TestSelectMissingSelectedField(t *testing.T) {
}

err := Select(fieldsToSelect, resource)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}

require.NoError(t, err)
bytes, err := json.MarshalIndent(resource, "", " ")
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
require.NoError(t, err)

expectedJSON := `{
"kind": "Secret"
}`
if string(bytes) != expectedJSON {
t.Fatalf("unexpected JSON: \ngot \n%s\nwant\n%s", string(bytes), expectedJSON)
}
expectedJSON := testutil.Undent(`
{
"kind": "Secret"
}`)
assert.Equal(t, expectedJSON, string(bytes))
}

func TestRedactSecret(t *testing.T) {
Expand Down Expand Up @@ -198,30 +176,25 @@ func TestRedactSecret(t *testing.T) {
}

err := Redact(fieldsToRedact, resource)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
require.NoError(t, err)

bytes, err := json.MarshalIndent(resource, "", " ")
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
expectedJSON := `{
"apiVersion": "v1",
"data": {
"tls.crt": "cert data"
},
"kind": "Secret",
"metadata": {
"annotations": {},
"name": "example",
"namespace": "example"
},
"type": "kubernetes.io/tls"
}`
if string(bytes) != expectedJSON {
t.Fatalf("unexpected JSON: \ngot \n%s\nwant\n%s", string(bytes), expectedJSON)
}
require.NoError(t, err)
expectedJSON := testutil.Undent(`
{
"apiVersion": "v1",
"data": {
"tls.crt": "cert data"
},
"kind": "Secret",
"metadata": {
"annotations": {},
"name": "example",
"namespace": "example"
},
"type": "kubernetes.io/tls"
}`)
assert.Equal(t, expectedJSON, string(bytes))
}

func TestRedactPod(t *testing.T) {
Expand All @@ -245,28 +218,23 @@ func TestRedactPod(t *testing.T) {
}

err := Redact(fieldsToRedact, resource)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
require.NoError(t, err)

bytes, err := json.MarshalIndent(resource, "", " ")
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
expectedJSON := `{
"apiVersion": "v1",
"kind": "Pod",
"metadata": {
"name": "example",
"namespace": "example"
},
"spec": {
"serviceAccountName": "example"
}
}`
if string(bytes) != expectedJSON {
t.Fatalf("unexpected JSON: \ngot \n%s\nwant\n%s", string(bytes), expectedJSON)
}
require.NoError(t, err)
expectedJSON := testutil.Undent(`
{
"apiVersion": "v1",
"kind": "Pod",
"metadata": {
"name": "example",
"namespace": "example"
},
"spec": {
"serviceAccountName": "example"
}
}`)
assert.Equal(t, expectedJSON, string(bytes))
}

func TestRedactMissingField(t *testing.T) {
Expand All @@ -282,19 +250,13 @@ func TestRedactMissingField(t *testing.T) {
}

err := Redact(fieldsToRedact, resource)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}

require.NoError(t, err)
bytes, err := json.MarshalIndent(resource, "", " ")
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
require.NoError(t, err)

expectedJSON := `{
"kind": "Secret"
}`
if string(bytes) != expectedJSON {
t.Fatalf("unexpected JSON: \ngot \n%s\nwant\n%s", string(bytes), expectedJSON)
}
expectedJSON := testutil.Undent(`
{
"kind": "Secret"
}`)
assert.Equal(t, expectedJSON, string(bytes))
}
2 changes: 1 addition & 1 deletion pkg/testutil/undent.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func Undent(s string) string {

// Case 1: we want the user to be able to omit some tabs or spaces in
// the last line for readability purposes.
case line == len(lineOffsets)-1 && s[last] != '\n' && isIndent(s[first:last]):
case line == len(lineOffsets)-1 && s[last] != '\n' && isIndent(s[first:last+1]):
lineStr = ""

// Case 2: we want the user to be able to omit the indentations for
Expand Down
1 change: 1 addition & 0 deletions pkg/testutil/undent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func Test_Undent(t *testing.T) {

bar
`, "foo\n\nbar\n"))
t.Run("bug fix: last char is not omitted", runTest_Undent("\t\t{\n\t\t \"kind\": \"Secret\"\n\t\t}", "{\n \"kind\": \"Secret\"\n}"))
}

func runTest_Undent(given, expected string) func(t *testing.T) {
Expand Down