-
Notifications
You must be signed in to change notification settings - Fork 277
/
ssh.go
72 lines (56 loc) 路 1.92 KB
/
ssh.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package tinkerbell
import (
"fmt"
"github.com/aws/eks-anywhere/pkg/api/v1alpha1"
"github.com/aws/eks-anywhere/pkg/providers/common"
)
const defaultUsername = "ec2-user"
func ensureMachineConfigsHaveAtLeast1User(machines map[string]*v1alpha1.TinkerbellMachineConfig) {
for _, machine := range machines {
if len(machine.Spec.Users) == 0 {
machine.Spec.Users = []v1alpha1.UserConfiguration{{Name: defaultUsername}}
}
}
}
func extractUserConfigurationsWithoutSshKeys(machines map[string]*v1alpha1.TinkerbellMachineConfig) []*v1alpha1.UserConfiguration {
var users []*v1alpha1.UserConfiguration
for _, machine := range machines {
if len(machine.Spec.Users[0].SshAuthorizedKeys) == 0 || len(machine.Spec.Users[0].SshAuthorizedKeys[0]) == 0 {
users = append(users, &machine.Spec.Users[0])
}
}
return users
}
func applySshKeyToUsers(users []*v1alpha1.UserConfiguration, key string) {
for _, user := range users {
if len(user.SshAuthorizedKeys) == 0 {
user.SshAuthorizedKeys = make([]string, 1)
}
user.SshAuthorizedKeys[0] = key
}
}
func stripCommentsFromSshKeys(machines map[string]*v1alpha1.TinkerbellMachineConfig) error {
for _, machine := range machines {
key, err := common.StripSshAuthorizedKeyComment(machine.Spec.Users[0].SshAuthorizedKeys[0])
if err != nil {
return fmt.Errorf("TinkerbellMachineConfig name=%v: %v", machine.Name, err)
}
machine.Spec.Users[0].SshAuthorizedKeys[0] = key
}
return nil
}
func (p *Provider) configureSshKeys() error {
ensureMachineConfigsHaveAtLeast1User(p.machineConfigs)
users := extractUserConfigurationsWithoutSshKeys(p.machineConfigs)
if len(users) > 0 {
publicAuthorizedKey, err := p.keyGenerator.GenerateSSHAuthKey(p.writer)
if err != nil {
return err
}
applySshKeyToUsers(users, publicAuthorizedKey)
}
if err := stripCommentsFromSshKeys(p.machineConfigs); err != nil {
return fmt.Errorf("stripping ssh key comment: %v", err)
}
return nil
}