-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiffy.go
More file actions
197 lines (160 loc) · 4.59 KB
/
Copy pathdiffy.go
File metadata and controls
197 lines (160 loc) · 4.59 KB
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
// Package diffy validates Terraform configurations against provider schemas.
package diffy
import (
"context"
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"sync"
)
func ValidateSchema(options ...SchemaValidatorOption) ([]ValidationFinding, error) {
opts := &SchemaValidatorOptions{
Logger: &SimpleLogger{},
CreateGitHubIssue: false,
Silent: false,
}
for _, option := range options {
option(opts)
}
if envRoot := os.Getenv("TERRAFORM_ROOT"); envRoot != "" {
opts.TerraformRoot = envRoot
}
if envExcludedResources := os.Getenv("EXCLUDED_RESOURCES"); envExcludedResources != "" {
resources := strings.Split(envExcludedResources, ",")
for i, r := range resources {
resources[i] = strings.TrimSpace(r)
}
opts.ExcludedResources = append(opts.ExcludedResources, resources...)
}
if envExcludedDataSources := os.Getenv("EXCLUDED_DATA_SOURCES"); envExcludedDataSources != "" {
dataSources := strings.Split(envExcludedDataSources, ",")
for i, ds := range dataSources {
dataSources[i] = strings.TrimSpace(ds)
}
opts.ExcludedDataSources = append(opts.ExcludedDataSources, dataSources...)
}
if opts.TerraformRoot == "" {
return nil, fmt.Errorf("terraform root path not specified - set TERRAFORM_ROOT environment variable or use WithTerraformRoot option")
}
findings, err := validateProject(opts)
if err != nil {
return nil, err
}
if !opts.Silent {
outputFindings(findings)
}
if opts.CreateGitHubIssue {
ctx := context.Background()
if err := createGitHubIssue(ctx, opts, findings); err != nil {
opts.Logger.Logf("Failed to create/update GitHub issue: %v", err)
}
}
return findings, nil
}
func validateProject(opts *SchemaValidatorOptions) ([]ValidationFinding, error) {
absRoot, err := filepath.Abs(opts.TerraformRoot)
if err != nil {
return nil, fmt.Errorf("failed to resolve absolute path for %s: %w", opts.TerraformRoot, err)
}
parser := opts.Parser
if parser == nil {
parser = NewHCLParser()
}
runner := opts.TerraformRunner
if runner == nil {
runner = NewTerraformRunner()
}
rootFindings, err := ValidateTerraformSchemaWithOptions(
opts.Logger,
absRoot,
"",
parser,
runner,
opts.ExcludedResources,
opts.ExcludedDataSources,
)
if err != nil {
return nil, fmt.Errorf("validation failed: %w", err)
}
var allFindings []ValidationFinding
allFindings = append(allFindings, rootFindings...)
modulesDir := filepath.Join(absRoot, "modules")
submodules, err := FindSubmodules(modulesDir)
if err != nil {
if !opts.Silent {
fmt.Printf("Note: No submodules found in %s\n", modulesDir)
}
} else if len(submodules) > 0 {
concurrency := max(runtime.NumCPU(), 1)
type moduleResult struct {
findings []ValidationFinding
}
results := make(chan moduleResult, len(submodules))
sem := make(chan struct{}, concurrency)
var wg sync.WaitGroup
for _, module := range submodules {
wg.Add(1)
go func(sm SubModule) {
defer wg.Done()
sem <- struct{}{}
defer func() { <-sem }()
findings, err := ValidateTerraformSchemaWithOptions(
opts.Logger,
sm.Path,
sm.Name,
parser,
runner,
opts.ExcludedResources,
opts.ExcludedDataSources,
)
if err != nil {
opts.Logger.Logf("Failed to validate submodule %s: %v", sm.Name, err)
return
}
results <- moduleResult{findings: findings}
}(module)
}
wg.Wait()
close(results)
for res := range results {
allFindings = append(allFindings, res.findings...)
}
for _, sm := range submodules {
cleanupTerraformArtifacts(sm.Path)
}
}
cleanupTerraformArtifacts(absRoot)
deduplicatedFindings := DeduplicateFindings(allFindings)
return deduplicatedFindings, nil
}
func outputFindings(findings []ValidationFinding) {
if len(findings) == 0 {
fmt.Println("No validation findings.")
return
}
fmt.Printf("Found %d issues:\n", len(findings))
for _, finding := range findings {
fmt.Println(FormatFinding(finding))
}
}
func createGitHubIssue(ctx context.Context, opts *SchemaValidatorOptions, findings []ValidationFinding) error {
if opts.GitHubToken == "" {
return fmt.Errorf("GitHub token not provided")
}
owner := opts.GitHubOwner
repo := opts.GitHubRepo
if owner == "" || repo == "" {
gi := NewGitRepoInfo(opts.TerraformRoot)
owner, repo = gi.GetRepoInfo()
if owner == "" || repo == "" {
return fmt.Errorf("could not determine repository info for GitHub issue creation")
}
}
issueManager := NewGitHubIssueManager(owner, repo, opts.GitHubToken)
if len(findings) == 0 {
return issueManager.CloseExistingIssuesIfEmpty(ctx)
}
return issueManager.CreateOrUpdateIssue(ctx, findings)
}