2023-02-24 05:36:41 -05:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/cloud/gcpshared"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/constants"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/file"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/logger"
|
|
|
|
"github.com/spf13/afero"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestIAMDestroy(t *testing.T) {
|
|
|
|
require := require.New(t)
|
|
|
|
someError := errors.New("failed")
|
|
|
|
|
|
|
|
newFsExists := func() file.Handler {
|
|
|
|
fh := file.NewHandler(afero.NewMemMapFs())
|
2023-08-08 09:42:06 -04:00
|
|
|
require.NoError(fh.Write(constants.GCPServiceAccountKeyFilename, []byte("{}")))
|
2023-02-24 05:36:41 -05:00
|
|
|
return fh
|
|
|
|
}
|
|
|
|
newFsMissing := func() file.Handler {
|
|
|
|
fh := file.NewHandler(afero.NewMemMapFs())
|
|
|
|
return fh
|
|
|
|
}
|
|
|
|
newFsWithAdminConf := func() file.Handler {
|
|
|
|
fh := file.NewHandler(afero.NewMemMapFs())
|
|
|
|
require.NoError(fh.Write(constants.AdminConfFilename, []byte("")))
|
|
|
|
return fh
|
|
|
|
}
|
2023-10-09 07:04:29 -04:00
|
|
|
newFsWithStateFile := func() file.Handler {
|
2023-02-24 05:36:41 -05:00
|
|
|
fh := file.NewHandler(afero.NewMemMapFs())
|
2023-10-09 07:04:29 -04:00
|
|
|
require.NoError(fh.Write(constants.StateFilename, []byte("")))
|
2023-02-24 05:36:41 -05:00
|
|
|
return fh
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
|
|
|
iamDestroyer *stubIAMDestroyer
|
|
|
|
fh file.Handler
|
|
|
|
stdin string
|
2023-10-16 09:05:29 -04:00
|
|
|
yesFlag bool
|
2023-02-24 05:36:41 -05:00
|
|
|
wantErr bool
|
|
|
|
wantDestroyCalled bool
|
|
|
|
}{
|
|
|
|
"cluster running admin conf": {
|
|
|
|
fh: newFsWithAdminConf(),
|
|
|
|
iamDestroyer: &stubIAMDestroyer{},
|
2023-10-16 09:05:29 -04:00
|
|
|
yesFlag: false,
|
2023-02-24 05:36:41 -05:00
|
|
|
wantErr: true,
|
|
|
|
},
|
2023-10-09 07:04:29 -04:00
|
|
|
"cluster running cluster state": {
|
|
|
|
fh: newFsWithStateFile(),
|
2023-02-24 05:36:41 -05:00
|
|
|
iamDestroyer: &stubIAMDestroyer{},
|
2023-10-16 09:05:29 -04:00
|
|
|
yesFlag: false,
|
2023-02-24 05:36:41 -05:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"file missing abort": {
|
|
|
|
fh: newFsMissing(),
|
|
|
|
stdin: "n\n",
|
2023-10-16 09:05:29 -04:00
|
|
|
yesFlag: false,
|
2023-02-24 05:36:41 -05:00
|
|
|
iamDestroyer: &stubIAMDestroyer{},
|
|
|
|
},
|
|
|
|
"file missing": {
|
|
|
|
fh: newFsMissing(),
|
|
|
|
stdin: "y\n",
|
2023-10-16 09:05:29 -04:00
|
|
|
yesFlag: false,
|
2023-02-24 05:36:41 -05:00
|
|
|
iamDestroyer: &stubIAMDestroyer{},
|
|
|
|
wantDestroyCalled: true,
|
|
|
|
},
|
|
|
|
"file exists abort": {
|
|
|
|
fh: newFsExists(),
|
|
|
|
stdin: "n\n",
|
2023-10-16 09:05:29 -04:00
|
|
|
yesFlag: false,
|
2023-02-24 05:36:41 -05:00
|
|
|
iamDestroyer: &stubIAMDestroyer{},
|
|
|
|
},
|
|
|
|
"error destroying user": {
|
|
|
|
fh: newFsMissing(),
|
|
|
|
stdin: "y\n",
|
2023-10-16 09:05:29 -04:00
|
|
|
yesFlag: false,
|
2023-02-24 05:36:41 -05:00
|
|
|
iamDestroyer: &stubIAMDestroyer{destroyErr: someError},
|
|
|
|
wantErr: true,
|
|
|
|
wantDestroyCalled: true,
|
|
|
|
},
|
|
|
|
"gcp delete error": {
|
|
|
|
fh: newFsExists(),
|
2023-10-16 09:05:29 -04:00
|
|
|
yesFlag: true,
|
2023-08-04 07:53:51 -04:00
|
|
|
iamDestroyer: &stubIAMDestroyer{getTfStateKeyErr: someError},
|
2023-02-24 05:36:41 -05:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
cmd := newIAMDestroyCmd()
|
|
|
|
cmd.SetOut(&bytes.Buffer{})
|
|
|
|
cmd.SetErr(&bytes.Buffer{})
|
|
|
|
cmd.SetIn(bytes.NewBufferString(tc.stdin))
|
2023-04-14 08:15:07 -04:00
|
|
|
|
2023-10-16 09:05:29 -04:00
|
|
|
c := &destroyCmd{log: logger.NewTest(t), flags: iamDestroyFlags{
|
|
|
|
yes: tc.yesFlag,
|
|
|
|
}}
|
2023-02-24 05:36:41 -05:00
|
|
|
|
|
|
|
err := c.iamDestroy(cmd, &nopSpinner{}, tc.iamDestroyer, tc.fh)
|
|
|
|
|
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
|
|
|
}
|
|
|
|
assert.Equal(tc.wantDestroyCalled, tc.iamDestroyer.destroyCalled)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteGCPServiceAccountKeyFile(t *testing.T) {
|
|
|
|
require := require.New(t)
|
|
|
|
someError := errors.New("failed")
|
|
|
|
|
|
|
|
gcpFile := `
|
|
|
|
{
|
|
|
|
"auth_provider_x509_cert_url": "",
|
|
|
|
"auth_uri": "",
|
|
|
|
"client_email": "",
|
|
|
|
"client_id": "",
|
|
|
|
"client_x509_cert_url": "",
|
|
|
|
"private_key": "",
|
|
|
|
"private_key_id": "",
|
|
|
|
"project_id": "",
|
|
|
|
"token_uri": "",
|
|
|
|
"type": ""
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
newFs := func() file.Handler {
|
|
|
|
fs := file.NewHandler(afero.NewMemMapFs())
|
2023-08-08 09:42:06 -04:00
|
|
|
require.NoError(fs.Write(constants.GCPServiceAccountKeyFilename, []byte(gcpFile)))
|
2023-02-24 05:36:41 -05:00
|
|
|
return fs
|
|
|
|
}
|
|
|
|
newFsInvalidJSON := func() file.Handler {
|
|
|
|
fh := file.NewHandler(afero.NewMemMapFs())
|
2023-08-08 09:42:06 -04:00
|
|
|
require.NoError(fh.Write(constants.GCPServiceAccountKeyFilename, []byte("asdf")))
|
2023-02-24 05:36:41 -05:00
|
|
|
return fh
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
|
|
|
destroyer *stubIAMDestroyer
|
|
|
|
fsHandler file.Handler
|
|
|
|
stdin string
|
|
|
|
wantErr bool
|
|
|
|
wantProceed bool
|
|
|
|
wantGetSaKeyCalled bool
|
|
|
|
}{
|
|
|
|
"invalid gcp json": {
|
|
|
|
destroyer: &stubIAMDestroyer{},
|
|
|
|
fsHandler: newFsInvalidJSON(),
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"error getting key terraform": {
|
2023-08-04 07:53:51 -04:00
|
|
|
destroyer: &stubIAMDestroyer{getTfStateKeyErr: someError},
|
2023-02-24 05:36:41 -05:00
|
|
|
fsHandler: newFs(),
|
|
|
|
wantErr: true,
|
|
|
|
wantGetSaKeyCalled: true,
|
|
|
|
},
|
|
|
|
"keys not same": {
|
|
|
|
destroyer: &stubIAMDestroyer{gcpSaKey: gcpshared.ServiceAccountKey{
|
|
|
|
Type: "somethingelse",
|
|
|
|
}},
|
|
|
|
fsHandler: newFs(),
|
|
|
|
wantGetSaKeyCalled: true,
|
|
|
|
wantProceed: true,
|
|
|
|
},
|
|
|
|
"valid": {
|
|
|
|
destroyer: &stubIAMDestroyer{},
|
|
|
|
fsHandler: newFs(),
|
|
|
|
wantGetSaKeyCalled: true,
|
|
|
|
wantProceed: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
cmd := newIAMDestroyCmd()
|
|
|
|
cmd.SetOut(&bytes.Buffer{})
|
|
|
|
cmd.SetErr(&bytes.Buffer{})
|
|
|
|
cmd.SetIn(bytes.NewBufferString(tc.stdin))
|
|
|
|
|
|
|
|
c := &destroyCmd{log: logger.NewTest(t)}
|
|
|
|
|
2023-08-08 09:42:06 -04:00
|
|
|
proceed, err := c.deleteGCPServiceAccountKeyFile(cmd, tc.destroyer, tc.fsHandler)
|
2023-02-24 05:36:41 -05:00
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(tc.wantProceed, proceed)
|
2023-08-04 07:53:51 -04:00
|
|
|
assert.Equal(tc.wantGetSaKeyCalled, tc.destroyer.getTfStateKeyCalled)
|
2023-02-24 05:36:41 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|