/
json.go
191 lines (148 loc) · 5.06 KB
/
json.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package verify
import (
"bytes"
"encoding/json"
"fmt"
"log"
"reflect"
"strings"
"github.com/YakDriver/regexache"
awspolicy "github.com/hashicorp/awspolicyequivalence"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/structure"
)
func SuppressEquivalentPolicyDiffs(k, old, new string, d *schema.ResourceData) bool {
if strings.TrimSpace(old) == "" && strings.TrimSpace(new) == "" {
return true
}
if strings.TrimSpace(old) == "{}" && strings.TrimSpace(new) == "" {
return true
}
if strings.TrimSpace(old) == "" && strings.TrimSpace(new) == "{}" {
return true
}
if strings.TrimSpace(old) == "{}" && strings.TrimSpace(new) == "{}" {
return true
}
equivalent, err := awspolicy.PoliciesAreEquivalent(old, new)
if err != nil {
return false
}
return equivalent
}
func SuppressEquivalentJSONDiffs(k, old, new string, d *schema.ResourceData) bool {
return JSONStringsEqual(old, new)
}
func SuppressEquivalentJSONOrYAMLDiffs(k, old, new string, d *schema.ResourceData) bool {
normalizedOld, err := NormalizeJSONOrYAMLString(old)
if err != nil {
log.Printf("[WARN] Unable to normalize Terraform state CloudFormation template body: %s", err)
return false
}
normalizedNew, err := NormalizeJSONOrYAMLString(new)
if err != nil {
log.Printf("[WARN] Unable to normalize Terraform configuration CloudFormation template body: %s", err)
return false
}
return normalizedOld == normalizedNew
}
func NormalizeJSONOrYAMLString(templateString interface{}) (string, error) {
if looksLikeJSONString(templateString) {
return structure.NormalizeJsonString(templateString.(string))
}
return checkYAMLString(templateString)
}
func looksLikeJSONString(s interface{}) bool {
return regexache.MustCompile(`^\s*{`).MatchString(s.(string))
}
func JSONStringsEqual(s1, s2 string) bool {
b1 := bytes.NewBufferString("")
if err := json.Compact(b1, []byte(s1)); err != nil {
return false
}
b2 := bytes.NewBufferString("")
if err := json.Compact(b2, []byte(s2)); err != nil {
return false
}
return JSONBytesEqual(b1.Bytes(), b2.Bytes())
}
func JSONBytesEqual(b1, b2 []byte) bool {
var o1 interface{}
if err := json.Unmarshal(b1, &o1); err != nil {
return false
}
var o2 interface{}
if err := json.Unmarshal(b2, &o2); err != nil {
return false
}
return reflect.DeepEqual(o1, o2)
}
func SecondJSONUnlessEquivalent(old, new string) (string, error) {
// valid empty JSON is "{}" not "" so handle special case to avoid
// Error unmarshaling policy: unexpected end of JSON input
if strings.TrimSpace(new) == "" {
return "", nil
}
if strings.TrimSpace(new) == "{}" {
return "{}", nil
}
if strings.TrimSpace(old) == "" || strings.TrimSpace(old) == "{}" {
return new, nil
}
equivalent, err := awspolicy.PoliciesAreEquivalent(old, new)
if err != nil {
return "", err
}
if equivalent {
return old, nil
}
return new, nil
}
// PolicyToSet returns the existing policy if the new policy is equivalent.
// Otherwise, it returns the new policy. Either policy is normalized.
func PolicyToSet(exist, new string) (string, error) {
policyToSet, err := SecondJSONUnlessEquivalent(exist, new)
if err != nil {
return "", fmt.Errorf("while checking equivalency of existing policy (%s) and new policy (%s), encountered: %w", exist, new, err)
}
policyToSet, err = structure.NormalizeJsonString(policyToSet)
if err != nil {
return "", fmt.Errorf("policy (%s) is invalid JSON: %w", policyToSet, err)
}
return policyToSet, nil
}
// LegacyPolicyNormalize returns a "normalized" JSON policy document except
// the Version element is first in the JSON as required by AWS in many places.
// Version not being first is one reason for this error:
// MalformedPolicyDocument: The policy failed legacy parsing
func LegacyPolicyNormalize(policy interface{}) (string, error) {
if policy == nil || policy.(string) == "" {
return "", nil
}
np, err := structure.NormalizeJsonString(policy)
if err != nil {
return policy.(string), fmt.Errorf("legacy policy (%s) is invalid JSON: %w", policy, err)
}
m := regexache.MustCompile(`(?s)^(\{\n?)(.*?)(,\s*)?( )?("Version":\s*"2012-10-17")(,)?(\n)?(.*?)(\})`)
n := m.ReplaceAllString(np, `$1$4$5$3$2$6$7$8$9`)
_, err = structure.NormalizeJsonString(n)
if err != nil {
return policy.(string), fmt.Errorf("LegacyPolicyNormalize created a policy (%s) that is invalid JSON: %w", n, err)
}
return n, nil
}
// LegacyPolicyToSet returns the existing policy if the new policy is equivalent.
// Otherwise, it returns the new policy. Either policy is legacy normalized.
func LegacyPolicyToSet(exist, new string) (string, error) {
policyToSet, err := SecondJSONUnlessEquivalent(exist, new)
if err != nil {
return "", fmt.Errorf("while checking equivalency of existing policy (%s) and new policy (%s), encountered: %w", exist, new, err)
}
policyToSet, err = LegacyPolicyNormalize(policyToSet)
if err != nil {
return "", fmt.Errorf("legacy policy (%s) is invalid JSON: %w", policyToSet, err)
}
return policyToSet, nil
}