Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pkg/cluster/action_apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"path"
"path/filepath"
"strings"

"github.com/MusicDin/kubitect/embed"
"github.com/MusicDin/kubitect/pkg/cluster/event"
Expand Down Expand Up @@ -312,6 +313,13 @@ func (c *Cluster) generateSshKeys() error {
// - Check if the user has provided a custom path to the key pair
pkPath := string(c.NewConfig.Cluster.NodeTemplate.SSH.PrivateKeyPath)
if pkPath != "" {
homeDir, err := os.UserHomeDir()
if err != nil {
return fmt.Errorf("keygen: %v", err)
}

pkPath = strings.Replace(pkPath, "~", homeDir, 1)

kp, err := keygen.ReadKeyPair(path.Dir(pkPath), path.Base(pkPath))
if err != nil {
return err
Expand Down
19 changes: 19 additions & 0 deletions pkg/cluster/action_apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package cluster

import (
"os"
"path"
"testing"
"time"

"github.com/MusicDin/kubitect/pkg/env"
"github.com/MusicDin/kubitect/pkg/models/config"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestToApplyAction(t *testing.T) {
Expand Down Expand Up @@ -66,6 +68,23 @@ func TestGenerateMissingKeys_PKPathProvided(t *testing.T) {
assert.NoError(t, c.generateSshKeys())
}

func TestGenerateMissingKeys_PKPathProvidedWithTilde(t *testing.T) {
c := MockCluster(t)

oldHome := os.Getenv("HOME")
defer func() { require.NoError(t, os.Setenv("HOME", oldHome)) }()

homeDir := t.TempDir()
keyDir := path.Join(homeDir, ".ssh")
require.NoError(t, os.MkdirAll(keyDir, 0755))
require.NoError(t, os.WriteFile(path.Join(keyDir, "id_rsa"), []byte("private"), 0600))
require.NoError(t, os.WriteFile(path.Join(keyDir, "id_rsa.pub"), []byte("public"), 0600))
require.NoError(t, os.Setenv("HOME", homeDir))

c.NewConfig.Cluster.NodeTemplate.SSH.PrivateKeyPath = "~/.ssh/id_rsa"
assert.NoError(t, c.generateSshKeys())
}

func TestPrepare(t *testing.T) {
c := MockLocalCluster(t)
assert.NoError(t, c.prepare())
Expand Down