-
Notifications
You must be signed in to change notification settings - Fork 363
/
iampolicy.go
56 lines (46 loc) · 2.03 KB
/
iampolicy.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
package iam
import (
"net/url"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/iam"
"github.com/google/go-cmp/cmp"
"github.com/crossplane/provider-aws/apis/identity/v1alpha1"
awsclients "github.com/crossplane/provider-aws/pkg/clients"
)
// PolicyClient is the external client used for IAMPolicy Custom Resource
type PolicyClient interface {
CreatePolicyRequest(*iam.CreatePolicyInput) iam.CreatePolicyRequest
GetPolicyRequest(*iam.GetPolicyInput) iam.GetPolicyRequest
DeletePolicyRequest(*iam.DeletePolicyInput) iam.DeletePolicyRequest
GetPolicyVersionRequest(*iam.GetPolicyVersionInput) iam.GetPolicyVersionRequest
CreatePolicyVersionRequest(*iam.CreatePolicyVersionInput) iam.CreatePolicyVersionRequest
ListPolicyVersionsRequest(*iam.ListPolicyVersionsInput) iam.ListPolicyVersionsRequest
DeletePolicyVersionRequest(*iam.DeletePolicyVersionInput) iam.DeletePolicyVersionRequest
}
// NewPolicyClient returns a new client using AWS credentials as JSON encoded data.
func NewPolicyClient(cfg aws.Config) PolicyClient {
return iam.New(cfg)
}
// IsPolicyUpToDate checks whether there is a change in any of the modifiable fields in policy.
func IsPolicyUpToDate(in v1alpha1.IAMPolicyParameters, policy iam.PolicyVersion) (bool, error) {
// The AWS API reutrns Policy Document as an escaped string.
// Due to differences in the methods to escape a string, the comparison result between
// the spec.Document and policy.Document can sometimes be false negative (due to spaces, line feeds).
// Escaping with a common method and then comparing is a safe way.
if *policy.Document == "" || in.Document == "" {
return false, nil
}
unescapedPolicy, err := url.QueryUnescape(aws.StringValue(policy.Document))
if err != nil {
return false, nil
}
compactIAMPolicy, err := awsclients.CompactAndEscapeJSON(unescapedPolicy)
if err != nil {
return false, err
}
compactSpecPolicy, err := awsclients.CompactAndEscapeJSON(in.Document)
if err != nil {
return false, err
}
return cmp.Equal(compactIAMPolicy, compactSpecPolicy), nil
}