/
bastion.go
101 lines (78 loc) · 4.41 KB
/
bastion.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright (c) 2021 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package validation
import (
"net"
"time"
v1beta1constants "github.com/gardener/gardener/pkg/apis/core/v1beta1/constants"
"github.com/gardener/gardener/pkg/apis/operations"
"golang.org/x/crypto/ssh"
apivalidation "k8s.io/apimachinery/pkg/api/validation"
"k8s.io/apimachinery/pkg/util/validation/field"
)
// ValidateBastion validates a Bastion object.
func ValidateBastion(bastion *operations.Bastion) field.ErrorList {
allErrs := field.ErrorList{}
allErrs = append(allErrs, apivalidation.ValidateObjectMeta(&bastion.ObjectMeta, true, apivalidation.NameIsDNSLabel, field.NewPath("metadata"))...)
allErrs = append(allErrs, ValidateBastionSpec(&bastion.Spec, field.NewPath("spec"))...)
return allErrs
}
// ValidateBastionUpdate validates a Bastion object before an update.
func ValidateBastionUpdate(newBastion, oldBastion *operations.Bastion) field.ErrorList {
allErrs := field.ErrorList{}
allErrs = append(allErrs, apivalidation.ValidateObjectMetaUpdate(&newBastion.ObjectMeta, &oldBastion.ObjectMeta, field.NewPath("metadata"))...)
allErrs = append(allErrs, apivalidation.ValidateImmutableField(newBastion.Annotations[v1beta1constants.GardenCreatedBy], oldBastion.Annotations[v1beta1constants.GardenCreatedBy], field.NewPath("metadata.annotations"))...)
allErrs = append(allErrs, ValidateBastionSpecUpdate(&newBastion.Spec, &oldBastion.Spec, field.NewPath("spec"))...)
allErrs = append(allErrs, ValidateBastion(newBastion)...)
return allErrs
}
// ValidateBastionSpec validates the specification of a Bastion object.
func ValidateBastionSpec(spec *operations.BastionSpec, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
if len(spec.ShootRef.Name) == 0 {
allErrs = append(allErrs, field.Invalid(fldPath.Child("shootRef.name"), spec.ShootRef.Name, "shoot reference must not be empty"))
}
if len(spec.SSHPublicKey) == 0 {
allErrs = append(allErrs, field.Invalid(fldPath.Child("sshPublicKey"), spec.SSHPublicKey, "sshPublicKey must not be empty"))
} else if _, _, _, _, err := ssh.ParseAuthorizedKey([]byte(spec.SSHPublicKey)); err != nil {
allErrs = append(allErrs, field.Invalid(fldPath.Child("sshPublicKey"), spec.SSHPublicKey, "invalid sshPublicKey"))
}
if len(spec.Ingress) == 0 {
allErrs = append(allErrs, field.Invalid(fldPath.Child("ingress"), spec.Ingress, "ingress must not be empty"))
}
for _, block := range spec.Ingress {
if len(block.IPBlock.CIDR) == 0 {
allErrs = append(allErrs, field.Invalid(fldPath.Child("ingress"), block.IPBlock.CIDR, "CIDR must not be empty"))
} else if _, _, err := net.ParseCIDR(block.IPBlock.CIDR); err != nil {
allErrs = append(allErrs, field.Invalid(fldPath.Child("ingress"), block.IPBlock.CIDR, "invalid CIDR"))
}
}
return allErrs
}
// ValidateBastionSpecUpdate validates the specification of a Bastion object.
func ValidateBastionSpecUpdate(newSpec, oldSpec *operations.BastionSpec, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
allErrs = append(allErrs, apivalidation.ValidateImmutableField(newSpec.ShootRef.Name, oldSpec.ShootRef.Name, fldPath.Child("shootRef.name"))...)
allErrs = append(allErrs, apivalidation.ValidateImmutableField(newSpec.SSHPublicKey, oldSpec.SSHPublicKey, fldPath.Child("sshPublicKey"))...)
return allErrs
}
// ValidateBastionStatusUpdate validates the status field of a Bastion object.
func ValidateBastionStatusUpdate(newBastion, oldBastion *operations.Bastion) field.ErrorList {
allErrs := field.ErrorList{}
now := time.Now()
if newBastion.Status.LastHeartbeatTimestamp.After(now) {
allErrs = append(allErrs, field.Invalid(field.NewPath("status.lastHeartbeatTimestamp"), newBastion.Status.LastHeartbeatTimestamp, "last heartbeat must not be in the future"))
}
return allErrs
}