|
| 1 | +// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"). You may |
| 4 | +// not use this file except in compliance with the License. A copy of the |
| 5 | +// License is located at |
| 6 | +// |
| 7 | +// http://aws.amazon.com/apache2.0/ |
| 8 | +// |
| 9 | +// or in the "license" file accompanying this file. This file is distributed |
| 10 | +// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 | +// express or implied. See the License for the specific language governing |
| 12 | +// permissions and limitations under the License. |
| 13 | + |
| 14 | +package ipamd |
| 15 | + |
| 16 | +import ( |
| 17 | + "encoding/json" |
| 18 | + "net" |
| 19 | + "net/http" |
| 20 | + "net/http/httptest" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/aws/amazon-vpc-cni-k8s/pkg/ipamd/datastore" |
| 24 | + "github.com/stretchr/testify/assert" |
| 25 | + "github.com/stretchr/testify/require" |
| 26 | +) |
| 27 | + |
| 28 | +func TestEniV1RequestHandler_ScrubsSensitiveFields(t *testing.T) { |
| 29 | + // Set up a datastore with a pod assigned |
| 30 | + ds := datastore.NewDataStore(log, datastore.NullCheckpoint{}, false, 0) |
| 31 | + ds.AddENI("eni-test-001", 0, true, false, false, 254, "subnet-abc") |
| 32 | + ipv4Addr := net.IPNet{IP: net.ParseIP("10.0.0.5"), Mask: net.IPv4Mask(255, 255, 255, 255)} |
| 33 | + ds.AddIPv4CidrToStore("eni-test-001", ipv4Addr, false) |
| 34 | + |
| 35 | + // Assign an IP to a pod |
| 36 | + key := datastore.IPAMKey{ |
| 37 | + NetworkName: "aws-cni", |
| 38 | + ContainerID: "abc123-secret-container-id", |
| 39 | + IfName: "eth0", |
| 40 | + } |
| 41 | + metadata := datastore.IPAMMetadata{ |
| 42 | + K8SPodNamespace: "default", |
| 43 | + K8SPodName: "my-pod", |
| 44 | + } |
| 45 | + _, _, _, _, err := ds.AssignPodIPAddress(key, metadata, true, false) |
| 46 | + require.NoError(t, err) |
| 47 | + |
| 48 | + // Build IPAMContext |
| 49 | + dsAccess := &datastore.DataStoreAccess{DataStores: []*datastore.DataStore{ds}} |
| 50 | + ipamCtx := &IPAMContext{ |
| 51 | + dataStoreAccess: dsAccess, |
| 52 | + } |
| 53 | + |
| 54 | + // Call the handler |
| 55 | + handler := eniV1RequestHandler(ipamCtx) |
| 56 | + req := httptest.NewRequest(http.MethodGet, "/v1/enis", nil) |
| 57 | + rec := httptest.NewRecorder() |
| 58 | + handler(rec, req) |
| 59 | + |
| 60 | + assert.Equal(t, http.StatusOK, rec.Code) |
| 61 | + |
| 62 | + // Parse response and verify sensitive fields are scrubbed |
| 63 | + body := rec.Body.String() |
| 64 | + assert.NotEmpty(t, body) |
| 65 | + |
| 66 | + // The response should NOT contain the container ID or interface name |
| 67 | + assert.NotContains(t, body, "abc123-secret-container-id", |
| 68 | + "ContainerID should be scrubbed from introspection response") |
| 69 | + assert.NotContains(t, body, `"ifName":"eth0"`, |
| 70 | + "IfName should be scrubbed from introspection response") |
| 71 | + |
| 72 | + // But should still contain useful non-sensitive info |
| 73 | + assert.Contains(t, body, "eni-test-001", "ENI ID should still be present") |
| 74 | + assert.Contains(t, body, "10.0.0.5", "IP address should still be present") |
| 75 | + |
| 76 | + // Verify the JSON structure is valid |
| 77 | + var result map[string]json.RawMessage |
| 78 | + err = json.Unmarshal(rec.Body.Bytes(), &result) |
| 79 | + assert.NoError(t, err, "Response should be valid JSON") |
| 80 | +} |
| 81 | + |
| 82 | +func TestEniV1RequestHandler_ScrubsIPv6Fields(t *testing.T) { |
| 83 | + // Set up a datastore with an IPv6 prefix |
| 84 | + ds := datastore.NewDataStore(log, datastore.NullCheckpoint{}, true, 0) |
| 85 | + ds.AddENI("eni-v6-001", 0, true, false, false, 254, "subnet-v6") |
| 86 | + _, ipv6Net, _ := net.ParseCIDR("2001:db8::/64") |
| 87 | + ds.AddIPv6CidrToStore("eni-v6-001", *ipv6Net, true) |
| 88 | + |
| 89 | + // Assign an IPv6 address to a pod |
| 90 | + key := datastore.IPAMKey{ |
| 91 | + NetworkName: "aws-cni", |
| 92 | + ContainerID: "v6-container-secret-id", |
| 93 | + IfName: "eth0", |
| 94 | + } |
| 95 | + metadata := datastore.IPAMMetadata{ |
| 96 | + K8SPodNamespace: "default", |
| 97 | + K8SPodName: "v6-pod", |
| 98 | + } |
| 99 | + _, _, _, _, err := ds.AssignPodIPAddress(key, metadata, false, true) |
| 100 | + require.NoError(t, err) |
| 101 | + |
| 102 | + dsAccess := &datastore.DataStoreAccess{DataStores: []*datastore.DataStore{ds}} |
| 103 | + ipamCtx := &IPAMContext{ |
| 104 | + dataStoreAccess: dsAccess, |
| 105 | + } |
| 106 | + |
| 107 | + handler := eniV1RequestHandler(ipamCtx) |
| 108 | + req := httptest.NewRequest(http.MethodGet, "/v1/enis", nil) |
| 109 | + rec := httptest.NewRecorder() |
| 110 | + handler(rec, req) |
| 111 | + |
| 112 | + assert.Equal(t, http.StatusOK, rec.Code) |
| 113 | + body := rec.Body.String() |
| 114 | + assert.NotContains(t, body, "v6-container-secret-id", |
| 115 | + "IPv6 ContainerID should be scrubbed from introspection response") |
| 116 | +} |
0 commit comments