grim/hgkeeper

access: Simplify authroized_keys file loading
access-control
2019-05-04, Wagner Riffel
2a61df813042
Parents f1395402bad4
Children 208e17128bb9
access: Simplify authroized_keys file loading
--- a/access/access.go Sat May 04 03:24:20 2019 -0300
+++ b/access/access.go Sat May 04 04:17:51 2019 -0300
@@ -1,10 +1,10 @@
package access
import (
- "bufio"
"bytes"
"fmt"
"io"
+ "io/ioutil"
"os"
"path/filepath"
"sync"
@@ -236,23 +236,16 @@
// loadKeys reads file trying to parse it as an authorized key
// format, returning the finger prints of all keys found
func loadKeys(file string) ([]string, error) {
- f, err := os.Open(filepath.Join(keysDir, file))
+ f, err := ioutil.ReadFile(filepath.Join(keysDir, file))
if err != nil {
return nil, fmt.Errorf("loadKeys %q: %v", file, err)
}
- defer f.Close()
- keys := make([]string, 0, 20)
- // according to sshd(8) each line of the file contains one key,
- // ignoring empty and lines starting with #
- bio := bufio.NewScanner(f)
- for line := 1; bio.Scan(); line++ {
- key := bio.Text()
- if key == "" || key[0] == '#' {
- continue
- }
- pub, _, _, _, err := ssh.ParseAuthorizedKey([]byte(key))
+ keys := make([]string, 0, 5)
+ for len(f) > 0 {
+ var pub ssh.PublicKey
+ pub, _, _, f, err = ssh.ParseAuthorizedKey(f)
if err != nil {
- log.Errorf("loadKeys %q:%d: %v", file, line, err)
+ log.Errorf("loadKeys %s: %v", file, err)
continue
}
keys = append(keys, ssh.FingerprintSHA256(pub))