-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathssh.go
More file actions
49 lines (41 loc) · 1.43 KB
/
ssh.go
File metadata and controls
49 lines (41 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package ssh
import (
"fmt"
"regexp"
)
var (
pubKeyRegex = regexp.MustCompile("^(ssh-rsa|ssh-dss|ssh-ed25519|ecdsa-[^ ]+) ([^ ]+) ?(.*)")
)
// PubKeyInfo contains the information on an SSH public key
type PubKeyInfo struct {
ID string
Public string
}
// ErrInvalidPubKey is the error returned when an SSH public key is unrecognizable
type ErrInvalidPubKey struct {
pubKey []byte
}
// Error is the error interface implementation
func (e ErrInvalidPubKey) Error() string {
return fmt.Sprintf("invalid SSH public key %s", string(e.pubKey))
}
// ErrUnknownPubKeyID is the error returned when an SSH public key is not the expected format
type ErrUnknownPubKeyID struct {
pubKey []byte
}
func (e ErrUnknownPubKeyID) Error() string {
return fmt.Sprintf("unknown SSH public key ID for %s", string(e.pubKey))
}
// ParsePubKey parses a byte slice representation of an SSH Public Key into an
// SSHPubKeyInfo struct. If it cannot find the key ID from the pubKey byte slice itself,
// it uses backupKeyID instead. Returns an appropriate error if parsing failed.
func ParsePubKey(backupKeyID string, pubKey []byte) (*PubKeyInfo, error) {
if !pubKeyRegex.Match(pubKey) {
return nil, ErrInvalidPubKey{pubKey: pubKey}
}
capture := pubKeyRegex.FindStringSubmatch(string(pubKey))
if len(capture) < 4 || capture[3] == "" {
return &PubKeyInfo{ID: backupKeyID, Public: string(pubKey)}, nil
}
return &PubKeyInfo{ID: capture[3], Public: string(pubKey)}, nil
}