/
policymanager.go
121 lines (103 loc) · 3.24 KB
/
policymanager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package rolemanager
import (
"fmt"
"log"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/Optum/dce/pkg/awsiface"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/iam"
)
// PolicyManager updates and manages policy versions
type PolicyManager interface {
MergePolicy(input *MergePolicyInput) error
SetIAMClient(iamClient awsiface.IAM)
PrunePolicyVersions(arn string) error
DeletePolicyVersion(arn string, versionID string) error
}
// MergePolicyInput defines what we need to create and upate a policy
type MergePolicyInput struct {
PolicyArn arn.ARN
PolicyName string
PolicyDocument string
PolicyDescription string
}
// IAMPolicyManager has the interface to the AWS Session
type IAMPolicyManager struct {
IAM awsiface.IAM
}
// SetIAMClient allows consumer to set IAM session in IAMRoleManager stuct
func (rm *IAMPolicyManager) SetIAMClient(iamClient awsiface.IAM) {
rm.IAM = iamClient
}
// MergePolicy creates or updates the policy
func (rm *IAMPolicyManager) MergePolicy(input *MergePolicyInput) error {
// Check if Policy Already exists
_, err := rm.IAM.CreatePolicy(&iam.CreatePolicyInput{
PolicyName: aws.String(input.PolicyName),
Description: aws.String(input.PolicyDescription),
PolicyDocument: aws.String(input.PolicyDocument),
})
if err != nil {
if isAWSAlreadyExistsError(err) {
log.Print(err.Error() + " (Ignoring)")
} else {
return err
}
} else {
// No error we create the policy with the correction version
return nil
}
// Prune old versions of the policy. Making sure we have room for one more policy version
err = rm.PrunePolicyVersions(input.PolicyArn.String())
if err != nil {
log.Printf("Found an issue pruning versions for policy '%s': %s", input.PolicyArn.String(), err)
return err
}
// Create a new Policy Version and set as default
_, err = rm.IAM.CreatePolicyVersion(&iam.CreatePolicyVersionInput{
PolicyArn: aws.String(input.PolicyArn.String()),
PolicyDocument: aws.String(input.PolicyDocument),
SetAsDefault: aws.Bool(true),
})
if err != nil {
log.Printf("Found an issue creating a new policy version for policy '%s': %s", input.PolicyArn.String(), err)
return err
}
return nil
}
// PrunePolicyVersions to prune the oldest version if at 5 versions
func (rm *IAMPolicyManager) PrunePolicyVersions(arn string) error {
versions, err := rm.IAM.ListPolicyVersions(&iam.ListPolicyVersionsInput{
PolicyArn: aws.String(arn),
})
if err != nil {
return err
}
if len(versions.Versions) < 5 {
return nil
}
var oldestVersion *iam.PolicyVersion
for _, version := range versions.Versions {
if *version.IsDefaultVersion {
continue
}
if oldestVersion == nil ||
version.CreateDate.Before(*oldestVersion.CreateDate) {
oldestVersion = version
}
}
err1 := rm.DeletePolicyVersion(arn, *oldestVersion.VersionId)
return err1
}
// DeletePolicyVersion delete a version of a template
func (rm *IAMPolicyManager) DeletePolicyVersion(arn string, versionID string) error {
request := &iam.DeletePolicyVersionInput{
PolicyArn: aws.String(arn),
VersionId: aws.String(versionID),
}
_, err := rm.IAM.DeletePolicyVersion(request)
if err != nil {
return fmt.Errorf("Error deleting version %s from IAM policy %s: %s", versionID, arn, err)
}
return nil
}