Skip to content

Commit

Permalink
Make host key validation configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
afritzler committed Dec 12, 2024
1 parent bd03518 commit 69ec025
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
40 changes: 35 additions & 5 deletions cmd/metalctl/app/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,23 @@ import (
"log"
"net"
"os"
"path/filepath"

"github.com/ironcore-dev/metal-operator/internal/console"
"github.com/spf13/cobra"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/knownhosts"
_ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/config"
)

var (
kubeconfigPath string
kubeconfig string
serialConsoleNumber int
kubeconfigPath string
kubeconfig string
serialConsoleNumber int
skipHostKeyValidation bool
knownHostsFile string
)

func NewConsoleCommand() *cobra.Command {
Expand All @@ -34,6 +38,8 @@ func NewConsoleCommand() *cobra.Command {

consoleCmd.Flags().StringVar(&kubeconfig, "kubeconfig", "", "Path to a kubeconfig.")
consoleCmd.Flags().IntVar(&serialConsoleNumber, "serial-console-number", 1, "Serial console number.")
consoleCmd.Flags().BoolVar(&skipHostKeyValidation, "skip-host-key-validation", false, "Skip host key validation.")
consoleCmd.Flags().StringVar(&knownHostsFile, "known-hosts-file", "~/.ssh/known_hosts", "Path to known_hosts file.")

return consoleCmd
}
Expand Down Expand Up @@ -69,14 +75,27 @@ func openConsoleStream(ctx context.Context, k8sClient client.Client, serverName
return fmt.Errorf("console config is nil")
}

var hostKeyCallback ssh.HostKeyCallback
if skipHostKeyValidation {
hostKeyCallback = ssh.InsecureIgnoreHostKey()
} else {
expandedPath, err := expandPath(knownHostsFile)
if err != nil {
return fmt.Errorf("failed to expand known_hosts file path: %w", err)
}
hostKeyCallback, err = knownhosts.New(expandedPath)
if err != nil {
return fmt.Errorf("failed to parse known_hosts file: %w", err)
}
}

// Create SSH client configuration
sshConfig := &ssh.ClientConfig{
User: consoleConfig.Username,
Auth: []ssh.AuthMethod{
ssh.Password(consoleConfig.Password),
},
// TODO: use proper key verification
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
HostKeyCallback: hostKeyCallback,
}

// Connect to the BMC
Expand Down Expand Up @@ -170,3 +189,14 @@ func createClient() (client.Client, error) {
}
return k8sClient, nil
}

func expandPath(path string) (string, error) {
if len(path) > 0 && path[0] == '~' {
homeDir, err := os.UserHomeDir()
if err != nil {
return "", err
}
return filepath.Join(homeDir, path[1:]), nil
}
return path, nil
}
3 changes: 3 additions & 0 deletions docs/usage/metalctl.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ or set the `KUBECONFIG` environment variable by pointing to an effective `kubeco

By default, the serial console on `ttyS1` will be opened. You can override this by setting `--serial-console-number`.

Additionally, you can skip the host validation by providing the `--skip-host-key-validation=true` flag. If set to `false`
it is possible provide a custom `known_hosts` file via the `--known-hosts-file` flag.

### move

The `metalctl move` command allows to move the metal Custom Resources, like e.g. `Endpoint`, `BMC`, `Server`, etc. from one
Expand Down
2 changes: 1 addition & 1 deletion internal/console/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,5 @@ func GetConfigForServerName(ctx context.Context, c client.Client, serverName str
}, nil
}

return nil, nil
return nil, fmt.Errorf("failed to create configuration for accessing server %q", serverName)
}
2 changes: 1 addition & 1 deletion internal/console/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestControllers(t *testing.T) {
SetDefaultConsistentlyDuration(consistentlyDuration)
RegisterFailHandler(Fail)

RunSpecs(t, "Controller Suite")
RunSpecs(t, "Console Suite")
}

var _ = BeforeSuite(func() {
Expand Down

0 comments on commit 69ec025

Please sign in to comment.