@@ -17,19 +17,14 @@ limitations under the License.
1717package  bootstrap
1818
1919import  (
20- 	"fmt" 
21- 	"os" 
22- 	"path" 
23- 
24- 	"sigs.k8s.io/cluster-api-provider-aws/v2/cmd/clusterawsadm/converters" 
25- 	iamv1 "sigs.k8s.io/cluster-api-provider-aws/v2/iam/api/v1beta1" 
20+ 	"github.com/awslabs/goformation/v4/cloudformation/iam" 
2621)
2722
2823// PolicyName defines the name of a managed IAM policy. 
2924type  PolicyName  string 
3025
3126// ManagedIAMPolicyNames slice of managed IAM policies. 
32- var  ManagedIAMPolicyNames  =  [5 ]PolicyName {ControllersPolicy , ControllersPolicyEKS , ControlPlanePolicy , NodePolicy , CSIPolicy }
27+ var  ManagedIAMPolicyNames  =  []PolicyName {ControllersPolicy , ControllersPolicyEKS , ControlPlanePolicy , NodePolicy , CSIPolicy }
3328
3429// IsValid will check if a given policy name is valid. That is, it will check if the given policy name is 
3530// one of the ManagedIAMPolicyNames. 
@@ -42,49 +37,21 @@ func (p PolicyName) IsValid() bool {
4237	return  false 
4338}
4439
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- 		}
40+ // RenderManagedIAMPolicies returns all the managed IAM Policies that would be rendered by the template. 
41+ func  (t  Template ) RenderManagedIAMPolicies () map [string ]* iam.ManagedPolicy  {
42+ 	cft  :=  t .RenderCloudFormation ()
5443
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 
44+ 	return  cft .GetAllIAMManagedPolicyResources ()
6245}
6346
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- }
47+ // RenderManagedIAMPolicy returns a specific managed IAM Policy by name, or nil if the policy is not found. 
48+ func  (t  Template ) RenderManagedIAMPolicy (name  PolicyName ) * iam.ManagedPolicy  {
49+ 	cft  :=  t .RenderCloudFormation ()
7350
74- // PrintPolicyDocs prints the JSON representation of policy documents for all ManagedIAMPolicy. 
75- func  (t  Template ) PrintPolicyDocs () error  {
76- 	for  _ , name  :=  range  ManagedIAMPolicyNames  {
77- 		policyDoc  :=  t .GetPolicyDocFromPolicyName (name )
78- 		value , err  :=  converters .IAMPolicyDocumentToJSON (* policyDoc )
79- 		if  err  !=  nil  {
80- 			return  err 
81- 		}
82- 		fmt .Println (name , value )
51+ 	p , err  :=  cft .GetIAMManagedPolicyWithName (string (name ))
52+ 	if  err  !=  nil  {
53+ 		// Return error only if the policy is not found. 
54+ 		return  nil 
8355	}
84- 	return  nil 
85- }
86- 
87- // GetPolicyDocFromPolicyName returns a Template's policy document. 
88- func  (t  Template ) GetPolicyDocFromPolicyName (policyName  PolicyName ) * iamv1.PolicyDocument  {
89- 	return  t .policyFunctionMap ()[policyName ]()
56+ 	return  p 
9057}
0 commit comments