|
| 1 | +package datadog |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "strconv" |
| 9 | + |
| 10 | + "github.com/DataDog/helm-charts/test/common" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | + appsv1 "k8s.io/api/apps/v1" |
| 13 | + corev1 "k8s.io/api/core/v1" |
| 14 | +) |
| 15 | + |
| 16 | +func TestFIPSModeConditions(t *testing.T) { |
| 17 | + tests := []struct { |
| 18 | + name string |
| 19 | + enableFIPSProxy bool |
| 20 | + enableFIPSAgent bool |
| 21 | + expectFIPSProxy bool |
| 22 | + expectFIPSAgent bool |
| 23 | + enableJMX bool |
| 24 | + }{ |
| 25 | + { |
| 26 | + name: "neither fips proxy nor fips agent", |
| 27 | + enableFIPSProxy: false, |
| 28 | + enableFIPSAgent: false, |
| 29 | + expectFIPSProxy: false, |
| 30 | + expectFIPSAgent: false, |
| 31 | + }, |
| 32 | + { |
| 33 | + name: "fips proxy only", |
| 34 | + enableFIPSProxy: true, |
| 35 | + enableFIPSAgent: false, |
| 36 | + expectFIPSProxy: true, |
| 37 | + expectFIPSAgent: false, |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "fips image only", |
| 41 | + enableFIPSProxy: false, |
| 42 | + enableFIPSAgent: true, |
| 43 | + expectFIPSProxy: false, |
| 44 | + expectFIPSAgent: true, |
| 45 | + }, |
| 46 | + { |
| 47 | + name: "fips proxy and fips image", |
| 48 | + enableFIPSProxy: true, |
| 49 | + enableFIPSAgent: true, |
| 50 | + expectFIPSProxy: false, // fips proxy should be disabled when fips agent is enabled |
| 51 | + expectFIPSAgent: true, |
| 52 | + }, |
| 53 | + { |
| 54 | + name: "fips image with JMX enabled", |
| 55 | + enableFIPSProxy: false, |
| 56 | + enableFIPSAgent: true, |
| 57 | + expectFIPSProxy: false, |
| 58 | + expectFIPSAgent: true, |
| 59 | + enableJMX: true, |
| 60 | + }, |
| 61 | + } |
| 62 | + |
| 63 | + for _, tt := range tests { |
| 64 | + t.Run(tt.name, func(t *testing.T) { |
| 65 | + values := map[string]string{ |
| 66 | + "useFIPSAgent": strconv.FormatBool(tt.enableFIPSAgent), |
| 67 | + "fips.enabled": strconv.FormatBool(tt.enableFIPSProxy), |
| 68 | + "datadog.apiKeyExistingSecret": "datadog-secret", |
| 69 | + "datadog.appKeyExistingSecret": "datadog-secret", |
| 70 | + } |
| 71 | + |
| 72 | + if tt.enableJMX { |
| 73 | + values["agents.image.tagSuffix"] = "jmx" |
| 74 | + } |
| 75 | + |
| 76 | + manifest, err := common.RenderChart(t, common.HelmCommand{ |
| 77 | + ReleaseName: "datadog", |
| 78 | + ChartPath: "../../charts/datadog", |
| 79 | + ShowOnly: []string{"templates/daemonset.yaml"}, |
| 80 | + Values: []string{"../../charts/datadog/values.yaml"}, |
| 81 | + Overrides: values, |
| 82 | + }) |
| 83 | + require.NoError(t, err, "couldn't render template") |
| 84 | + |
| 85 | + // Parse the manifest to find the should-enable-fips-proxy value and check image tags |
| 86 | + var daemonSet appsv1.DaemonSet |
| 87 | + common.Unmarshal(t, manifest, &daemonSet) |
| 88 | + |
| 89 | + // Checking that daemonSet contains or not fips-proxy container based on the fips proxy configuration |
| 90 | + checkFIPSProxy(t, daemonSet.Spec.Template.Spec.Containers, tt.expectFIPSProxy) |
| 91 | + |
| 92 | + // Checking that all containers have the fips image suffix if fips agent is enabled |
| 93 | + checkFIPSImage(t, daemonSet.Spec.Template.Spec.Containers, tt.expectFIPSAgent) |
| 94 | + }) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +func checkFIPSProxy(t *testing.T, containers []corev1.Container, expectFIPSProxy bool) { |
| 99 | + hasFIPSProxy := false |
| 100 | + for _, container := range containers { |
| 101 | + if strings.Contains(container.Image, "fips-proxy") { |
| 102 | + hasFIPSProxy = true |
| 103 | + break |
| 104 | + } |
| 105 | + } |
| 106 | + if expectFIPSProxy { |
| 107 | + require.True(t, hasFIPSProxy, "fips proxy container should be present") |
| 108 | + } else { |
| 109 | + require.False(t, hasFIPSProxy, "fips proxy container should not be present") |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +func checkFIPSImage(t *testing.T, containers []corev1.Container, expectFIPSImage bool) { |
| 114 | + if expectFIPSImage { |
| 115 | + for _, container := range containers { |
| 116 | + require.Contains(t, container.Image, "-fips", fmt.Sprintf("fips container %s should have the fips image suffix: %s", container.Name, container.Image)) |
| 117 | + } |
| 118 | + } else { |
| 119 | + for _, container := range containers { |
| 120 | + require.NotContains(t, container.Image, "-fips", fmt.Sprintf("fips container %s should not have the fips image suffix: %s", container.Name, container.Image)) |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments