Skip to content

Commit

Permalink
Fix panic when no keys present for srl node (#1604)
Browse files Browse the repository at this point in the history
* fix keys catenation for 0.0 builds when no keys present

* added rn
  • Loading branch information
hellt committed Sep 21, 2023
1 parent 0259300 commit 9197213
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
6 changes: 6 additions & 0 deletions docs/rn/0.45.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,9 @@ Now @steiler enhanced this functionality by making sure that if a user provides
* #1571 - ansible proxy variable is added to containerlab produced ansible inventory
* #1592 - ca cert and node certs now have a default country code set to US
* #1583 - default link MTU is now fixed to be 9500 again

## Patches

### 0.45.1

* fix panic when no keys are present for srlinux node
2 changes: 1 addition & 1 deletion nodes/srl/srl.go
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ func (n *srl) addDefaultConfig(ctx context.Context) error {

// in srlinux >= v23.10+ linuxadmin and admin user ssh keys can only be configured via the cli
// so we add the keys to the template data for rendering.
if semver.Compare(n.swVersion.String(), "v23.10") >= 0 || n.swVersion.major == "0" {
if len(n.sshPubKeys) > 0 && (semver.Compare(n.swVersion.String(), "v23.10") >= 0 || n.swVersion.major == "0") {
tplData.SSHPubKeys = catenateKeys(n.sshPubKeys)
}

Expand Down
22 changes: 16 additions & 6 deletions nodes/srl/sshkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package srl

import (
"bytes"
"fmt"
"strings"

"golang.org/x/crypto/ssh"
Expand All @@ -14,19 +13,30 @@ import (
// for users.
func catenateKeys(in []ssh.PublicKey) string {
var keys strings.Builder
// pre-allocate the string builder capacity
keys.Grow(len(in) * 100)
// iterate through keys
for _, k := range in {
for i, k := range in {
// extract the keys in AuthorizedKeys format (e.g. "ssh-rsa <KEY>")
ks := bytes.TrimSpace(ssh.MarshalAuthorizedKey(k))
// add a seperator, leading quote, the key string and trailing quote
fmt.Fprintf(&keys, " \"%s\"", ks)
// add a separator, leading quote, the key string and trailing quote
if i > 0 {
keys.WriteByte(' ')
}
keys.WriteByte('"')
keys.Write(ks)
keys.WriteByte('"')
}
// return all but the first leading seperator of the string builders content as string
return keys.String()[1:]
// return the string builders content as string
return keys.String()
}

// filterSSHPubKeys removes non-rsa keys from n.sshPubKeys until srl adds support for them.
func (n *srl) filterSSHPubKeys() {
if len(n.sshPubKeys) == 0 {
return
}

var filteredKeys []ssh.PublicKey

for _, k := range n.sshPubKeys {
Expand Down

0 comments on commit 9197213

Please sign in to comment.