Skip to content

Commit 87f49a2

Browse files
author
Aaron Schlesinger
committed
ref(pkg/controller/utils.go,pkg/sshd/sshd.go): send public key fingerprint to controller
Instead of encoded public key
1 parent 3daf28d commit 87f49a2

2 files changed

Lines changed: 30 additions & 9 deletions

File tree

pkg/controller/utils.go

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
package controller
22

33
import (
4-
"encoding/base64"
4+
"crypto/md5"
5+
"encoding/hex"
56
"encoding/json"
67
"fmt"
78
"net/http"
89
"os"
910
"strings"
1011

1112
"github.com/deis/builder/pkg/conf"
13+
"golang.org/x/crypto/ssh"
1214
)
1315

1416
const (
@@ -39,9 +41,30 @@ func controllerURLStr(additionalPath ...string) (string, error) {
3941
return fmt.Sprintf("http://%s:%s/%s", host, port, strings.Join(additionalPath, "/")), nil
4042
}
4143

42-
func UserInfoFromKey(key string) (*UserInfo, error) {
43-
keyB64 := base64.RawURLEncoding.EncodeToString([]byte(key))
44-
url, err := controllerURLStr("v2", "hooks", "key", keyB64)
44+
// fingerprint generates a colon-separated fingerprint string from a public key.
45+
func fingerprint(key ssh.PublicKey) string {
46+
hash := md5.Sum(key.Marshal())
47+
buf := make([]byte, hex.EncodedLen(len(hash)))
48+
hex.Encode(buf, hash[:])
49+
// We need this in colon notation:
50+
fp := make([]byte, len(buf)+15)
51+
52+
i, j := 0, 0
53+
for ; i < len(buf); i++ {
54+
if i > 0 && i%2 == 0 {
55+
fp[j] = ':'
56+
j++
57+
}
58+
fp[j] = buf[i]
59+
j++
60+
}
61+
return string(fp)
62+
}
63+
64+
// UserInfoFromKey makes a request to the controller to get the user info from they given key
65+
func UserInfoFromKey(key ssh.PublicKey) (*UserInfo, error) {
66+
fp := fingerprint(key)
67+
url, err := controllerURLStr("v2", "hooks", "key", fp)
4568
if err != nil {
4669
return nil, err
4770
}
@@ -75,5 +98,6 @@ func UserInfoFromKey(key string) (*UserInfo, error) {
7598
if err := json.NewDecoder(res.Body).Decode(ret); err != nil {
7699
return nil, err
77100
}
101+
ret.FingerPrint = fp
78102
return ret, nil
79103
}

pkg/sshd/sshd.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,12 @@ func ParseHostKeys(c cookoo.Context, p *cookoo.Params) (interface{}, cookoo.Inte
7171
func AuthKey(c cookoo.Context, p *cookoo.Params) (interface{}, cookoo.Interrupt) {
7272
log.Debugf(c, "Starting ssh authentication")
7373
key := p.Get("key", nil).(ssh.PublicKey)
74-
75-
strKey := string(ssh.MarshalAuthorizedKey(key))
76-
log.Debugf(c, "Checking auth for user key %v", strKey)
77-
userInfo, err := controller.UserInfoFromKey(strKey)
74+
userInfo, err := controller.UserInfoFromKey(key)
7875
if err != nil {
7976
return nil, err
8077
}
8178

82-
userInfo.Key = strKey
79+
userInfo.Key = string(ssh.MarshalAuthorizedKey(key))
8380
c.Put("userinfo", userInfo)
8481

8582
log.Infof(c, "Key accepted for user %s.", userInfo.Username)

0 commit comments

Comments
 (0)