fix: make key names deterministic

This commit is contained in:
Jon Lundy 2021-08-26 14:55:02 -06:00
parent 19b0a80f57
commit 59a0e9bb44
No known key found for this signature in database
GPG Key ID: 13022278CED7D8EC
3 changed files with 19 additions and 7 deletions

1
go.mod
View File

@ -7,6 +7,7 @@ require (
github.com/gliderlabs/ssh v0.3.2
github.com/soheilhy/cmux v0.1.5
github.com/tjarratt/babble v0.0.0-20210505082055-cbca2a4833c1
github.com/wolfeidau/humanhash v1.1.0 // indirect
go.uber.org/multierr v1.7.0
golang.org/dl v0.0.0-20210816190658-eea66df5a73d // indirect
golang.org/x/crypto v0.0.0-20210506145944-38f3c27a63bf // indirect

2
go.sum
View File

@ -12,6 +12,8 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tjarratt/babble v0.0.0-20210505082055-cbca2a4833c1 h1:j8whCiEmvLCXI3scVn+YnklCU8mwJ9ZJ4/DGAKqQbRE=
github.com/tjarratt/babble v0.0.0-20210505082055-cbca2a4833c1/go.mod h1:O5hBrCGqzfb+8WyY8ico2AyQau7XQwAfEQeEQ5/5V9E=
github.com/wolfeidau/humanhash v1.1.0 h1:06KgtyyABJGBbrfMONrW7S+b5TTYVyrNB/jss5n7F3E=
github.com/wolfeidau/humanhash v1.1.0/go.mod h1:jkpynR1bfyfkmKEQudIC0osWKynFAoayRjzH9OJdVIg=
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/multierr v1.7.0 h1:zaiO/rmgFjbmCXdSYJWQcdvOCsthmdaHfr3Gm2Kx4Ec=

23
main.go
View File

@ -2,6 +2,7 @@ package main
import (
"context"
"crypto/sha256"
"fmt"
"io/ioutil"
"log"
@ -19,7 +20,7 @@ import (
"github.com/gliderlabs/ssh"
"github.com/soheilhy/cmux"
"github.com/tjarratt/babble"
"github.com/wolfeidau/humanhash"
"go.uber.org/multierr"
)
@ -153,18 +154,20 @@ type user struct {
func (srv *server) AddUser(pubkey ssh.PublicKey) *user {
u := &user{}
u.pubkey = pubkey
babbler := babble.NewBabbler()
u.name = babbler.Babble()
u.name = fingerprintHuman(pubkey)
u.name = strings.ToLower(u.name)
u.name = filterName.ReplaceAllString(u.name, "")
if g, ok := srv.users.LoadOrStore(u.name, u); ok {
u = g.(*user)
return u
}
u.pubkey = pubkey
u.bindPort = srv.nextPort()
u.bindHost = srv.bindHost
srv.users.Store(u.name, u)
return u
}
func (srv *server) nextPort() uint32 {
@ -310,7 +313,7 @@ func (srv *server) handleHTTP(rw http.ResponseWriter, r *http.Request) {
}
if r.Method == http.MethodPost {
pubkey, _, _, _, err := ssh.ParseAuthorizedKey([]byte(r.PostFormValue("pub")))
pubkey, _, _, _, err := ssh.ParseAuthorizedKey([]byte(r.FormValue("pub")))
if err != nil {
rw.WriteHeader(400)
fmt.Fprintln(rw, "ERR READING KEY")
@ -423,3 +426,9 @@ func (m *serverMux) Serve(ctx context.Context) error {
return err
}
}
func fingerprintHuman(pubKey ssh.PublicKey) string {
sha256sum := sha256.Sum256(pubKey.Marshal())
h, _ := humanhash.Humanize(sha256sum[:], 3)
return h
}