Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ssh: Add output flag #258

Merged
merged 10 commits into from
Mar 29, 2023
Prev Previous commit
Next Next commit
PR feedback - use PrivateKeyFile type
  • Loading branch information
petersutter committed Mar 29, 2023
commit cadff5a303525c01b744de557573d6e4fb540c73
14 changes: 7 additions & 7 deletions pkg/cmd/ssh/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,15 +498,15 @@ func (o *SSHOptions) Run(f util.Factory) error {
}

// save the keys into temporary files that we try to clean up when exiting
nodePrivateKeyFiles := []string{}
var nodePrivateKeyFiles []PrivateKeyFile

for _, pk := range nodePrivateKeys {
filename, err := writeToTemporaryFile(pk)
if err != nil {
return err
}

nodePrivateKeyFiles = append(nodePrivateKeyFiles, filename)
nodePrivateKeyFiles = append(nodePrivateKeyFiles, PrivateKeyFile(filename))
}

shootClient, err := manager.ShootClient(ctx, sshTarget)
Expand Down Expand Up @@ -678,7 +678,7 @@ func printTargetInformation(logger klog.Logger, t target.Target) {
logger.Info("Preparing SSH access", "target", target, "garden", t.GardenName())
}

func cleanup(ctx context.Context, o *SSHOptions, gardenClient client.Client, bastion *operationsv1alpha1.Bastion, nodePrivateKeyFiles []string) {
func cleanup(ctx context.Context, o *SSHOptions, gardenClient client.Client, bastion *operationsv1alpha1.Bastion, nodePrivateKeyFiles []PrivateKeyFile) {
logger := klog.FromContext(ctx)

if !o.KeepBastion {
Expand All @@ -702,7 +702,7 @@ func cleanup(ctx context.Context, o *SSHOptions, gardenClient client.Client, bas
// these files remaining, the user would not be able to use the SSH
// command we provided to connect to the shoot nodes
for _, filename := range nodePrivateKeyFiles {
if err := os.Remove(filename); err != nil {
if err := os.Remove(filename.String()); err != nil {
logger.Error(err, "Failed to delete node private key", "path", filename)
}
}
Expand Down Expand Up @@ -831,7 +831,7 @@ func getShootNode(ctx context.Context, o *SSHOptions, shootClient client.Client)
return node, nil
}

func remoteShell(ctx context.Context, o *SSHOptions, bastion *operationsv1alpha1.Bastion, nodeHostname string, nodePrivateKeyFiles []string) error {
func remoteShell(ctx context.Context, o *SSHOptions, bastion *operationsv1alpha1.Bastion, nodeHostname string, nodePrivateKeyFiles []PrivateKeyFile) error {
bastionAddr := preferredBastionAddress(bastion)
connectCmd := sshCommandLine(o.SSHPrivateKeyFile, bastionAddr, nodePrivateKeyFiles, nodeHostname)

Expand Down Expand Up @@ -859,15 +859,15 @@ func remoteShell(ctx context.Context, o *SSHOptions, bastion *operationsv1alpha1
}

for _, file := range nodePrivateKeyFiles {
args = append(args, "-i", file)
args = append(args, "-i", file.String())
}

args = append(args, fmt.Sprintf("%s@%s", SSHNodeUsername, nodeHostname))

return execCommand(ctx, "ssh", args, o)
}

func sshCommandLine(sshPrivateKeyFile PrivateKeyFile, bastionAddr string, nodePrivateKeyFiles []string, nodeName string) string {
func sshCommandLine(sshPrivateKeyFile PrivateKeyFile, bastionAddr string, nodePrivateKeyFiles []PrivateKeyFile, nodeName string) string {
proxyPrivateKeyFlag := ""
if sshPrivateKeyFile != "" {
proxyPrivateKeyFlag = fmt.Sprintf(" -o IdentitiesOnly=yes -i %s", sshPrivateKeyFile)
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/ssh/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ type ConnectInformation struct {
NodeHostname string `json:"nodeHostname,omitempty"`

// NodePrivateKeyFiles is a list of file paths containing the private SSH keys for the worker nodes.
NodePrivateKeyFiles []string `json:"nodePrivateKeyFiles"`
NodePrivateKeyFiles []PrivateKeyFile `json:"nodePrivateKeyFiles"`

// Nodes is a list of Node objects containing information about the worker nodes.
Nodes []Node `json:"nodes"`
Expand Down Expand Up @@ -100,7 +100,7 @@ type Address struct {

var _ fmt.Stringer = &Address{}

func NewConnectInformation(bastion *operationsv1alpha1.Bastion, nodeHostname string, sshPublicKeyFile PublicKeyFile, sshPrivateKeyFile PrivateKeyFile, nodePrivateKeyFiles []string, nodes []corev1.Node) (*ConnectInformation, error) {
func NewConnectInformation(bastion *operationsv1alpha1.Bastion, nodeHostname string, sshPublicKeyFile PublicKeyFile, sshPrivateKeyFile PrivateKeyFile, nodePrivateKeyFiles []PrivateKeyFile, nodes []corev1.Node) (*ConnectInformation, error) {
var nodeList []Node

for _, node := range nodes {
Expand Down