2022-10-21 06:01:28 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
|
|
|
package helm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2022-10-31 14:25:02 -04:00
|
|
|
"fmt"
|
|
|
|
"io/fs"
|
|
|
|
"os"
|
|
|
|
"path"
|
2022-11-25 08:56:30 -05:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2022-10-21 06:01:28 -04:00
|
|
|
"testing"
|
|
|
|
|
2022-11-24 04:57:58 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation/measurements"
|
2022-11-21 04:35:40 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/cloud/cloudprovider"
|
2022-11-18 04:05:02 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/config"
|
2022-10-21 06:01:28 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/deploy/helm"
|
2022-10-31 14:25:02 -04:00
|
|
|
"github.com/pkg/errors"
|
2022-10-21 06:01:28 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
2022-10-31 14:25:02 -04:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-10-21 06:01:28 -04:00
|
|
|
"helm.sh/helm/v3/pkg/chart/loader"
|
2022-10-31 14:25:02 -04:00
|
|
|
"helm.sh/helm/v3/pkg/chartutil"
|
|
|
|
"helm.sh/helm/v3/pkg/engine"
|
2022-10-21 06:01:28 -04:00
|
|
|
)
|
|
|
|
|
2022-10-31 14:25:02 -04:00
|
|
|
// TestLoad checks if the serialized format that Load returns correctly preserves the dependencies of the loaded chart.
|
2022-10-21 06:01:28 -04:00
|
|
|
func TestLoad(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
2022-10-31 14:25:02 -04:00
|
|
|
require := require.New(t)
|
2022-10-21 06:01:28 -04:00
|
|
|
|
|
|
|
chartLoader := ChartLoader{}
|
2022-11-18 04:05:02 -05:00
|
|
|
config := &config.Config{Provider: config.ProviderConfig{GCP: &config.GCPConfig{}}}
|
|
|
|
release, err := chartLoader.Load(config, true, []byte("secret"), []byte("salt"))
|
2022-10-31 14:25:02 -04:00
|
|
|
require.NoError(err)
|
2022-10-21 06:01:28 -04:00
|
|
|
|
|
|
|
var helmReleases helm.Releases
|
|
|
|
err = json.Unmarshal(release, &helmReleases)
|
2022-10-31 14:25:02 -04:00
|
|
|
require.NoError(err)
|
2022-10-21 06:01:28 -04:00
|
|
|
reader := bytes.NewReader(helmReleases.ConstellationServices.Chart)
|
|
|
|
chart, err := loader.LoadArchive(reader)
|
2022-10-31 14:25:02 -04:00
|
|
|
require.NoError(err)
|
2022-10-21 06:01:28 -04:00
|
|
|
assert.NotNil(chart.Dependencies())
|
|
|
|
}
|
2022-10-31 14:25:02 -04:00
|
|
|
|
2022-11-21 04:35:40 -05:00
|
|
|
// TestConstellationServices checks if the rendered constellation-services chart produces the expected yaml files.
|
|
|
|
func TestConstellationServices(t *testing.T) {
|
2022-10-31 14:25:02 -04:00
|
|
|
testCases := map[string]struct {
|
2022-11-18 04:05:02 -05:00
|
|
|
config *config.Config
|
2022-10-31 14:25:02 -04:00
|
|
|
enforceIDKeyDigest bool
|
|
|
|
valuesModifier func(map[string]any) error
|
|
|
|
ccmImage string
|
2022-11-02 12:47:10 -04:00
|
|
|
cnmImage string
|
2022-10-31 14:25:02 -04:00
|
|
|
}{
|
|
|
|
"GCP": {
|
2022-11-18 04:05:02 -05:00
|
|
|
config: &config.Config{Provider: config.ProviderConfig{GCP: &config.GCPConfig{
|
2022-11-24 04:57:58 -05:00
|
|
|
DeployCSIDriver: func() *bool { b := true; return &b }(),
|
2022-11-18 04:05:02 -05:00
|
|
|
}}},
|
2022-10-31 14:25:02 -04:00
|
|
|
enforceIDKeyDigest: false,
|
|
|
|
valuesModifier: prepareGCPValues,
|
|
|
|
ccmImage: "ccmImageForGCP",
|
|
|
|
},
|
|
|
|
"Azure": {
|
2022-11-18 04:05:02 -05:00
|
|
|
config: &config.Config{Provider: config.ProviderConfig{Azure: &config.AzureConfig{
|
2022-11-24 04:57:58 -05:00
|
|
|
DeployCSIDriver: func() *bool { b := true; return &b }(),
|
|
|
|
EnforceIDKeyDigest: func() *bool { b := true; return &b }(),
|
2022-11-18 04:05:02 -05:00
|
|
|
}}},
|
2022-10-31 14:25:02 -04:00
|
|
|
enforceIDKeyDigest: true,
|
|
|
|
valuesModifier: prepareAzureValues,
|
|
|
|
ccmImage: "ccmImageForAzure",
|
2022-11-02 12:47:10 -04:00
|
|
|
cnmImage: "cnmImageForAzure",
|
2022-10-31 14:25:02 -04:00
|
|
|
},
|
|
|
|
"QEMU": {
|
2022-11-24 04:57:58 -05:00
|
|
|
config: &config.Config{Provider: config.ProviderConfig{QEMU: &config.QEMUConfig{}}},
|
2022-10-31 14:25:02 -04:00
|
|
|
enforceIDKeyDigest: false,
|
|
|
|
valuesModifier: prepareQEMUValues,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
2022-11-24 04:57:58 -05:00
|
|
|
chartLoader := ChartLoader{
|
|
|
|
joinServiceImage: "joinServiceImage",
|
|
|
|
kmsImage: "kmsImage",
|
|
|
|
ccmImage: tc.ccmImage,
|
|
|
|
cnmImage: tc.cnmImage,
|
|
|
|
autoscalerImage: "autoscalerImage",
|
|
|
|
verificationServiceImage: "verificationImage",
|
|
|
|
}
|
2022-11-21 04:35:40 -05:00
|
|
|
chart, values, err := chartLoader.loadConstellationServicesHelper(tc.config, []byte("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"), []byte("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"))
|
2022-10-31 14:25:02 -04:00
|
|
|
require.NoError(err)
|
|
|
|
|
2022-11-21 04:35:40 -05:00
|
|
|
options := chartutil.ReleaseOptions{
|
|
|
|
Name: "testRelease",
|
|
|
|
Namespace: "testNamespace",
|
|
|
|
Revision: 1,
|
|
|
|
IsInstall: true,
|
|
|
|
IsUpgrade: false,
|
|
|
|
}
|
|
|
|
caps := &chartutil.Capabilities{}
|
|
|
|
|
|
|
|
err = tc.valuesModifier(values)
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
// This step is needed to enabled/disable subcharts according to their tags/conditions.
|
|
|
|
err = chartutil.ProcessDependencies(chart, values)
|
2022-10-31 14:25:02 -04:00
|
|
|
require.NoError(err)
|
2022-11-21 04:35:40 -05:00
|
|
|
|
|
|
|
valuesToRender, err := chartutil.ToRenderValues(chart, values, options, caps)
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
result, err := engine.Render(chart, valuesToRender)
|
|
|
|
require.NoError(err)
|
2022-11-25 08:56:30 -05:00
|
|
|
for k, actual := range result {
|
2022-11-21 04:35:40 -05:00
|
|
|
currentFile := path.Join("testdata", tc.config.GetProvider().String(), k)
|
2022-11-25 08:56:30 -05:00
|
|
|
expected, err := os.ReadFile(currentFile)
|
2022-11-21 04:35:40 -05:00
|
|
|
// If a file does not exist, we expect the render for that path to be empty.
|
|
|
|
if errors.Is(err, fs.ErrNotExist) {
|
2022-11-25 08:56:30 -05:00
|
|
|
assert.YAMLEq("", actual, fmt.Sprintf("current file: %s", currentFile))
|
2022-11-21 04:35:40 -05:00
|
|
|
continue
|
|
|
|
}
|
2022-11-25 08:56:30 -05:00
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
// testify has an issue where when multiple documents are contained in one YAML string,
|
|
|
|
// only the first document is parsed [1]. For this reason we split the YAML string
|
|
|
|
// into a slice of strings, each entry containing one document.
|
|
|
|
// [1] https://github.com/stretchr/testify/issues/1281
|
|
|
|
expectedSplit := strings.Split(string(expected), "\n---\n")
|
|
|
|
sort.Strings(expectedSplit)
|
|
|
|
actualSplit := strings.Split(actual, "\n---\n")
|
|
|
|
sort.Strings(actualSplit)
|
|
|
|
assert.Equal(len(expectedSplit), len(actualSplit))
|
|
|
|
|
|
|
|
for i := range expectedSplit {
|
|
|
|
assert.YAMLEq(expectedSplit[i], actualSplit[i], fmt.Sprintf("current file: %s", currentFile))
|
|
|
|
}
|
2022-11-21 04:35:40 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestOperators checks if the rendered constellation-services chart produces the expected yaml files.
|
|
|
|
func TestOperators(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
|
|
|
csp cloudprovider.Provider
|
|
|
|
}{
|
|
|
|
"GCP": {
|
|
|
|
csp: cloudprovider.GCP,
|
|
|
|
},
|
|
|
|
"Azure": {
|
|
|
|
csp: cloudprovider.Azure,
|
|
|
|
},
|
|
|
|
"QEMU": {
|
|
|
|
csp: cloudprovider.QEMU,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
chartLoader := ChartLoader{joinServiceImage: "joinServiceImage", kmsImage: "kmsImage", ccmImage: "ccmImage", cnmImage: "cnmImage", autoscalerImage: "autoscalerImage"}
|
|
|
|
chart, vals, err := chartLoader.loadOperatorsHelper(tc.csp)
|
2022-10-31 14:25:02 -04:00
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
options := chartutil.ReleaseOptions{
|
|
|
|
Name: "testRelease",
|
|
|
|
Namespace: "testNamespace",
|
|
|
|
Revision: 1,
|
|
|
|
IsInstall: true,
|
|
|
|
IsUpgrade: false,
|
|
|
|
}
|
|
|
|
caps := &chartutil.Capabilities{}
|
|
|
|
|
2022-11-21 04:35:40 -05:00
|
|
|
conOpVals, ok := vals["constellation-operator"].(map[string]any)
|
|
|
|
require.True(ok)
|
|
|
|
conOpVals["constellationUID"] = "42424242424242"
|
2022-10-31 14:25:02 -04:00
|
|
|
|
2022-11-02 12:47:10 -04:00
|
|
|
// This step is needed to enabled/disable subcharts according to their tags/conditions.
|
2022-11-21 04:35:40 -05:00
|
|
|
err = chartutil.ProcessDependencies(chart, vals)
|
2022-11-02 12:47:10 -04:00
|
|
|
require.NoError(err)
|
|
|
|
|
2022-11-21 04:35:40 -05:00
|
|
|
valuesToRender, err := chartutil.ToRenderValues(chart, vals, options, caps)
|
2022-10-31 14:25:02 -04:00
|
|
|
require.NoError(err)
|
2022-11-02 12:47:10 -04:00
|
|
|
|
2022-10-31 14:25:02 -04:00
|
|
|
result, err := engine.Render(chart, valuesToRender)
|
|
|
|
require.NoError(err)
|
2022-11-25 08:56:30 -05:00
|
|
|
for k, actual := range result {
|
2022-11-21 04:35:40 -05:00
|
|
|
currentFile := path.Join("testdata", tc.csp.String(), k)
|
2022-11-25 08:56:30 -05:00
|
|
|
expected, err := os.ReadFile(currentFile)
|
2022-10-31 14:25:02 -04:00
|
|
|
// If a file does not exist, we expect the render for that path to be empty.
|
|
|
|
if errors.Is(err, fs.ErrNotExist) {
|
2022-11-25 08:56:30 -05:00
|
|
|
assert.YAMLEq("", actual, fmt.Sprintf("current file: %s", currentFile))
|
2022-10-31 14:25:02 -04:00
|
|
|
continue
|
|
|
|
}
|
2022-11-25 08:56:30 -05:00
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
// testify has an issue where when multiple documents are contained in one YAML string,
|
|
|
|
// only the first document is parsed [1]. For this reason we split the YAML string
|
|
|
|
// into a slice of strings, each entry containing one document.
|
|
|
|
// [1] https://github.com/stretchr/testify/issues/1281
|
|
|
|
expectedSplit := strings.Split(string(expected), "\n---\n")
|
|
|
|
sort.Strings(expectedSplit)
|
|
|
|
actualSplit := strings.Split(actual, "\n---\n")
|
|
|
|
sort.Strings(actualSplit)
|
|
|
|
assert.Equal(len(expectedSplit), len(actualSplit))
|
|
|
|
|
|
|
|
for i := range expectedSplit {
|
|
|
|
assert.YAMLEq(expectedSplit[i], actualSplit[i], fmt.Sprintf("current file: %s", currentFile))
|
|
|
|
}
|
2022-10-31 14:25:02 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func prepareGCPValues(values map[string]any) error {
|
|
|
|
joinVals, ok := values["join-service"].(map[string]any)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("missing 'join-service' key")
|
|
|
|
}
|
2022-11-24 04:57:58 -05:00
|
|
|
|
|
|
|
m := measurements.M{
|
|
|
|
1: measurements.WithAllBytes(0xAA, false),
|
|
|
|
}
|
|
|
|
mJSON, err := json.Marshal(m)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
joinVals["measurements"] = string(mJSON)
|
2022-10-31 14:25:02 -04:00
|
|
|
joinVals["measurementSalt"] = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
|
|
|
|
|
|
|
|
ccmVals, ok := values["ccm"].(map[string]any)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("missing 'ccm' key")
|
|
|
|
}
|
2022-11-02 08:06:07 -04:00
|
|
|
ccmVals["GCP"].(map[string]any)["subnetworkPodCIDR"] = "192.0.2.0/24"
|
2022-10-31 14:25:02 -04:00
|
|
|
ccmVals["GCP"].(map[string]any)["projectID"] = "42424242424242"
|
|
|
|
ccmVals["GCP"].(map[string]any)["uid"] = "242424242424"
|
|
|
|
ccmVals["GCP"].(map[string]any)["secretData"] = "baaaaaad"
|
|
|
|
|
2022-11-18 04:05:02 -05:00
|
|
|
testTag := "v0.0.0"
|
|
|
|
pullPolicy := "IfNotPresent"
|
|
|
|
csiVals, ok := values["gcp-compute-persistent-disk-csi-driver"].(map[string]any)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("missing 'gcp-compute-persistent-disk-csi-driver' key")
|
|
|
|
}
|
|
|
|
csiVals["image"] = map[string]any{
|
|
|
|
"csiProvisioner": map[string]any{
|
|
|
|
"repo": "csi-provisioner",
|
|
|
|
"tag": testTag,
|
|
|
|
"pullPolicy": pullPolicy,
|
|
|
|
},
|
|
|
|
"csiAttacher": map[string]any{
|
|
|
|
"repo": "csi-attacher",
|
|
|
|
"tag": testTag,
|
|
|
|
"pullPolicy": pullPolicy,
|
|
|
|
},
|
|
|
|
"csiResizer": map[string]any{
|
|
|
|
"repo": "csi-resizer",
|
|
|
|
"tag": testTag,
|
|
|
|
"pullPolicy": pullPolicy,
|
|
|
|
},
|
|
|
|
"csiSnapshotter": map[string]any{
|
|
|
|
"repo": "csi-snapshotter",
|
|
|
|
"tag": testTag,
|
|
|
|
"pullPolicy": pullPolicy,
|
|
|
|
},
|
|
|
|
"csiNodeRegistrar": map[string]any{
|
|
|
|
"repo": "csi-registrar",
|
|
|
|
"tag": testTag,
|
|
|
|
"pullPolicy": pullPolicy,
|
|
|
|
},
|
|
|
|
"gcepdDriver": map[string]any{
|
|
|
|
"repo": "csi-driver",
|
|
|
|
"tag": testTag,
|
|
|
|
"pullPolicy": pullPolicy,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-11-21 11:06:41 -05:00
|
|
|
verificationVals, ok := values["verification-service"].(map[string]any)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("missing 'verification-service' key")
|
|
|
|
}
|
|
|
|
verificationVals["loadBalancerIP"] = "127.0.0.1"
|
2022-11-23 02:26:09 -05:00
|
|
|
|
|
|
|
konnectivityVals, ok := values["konnectivity"].(map[string]any)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("missing 'konnectivity' key")
|
|
|
|
}
|
|
|
|
konnectivityVals["loadBalancerIP"] = "127.0.0.1"
|
|
|
|
|
2022-10-31 14:25:02 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func prepareAzureValues(values map[string]any) error {
|
|
|
|
joinVals, ok := values["join-service"].(map[string]any)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("missing 'join-service' key")
|
|
|
|
}
|
|
|
|
joinVals["idkeydigest"] = "baaaaaadbaaaaaadbaaaaaadbaaaaaadbaaaaaadbaaaaaadbaaaaaadbaaaaaad"
|
2022-11-24 04:57:58 -05:00
|
|
|
m := measurements.M{1: measurements.WithAllBytes(0xAA, false)}
|
|
|
|
mJSON, err := json.Marshal(m)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
joinVals["measurements"] = string(mJSON)
|
2022-10-31 14:25:02 -04:00
|
|
|
joinVals["measurementSalt"] = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
|
|
|
|
|
|
|
|
ccmVals, ok := values["ccm"].(map[string]any)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("missing 'ccm' key")
|
|
|
|
}
|
2022-11-02 08:06:07 -04:00
|
|
|
ccmVals["Azure"].(map[string]any)["subnetworkPodCIDR"] = "192.0.2.0/24"
|
2022-10-31 14:25:02 -04:00
|
|
|
ccmVals["Azure"].(map[string]any)["azureConfig"] = "baaaaaad"
|
|
|
|
|
2022-11-03 11:42:19 -04:00
|
|
|
autoscalerVals, ok := values["autoscaler"].(map[string]any)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("missing 'autoscaler' key")
|
|
|
|
}
|
|
|
|
autoscalerVals["Azure"] = map[string]any{
|
|
|
|
"clientID": "AppClientID",
|
|
|
|
"clientSecret": "ClientSecretValue",
|
|
|
|
"resourceGroup": "resourceGroup",
|
|
|
|
"subscriptionID": "subscriptionID",
|
|
|
|
"tenantID": "TenantID",
|
|
|
|
}
|
2022-11-21 11:06:41 -05:00
|
|
|
|
|
|
|
verificationVals, ok := values["verification-service"].(map[string]any)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("missing 'verification-service' key")
|
|
|
|
}
|
|
|
|
verificationVals["loadBalancerIP"] = "127.0.0.1"
|
2022-11-23 02:26:09 -05:00
|
|
|
|
|
|
|
konnectivityVals, ok := values["konnectivity"].(map[string]any)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("missing 'konnectivity' key")
|
|
|
|
}
|
|
|
|
konnectivityVals["loadBalancerIP"] = "127.0.0.1"
|
|
|
|
|
2022-10-31 14:25:02 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func prepareQEMUValues(values map[string]any) error {
|
|
|
|
joinVals, ok := values["join-service"].(map[string]any)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("missing 'join-service' key")
|
|
|
|
}
|
2022-11-24 04:57:58 -05:00
|
|
|
m := measurements.M{1: measurements.WithAllBytes(0xAA, false)}
|
|
|
|
mJSON, err := json.Marshal(m)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
joinVals["measurements"] = string(mJSON)
|
2022-10-31 14:25:02 -04:00
|
|
|
joinVals["measurementSalt"] = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
|
|
|
|
|
2022-11-21 11:06:41 -05:00
|
|
|
verificationVals, ok := values["verification-service"].(map[string]any)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("missing 'verification-service' key")
|
|
|
|
}
|
|
|
|
verificationVals["loadBalancerIP"] = "127.0.0.1"
|
|
|
|
|
2022-11-23 02:26:09 -05:00
|
|
|
konnectivityVals, ok := values["konnectivity"].(map[string]any)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("missing 'konnectivity' key")
|
|
|
|
}
|
|
|
|
konnectivityVals["loadBalancerIP"] = "127.0.0.1"
|
|
|
|
|
2022-10-31 14:25:02 -04:00
|
|
|
return nil
|
|
|
|
}
|