-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
286 lines (263 loc) · 7.74 KB
/
main.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"strings"
tf "github.com/hashicorp/terraform-json"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
type SourceResource struct {
Key string
Value string
Provider string
Address string
State string
Destinations []DestinationResource
}
type DestinationResource struct {
Provider string
Address string
State string
Mode string
Key string
}
var (
conf Configuration
)
func main() {
////////////////
// CLI Params
////////////////
debug := flag.Bool("debug", false, "Set log level to debug")
confPath := flag.String("config", "test/config-full.yaml", "Path to config")
sourceState := flag.String("sourceState", "test/states/source.json", "Path to json state that contains source resources")
destinationStates := flag.String("destinationStates", "test/states/", "Path to json states directory that contains destination resources")
flag.Parse()
////////////////
// Instantiate Pretty Logger
////////////////
log.Logger = zerolog.New(os.Stdout).With().Timestamp().Caller().Logger()
zerolog.SetGlobalLevel(zerolog.InfoLevel)
if *debug {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
}
////////////////
// Load conf and states
////////////////
conf.LoadConfiguration(*confPath)
dStates := listDestinationStates(*sourceState, *destinationStates)
statefile, err := os.ReadFile(*sourceState)
if err != nil {
log.Fatal().Err(err).Msg("Can't read state file")
}
var state tf.State
err = state.UnmarshalJSON(statefile)
if err != nil {
log.Fatal().Err(err).Msg("Can't read state file")
}
log.Debug().Msgf("Source State format version %s", state.FormatVersion)
log.Debug().Msgf("Source Terraform version %s", state.TerraformVersion)
////////////////
// Extract source resources interesting keys
////////////////
sourceResources := []SourceResource{}
sourceResources = append(sourceResources, extractSourceResources(*sourceState, state.Values.RootModule)...)
for _, child := range state.Values.RootModule.ChildModules {
sourceResources = append(sourceResources, extractSourceResources(*sourceState, child)...)
}
////////////////
// Find if source resources are in destination resources
////////////////
sourceResources = analyzeDestinationStates(dStates, sourceResources)
////////////////
// Display Results
////////////////
for _, source := range sourceResources {
if source.Destinations != nil {
log.Info().Interface("Source", source).Send()
}
}
}
// List all states that will be used as destination
func listDestinationStates(source, dir string) []string {
paths := []string{}
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
log.Error().Err(err).Send()
return nil
}
if !info.IsDir() && strings.HasSuffix(path, ".json") && path != source {
log.Debug().Str("Found destination state", path).Send()
paths = append(paths, path)
}
return nil
})
if err != nil {
log.Error().Err(err).Send()
}
return paths
}
// Extract all key/value from resources that will be used to search in destination resource
func extractSourceResources(sourceState string, state *tf.StateModule) []SourceResource {
resources := []SourceResource{}
for _, resource := range state.Resources {
if string(resource.Mode) == "managed" {
for key, value := range resource.AttributeValues {
// Check if key is a configured primary key
matchers := getMatchers(resource.Type, resource.ProviderName)
valid := false
for _, validKey := range matchers {
if key == validKey {
valid = true
}
}
if valueStr, ok := value.(string); ok && valid {
resources = append(resources, SourceResource{
Key: key,
Value: valueStr,
Provider: resource.ProviderName,
Address: resource.Address,
State: sourceState,
})
}
}
}
}
return resources
}
// Try to find source resource references in destination states
func analyzeDestinationStates(destinationStates []string, sourceResources []SourceResource) []SourceResource {
sResources := []SourceResource{}
for _, statefile := range destinationStates {
// Load the state object from file
var st tf.State
file, err := os.ReadFile(statefile)
if err != nil {
log.Error().Err(err).Msgf("Can't read state file %s", statefile)
continue
}
err = st.UnmarshalJSON(file)
if err != nil {
log.Error().Err(err).Msgf("Can't read state file %s", statefile)
continue
}
// Add all destinations in source resources
for _, source := range sourceResources {
source.Destinations = append(source.Destinations, extractDestinationsResourcesThatUseSourceResource(statefile, st.Values.RootModule, source)...)
for _, child := range st.Values.RootModule.ChildModules {
source.Destinations = append(source.Destinations, extractDestinationsResourcesThatUseSourceResource(statefile, child, source)...)
}
sResources = append(sResources, source)
}
}
return sResources
}
// Check all values from a given destination resource
func extractDestinationsResourcesThatUseSourceResource(statefile string, state *tf.StateModule, source SourceResource) []DestinationResource {
resourceMatch := []DestinationResource{}
for _, resource := range state.Resources {
for key, value := range resource.AttributeValues {
// Check if resource key is banned
matchers := getMatchers(resource.Type, resource.ProviderName)
banned := false
for _, matcher := range matchers {
if key == matcher {
banned = true
}
}
if string(resource.Mode) == "data" {
banned = false
}
if banned {
continue
}
// Define value type
switch v := value.(type) {
case string:
// log.Debug().Interface("value", v).Send()
if v == source.Value {
resourceMatch = append(resourceMatch, DestinationResource{
Provider: resource.ProviderName,
Address: resource.Address,
State: statefile,
Mode: string(resource.Mode),
Key: key,
})
}
case []interface{}:
for _, vv := range v {
switch vvv := vv.(type) {
case string:
if vvv == source.Value {
resourceMatch = append(resourceMatch, DestinationResource{
Provider: resource.ProviderName,
Address: resource.Address,
State: statefile,
Mode: string(resource.Mode),
Key: key,
})
}
}
}
case map[string]interface{}:
if ok, k := interfaceTreeMatch(v, source.Value); ok {
resourceMatch = append(resourceMatch, DestinationResource{
Provider: resource.ProviderName,
Address: resource.Address,
State: statefile,
Mode: string(resource.Mode),
Key: fmt.Sprintf("%s.%s", key, k),
})
}
}
}
}
return resourceMatch
}
func interfaceTreeMatch(obj map[string]interface{}, sourceValue string) (bool, string) {
for key, value := range obj {
switch v := value.(type) {
case string:
if v == sourceValue {
return true, key
}
case []interface{}:
for _, vv := range v {
switch vvv := vv.(type) {
case string:
if vvv == sourceValue {
return true, key
}
}
}
case map[string]interface{}:
if ok, k := interfaceTreeMatch(v, sourceValue); ok {
return true, fmt.Sprintf("%s.%s", key, k)
}
default:
}
}
return false, ""
}
func getMatchers(typeName, providerName string) []string {
for _, matcher := range conf.TypeMatchers {
for _, tName := range matcher.Names {
for _, pName := range matcher.ProviderMatcher.Names {
if tName == typeName && pName == providerName {
return matcher.ProviderMatcher.KeyMatchers
}
}
}
}
for _, matcher := range conf.ProviderMatchers {
for _, pName := range matcher.Names {
if pName == providerName {
return matcher.KeyMatchers
}
}
}
return conf.KeyMatchers
}