Skip to content

Commit ee0f542

Browse files
helltsteiler
andcommitted
use slice pointers
Co-authored-by: steiler <github@vahlenkamp.net>
1 parent 04b47c2 commit ee0f542

File tree

1 file changed

+19
-34
lines changed

1 file changed

+19
-34
lines changed

nodes/vr_sros/sshKey.go

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,16 @@ import (
1818
//go:embed ssh_keys.go.tpl
1919
var SROSSSHKeysTemplate string
2020

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) {
2728
for _, k := range s.sshPubKeys {
28-
if _, ok := supportedSSHKeyAlgos[k.Type()]; !ok {
29+
sshKeys, ok := supportedSSHKeyAlgos[k.Type()]
30+
if !ok {
2931
log.Debugf("unsupported SSH Key Algo %q, skipping key", k.Type())
3032
continue
3133
}
@@ -34,27 +36,8 @@ func (s *vrSROS) filterSSHPubKeys(supportedSSHKeyAlgos map[string]struct{}) (rsa
3436
// <keytype> <key> <comment>
3537
keyFields := strings.Fields(string(ssh.MarshalAuthorizedKey(k)))
3638

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])
4340
}
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
5841
}
5942

6043
// SROSTemplateData holds ssh keys for template generation.
@@ -70,15 +53,17 @@ func (s *vrSROS) configureSSHPublicKeys(
7053
username, password string, pubKeys []ssh.PublicKey) error {
7154
tplData := SROSTemplateData{}
7255

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,
7964
}
8065

81-
tplData.SSHPubKeysRSA, tplData.SSHPubKeysECDSA = s.filterSSHPubKeys(supportedSSHKeyAlgos)
66+
s.mapSSHPubKeys(supportedSSHKeyAlgos)
8267

8368
t, err := template.New("SSHKeys").Funcs(
8469
gomplate.CreateFuncs(context.Background(), new(data.Data))).Parse(SROSSSHKeysTemplate)

0 commit comments

Comments
 (0)