-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig.go
54 lines (42 loc) · 1015 Bytes
/
config.go
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
50
51
52
53
54
package ssh
import (
"crypto/ed25519"
"os"
"golang.org/x/crypto/ssh"
)
// IsEnabled for ssh.
func IsEnabled(cfg *Config) bool {
return cfg != nil
}
// Config for ssh.
type Config struct {
Public string `yaml:"public,omitempty" json:"public,omitempty" toml:"public,omitempty"`
Private string `yaml:"private,omitempty" json:"private,omitempty" toml:"private,omitempty"`
}
// PublicKey ssh.
func (c *Config) PublicKey() (ed25519.PublicKey, error) {
d, err := os.ReadFile(c.Public)
if err != nil {
return nil, err
}
//nolint:dogsled
parsed, _, _, _, err := ssh.ParseAuthorizedKey(d)
if err != nil {
return nil, err
}
key := parsed.(ssh.CryptoPublicKey)
return key.CryptoPublicKey().(ed25519.PublicKey), nil
}
// PrivateKey ssh.
func (c *Config) PrivateKey() (ed25519.PrivateKey, error) {
d, err := os.ReadFile(c.Private)
if err != nil {
return nil, err
}
key, err := ssh.ParseRawPrivateKey(d)
if err != nil {
return nil, err
}
k := key.(*ed25519.PrivateKey)
return *k, nil
}