/
ssh.go
72 lines (57 loc) 路 2.02 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 providers
import (
"context"
"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"github.com/aws/eks-anywhere/pkg/api/v1alpha1"
"github.com/aws/eks-anywhere/pkg/cluster"
eksaerrors "github.com/aws/eks-anywhere/pkg/errors"
"github.com/aws/eks-anywhere/pkg/validation"
)
// ValidateSSHKeyPresentForUpgrade checks that all machine configs in the cluster spec
// contain at least one SSH key.
func ValidateSSHKeyPresentForUpgrade(_ context.Context, spec *cluster.Spec) error {
machines := make([]machineWithUsers, 0)
// We don't add snow since SnowMachineConfig's don't have []User
machines = appendMachinesWithUsers(machines, spec.VSphereMachineConfigs)
machines = appendMachinesWithUsers(machines, spec.CloudStackMachineConfigs)
machines = appendMachinesWithUsers(machines, spec.NutanixMachineConfigs)
machines = appendMachinesWithUsers(machines, spec.TinkerbellMachineConfigs)
if err := validateAtLeastOneSSHKey(machines); err != nil {
return validation.WithRemediation(err, "Please include at least one SSH key per machine config. If your keys were autogenerated during the create cluster operation, make sure you include them in your cluster config for all lifecycle operations")
}
return nil
}
type machineWithUsers interface {
metav1.Object
runtime.Object
Users() []v1alpha1.UserConfiguration
}
func validateAtLeastOneSSHKey(machines []machineWithUsers) error {
var errs []error
Machines:
for _, m := range machines {
for _, user := range m.Users() {
for _, key := range user.SshAuthorizedKeys {
if key != "" {
continue Machines
}
}
}
errs = append(errs,
errors.Errorf(
"%s %s is invalid: it should contain at least one SSH key",
m.GetName(),
m.GetObjectKind().GroupVersionKind().Kind,
),
)
}
return eksaerrors.NewAggregate(errs)
}
func appendMachinesWithUsers[O machineWithUsers](m []machineWithUsers, addMap map[string]O) []machineWithUsers {
for _, machine := range addMap {
m = append(m, machine)
}
return m
}