@@ -18,14 +18,16 @@ import (
18
18
//go:embed ssh_keys.go.tpl
19
19
var SROSSSHKeysTemplate string
20
20
21
- // filterSSHPubKeys returns rsa and ecdsa keys from the list of ssh keys stored at s.sshPubKeys.
22
- // Since SR OS supports only certain key types, we need to filter out the rest.
23
- func (s * vrSROS ) filterSSHPubKeys (supportedSSHKeyAlgos map [string ]struct {}) (rsaKeys []string , ecdsaKeys []string ) {
24
- rsaKeys = make ([]string , 0 , len (s .sshPubKeys ))
25
- ecdsaKeys = make ([]string , 0 , len (s .sshPubKeys ))
26
-
21
+ // mapSSHPubKeys goes over s.sshPubKeys and puts the supported keys to the corresponding
22
+ // slices associated with the supported SSH key algorithms.
23
+ // supportedSSHKeyAlgos key is a SSH key algorithm and the value is a pointer to the slice
24
+ // that is used to store the keys of the corresponding algorithm family.
25
+ // Two slices are used to store RSA and ECDSA keys separately.
26
+ // The slices are modified in place by reference, so no return values are needed.
27
+ func (s * vrSROS ) mapSSHPubKeys (supportedSSHKeyAlgos map [string ]* []string ) {
27
28
for _ , k := range s .sshPubKeys {
28
- if _ , ok := supportedSSHKeyAlgos [k .Type ()]; ! ok {
29
+ sshKeys , ok := supportedSSHKeyAlgos [k .Type ()]
30
+ if ! ok {
29
31
log .Debugf ("unsupported SSH Key Algo %q, skipping key" , k .Type ())
30
32
continue
31
33
}
@@ -34,27 +36,8 @@ func (s *vrSROS) filterSSHPubKeys(supportedSSHKeyAlgos map[string]struct{}) (rsa
34
36
// <keytype> <key> <comment>
35
37
keyFields := strings .Fields (string (ssh .MarshalAuthorizedKey (k )))
36
38
37
- switch {
38
- case isRSAKey (k ):
39
- rsaKeys = append (rsaKeys , keyFields [1 ])
40
- case isECDSAKey (k ):
41
- ecdsaKeys = append (ecdsaKeys , keyFields [1 ])
42
- }
39
+ * sshKeys = append (* sshKeys , keyFields [1 ])
43
40
}
44
-
45
- return rsaKeys , ecdsaKeys
46
- }
47
-
48
- func isRSAKey (key ssh.PublicKey ) bool {
49
- return key .Type () == ssh .KeyAlgoRSA
50
- }
51
-
52
- func isECDSAKey (key ssh.PublicKey ) bool {
53
- kType := key .Type ()
54
-
55
- return kType == ssh .KeyAlgoECDSA521 ||
56
- kType == ssh .KeyAlgoECDSA384 ||
57
- kType == ssh .KeyAlgoECDSA256
58
41
}
59
42
60
43
// SROSTemplateData holds ssh keys for template generation.
@@ -70,15 +53,17 @@ func (s *vrSROS) configureSSHPublicKeys(
70
53
username , password string , pubKeys []ssh.PublicKey ) error {
71
54
tplData := SROSTemplateData {}
72
55
73
- // a map of supported SSH key algorithms
74
- supportedSSHKeyAlgos := map [string ]struct {}{
75
- ssh .KeyAlgoRSA : {},
76
- ssh .KeyAlgoECDSA521 : {},
77
- ssh .KeyAlgoECDSA384 : {},
78
- ssh .KeyAlgoECDSA256 : {},
56
+ // a map of supported SSH key algorithms and the template slices
57
+ // the keys should be added to.
58
+ // In mapSSHPubKeys we map supported SSH key algorithms to the template slices.
59
+ supportedSSHKeyAlgos := map [string ]* []string {
60
+ ssh .KeyAlgoRSA : & tplData .SSHPubKeysRSA ,
61
+ ssh .KeyAlgoECDSA521 : & tplData .SSHPubKeysECDSA ,
62
+ ssh .KeyAlgoECDSA384 : & tplData .SSHPubKeysECDSA ,
63
+ ssh .KeyAlgoECDSA256 : & tplData .SSHPubKeysECDSA ,
79
64
}
80
65
81
- tplData . SSHPubKeysRSA , tplData . SSHPubKeysECDSA = s . filterSSHPubKeys (supportedSSHKeyAlgos )
66
+ s . mapSSHPubKeys (supportedSSHKeyAlgos )
82
67
83
68
t , err := template .New ("SSHKeys" ).Funcs (
84
69
gomplate .CreateFuncs (context .Background (), new (data.Data ))).Parse (SROSSSHKeysTemplate )
0 commit comments