diff --git a/internal/pkg/configure/ssh.go b/internal/pkg/configure/ssh.go index 16b3c2d5..4d12c1db 100644 --- a/internal/pkg/configure/ssh.go +++ b/internal/pkg/configure/ssh.go @@ -52,21 +52,26 @@ func SSH(keyTypes ...string) error { } 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") + if len(keyTypes) > 0 { + keyType := keyTypes[0] + fmt.Printf("Setting up: %s\n", authorizedKeys) + privKey := path.Join(homeDir, "/.ssh/id_"+keyType) + pubKey := privKey + ".pub" + err = util.ExecInteractive("ssh-keygen", "-q", "-t", keyType, "-f", privKey, "-C", "", "-N", "") + if err != nil { + return errors.Wrap(err, "Failed to exec ssh-keygen command") + } + err := util.CopyFile(pubKey, authorizedKeys) + if err != nil { + return errors.Wrap(err, fmt.Sprintf("Failed to copy %s to authorized_keys", pubKey)) + } + } else { + fmt.Printf("Skipping authorized_keys: no key types configured\n") } } else { - fmt.Printf("Skipping, authorized_keys already exists: %s\n", authorizedKeys) + fmt.Printf("Skipping authorized_keys: already exists: %s\n", authorizedKeys) } return nil diff --git a/internal/pkg/overlay/datastructure.go b/internal/pkg/overlay/datastructure.go index 43f018a9..947bf4ff 100644 --- a/internal/pkg/overlay/datastructure.go +++ b/internal/pkg/overlay/datastructure.go @@ -31,6 +31,7 @@ type TemplateStruct struct { Ipv6 bool Dhcp warewulfconf.DHCPConf Nfs warewulfconf.NFSConf + Ssh warewulfconf.SSHConf Warewulf warewulfconf.WarewulfConf Tftp warewulfconf.TFTPConf Paths warewulfconf.BuildConfig @@ -60,6 +61,7 @@ func InitStruct(nodeInfo *node.NodeInfo) TemplateStruct { tstruct.Id = nodeInfo.Id.Get() tstruct.Hostname = nodeInfo.Id.Get() tstruct.Nfs = *controller.NFS + tstruct.Ssh = *controller.SSH tstruct.Dhcp = *controller.DHCP tstruct.Tftp = *controller.TFTP tstruct.Paths = *controller.Paths diff --git a/overlays/host/rootfs/etc/profile.d/ssh_setup.csh b/overlays/host/rootfs/etc/profile.d/ssh_setup.csh.ww similarity index 77% rename from overlays/host/rootfs/etc/profile.d/ssh_setup.csh rename to overlays/host/rootfs/etc/profile.d/ssh_setup.csh.ww index 8befc753..0f3f023b 100644 --- a/overlays/host/rootfs/etc/profile.d/ssh_setup.csh +++ b/overlays/host/rootfs/etc/profile.d/ssh_setup.csh.ww @@ -3,12 +3,13 @@ ## Automatically configure SSH keys for a user on C SHell login ## Copy this file to /etc/profile.d along with ssh_setup.sh +{{- if gt (len .Ssh.KeyTypes) 0 }} +{{ $keyType := index .Ssh.KeyTypes 0 }} set _UID=`id -u` - if ( ( $_UID > 500 || $_UID == 0 ) && ( ! -f "$HOME/.ssh/config" && ! -f "$HOME/.ssh/cluster" ) ) then echo "Configuring SSH for cluster access" install -d -m 700 $HOME/.ssh - ssh-keygen -t rsa -f $HOME/.ssh/cluster -N '' -C "Warewulf Cluster key" >& /dev/null + ssh-keygen -t {{ $keyType }} -f $HOME/.ssh/cluster -N '' -C "Warewulf Cluster key" >& /dev/null cat $HOME/.ssh/cluster.pub >>! $HOME/.ssh/authorized_keys chmod 0600 $HOME/.ssh/authorized_keys @@ -20,3 +21,6 @@ if ( ( $_UID > 500 || $_UID == 0 ) && ( ! -f "$HOME/.ssh/config" && ! -f "$HOME/ echo " StrictHostKeyChecking=no" >> $HOME/.ssh/config chmod 0600 $HOME/.ssh/config endif +{{- else }} +# No ssh key types configured +{{- end }} diff --git a/overlays/host/rootfs/etc/profile.d/ssh_setup.sh b/overlays/host/rootfs/etc/profile.d/ssh_setup.sh.ww similarity index 81% rename from overlays/host/rootfs/etc/profile.d/ssh_setup.sh rename to overlays/host/rootfs/etc/profile.d/ssh_setup.sh.ww index daa1dfb8..3ab1fffc 100644 --- a/overlays/host/rootfs/etc/profile.d/ssh_setup.sh +++ b/overlays/host/rootfs/etc/profile.d/ssh_setup.sh.ww @@ -11,12 +11,13 @@ ## Automatically configure SSH keys for a user on login ## Copy this file to /etc/profile.d +{{- if gt (len .Ssh.KeyTypes) 0 }} +{{ $keyType := index .Ssh.KeyTypes 0 }} _UID=`id -u` - if [ $_UID -ge 500 -o $_UID -eq 0 ] && [ ! -f "$HOME/.ssh/config" -a ! -f "$HOME/.ssh/cluster" ]; then echo "Configuring SSH for cluster access" install -d -m 700 $HOME/.ssh - ssh-keygen -t rsa -f $HOME/.ssh/cluster -N '' -C "Warewulf Cluster key" > /dev/null 2>&1 + ssh-keygen -t {{ $keyType }} -f $HOME/.ssh/cluster -N '' -C "Warewulf Cluster key" > /dev/null 2>&1 cat $HOME/.ssh/cluster.pub >> $HOME/.ssh/authorized_keys chmod 0600 $HOME/.ssh/authorized_keys @@ -26,3 +27,6 @@ if [ $_UID -ge 500 -o $_UID -eq 0 ] && [ ! -f "$HOME/.ssh/config" -a ! -f "$HOME echo " StrictHostKeyChecking=no" >> $HOME/.ssh/config chmod 0600 $HOME/.ssh/config fi +{{- else }} +# No ssh key types configured +{{- end }}