sigs.k8s.io/cluster-api-provider-aws@v1.5.5/cmd/clusterawsadm/cloudformation/bootstrap/iam.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 bootstrap
    18  
    19  import (
    20  	"fmt"
    21  	"os"
    22  	"path"
    23  
    24  	"sigs.k8s.io/cluster-api-provider-aws/cmd/clusterawsadm/converters"
    25  	iamv1 "sigs.k8s.io/cluster-api-provider-aws/iam/api/v1beta1"
    26  )
    27  
    28  // PolicyName defines the name of a managed IAM policy.
    29  type PolicyName string
    30  
    31  // ManagedIAMPolicyNames slice of managed IAM policies.
    32  var ManagedIAMPolicyNames = [5]PolicyName{ControllersPolicy, ControllersPolicyEKS, ControlPlanePolicy, NodePolicy, CSIPolicy}
    33  
    34  // IsValid will check if a given policy name is valid. That is, it will check if the given policy name is
    35  // one of the ManagedIAMPolicyNames.
    36  func (p PolicyName) IsValid() bool {
    37  	for i := range ManagedIAMPolicyNames {
    38  		if ManagedIAMPolicyNames[i] == p {
    39  			return true
    40  		}
    41  	}
    42  	return false
    43  }
    44  
    45  // GenerateManagedIAMPolicyDocuments generates JSON representation of policy documents for all ManagedIAMPolicy.
    46  func (t Template) GenerateManagedIAMPolicyDocuments(policyDocDir string) error {
    47  	for _, pn := range ManagedIAMPolicyNames {
    48  		pd := t.GetPolicyDocFromPolicyName(pn)
    49  
    50  		pds, err := converters.IAMPolicyDocumentToJSON(*pd)
    51  		if err != nil {
    52  			return fmt.Errorf("failed to marshal policy document for ManagedIAMPolicy %q: %w", pn, err)
    53  		}
    54  
    55  		fn := path.Join(policyDocDir, fmt.Sprintf("%s.json", pn))
    56  		err = os.WriteFile(fn, []byte(pds), 0o600)
    57  		if err != nil {
    58  			return fmt.Errorf("failed to generate policy document for ManagedIAMPolicy %q: %w", pn, err)
    59  		}
    60  	}
    61  	return nil
    62  }
    63  
    64  func (t Template) policyFunctionMap() map[PolicyName]func() *iamv1.PolicyDocument {
    65  	return map[PolicyName]func() *iamv1.PolicyDocument{
    66  		ControlPlanePolicy:   t.cloudProviderControlPlaneAwsPolicy,
    67  		ControllersPolicy:    t.ControllersPolicy,
    68  		ControllersPolicyEKS: t.ControllersPolicyEKS,
    69  		NodePolicy:           t.cloudProviderNodeAwsPolicy,
    70  		CSIPolicy:            t.csiControllerPolicy,
    71  	}
    72  }
    73  
    74  // GetPolicyDocFromPolicyName returns a Template's policy document.
    75  func (t Template) GetPolicyDocFromPolicyName(policyName PolicyName) *iamv1.PolicyDocument {
    76  	return t.policyFunctionMap()[policyName]()
    77  }