mirror of
https://github.com/edgelesssys/constellation.git
synced 2025-08-01 11:36:10 -04:00
wrote test for ssh subcommand
This commit is contained in:
parent
bee3f6c159
commit
b5fddcd254
3 changed files with 111 additions and 8 deletions
|
@ -145,6 +145,7 @@ go_test(
|
||||||
"maapatch_test.go",
|
"maapatch_test.go",
|
||||||
"recover_test.go",
|
"recover_test.go",
|
||||||
"spinner_test.go",
|
"spinner_test.go",
|
||||||
|
"ssh_test.go",
|
||||||
"status_test.go",
|
"status_test.go",
|
||||||
"terminate_test.go",
|
"terminate_test.go",
|
||||||
"upgradeapply_test.go",
|
"upgradeapply_test.go",
|
||||||
|
|
|
@ -7,6 +7,7 @@ SPDX-License-Identifier: AGPL-3.0-only
|
||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"crypto/ed25519"
|
"crypto/ed25519"
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
@ -52,7 +53,16 @@ func runSSH(cmd *cobra.Command, _ []string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = fh.Stat(constants.TerraformWorkingDir)
|
keyPath, err := cmd.Flags().GetString("key")
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("retrieving path to public key from flags: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return generateKey(cmd.Context(), keyPath, fh, debugLogger)
|
||||||
|
}
|
||||||
|
|
||||||
|
func generateKey(ctx context.Context, keyPath string, fh file.Handler, debugLogger debugLog) error {
|
||||||
|
_, err := fh.Stat(constants.TerraformWorkingDir)
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
return fmt.Errorf("directory %q does not exist", constants.TerraformWorkingDir)
|
return fmt.Errorf("directory %q does not exist", constants.TerraformWorkingDir)
|
||||||
}
|
}
|
||||||
|
@ -67,11 +77,11 @@ func runSSH(cmd *cobra.Command, _ []string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
mastersecretURI := uri.MasterSecret{Key: mastersecret.Key, Salt: mastersecret.Salt}
|
mastersecretURI := uri.MasterSecret{Key: mastersecret.Key, Salt: mastersecret.Salt}
|
||||||
kms, err := setup.KMS(cmd.Context(), uri.NoStoreURI, mastersecretURI.EncodeToURI())
|
kms, err := setup.KMS(ctx, uri.NoStoreURI, mastersecretURI.EncodeToURI())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("setting up KMS: %s", err)
|
return fmt.Errorf("setting up KMS: %s", err)
|
||||||
}
|
}
|
||||||
key, err := kms.GetDEK(cmd.Context(), crypto.DEKPrefix+constants.SSHCAKeySuffix, ed25519.SeedSize)
|
key, err := kms.GetDEK(ctx, crypto.DEKPrefix+constants.SSHCAKeySuffix, ed25519.SeedSize)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("retrieving key from KMS: %s", err)
|
return fmt.Errorf("retrieving key from KMS: %s", err)
|
||||||
}
|
}
|
||||||
|
@ -83,11 +93,6 @@ func runSSH(cmd *cobra.Command, _ []string) error {
|
||||||
|
|
||||||
debugLogger.Debug("SSH CA KEY generated", "public-key", string(ssh.MarshalAuthorizedKey(ca.PublicKey())))
|
debugLogger.Debug("SSH CA KEY generated", "public-key", string(ssh.MarshalAuthorizedKey(ca.PublicKey())))
|
||||||
|
|
||||||
keyPath, err := cmd.Flags().GetString("key")
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("retrieving path to public key from flags: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
keyBuffer, err := fh.Read(keyPath)
|
keyBuffer, err := fh.Read(keyPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("reading public key %q: %s", keyPath, err)
|
return fmt.Errorf("reading public key %q: %s", keyPath, err)
|
||||||
|
|
97
cli/internal/cmd/ssh_test.go
Normal file
97
cli/internal/cmd/ssh_test.go
Normal file
|
@ -0,0 +1,97 @@
|
||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"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 TestSSH(t *testing.T) {
|
||||||
|
require := require.New(t)
|
||||||
|
|
||||||
|
someSSHPubKey := "ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBDA1yYg1PIJNjAGjyuv66r8AJtpfBDFLdp3u9lVwkgbVKv1AzcaeTF/NEw+nhNJOjuCZ61LTPj12LZ8Wy/oSm0A= motte@lolcatghost"
|
||||||
|
someSSHPubKeyPath := "some-key.pub"
|
||||||
|
someMasterSecret := `
|
||||||
|
{
|
||||||
|
"key": "MDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAK",
|
||||||
|
"salt": "MDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAK"
|
||||||
|
}
|
||||||
|
`
|
||||||
|
|
||||||
|
newFsWithDirectory := func() file.Handler {
|
||||||
|
fh := file.NewHandler(afero.NewMemMapFs())
|
||||||
|
require.NoError(fh.MkdirAll(constants.TerraformWorkingDir))
|
||||||
|
return fh
|
||||||
|
}
|
||||||
|
newFsNoDirectory := func() file.Handler {
|
||||||
|
fh := file.NewHandler(afero.NewMemMapFs())
|
||||||
|
return fh
|
||||||
|
}
|
||||||
|
|
||||||
|
testCases := map[string]struct {
|
||||||
|
fh file.Handler
|
||||||
|
pubKey string
|
||||||
|
masterSecret string
|
||||||
|
wantErr bool
|
||||||
|
}{
|
||||||
|
"everything exists": {
|
||||||
|
fh: newFsWithDirectory(),
|
||||||
|
pubKey: someSSHPubKey,
|
||||||
|
masterSecret: someMasterSecret,
|
||||||
|
},
|
||||||
|
"no public key": {
|
||||||
|
fh: newFsWithDirectory(),
|
||||||
|
masterSecret: someMasterSecret,
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
"no master secret": {
|
||||||
|
fh: newFsWithDirectory(),
|
||||||
|
pubKey: someSSHPubKey,
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
"malformatted public key": {
|
||||||
|
fh: newFsWithDirectory(),
|
||||||
|
pubKey: "asdf",
|
||||||
|
masterSecret: someMasterSecret,
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
"malformatted master secret": {
|
||||||
|
fh: newFsWithDirectory(),
|
||||||
|
masterSecret: "asdf",
|
||||||
|
pubKey: someSSHPubKey,
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
"directory does not exist": {
|
||||||
|
fh: newFsNoDirectory(),
|
||||||
|
pubKey: someSSHPubKey,
|
||||||
|
masterSecret: someMasterSecret,
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for name, tc := range testCases {
|
||||||
|
t.Run(name, func(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
|
if tc.pubKey != "" {
|
||||||
|
tc.fh.Write(someSSHPubKeyPath, []byte(tc.pubKey))
|
||||||
|
}
|
||||||
|
if tc.masterSecret != "" {
|
||||||
|
tc.fh.Write(constants.MasterSecretFilename, []byte(tc.masterSecret))
|
||||||
|
}
|
||||||
|
|
||||||
|
err := generateKey(context.Background(), someSSHPubKeyPath, tc.fh, logger.NewTest(t))
|
||||||
|
if tc.wantErr {
|
||||||
|
assert.Error(err)
|
||||||
|
} else {
|
||||||
|
assert.NoError(err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue