Skip to content

Commit

Permalink
use cobra.Command
Browse files Browse the repository at this point in the history
  • Loading branch information
miampf committed Jan 16, 2025
1 parent 57da20b commit a72d7b4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
11 changes: 5 additions & 6 deletions cli/internal/cmd/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ SPDX-License-Identifier: AGPL-3.0-only
package cmd

import (
"context"
"crypto/ed25519"
"crypto/rand"
"fmt"
Expand Down Expand Up @@ -51,10 +50,10 @@ func runSSH(cmd *cobra.Command, _ []string) error {
return fmt.Errorf("retrieving path to public key from flags: %s", err)
}

return generateKey(cmd.Context(), keyPath, fh, debugLogger)
return generateKey(cmd, keyPath, fh, debugLogger)
}

func generateKey(ctx context.Context, keyPath string, fh file.Handler, debugLogger debugLog) error {
func generateKey(cmd *cobra.Command, keyPath string, fh file.Handler, debugLogger debugLog) error {
_, err := fh.Stat(constants.TerraformWorkingDir)
if os.IsNotExist(err) {
return fmt.Errorf("directory %q does not exist", constants.TerraformWorkingDir)
Expand All @@ -70,11 +69,11 @@ func generateKey(ctx context.Context, keyPath string, fh file.Handler, debugLogg
}

mastersecretURI := uri.MasterSecret{Key: mastersecret.Key, Salt: mastersecret.Salt}
kms, err := setup.KMS(ctx, uri.NoStoreURI, mastersecretURI.EncodeToURI())
kms, err := setup.KMS(cmd.Context(), uri.NoStoreURI, mastersecretURI.EncodeToURI())
if err != nil {
return fmt.Errorf("setting up KMS: %s", err)
}
sshCAKeySeed, err := kms.GetDEK(ctx, crypto.DEKPrefix+constants.SSHCAKeySuffix, ed25519.SeedSize)
sshCAKeySeed, err := kms.GetDEK(cmd.Context(), crypto.DEKPrefix+constants.SSHCAKeySuffix, ed25519.SeedSize)
if err != nil {
return fmt.Errorf("retrieving key from KMS: %s", err)
}
Expand Down Expand Up @@ -117,7 +116,7 @@ func generateKey(ctx context.Context, keyPath string, fh file.Handler, debugLogg
if err := fh.Write(fmt.Sprintf("%s/ca_cert.pub", constants.TerraformWorkingDir), ssh.MarshalAuthorizedKey(&certificate), file.OptOverwrite); err != nil {
return fmt.Errorf("writing certificate: %s", err)
}
fmt.Printf("You can now connect to a node using 'ssh -F %s/ssh_config -i <your private key> <node ip>'.\nYou can obtain the private node IP via the web UI of your CSP.\n", constants.TerraformWorkingDir)
cmd.Printf("You can now connect to a node using 'ssh -F %s/ssh_config -i <your private key> <node ip>'.\nYou can obtain the private node IP via the web UI of your CSP.\n", constants.TerraformWorkingDir)

return nil
}
9 changes: 7 additions & 2 deletions cli/internal/cmd/ssh_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cmd

import (
"context"
"bytes"
"fmt"
"testing"

Expand Down Expand Up @@ -88,7 +88,12 @@ func TestSSH(t *testing.T) {
require.NoError(tc.fh.Write(constants.MasterSecretFilename, []byte(tc.masterSecret)))
}

err := generateKey(context.Background(), someSSHPubKeyPath, tc.fh, logger.NewTest(t))
cmd := NewSSHCmd()
cmd.SetOut(&bytes.Buffer{})
cmd.SetErr(&bytes.Buffer{})
cmd.SetIn(&bytes.Buffer{})

err := generateKey(cmd, someSSHPubKeyPath, tc.fh, logger.NewTest(t))
if tc.wantErr {
assert.Error(err)
} else {
Expand Down

0 comments on commit a72d7b4

Please sign in to comment.