|
1 | 1 | package controller |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "encoding/base64" |
| 4 | + "crypto/md5" |
| 5 | + "encoding/hex" |
5 | 6 | "encoding/json" |
6 | 7 | "fmt" |
7 | 8 | "net/http" |
8 | 9 | "os" |
9 | 10 | "strings" |
10 | 11 |
|
11 | 12 | "github.com/deis/builder/pkg/conf" |
| 13 | + "golang.org/x/crypto/ssh" |
12 | 14 | ) |
13 | 15 |
|
14 | 16 | const ( |
@@ -39,9 +41,30 @@ func controllerURLStr(additionalPath ...string) (string, error) { |
39 | 41 | return fmt.Sprintf("http://%s:%s/%s", host, port, strings.Join(additionalPath, "/")), nil |
40 | 42 | } |
41 | 43 |
|
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) |
45 | 68 | if err != nil { |
46 | 69 | return nil, err |
47 | 70 | } |
@@ -75,5 +98,6 @@ func UserInfoFromKey(key string) (*UserInfo, error) { |
75 | 98 | if err := json.NewDecoder(res.Body).Decode(ret); err != nil { |
76 | 99 | return nil, err |
77 | 100 | } |
| 101 | + ret.FingerPrint = fp |
78 | 102 | return ret, nil |
79 | 103 | } |
0 commit comments