sigs.k8s.io/cluster-api-provider-aws@v1.5.5/controlplane/eks/api/v1alpha3/validate.go (about)

     1  /*
     2  Copyright 2020 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 v1alpha3
    18  
    19  import (
    20  	"strings"
    21  
    22  	"github.com/aws/aws-sdk-go/aws/arn"
    23  	"github.com/pkg/errors"
    24  )
    25  
    26  // Errors for validation of Amazon EKS nodes that are registered with the control plane.
    27  var (
    28  	ErrRoleARNRequired  = errors.New("rolearn is required")
    29  	ErrUserARNRequired  = errors.New("userarn is required")
    30  	ErrUserNameRequired = errors.New("username is required")
    31  	ErrGroupsRequired   = errors.New("groups are required")
    32  	ErrIsNotARN         = errors.New("supplied value is not a ARN")
    33  	ErrIsNotRoleARN     = errors.New("supplied ARN is not a role ARN")
    34  	ErrIsNotUserARN     = errors.New("supplied ARN is not a user ARN")
    35  )
    36  
    37  // Validate will return nil is there are no errors with the role mapping.
    38  func (r *RoleMapping) Validate() []error {
    39  	errs := []error{}
    40  
    41  	if strings.TrimSpace(r.RoleARN) == "" {
    42  		errs = append(errs, ErrRoleARNRequired)
    43  	}
    44  	if strings.TrimSpace(r.UserName) == "" {
    45  		errs = append(errs, ErrUserNameRequired)
    46  	}
    47  	if len(r.Groups) == 0 {
    48  		errs = append(errs, ErrGroupsRequired)
    49  	}
    50  
    51  	if !arn.IsARN(r.RoleARN) {
    52  		errs = append(errs, ErrIsNotARN)
    53  	} else {
    54  		parsedARN, err := arn.Parse(r.RoleARN)
    55  		if err != nil {
    56  			errs = append(errs, err)
    57  		} else if !strings.Contains(parsedARN.Resource, "role/") {
    58  			errs = append(errs, ErrIsNotRoleARN)
    59  		}
    60  	}
    61  
    62  	if len(errs) == 0 {
    63  		return nil
    64  	}
    65  
    66  	return errs
    67  }
    68  
    69  // Validate will return nil is there are no errors with the user mapping.
    70  func (u *UserMapping) Validate() []error {
    71  	errs := []error{}
    72  
    73  	if strings.TrimSpace(u.UserARN) == "" {
    74  		errs = append(errs, ErrUserARNRequired)
    75  	}
    76  	if strings.TrimSpace(u.UserName) == "" {
    77  		errs = append(errs, ErrUserNameRequired)
    78  	}
    79  	if len(u.Groups) == 0 {
    80  		errs = append(errs, ErrGroupsRequired)
    81  	}
    82  
    83  	if !arn.IsARN(u.UserARN) {
    84  		errs = append(errs, ErrIsNotARN)
    85  	} else {
    86  		parsedARN, err := arn.Parse(u.UserARN)
    87  		if err != nil {
    88  			errs = append(errs, err)
    89  		} else if !strings.Contains(parsedARN.Resource, "user/") {
    90  			errs = append(errs, ErrIsNotUserARN)
    91  		}
    92  	}
    93  
    94  	if len(errs) == 0 {
    95  		return nil
    96  	}
    97  
    98  	return errs
    99  }