Create authorized_keys file if it doesn't exist

Fixes #53
This commit is contained in:
Anders Pitman
2021-02-15 12:24:54 -07:00
parent 349ec7997f
commit e0ef693e99

View File

@@ -11,6 +11,7 @@ import (
"golang.org/x/crypto/ssh"
"io/ioutil"
"log"
"os"
"os/user"
"strings"
"sync"
@@ -149,7 +150,13 @@ func (m *TunnelManager) addToAuthorizedKeys(domain string, port int, allowExtern
authKeysPath := fmt.Sprintf("%s/.ssh/authorized_keys", m.user.HomeDir)
akBytes, err := ioutil.ReadFile(authKeysPath)
akFile, err := os.OpenFile(authKeysPath, os.O_RDWR|os.O_CREATE, 0600)
if err != nil {
return "", err
}
defer akFile.Close()
akBytes, err := ioutil.ReadAll(akFile)
if err != nil {
return "", err
}
@@ -182,7 +189,7 @@ func (m *TunnelManager) addToAuthorizedKeys(domain string, port int, allowExtern
newAk := fmt.Sprintf("%s%s %s %s\n", akStr, options, pubKey, tunnelId)
err = ioutil.WriteFile(authKeysPath, []byte(newAk), 0600)
_, err = akFile.Write([]byte(newAk))
if err != nil {
return "", err
}