forked from forgejo/forgejo
Add postgres support, clean code, code review
This commit is contained in:
parent
9d3b003add
commit
e51afe4621
19 changed files with 1124 additions and 303 deletions
|
@ -9,6 +9,7 @@ import (
|
|||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
|
@ -20,16 +21,19 @@ import (
|
|||
"github.com/Unknwon/com"
|
||||
)
|
||||
|
||||
var (
|
||||
sshOpLocker = sync.Mutex{}
|
||||
//publicKeyRootPath string
|
||||
sshPath string
|
||||
appPath string
|
||||
const (
|
||||
// "### autogenerated by gitgos, DO NOT EDIT\n"
|
||||
tmplPublicKey = "command=\"%s serv key-%d\",no-port-forwarding," +
|
||||
"no-X11-forwarding,no-agent-forwarding,no-pty %s\n"
|
||||
TPL_PUBLICK_KEY = `command="%s serv key-%d",no-port-forwarding, no-X11-forwarding,no-agent-forwarding,no-pty %s`
|
||||
)
|
||||
|
||||
var (
|
||||
sshOpLocker = sync.Mutex{}
|
||||
|
||||
sshPath string
|
||||
appPath string
|
||||
)
|
||||
|
||||
// exePath returns the executable path.
|
||||
func exePath() (string, error) {
|
||||
file, err := exec.LookPath(os.Args[0])
|
||||
if err != nil {
|
||||
|
@ -38,6 +42,7 @@ func exePath() (string, error) {
|
|||
return filepath.Abs(file)
|
||||
}
|
||||
|
||||
// homeDir returns the home directory of current user.
|
||||
func homeDir() string {
|
||||
home, err := com.HomeDir()
|
||||
if err != nil {
|
||||
|
@ -48,15 +53,22 @@ func homeDir() string {
|
|||
|
||||
func init() {
|
||||
var err error
|
||||
|
||||
appPath, err = exePath()
|
||||
if err != nil {
|
||||
println(err.Error())
|
||||
fmt.Printf("publickey.init(fail to get app path): %v\n", err)
|
||||
os.Exit(2)
|
||||
}
|
||||
|
||||
// Determine and create .ssh path.
|
||||
sshPath = filepath.Join(homeDir(), ".ssh")
|
||||
if err = os.MkdirAll(sshPath, os.ModePerm); err != nil {
|
||||
fmt.Printf("publickey.init(fail to create sshPath(%s)): %v\n", sshPath, err)
|
||||
os.Exit(2)
|
||||
}
|
||||
}
|
||||
|
||||
// PublicKey represents a SSH key of user.
|
||||
type PublicKey struct {
|
||||
Id int64
|
||||
OwnerId int64 `xorm:"index"`
|
||||
|
@ -71,10 +83,12 @@ var (
|
|||
ErrKeyAlreadyExist = errors.New("Public key already exist")
|
||||
)
|
||||
|
||||
// GenAuthorizedKey returns formatted public key string.
|
||||
func GenAuthorizedKey(keyId int64, key string) string {
|
||||
return fmt.Sprintf(tmplPublicKey, appPath, keyId, key)
|
||||
return fmt.Sprintf(TPL_PUBLICK_KEY+"\n", appPath, keyId, key)
|
||||
}
|
||||
|
||||
// AddPublicKey adds new public key to database and SSH key file.
|
||||
func AddPublicKey(key *PublicKey) (err error) {
|
||||
// Check if public key name has been used.
|
||||
has, err := orm.Get(key)
|
||||
|
@ -88,14 +102,9 @@ func AddPublicKey(key *PublicKey) (err error) {
|
|||
tmpPath := filepath.Join(os.TempDir(), fmt.Sprintf("%d", time.Now().Nanosecond()),
|
||||
"id_rsa.pub")
|
||||
os.MkdirAll(path.Dir(tmpPath), os.ModePerm)
|
||||
f, err := os.Create(tmpPath)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if _, err = f.WriteString(key.Content); err != nil {
|
||||
if err = ioutil.WriteFile(tmpPath, []byte(key.Content), os.ModePerm); err != nil {
|
||||
return err
|
||||
}
|
||||
f.Close()
|
||||
stdout, _, err := com.ExecCmd("ssh-keygen", "-l", "-f", tmpPath)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -108,7 +117,6 @@ func AddPublicKey(key *PublicKey) (err error) {
|
|||
if _, err = orm.Insert(key); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = SaveAuthorizedKeyFile(key); err != nil {
|
||||
if _, err2 := orm.Delete(key); err2 != nil {
|
||||
return err2
|
||||
|
@ -121,6 +129,7 @@ func AddPublicKey(key *PublicKey) (err error) {
|
|||
|
||||
// DeletePublicKey deletes SSH key information both in database and authorized_keys file.
|
||||
func DeletePublicKey(key *PublicKey) (err error) {
|
||||
// Delete SSH key in database.
|
||||
has, err := orm.Id(key.Id).Get(key)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -131,6 +140,7 @@ func DeletePublicKey(key *PublicKey) (err error) {
|
|||
return err
|
||||
}
|
||||
|
||||
// Delete SSH key in SSH key file.
|
||||
sshOpLocker.Lock()
|
||||
defer sshOpLocker.Unlock()
|
||||
|
||||
|
@ -182,16 +192,17 @@ func DeletePublicKey(key *PublicKey) (err error) {
|
|||
if err = os.Remove(p); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return os.Rename(tmpP, p)
|
||||
}
|
||||
|
||||
// ListPublicKey returns a list of public keys that user has.
|
||||
func ListPublicKey(userId int64) ([]PublicKey, error) {
|
||||
keys := make([]PublicKey, 0)
|
||||
err := orm.Find(&keys, &PublicKey{OwnerId: userId})
|
||||
return keys, err
|
||||
}
|
||||
|
||||
// SaveAuthorizedKeyFile writes SSH key content to SSH key file.
|
||||
func SaveAuthorizedKeyFile(key *PublicKey) error {
|
||||
sshOpLocker.Lock()
|
||||
defer sshOpLocker.Unlock()
|
||||
|
@ -203,7 +214,6 @@ func SaveAuthorizedKeyFile(key *PublicKey) error {
|
|||
}
|
||||
defer f.Close()
|
||||
|
||||
//os.Chmod(p, 0600)
|
||||
_, err = f.WriteString(GenAuthorizedKey(key.Id, key.Content))
|
||||
return err
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue