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 (
|
|
|
|
"bytes"
|
2023-05-17 10:53:56 -04:00
|
|
|
"fmt"
|
2022-05-12 09:14:52 -04:00
|
|
|
"testing"
|
|
|
|
|
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"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/file"
|
2023-01-04 04:46:29 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/logger"
|
2023-05-17 10:53:56 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/variant"
|
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"
|
2023-05-17 10:53:56 -04:00
|
|
|
"github.com/spf13/cobra"
|
2022-05-12 09:14:52 -04:00
|
|
|
"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
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
2023-02-21 08:05:41 -05:00
|
|
|
func TestConfigGenerateKubernetesVersion(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
|
|
|
version string
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
"success": {
|
|
|
|
version: semver.MajorMinor(string(versions.Default)),
|
|
|
|
},
|
|
|
|
"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)
|
|
|
|
|
|
|
|
fileHandler := file.NewHandler(afero.NewMemMapFs())
|
|
|
|
cmd := newConfigGenerateCmd()
|
|
|
|
err := cmd.Flags().Set("kubernetes", tc.version)
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
cg := &configGenerateCmd{log: logger.NewTest(t)}
|
2023-05-30 03:02:50 -04:00
|
|
|
err = cg.configGenerate(cmd, fileHandler, cloudprovider.Unknown, "")
|
2023-02-21 08:05:41 -05:00
|
|
|
|
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert.NoError(err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-01-04 04:46:29 -05:00
|
|
|
cg := &configGenerateCmd{log: logger.NewTest(t)}
|
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)
|
2022-05-12 09:14:52 -04:00
|
|
|
}
|
|
|
|
|
2023-05-30 03:02:50 -04:00
|
|
|
func TestConfigGenerateDefaultProviderSpecific(t *testing.T) {
|
|
|
|
providers := []cloudprovider.Provider{
|
|
|
|
cloudprovider.AWS,
|
|
|
|
cloudprovider.Azure,
|
|
|
|
cloudprovider.GCP,
|
|
|
|
cloudprovider.OpenStack,
|
|
|
|
}
|
2022-05-18 05:39:14 -04:00
|
|
|
|
2023-05-30 03:02:50 -04:00
|
|
|
for _, provider := range providers {
|
|
|
|
t.Run(provider.String(), func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
2022-05-18 05:39:14 -04:00
|
|
|
|
2023-05-30 03:02:50 -04:00
|
|
|
fileHandler := file.NewHandler(afero.NewMemMapFs())
|
|
|
|
cmd := newConfigGenerateCmd()
|
2023-02-10 07:27:22 -05:00
|
|
|
|
2023-05-30 03:02:50 -04:00
|
|
|
wantConf := config.Default()
|
|
|
|
wantConf.RemoveProviderAndAttestationExcept(provider)
|
2022-05-18 05:39:14 -04:00
|
|
|
|
2023-05-30 03:02:50 -04:00
|
|
|
cg := &configGenerateCmd{log: logger.NewTest(t)}
|
|
|
|
require.NoError(cg.configGenerate(cmd, fileHandler, provider, ""))
|
|
|
|
|
|
|
|
var readConfig config.Config
|
|
|
|
err := fileHandler.ReadYAML(constants.ConfigFilename, &readConfig)
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.Equal(*wantConf, readConfig)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigGenerateWithStackIt(t *testing.T) {
|
|
|
|
openStackProviders := []string{"stackit"}
|
|
|
|
|
|
|
|
for _, openStackProvider := range openStackProviders {
|
|
|
|
t.Run(openStackProvider, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
fileHandler := file.NewHandler(afero.NewMemMapFs())
|
|
|
|
cmd := newConfigGenerateCmd()
|
|
|
|
|
|
|
|
wantConf := config.Default().WithOpenStackProviderDefaults(openStackProvider)
|
|
|
|
wantConf.RemoveProviderAndAttestationExcept(cloudprovider.OpenStack)
|
|
|
|
|
|
|
|
cg := &configGenerateCmd{log: logger.NewTest(t)}
|
|
|
|
require.NoError(cg.configGenerate(cmd, fileHandler, cloudprovider.OpenStack, openStackProvider))
|
|
|
|
|
|
|
|
var readConfig config.Config
|
|
|
|
err := fileHandler.ReadYAML(constants.ConfigFilename, &readConfig)
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.Equal(*wantConf, readConfig)
|
|
|
|
})
|
|
|
|
}
|
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-01-04 04:46:29 -05:00
|
|
|
cg := &configGenerateCmd{log: logger.NewTest(t)}
|
2023-05-30 03:02:50 -04:00
|
|
|
require.Error(cg.configGenerate(cmd, fileHandler, cloudprovider.Unknown, ""))
|
2022-05-12 09:14:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigGenerateFileFlagRemoved(t *testing.T) {
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
fileHandler := file.NewHandler(afero.NewMemMapFs())
|
|
|
|
cmd := newConfigGenerateCmd()
|
|
|
|
cmd.ResetFlags()
|
|
|
|
|
2023-01-04 04:46:29 -05:00
|
|
|
cg := &configGenerateCmd{log: logger.NewTest(t)}
|
2023-05-30 03:02:50 -04:00
|
|
|
require.Error(cg.configGenerate(cmd, fileHandler, cloudprovider.Unknown, ""))
|
2022-05-12 09:14:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigGenerateStdOut(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
fileHandler := file.NewHandler(afero.NewMemMapFs())
|
|
|
|
|
|
|
|
var outBuffer bytes.Buffer
|
|
|
|
cmd := newConfigGenerateCmd()
|
|
|
|
cmd.SetOut(&outBuffer)
|
|
|
|
require.NoError(cmd.Flags().Set("file", "-"))
|
|
|
|
|
2023-01-04 04:46:29 -05:00
|
|
|
cg := &configGenerateCmd{log: logger.NewTest(t)}
|
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
|
|
|
|
require.NoError(yaml.NewDecoder(&outBuffer).Decode(&readConfig))
|
|
|
|
|
|
|
|
assert.Equal(*config.Default(), readConfig)
|
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-05-30 03:02:50 -04:00
|
|
|
_, err := createConfigWithAttestationType(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},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
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-05-30 03:02:50 -04:00
|
|
|
sut, err := createConfigWithAttestationType(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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAttestationArgument(t *testing.T) {
|
|
|
|
defaultAttestation := config.Default().Attestation
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
provider cloudprovider.Provider
|
|
|
|
expectErr bool
|
|
|
|
expectedCfg config.AttestationConfig
|
|
|
|
setFlag func(*cobra.Command) error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "InvalidAttestationArgument",
|
|
|
|
provider: cloudprovider.Unknown,
|
|
|
|
expectErr: true,
|
|
|
|
setFlag: func(cmd *cobra.Command) error {
|
|
|
|
return cmd.Flags().Set("attestation", "unknown")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ValidAttestationArgument",
|
|
|
|
provider: cloudprovider.Azure,
|
|
|
|
expectErr: false,
|
|
|
|
setFlag: func(cmd *cobra.Command) error {
|
|
|
|
return cmd.Flags().Set("attestation", "azure-trustedlaunch")
|
|
|
|
},
|
|
|
|
expectedCfg: config.AttestationConfig{AzureTrustedLaunch: defaultAttestation.AzureTrustedLaunch},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "WithoutAttestationArgument",
|
|
|
|
provider: cloudprovider.Azure,
|
|
|
|
expectErr: false,
|
|
|
|
setFlag: func(cmd *cobra.Command) error {
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
expectedCfg: config.AttestationConfig{AzureSEVSNP: defaultAttestation.AzureSEVSNP},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
require := assert.New(t)
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
cmd := newConfigGenerateCmd()
|
|
|
|
require.NoError(test.setFlag(cmd))
|
|
|
|
|
|
|
|
fileHandler := file.NewHandler(afero.NewMemMapFs())
|
|
|
|
|
|
|
|
cg := &configGenerateCmd{log: logger.NewTest(t)}
|
2023-05-30 03:02:50 -04:00
|
|
|
err := cg.configGenerate(cmd, fileHandler, test.provider, "")
|
2023-05-17 10:53:56 -04:00
|
|
|
if test.expectErr {
|
|
|
|
assert.Error(err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
|
|
|
var readConfig config.Config
|
|
|
|
require.NoError(fileHandler.ReadYAML(constants.ConfigFilename, &readConfig))
|
|
|
|
|
|
|
|
assert.Equal(test.expectedCfg, readConfig.Attestation)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|