Use the first configured ssh key type for authn

Previously, the authentication key was always generated using rsa. Now
it uses the first configured ssh key type.

Signed-off-by: Jonathon Anderson <janderson@ciq.com>
This commit is contained in:
Jonathon Anderson
2024-04-19 17:26:18 -06:00
parent 21150d2975
commit 7046c6dc8c
4 changed files with 30 additions and 15 deletions

View File

@@ -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

View File

@@ -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