Skip to content
This repository was archived by the owner on Dec 14, 2023. It is now read-only.

Commit 0575b67

Browse files
authored
Merge pull request #18 from VoodooTeam/fix(policy)--delete-old-policy-version-before-updating-if-needed
fix(policy): delete old policy version before updating if needed
2 parents 01b54bf + b8e5ac5 commit 0575b67

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

aws/aws.go

+37
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ func (m RealAwsManager) UpdatePolicy(policy api.Policy) error {
8282
return err
8383
}
8484

85+
if err := m.deleteOldestPolicyVersionIfNeeded(policy.Spec.ARN); err != nil {
86+
return err
87+
}
88+
8589
_, err = m.Client.CreatePolicyVersion(&iam.CreatePolicyVersionInput{PolicyArn: &policy.Spec.ARN, PolicyDocument: &policyDoc, SetAsDefault: aws.Bool(true)})
8690
if err != nil {
8791
return err
@@ -90,6 +94,39 @@ func (m RealAwsManager) UpdatePolicy(policy api.Policy) error {
9094
return nil
9195
}
9296

97+
// deleteOldestPolicyVersionIfNeeded deletes the oldest policy version of a manage policy
98+
// if it is full, ie if it has already 5 versions
99+
func (m RealAwsManager) deleteOldestPolicyVersionIfNeeded(arn string) error {
100+
res, err := m.Client.ListPolicyVersions(&iam.ListPolicyVersionsInput{PolicyArn: &arn})
101+
if err != nil {
102+
return err
103+
}
104+
105+
// no need to delete a version if we have less than 5
106+
if len(res.Versions) < 5 {
107+
return nil
108+
}
109+
110+
// looking for the oldest non-default version
111+
var oldest *iam.PolicyVersion
112+
113+
for _, pv := range res.Versions {
114+
if *pv.IsDefaultVersion {
115+
continue
116+
}
117+
if oldest == nil || pv.CreateDate.Before(*oldest.CreateDate) {
118+
oldest = pv
119+
}
120+
}
121+
122+
if oldest == nil {
123+
return nil
124+
}
125+
126+
_, err = m.Client.DeletePolicyVersion(&iam.DeletePolicyVersionInput{PolicyArn: &arn, VersionId: oldest.VersionId})
127+
return err
128+
}
129+
93130
func (m RealAwsManager) CreatePolicy(policy api.Policy) error {
94131
_ = m.log.WithName("aws").WithName("policy")
95132

aws/aws_test.go

+13-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ import (
66
. "github.com/onsi/gomega"
77
)
88

9-
var validPolicy = api.NewPolicy("name", "testns", []api.StatementSpec{
10-
{Resource: "arn:aws:s3:::my_corporate_bucket/exampleobject.png", Action: []string{"an:action"}},
11-
})
9+
var (
10+
validPolicy = api.NewPolicy("name", "testns", []api.StatementSpec{
11+
{Resource: "arn:aws:s3:::my_corporate_bucket/exampleobject.png", Action: []string{"an:action"}},
12+
})
13+
)
1214

1315
var _ = Describe("policy", func() {
1416
It("given a valid policy", func() {
@@ -26,6 +28,14 @@ var _ = Describe("policy", func() {
2628
Expect(err).NotTo(HaveOccurred())
2729
Expect(policyARN).NotTo(BeEmpty())
2830

31+
By("creating new policy versions 5 times")
32+
validPolicy.Spec.ARN = policyARN
33+
34+
for i := 0; i < 5; i++ {
35+
err = awsmngr.UpdatePolicy(*validPolicy)
36+
Expect(err).ToNot(HaveOccurred())
37+
}
38+
2939
By("deleting it")
3040
Expect(policyARN).NotTo(BeEmpty())
3141
err = awsmngr.DeletePolicy(policyARN)

0 commit comments

Comments
 (0)