sigs.k8s.io/cluster-api-provider-aws@v1.5.5/api/v1beta1/bastion.go (about)

     1  /*
     2  Copyright 2021 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  	http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package v1beta1
    18  
    19  import (
    20  	"fmt"
    21  	"net"
    22  	"regexp"
    23  
    24  	"k8s.io/apimachinery/pkg/util/validation/field"
    25  )
    26  
    27  var (
    28  	sshKeyValidNameRegex = regexp.MustCompile(`^[[:graph:]]+([[:print:]]*[[:graph:]]+)*$`)
    29  )
    30  
    31  // Validate will validate the bastion fields.
    32  func (b *Bastion) Validate() []*field.Error {
    33  	var errs field.ErrorList
    34  
    35  	if b.DisableIngressRules && len(b.AllowedCIDRBlocks) > 0 {
    36  		errs = append(errs,
    37  			field.Forbidden(field.NewPath("spec", "bastion", "allowedCIDRBlocks"), "cannot be set if spec.bastion.disableIngressRules is true"),
    38  		)
    39  		return errs
    40  	}
    41  
    42  	for i, cidr := range b.AllowedCIDRBlocks {
    43  		if _, _, err := net.ParseCIDR(cidr); err != nil {
    44  			errs = append(errs,
    45  				field.Invalid(field.NewPath("spec", "bastion", fmt.Sprintf("allowedCIDRBlocks[%d]", i)), cidr, "must be a valid CIDR block"),
    46  			)
    47  		}
    48  	}
    49  	return errs
    50  }
    51  
    52  func validateSSHKeyName(sshKeyName *string) field.ErrorList {
    53  	var allErrs field.ErrorList
    54  	switch {
    55  	case sshKeyName == nil:
    56  	// nil is accepted
    57  	case sshKeyName != nil && *sshKeyName == "":
    58  	// empty string is accepted
    59  	case sshKeyName != nil && !sshKeyValidNameRegex.Match([]byte(*sshKeyName)):
    60  		allErrs = append(allErrs, field.Invalid(field.NewPath("sshKeyName"), sshKeyName, "Name is invalid. Must be specified in ASCII and must not start or end in whitespace"))
    61  	}
    62  	return allErrs
    63  }