|
| 1 | +package integration |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + ecv1beta1 "github.com/replicatedhq/embedded-cluster/kinds/apis/v1beta1" |
| 10 | + "github.com/replicatedhq/embedded-cluster/pkg/addons/seaweedfs" |
| 11 | + "github.com/replicatedhq/embedded-cluster/pkg/helm" |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | + corev1 "k8s.io/api/core/v1" |
| 15 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 16 | + "sigs.k8s.io/yaml" |
| 17 | +) |
| 18 | + |
| 19 | +func TestImageSubstitution(t *testing.T) { |
| 20 | + addon := &seaweedfs.SeaweedFS{ |
| 21 | + DryRun: true, |
| 22 | + ServiceCIDR: "10.96.0.0/16", |
| 23 | + } |
| 24 | + |
| 25 | + hcli, err := helm.NewClient(helm.HelmOptions{}) |
| 26 | + require.NoError(t, err, "NewClient should not return an error") |
| 27 | + |
| 28 | + err = addon.Install(context.Background(), t.Logf, nil, nil, hcli, ecv1beta1.Domains{}, nil) |
| 29 | + require.NoError(t, err, "seaweedfs.Install should not return an error") |
| 30 | + |
| 31 | + manifests := addon.DryRunManifests() |
| 32 | + require.NotEmpty(t, manifests, "DryRunManifests should not be empty") |
| 33 | + |
| 34 | + // Build set of allowed images from metadata |
| 35 | + allowedImages := make(map[string]bool) |
| 36 | + for _, img := range seaweedfs.Metadata.Images { |
| 37 | + allowedImages[img.String()] = true |
| 38 | + } |
| 39 | + require.NotEmpty(t, allowedImages, "Metadata should contain at least one image") |
| 40 | + |
| 41 | + // Track all images found in manifests |
| 42 | + foundImages := make(map[string][]string) // map[image][]locations |
| 43 | + |
| 44 | + // Parse all manifests and extract images from any workload |
| 45 | + for _, manifest := range manifests { |
| 46 | + // Skip empty manifests |
| 47 | + if len(manifest) == 0 { |
| 48 | + continue |
| 49 | + } |
| 50 | + |
| 51 | + // Parse as unstructured to get Kind and Name |
| 52 | + var obj unstructured.Unstructured |
| 53 | + if err := yaml.Unmarshal(manifest, &obj); err != nil { |
| 54 | + // Skip invalid manifests |
| 55 | + continue |
| 56 | + } |
| 57 | + |
| 58 | + kind := obj.GetKind() |
| 59 | + name := obj.GetName() |
| 60 | + |
| 61 | + // Skip non-workload resources |
| 62 | + if !isWorkloadKind(kind) { |
| 63 | + continue |
| 64 | + } |
| 65 | + |
| 66 | + // Extract pod template spec |
| 67 | + podSpec, found, err := unstructured.NestedMap(obj.Object, "spec", "template", "spec") |
| 68 | + if err != nil || !found { |
| 69 | + continue |
| 70 | + } |
| 71 | + |
| 72 | + // Convert to PodSpec for easier access |
| 73 | + podSpecBytes, err := yaml.Marshal(podSpec) |
| 74 | + if err != nil { |
| 75 | + continue |
| 76 | + } |
| 77 | + var ps corev1.PodSpec |
| 78 | + if err := yaml.Unmarshal(podSpecBytes, &ps); err != nil { |
| 79 | + continue |
| 80 | + } |
| 81 | + |
| 82 | + // Check all containers |
| 83 | + location := fmt.Sprintf("%s/%s", kind, name) |
| 84 | + for i, container := range ps.Containers { |
| 85 | + if container.Image != "" { |
| 86 | + containerLocation := fmt.Sprintf("%s.spec.containers[%d](%s)", location, i, container.Name) |
| 87 | + foundImages[container.Image] = append(foundImages[container.Image], containerLocation) |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + // Check all init containers |
| 92 | + for i, container := range ps.InitContainers { |
| 93 | + if container.Image != "" { |
| 94 | + containerLocation := fmt.Sprintf("%s.spec.initContainers[%d](%s)", location, i, container.Name) |
| 95 | + foundImages[container.Image] = append(foundImages[container.Image], containerLocation) |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + require.NotEmpty(t, foundImages, "Should find at least one image in manifests") |
| 101 | + |
| 102 | + // Verify all found images are in the allowed list |
| 103 | + var unauthorizedImages []string |
| 104 | + for image, locations := range foundImages { |
| 105 | + if !allowedImages[image] { |
| 106 | + for _, loc := range locations { |
| 107 | + unauthorizedImages = append(unauthorizedImages, fmt.Sprintf("%s uses unauthorized image: %s", loc, image)) |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + // Additional checks for all images |
| 112 | + assert.NotContains(t, image, ":latest", "Image should not use :latest tag: %s", image) |
| 113 | + assert.Contains(t, image, "proxy.replicated.com/library", "Image should use proxy library registry: %s", image) |
| 114 | + } |
| 115 | + |
| 116 | + // Fail if any unauthorized images were found |
| 117 | + if len(unauthorizedImages) > 0 { |
| 118 | + t.Errorf("Found %d unauthorized images:\n%s", len(unauthorizedImages), strings.Join(unauthorizedImages, "\n")) |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +// isWorkloadKind returns true if the kind can have a pod spec |
| 123 | +func isWorkloadKind(kind string) bool { |
| 124 | + switch kind { |
| 125 | + case "Deployment", "StatefulSet", "DaemonSet", "Job", "CronJob", "ReplicaSet": |
| 126 | + return true |
| 127 | + default: |
| 128 | + return false |
| 129 | + } |
| 130 | +} |
0 commit comments