2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-05-12 09:14:52 -04:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2023-05-17 10:53:56 -04:00
|
|
|
"fmt"
|
2023-09-19 07:50:00 -04:00
|
|
|
"strings"
|
2022-05-12 09:14:52 -04:00
|
|
|
"testing"
|
|
|
|
|
2023-06-09 09:41:02 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation/variant"
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/cloud/cloudprovider"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/config"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/constants"
|
2023-12-08 10:27:04 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/constellation/state"
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/file"
|
2023-01-04 04:46:29 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/logger"
|
2023-02-21 08:05:41 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/versions"
|
2022-05-12 09:14:52 -04:00
|
|
|
"github.com/spf13/afero"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2023-02-21 08:05:41 -05:00
|
|
|
"golang.org/x/mod/semver"
|
2022-05-12 09:14:52 -04:00
|
|
|
)
|
|
|
|
|
2023-10-16 09:05:29 -04:00
|
|
|
func TestParseKubernetesVersion(t *testing.T) {
|
2023-02-21 08:05:41 -05:00
|
|
|
testCases := map[string]struct {
|
|
|
|
version string
|
|
|
|
wantErr bool
|
|
|
|
}{
|
2023-09-19 07:50:00 -04:00
|
|
|
"default version": {
|
|
|
|
version: "",
|
|
|
|
},
|
|
|
|
"without v prefix": {
|
|
|
|
version: strings.TrimPrefix(string(versions.Default), "v"),
|
|
|
|
},
|
|
|
|
"K8s version without patch version": {
|
2023-02-21 08:05:41 -05:00
|
|
|
version: semver.MajorMinor(string(versions.Default)),
|
|
|
|
},
|
2023-09-19 07:50:00 -04:00
|
|
|
"K8s version with patch version": {
|
|
|
|
version: string(versions.Default),
|
|
|
|
},
|
|
|
|
"K8s version with invalid patch version": {
|
|
|
|
version: func() string {
|
|
|
|
s := string(versions.Default)
|
|
|
|
return s[:len(s)-1] + "99"
|
|
|
|
}(),
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"outdated K8s version": {
|
|
|
|
version: "v1.0.0",
|
|
|
|
wantErr: true,
|
|
|
|
},
|
2023-02-21 08:05:41 -05:00
|
|
|
"no semver": {
|
|
|
|
version: "asdf",
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"not supported": {
|
|
|
|
version: "1111",
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
2023-10-16 09:05:29 -04:00
|
|
|
flags := newConfigGenerateCmd().Flags()
|
2023-09-19 07:50:00 -04:00
|
|
|
if tc.version != "" {
|
2023-10-16 09:05:29 -04:00
|
|
|
require.NoError(flags.Set("kubernetes", tc.version))
|
2023-09-19 07:50:00 -04:00
|
|
|
}
|
2023-02-21 08:05:41 -05:00
|
|
|
|
2023-10-16 09:05:29 -04:00
|
|
|
version, err := parseK8sFlag(flags)
|
2023-02-21 08:05:41 -05:00
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert.NoError(err)
|
2023-10-16 09:05:29 -04:00
|
|
|
assert.Equal(versions.Default, version)
|
2023-02-21 08:05:41 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-12 09:14:52 -04:00
|
|
|
func TestConfigGenerateDefault(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
fileHandler := file.NewHandler(afero.NewMemMapFs())
|
|
|
|
cmd := newConfigGenerateCmd()
|
|
|
|
|
2023-10-16 09:05:29 -04:00
|
|
|
cg := &configGenerateCmd{
|
|
|
|
log: logger.NewTest(t),
|
|
|
|
flags: generateFlags{
|
|
|
|
attestationVariant: variant.Dummy{},
|
|
|
|
k8sVersion: versions.Default,
|
|
|
|
},
|
|
|
|
}
|
2023-05-30 03:02:50 -04:00
|
|
|
require.NoError(cg.configGenerate(cmd, fileHandler, cloudprovider.Unknown, ""))
|
2022-05-12 09:14:52 -04:00
|
|
|
|
2022-05-16 12:54:25 -04:00
|
|
|
var readConfig config.Config
|
|
|
|
err := fileHandler.ReadYAML(constants.ConfigFilename, &readConfig)
|
2022-05-12 09:14:52 -04:00
|
|
|
assert.NoError(err)
|
2022-05-16 12:54:25 -04:00
|
|
|
assert.Equal(*config.Default(), readConfig)
|
2023-10-16 14:18:59 -04:00
|
|
|
|
|
|
|
_, err = state.ReadFromFile(fileHandler, constants.StateFilename)
|
|
|
|
assert.NoError(err)
|
2022-05-12 09:14:52 -04:00
|
|
|
}
|
|
|
|
|
2023-05-30 03:02:50 -04:00
|
|
|
func TestConfigGenerateDefaultProviderSpecific(t *testing.T) {
|
2023-10-16 09:05:29 -04:00
|
|
|
testCases := map[string]struct {
|
|
|
|
provider cloudprovider.Provider
|
|
|
|
rawProvider string
|
|
|
|
}{
|
|
|
|
"aws": {
|
|
|
|
provider: cloudprovider.AWS,
|
|
|
|
},
|
|
|
|
"azure": {
|
|
|
|
provider: cloudprovider.Azure,
|
|
|
|
},
|
|
|
|
"gcp": {
|
|
|
|
provider: cloudprovider.GCP,
|
|
|
|
},
|
|
|
|
"openstack": {
|
|
|
|
provider: cloudprovider.OpenStack,
|
|
|
|
},
|
|
|
|
"stackit": {
|
|
|
|
provider: cloudprovider.OpenStack,
|
|
|
|
rawProvider: "stackit",
|
|
|
|
},
|
2023-05-30 03:02:50 -04:00
|
|
|
}
|
|
|
|
|
2023-10-16 09:05:29 -04:00
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
2023-05-30 03:02:50 -04:00
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
fileHandler := file.NewHandler(afero.NewMemMapFs())
|
|
|
|
cmd := newConfigGenerateCmd()
|
|
|
|
|
2023-10-16 09:05:29 -04:00
|
|
|
wantConf := config.Default().WithOpenStackProviderDefaults(tc.rawProvider)
|
|
|
|
wantConf.RemoveProviderAndAttestationExcept(tc.provider)
|
2023-05-30 03:02:50 -04:00
|
|
|
|
2023-10-16 09:05:29 -04:00
|
|
|
cg := &configGenerateCmd{
|
|
|
|
log: logger.NewTest(t),
|
|
|
|
flags: generateFlags{
|
|
|
|
attestationVariant: variant.Dummy{},
|
|
|
|
k8sVersion: versions.Default,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
require.NoError(cg.configGenerate(cmd, fileHandler, tc.provider, tc.rawProvider))
|
2023-05-30 03:02:50 -04:00
|
|
|
|
|
|
|
var readConfig config.Config
|
|
|
|
err := fileHandler.ReadYAML(constants.ConfigFilename, &readConfig)
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.Equal(*wantConf, readConfig)
|
2023-10-16 14:18:59 -04:00
|
|
|
|
|
|
|
stateFile, err := state.ReadFromFile(fileHandler, constants.StateFilename)
|
|
|
|
assert.NoError(err)
|
|
|
|
switch tc.provider {
|
|
|
|
case cloudprovider.GCP:
|
|
|
|
assert.NotNil(stateFile.Infrastructure.GCP)
|
|
|
|
case cloudprovider.Azure:
|
|
|
|
assert.NotNil(stateFile.Infrastructure.Azure)
|
|
|
|
}
|
2023-05-30 03:02:50 -04:00
|
|
|
})
|
|
|
|
}
|
2022-05-18 05:39:14 -04:00
|
|
|
}
|
|
|
|
|
2022-05-12 09:14:52 -04:00
|
|
|
func TestConfigGenerateDefaultExists(t *testing.T) {
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
fileHandler := file.NewHandler(afero.NewMemMapFs())
|
|
|
|
require.NoError(fileHandler.Write(constants.ConfigFilename, []byte("foobar"), file.OptNone))
|
|
|
|
cmd := newConfigGenerateCmd()
|
|
|
|
|
2023-10-16 09:05:29 -04:00
|
|
|
cg := &configGenerateCmd{
|
|
|
|
log: logger.NewTest(t),
|
|
|
|
flags: generateFlags{attestationVariant: variant.Dummy{}},
|
|
|
|
}
|
2023-05-30 03:02:50 -04:00
|
|
|
require.Error(cg.configGenerate(cmd, fileHandler, cloudprovider.Unknown, ""))
|
2022-05-12 09:14:52 -04:00
|
|
|
}
|
|
|
|
|
2023-05-17 10:53:56 -04:00
|
|
|
func TestNoValidProviderAttestationCombination(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
tests := []struct {
|
|
|
|
provider cloudprovider.Provider
|
|
|
|
attestation variant.Variant
|
|
|
|
}{
|
|
|
|
{cloudprovider.Azure, variant.AWSNitroTPM{}},
|
|
|
|
{cloudprovider.AWS, variant.AzureTrustedLaunch{}},
|
|
|
|
{cloudprovider.GCP, variant.AWSNitroTPM{}},
|
|
|
|
{cloudprovider.QEMU, variant.GCPSEVES{}},
|
2023-05-30 03:02:50 -04:00
|
|
|
{cloudprovider.OpenStack, variant.AWSNitroTPM{}},
|
2023-05-17 10:53:56 -04:00
|
|
|
}
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run("", func(t *testing.T) {
|
2023-06-25 17:32:39 -04:00
|
|
|
_, err := createConfigWithAttestationVariant(test.provider, "", test.attestation)
|
2023-05-17 10:53:56 -04:00
|
|
|
assert.Error(err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidProviderAttestationCombination(t *testing.T) {
|
|
|
|
defaultAttestation := config.Default().Attestation
|
|
|
|
tests := []struct {
|
|
|
|
provider cloudprovider.Provider
|
|
|
|
attestation variant.Variant
|
|
|
|
expected config.AttestationConfig
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
cloudprovider.Azure,
|
|
|
|
variant.AzureTrustedLaunch{},
|
|
|
|
config.AttestationConfig{AzureTrustedLaunch: defaultAttestation.AzureTrustedLaunch},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
cloudprovider.Azure,
|
|
|
|
variant.AzureSEVSNP{},
|
|
|
|
config.AttestationConfig{AzureSEVSNP: defaultAttestation.AzureSEVSNP},
|
|
|
|
},
|
2023-06-09 09:41:02 -04:00
|
|
|
{
|
|
|
|
cloudprovider.AWS,
|
|
|
|
variant.AWSSEVSNP{},
|
|
|
|
config.AttestationConfig{AWSSEVSNP: defaultAttestation.AWSSEVSNP},
|
|
|
|
},
|
2023-05-17 10:53:56 -04:00
|
|
|
{
|
|
|
|
cloudprovider.AWS,
|
|
|
|
variant.AWSNitroTPM{},
|
|
|
|
config.AttestationConfig{AWSNitroTPM: defaultAttestation.AWSNitroTPM},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
cloudprovider.GCP,
|
|
|
|
variant.GCPSEVES{},
|
|
|
|
config.AttestationConfig{GCPSEVES: defaultAttestation.GCPSEVES},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
cloudprovider.QEMU,
|
|
|
|
variant.QEMUVTPM{},
|
|
|
|
config.AttestationConfig{QEMUVTPM: defaultAttestation.QEMUVTPM},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
cloudprovider.OpenStack,
|
|
|
|
variant.QEMUVTPM{},
|
|
|
|
config.AttestationConfig{QEMUVTPM: defaultAttestation.QEMUVTPM},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(fmt.Sprintf("Provider:%s,Attestation:%s", test.provider, test.attestation), func(t *testing.T) {
|
2023-06-25 17:32:39 -04:00
|
|
|
sut, err := createConfigWithAttestationVariant(test.provider, "", test.attestation)
|
2023-05-17 10:53:56 -04:00
|
|
|
assert := assert.New(t)
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.Equal(test.expected, sut.Attestation)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-16 09:05:29 -04:00
|
|
|
func TestParseAttestationFlag(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
|
|
|
wantErr bool
|
|
|
|
attestationFlag string
|
|
|
|
wantVariant variant.Variant
|
2023-05-17 10:53:56 -04:00
|
|
|
}{
|
2023-10-16 09:05:29 -04:00
|
|
|
"invalid": {
|
|
|
|
wantErr: true,
|
|
|
|
attestationFlag: "unknown",
|
2023-05-17 10:53:56 -04:00
|
|
|
},
|
2023-10-16 09:05:29 -04:00
|
|
|
"AzureTrustedLaunch": {
|
|
|
|
attestationFlag: "azure-trustedlaunch",
|
|
|
|
wantVariant: variant.AzureTrustedLaunch{},
|
2023-05-17 10:53:56 -04:00
|
|
|
},
|
2023-10-16 09:05:29 -04:00
|
|
|
"AzureSEVSNP": {
|
|
|
|
attestationFlag: "azure-sev-snp",
|
|
|
|
wantVariant: variant.AzureSEVSNP{},
|
|
|
|
},
|
|
|
|
"AWSSEVSNP": {
|
|
|
|
attestationFlag: "aws-sev-snp",
|
|
|
|
wantVariant: variant.AWSSEVSNP{},
|
|
|
|
},
|
|
|
|
"AWSNitroTPM": {
|
|
|
|
attestationFlag: "aws-nitro-tpm",
|
|
|
|
wantVariant: variant.AWSNitroTPM{},
|
|
|
|
},
|
|
|
|
"GCPSEVES": {
|
|
|
|
attestationFlag: "gcp-sev-es",
|
|
|
|
wantVariant: variant.GCPSEVES{},
|
|
|
|
},
|
|
|
|
"QEMUVTPM": {
|
|
|
|
attestationFlag: "qemu-vtpm",
|
|
|
|
wantVariant: variant.QEMUVTPM{},
|
|
|
|
},
|
|
|
|
"no flag": {
|
|
|
|
wantVariant: variant.Dummy{},
|
2023-05-17 10:53:56 -04:00
|
|
|
},
|
|
|
|
}
|
2023-10-16 09:05:29 -04:00
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
require := require.New(t)
|
2023-05-17 10:53:56 -04:00
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
cmd := newConfigGenerateCmd()
|
2023-10-16 09:05:29 -04:00
|
|
|
if tc.attestationFlag != "" {
|
|
|
|
require.NoError(cmd.Flags().Set("attestation", tc.attestationFlag))
|
|
|
|
}
|
2023-05-17 10:53:56 -04:00
|
|
|
|
2023-10-16 09:05:29 -04:00
|
|
|
attestation, err := parseAttestationFlag(cmd.Flags())
|
|
|
|
if tc.wantErr {
|
2023-05-17 10:53:56 -04:00
|
|
|
assert.Error(err)
|
2023-10-16 09:05:29 -04:00
|
|
|
return
|
2023-05-17 10:53:56 -04:00
|
|
|
}
|
2023-10-16 09:05:29 -04:00
|
|
|
require.NoError(err)
|
|
|
|
assert.True(tc.wantVariant.Equal(attestation))
|
2023-05-17 10:53:56 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|