-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added SSH key provisioning for SR OS (#1706)
* added key provisioning for sros Co-authored-by: Mathis Bramkamp <[email protected]> * use O(n) when filtering pub keys * use slice pointers Co-authored-by: steiler <[email protected]> * additional log message to indicate booting period * added main kind names concept and tune ssh config for sros nodes * Introduce KindSpecifics * use ssh config struct instead of kindSpecifics KindSpecifics seems to not work when we will have ssh config specific to a particular node version. So it is a bit over generalizing * remove main kind and nokia_sros kind name for now. will be addressed in a separate PR --------- Co-authored-by: Mathis Bramkamp <[email protected]> Co-authored-by: steiler <[email protected]>
- Loading branch information
1 parent
7c84208
commit 4e4748c
Showing
9 changed files
with
189 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package vr_sros | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
_ "embed" | ||
"strings" | ||
"text/template" | ||
|
||
"github.com/hairyhenderson/gomplate/v3" | ||
"github.com/hairyhenderson/gomplate/v3/data" | ||
log "github.com/sirupsen/logrus" | ||
"golang.org/x/crypto/ssh" | ||
) | ||
|
||
// importing Default Config template at compile time | ||
// | ||
//go:embed ssh_keys.go.tpl | ||
var SROSSSHKeysTemplate string | ||
|
||
// mapSSHPubKeys goes over s.sshPubKeys and puts the supported keys to the corresponding | ||
// slices associated with the supported SSH key algorithms. | ||
// supportedSSHKeyAlgos key is a SSH key algorithm and the value is a pointer to the slice | ||
// that is used to store the keys of the corresponding algorithm family. | ||
// Two slices are used to store RSA and ECDSA keys separately. | ||
// The slices are modified in place by reference, so no return values are needed. | ||
func (s *vrSROS) mapSSHPubKeys(supportedSSHKeyAlgos map[string]*[]string) { | ||
for _, k := range s.sshPubKeys { | ||
sshKeys, ok := supportedSSHKeyAlgos[k.Type()] | ||
if !ok { | ||
log.Debugf("unsupported SSH Key Algo %q, skipping key", k.Type()) | ||
continue | ||
} | ||
|
||
// extract the fields | ||
// <keytype> <key> <comment> | ||
keyFields := strings.Fields(string(ssh.MarshalAuthorizedKey(k))) | ||
|
||
*sshKeys = append(*sshKeys, keyFields[1]) | ||
} | ||
} | ||
|
||
// SROSTemplateData holds ssh keys for template generation. | ||
type SROSTemplateData struct { | ||
SSHPubKeysRSA []string | ||
SSHPubKeysECDSA []string | ||
} | ||
|
||
// configureSSHPublicKeys cofigures public keys extracted from clab host | ||
// on SR OS node using SSH. | ||
func (s *vrSROS) configureSSHPublicKeys( | ||
ctx context.Context, addr, platformName, | ||
username, password string, pubKeys []ssh.PublicKey) error { | ||
tplData := SROSTemplateData{} | ||
|
||
// a map of supported SSH key algorithms and the template slices | ||
// the keys should be added to. | ||
// In mapSSHPubKeys we map supported SSH key algorithms to the template slices. | ||
supportedSSHKeyAlgos := map[string]*[]string{ | ||
ssh.KeyAlgoRSA: &tplData.SSHPubKeysRSA, | ||
ssh.KeyAlgoECDSA521: &tplData.SSHPubKeysECDSA, | ||
ssh.KeyAlgoECDSA384: &tplData.SSHPubKeysECDSA, | ||
ssh.KeyAlgoECDSA256: &tplData.SSHPubKeysECDSA, | ||
} | ||
|
||
s.mapSSHPubKeys(supportedSSHKeyAlgos) | ||
|
||
t, err := template.New("SSHKeys").Funcs( | ||
gomplate.CreateFuncs(context.Background(), new(data.Data))).Parse(SROSSSHKeysTemplate) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
buf := new(bytes.Buffer) | ||
err = t.Execute(buf, tplData) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = s.applyPartialConfig(ctx, s.Cfg.MgmtIPv4Address, scrapliPlatformName, | ||
defaultCredentials.GetUsername(), defaultCredentials.GetPassword(), | ||
buf, | ||
) | ||
|
||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{{/* this is a template for sros public key config for ssh admin user access */}} | ||
|
||
{{/* to enable long list of keys from agent where the configured key may not be in the default first three keys */}} | ||
/configure system security user-params attempts count 64 | ||
|
||
{{ range $index, $key := .SSHPubKeysRSA }} | ||
/configure system security user-params local-user user "admin" public-keys rsa rsa-key {{ add $index 1 }} key-value {{ $key }} | ||
{{ end }} | ||
|
||
{{ range $index, $key := .SSHPubKeysECDSA }} | ||
/configure system security user-params local-user user "admin" public-keys ecdsa ecdsa-key {{ add $index 1 }} key-value {{ $key }} | ||
{{ end }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package types | ||
|
||
type PubkeyAuthValue string | ||
|
||
const ( | ||
PubkeyAuthValueYes PubkeyAuthValue = "yes" | ||
PubkeyAuthValueNo PubkeyAuthValue = "no" | ||
PubkeyAuthValueHostBound PubkeyAuthValue = "host-bound" | ||
PubkeyAuthValueUnbound PubkeyAuthValue = "unbound" | ||
) | ||
|
||
func (p PubkeyAuthValue) String() string { | ||
return string(p) | ||
} | ||
|
||
// SSHConfig is the SSH client configuration that a clab node requires. | ||
type SSHConfig struct { | ||
PubkeyAuthentication PubkeyAuthValue | ||
} | ||
|
||
func NewSSHConfig() *SSHConfig { | ||
return &SSHConfig{} | ||
} |