2022-12-07 05:48:54 -05:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2023-01-12 05:35:26 -05:00
|
|
|
"strings"
|
2022-12-07 05:48:54 -05:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/edgelesssys/constellation/v2/cli/internal/iamid"
|
|
|
|
"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"
|
|
|
|
"github.com/spf13/afero"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestIAMCreateAWS(t *testing.T) {
|
2023-01-12 05:35:26 -05:00
|
|
|
defaultFs := func(require *require.Assertions, provider cloudprovider.Provider, existingFiles []string) afero.Fs {
|
2022-12-07 05:48:54 -05:00
|
|
|
fs := afero.NewMemMapFs()
|
2023-01-12 05:35:26 -05:00
|
|
|
fileHandler := file.NewHandler(fs)
|
|
|
|
for _, f := range existingFiles {
|
|
|
|
require.NoError(fileHandler.Write(f, []byte{1, 2, 3}, file.OptNone))
|
|
|
|
}
|
|
|
|
return fs
|
|
|
|
}
|
|
|
|
readOnlyFs := func(require *require.Assertions, provider cloudprovider.Provider, existingFiles []string) afero.Fs {
|
|
|
|
fs := afero.NewReadOnlyFs(afero.NewMemMapFs())
|
2022-12-07 05:48:54 -05:00
|
|
|
return fs
|
|
|
|
}
|
|
|
|
validIAMIDFile := iamid.File{
|
|
|
|
CloudProvider: cloudprovider.AWS,
|
|
|
|
AWSOutput: iamid.AWSFile{
|
|
|
|
ControlPlaneInstanceProfile: "test_control_plane_instance_profile",
|
|
|
|
WorkerNodeInstanceProfile: "test_worker_nodes_instance_profile",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
2023-01-12 05:35:26 -05:00
|
|
|
setupFs func(require *require.Assertions, provider cloudprovider.Provider, existingFiles []string) afero.Fs
|
|
|
|
creator *stubIAMCreator
|
|
|
|
provider cloudprovider.Provider
|
|
|
|
zoneFlag string
|
|
|
|
prefixFlag string
|
|
|
|
yesFlag bool
|
|
|
|
generateConfigFlag bool
|
|
|
|
configFlag string
|
|
|
|
existingFiles []string
|
|
|
|
stdin string
|
|
|
|
wantAbort bool
|
|
|
|
wantErr bool
|
2022-12-07 05:48:54 -05:00
|
|
|
}{
|
|
|
|
"iam create aws": {
|
2023-01-12 05:35:26 -05:00
|
|
|
setupFs: defaultFs,
|
2022-12-07 05:48:54 -05:00
|
|
|
creator: &stubIAMCreator{id: validIAMIDFile},
|
|
|
|
provider: cloudprovider.AWS,
|
|
|
|
zoneFlag: "us-east-2a",
|
|
|
|
prefixFlag: "test",
|
|
|
|
yesFlag: true,
|
|
|
|
},
|
2023-01-12 05:35:26 -05:00
|
|
|
"iam create aws generate config": {
|
|
|
|
setupFs: defaultFs,
|
|
|
|
creator: &stubIAMCreator{id: validIAMIDFile},
|
|
|
|
provider: cloudprovider.AWS,
|
|
|
|
zoneFlag: "us-east-2a",
|
|
|
|
prefixFlag: "test",
|
|
|
|
yesFlag: true,
|
|
|
|
configFlag: constants.ConfigFilename,
|
|
|
|
generateConfigFlag: true,
|
|
|
|
},
|
|
|
|
"iam create aws generate config custom path": {
|
|
|
|
setupFs: defaultFs,
|
|
|
|
creator: &stubIAMCreator{id: validIAMIDFile},
|
|
|
|
provider: cloudprovider.AWS,
|
|
|
|
zoneFlag: "us-east-2a",
|
|
|
|
prefixFlag: "test",
|
|
|
|
yesFlag: true,
|
|
|
|
generateConfigFlag: true,
|
|
|
|
configFlag: "custom-config.yaml",
|
|
|
|
},
|
|
|
|
"iam create aws generate config path already exists": {
|
|
|
|
setupFs: defaultFs,
|
|
|
|
creator: &stubIAMCreator{id: validIAMIDFile},
|
|
|
|
provider: cloudprovider.AWS,
|
|
|
|
zoneFlag: "us-east-2a",
|
|
|
|
prefixFlag: "test",
|
|
|
|
yesFlag: true,
|
|
|
|
generateConfigFlag: true,
|
|
|
|
wantErr: true,
|
|
|
|
configFlag: constants.ConfigFilename,
|
|
|
|
existingFiles: []string{constants.ConfigFilename},
|
|
|
|
},
|
|
|
|
"iam create aws generate config custom path already exists": {
|
|
|
|
setupFs: defaultFs,
|
|
|
|
creator: &stubIAMCreator{id: validIAMIDFile},
|
|
|
|
provider: cloudprovider.AWS,
|
|
|
|
zoneFlag: "us-east-2a",
|
|
|
|
prefixFlag: "test",
|
|
|
|
yesFlag: true,
|
|
|
|
generateConfigFlag: true,
|
|
|
|
wantErr: true,
|
|
|
|
configFlag: "custom-config.yaml",
|
|
|
|
existingFiles: []string{"custom-config.yaml"},
|
|
|
|
},
|
2022-12-07 05:48:54 -05:00
|
|
|
"interactive": {
|
2023-01-12 05:35:26 -05:00
|
|
|
setupFs: defaultFs,
|
2022-12-07 05:48:54 -05:00
|
|
|
creator: &stubIAMCreator{id: validIAMIDFile},
|
|
|
|
provider: cloudprovider.AWS,
|
|
|
|
zoneFlag: "us-east-2a",
|
|
|
|
prefixFlag: "test",
|
|
|
|
stdin: "yes\n",
|
|
|
|
},
|
2023-01-12 05:35:26 -05:00
|
|
|
"interactive generate config": {
|
|
|
|
setupFs: defaultFs,
|
|
|
|
creator: &stubIAMCreator{id: validIAMIDFile},
|
|
|
|
provider: cloudprovider.AWS,
|
|
|
|
zoneFlag: "us-east-2a",
|
|
|
|
prefixFlag: "test",
|
|
|
|
stdin: "yes\n",
|
|
|
|
configFlag: constants.ConfigFilename,
|
|
|
|
generateConfigFlag: true,
|
|
|
|
},
|
2022-12-07 05:48:54 -05:00
|
|
|
"interactive abort": {
|
2023-01-12 05:35:26 -05:00
|
|
|
setupFs: defaultFs,
|
2022-12-07 05:48:54 -05:00
|
|
|
creator: &stubIAMCreator{id: validIAMIDFile},
|
|
|
|
provider: cloudprovider.AWS,
|
|
|
|
zoneFlag: "us-east-2a",
|
|
|
|
prefixFlag: "test",
|
|
|
|
stdin: "no\n",
|
|
|
|
wantAbort: true,
|
|
|
|
},
|
2023-01-12 05:35:26 -05:00
|
|
|
"interactive generate config abort": {
|
|
|
|
setupFs: defaultFs,
|
|
|
|
creator: &stubIAMCreator{id: validIAMIDFile},
|
|
|
|
provider: cloudprovider.AWS,
|
|
|
|
zoneFlag: "us-east-2a",
|
|
|
|
prefixFlag: "test",
|
|
|
|
stdin: "no\n",
|
|
|
|
generateConfigFlag: true,
|
|
|
|
configFlag: constants.ConfigFilename,
|
|
|
|
wantAbort: true,
|
|
|
|
},
|
2022-12-07 05:48:54 -05:00
|
|
|
"invalid zone": {
|
2023-01-12 05:35:26 -05:00
|
|
|
setupFs: defaultFs,
|
2022-12-07 05:48:54 -05:00
|
|
|
creator: &stubIAMCreator{id: validIAMIDFile},
|
|
|
|
provider: cloudprovider.AWS,
|
|
|
|
zoneFlag: "us-west-5b",
|
|
|
|
prefixFlag: "test",
|
|
|
|
yesFlag: true,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
2023-01-12 05:35:26 -05:00
|
|
|
"unwritable fs": {
|
|
|
|
setupFs: readOnlyFs,
|
|
|
|
creator: &stubIAMCreator{id: validIAMIDFile},
|
|
|
|
provider: cloudprovider.AWS,
|
|
|
|
zoneFlag: "us-east-2a",
|
|
|
|
prefixFlag: "test",
|
|
|
|
yesFlag: true,
|
|
|
|
generateConfigFlag: true,
|
|
|
|
wantErr: true,
|
|
|
|
configFlag: constants.ConfigFilename,
|
|
|
|
},
|
2022-12-07 05:48:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
cmd := newIAMCreateAWSCmd()
|
|
|
|
cmd.SetOut(&bytes.Buffer{})
|
|
|
|
cmd.SetErr(&bytes.Buffer{})
|
|
|
|
cmd.SetIn(bytes.NewBufferString(tc.stdin))
|
2023-01-12 05:35:26 -05:00
|
|
|
|
|
|
|
cmd.Flags().String("config", constants.ConfigFilename, "") // register persistent flag manually
|
|
|
|
cmd.Flags().Bool("generate-config", false, "") // register persistent flag manually
|
|
|
|
|
2022-12-07 05:48:54 -05:00
|
|
|
if tc.zoneFlag != "" {
|
|
|
|
require.NoError(cmd.Flags().Set("zone", tc.zoneFlag))
|
|
|
|
}
|
|
|
|
if tc.prefixFlag != "" {
|
|
|
|
require.NoError(cmd.Flags().Set("prefix", tc.prefixFlag))
|
|
|
|
}
|
|
|
|
if tc.yesFlag {
|
|
|
|
require.NoError(cmd.Flags().Set("yes", "true"))
|
|
|
|
}
|
2023-01-12 05:35:26 -05:00
|
|
|
if tc.generateConfigFlag {
|
|
|
|
require.NoError(cmd.Flags().Set("generate-config", "true"))
|
|
|
|
}
|
|
|
|
if tc.configFlag != "" {
|
|
|
|
require.NoError(cmd.Flags().Set("config", tc.configFlag))
|
|
|
|
}
|
|
|
|
|
|
|
|
fileHandler := file.NewHandler(tc.setupFs(require, tc.provider, tc.existingFiles))
|
2022-12-07 05:48:54 -05:00
|
|
|
|
2023-01-12 05:35:26 -05:00
|
|
|
err := iamCreateAWS(cmd, &nopSpinner{}, tc.creator, fileHandler)
|
2022-12-07 05:48:54 -05:00
|
|
|
|
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
2023-01-12 05:35:26 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if tc.wantAbort {
|
|
|
|
assert.False(tc.creator.createCalled)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if tc.generateConfigFlag {
|
|
|
|
readConfig := &config.Config{}
|
|
|
|
readErr := fileHandler.ReadYAML(tc.configFlag, readConfig)
|
|
|
|
require.NoError(readErr)
|
|
|
|
assert.Equal(tc.creator.id.AWSOutput.ControlPlaneInstanceProfile, readConfig.Provider.AWS.IAMProfileControlPlane)
|
|
|
|
assert.Equal(tc.creator.id.AWSOutput.WorkerNodeInstanceProfile, readConfig.Provider.AWS.IAMProfileWorkerNodes)
|
|
|
|
assert.Equal(tc.zoneFlag, readConfig.Provider.AWS.Zone)
|
|
|
|
assert.True(strings.HasPrefix(readConfig.Provider.AWS.Zone, readConfig.Provider.AWS.Region))
|
2022-12-07 05:48:54 -05:00
|
|
|
}
|
2023-01-12 05:35:26 -05:00
|
|
|
require.NoError(err)
|
|
|
|
assert.True(tc.creator.createCalled)
|
|
|
|
assert.Equal(tc.creator.id.AWSOutput, validIAMIDFile.AWSOutput)
|
2022-12-07 05:48:54 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|