Revert "Move configuration code out of wwctl"
This commit is contained in:
committed by
GitHub
parent
5a7f99af18
commit
67ee3bf5f8
@@ -1,10 +1,99 @@
|
||||
package ssh
|
||||
|
||||
import (
|
||||
"github.com/hpcng/warewulf/internal/pkg/configure"
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
|
||||
"github.com/hpcng/warewulf/internal/pkg/buildconfig"
|
||||
"github.com/hpcng/warewulf/internal/pkg/util"
|
||||
"github.com/hpcng/warewulf/internal/pkg/wwlog"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func CobraRunE(cmd *cobra.Command, args []string) error {
|
||||
return configure.Configure("SSH", setShow)
|
||||
return Configure(SetShow)
|
||||
}
|
||||
|
||||
func Configure(show bool) error {
|
||||
if os.Getuid() == 0 {
|
||||
fmt.Printf("Updating system keys\n")
|
||||
|
||||
wwkeydir := path.Join(buildconfig.SYSCONFDIR(), "warewulf/keys") + "/"
|
||||
|
||||
err := os.MkdirAll(path.Join(buildconfig.SYSCONFDIR(), "warewulf/keys"), 0755)
|
||||
if err != nil {
|
||||
wwlog.Printf(wwlog.ERROR, "Could not create base directory: %s\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if !util.IsFile(wwkeydir + "ssh_host_rsa_key") {
|
||||
fmt.Printf("Setting up key: ssh_host_rsa_key\n")
|
||||
err = util.ExecInteractive("ssh-keygen", "-q", "-t", "rsa", "-f", wwkeydir+"ssh_host_rsa_key", "-C", "", "-N", "")
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to exec ssh-keygen command")
|
||||
}
|
||||
} else {
|
||||
fmt.Printf("Skipping, key already exists: ssh_host_rsa_key\n")
|
||||
}
|
||||
|
||||
if !util.IsFile(wwkeydir + "ssh_host_dsa_key") {
|
||||
fmt.Printf("Setting up key: ssh_host_dsa_key\n")
|
||||
err = util.ExecInteractive("ssh-keygen", "-q", "-t", "dsa", "-f", wwkeydir+"ssh_host_dsa_key", "-C", "", "-N", "")
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to exec ssh-keygen command")
|
||||
}
|
||||
} else {
|
||||
fmt.Printf("Skipping, key already exists: ssh_host_dsa_key\n")
|
||||
}
|
||||
|
||||
if !util.IsFile(wwkeydir + "ssh_host_ecdsa_key") {
|
||||
fmt.Printf("Setting up key: ssh_host_ecdsa_key\n")
|
||||
err = util.ExecInteractive("ssh-keygen", "-q", "-t", "ecdsa", "-f", wwkeydir+"ssh_host_ecdsa_key", "-C", "", "-N", "")
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to exec ssh-keygen command")
|
||||
}
|
||||
} else {
|
||||
fmt.Printf("Skipping, key already exists: ssh_host_ecdsa_key\n")
|
||||
}
|
||||
|
||||
if !util.IsFile(wwkeydir + "ssh_host_ed25519_key") {
|
||||
fmt.Printf("Setting up key: ssh_host_ed25519_key\n")
|
||||
err = util.ExecInteractive("ssh-keygen", "-q", "-t", "ed25519", "-f", wwkeydir+"ssh_host_ed25519_key", "-C", "", "-N", "")
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to exec ssh-keygen command")
|
||||
}
|
||||
} else {
|
||||
fmt.Printf("Skipping, key already exists: ssh_host_ed25519_key\n")
|
||||
}
|
||||
} else {
|
||||
fmt.Printf("Updating user's keys\n")
|
||||
}
|
||||
|
||||
homeDir, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
wwlog.Printf(wwlog.ERROR, "Could not obtain the user's home directory: %s\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
authorizedKeys := path.Join(homeDir, "/.ssh/authorized_keys")
|
||||
rsaPriv := path.Join(homeDir, "/.ssh/id_rsa")
|
||||
rsaPub := path.Join(homeDir, "/.ssh/id_rsa.pub")
|
||||
|
||||
if !util.IsFile(authorizedKeys) {
|
||||
fmt.Printf("Setting up: %s\n", authorizedKeys)
|
||||
err = util.ExecInteractive("ssh-keygen", "-q", "-t", "rsa", "-f", rsaPriv, "-C", "", "-N", "")
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to exec ssh-keygen command")
|
||||
}
|
||||
err := util.CopyFile(rsaPub, authorizedKeys)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to copy keys")
|
||||
}
|
||||
} else {
|
||||
fmt.Printf("Skipping, authorized_keys already exists: %s\n", authorizedKeys)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -5,18 +5,18 @@ import "github.com/spf13/cobra"
|
||||
var (
|
||||
baseCmd = &cobra.Command{
|
||||
DisableFlagsInUseLine: true,
|
||||
Use: "ssh [OPTIONS]",
|
||||
Short: "Manage and initialize SSH",
|
||||
Use: "ssh [OPTIONS]",
|
||||
Short: "Manage and initialize SSH",
|
||||
Long: "SSH is an optionally dependent service for Warewulf, this tool will automatically\n" +
|
||||
"setup the ssh keys nodes using the 'default' system overlay as well as user owned\n" +
|
||||
"keys.",
|
||||
RunE: CobraRunE,
|
||||
}
|
||||
setShow bool
|
||||
SetShow bool
|
||||
)
|
||||
|
||||
func init() {
|
||||
baseCmd.PersistentFlags().BoolVarP(&setShow, "show", "s", false, "Show configuration (don't update)")
|
||||
baseCmd.PersistentFlags().BoolVarP(&SetShow, "show", "s", false, "Show configuration (don't update)")
|
||||
}
|
||||
|
||||
// GetRootCommand returns the root cobra.Command for the application.
|
||||
|
||||
Reference in New Issue
Block a user