Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(engine): ignore terraform cache folders #6240

Merged
merged 26 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
5198e09
feat: ignore terraform cache folders
dim-ops Mar 21, 2023
a80785d
style: fmt and delete comment
dim-ops Mar 28, 2023
6032e2e
feat: ignore terraform cache folders
dim-ops Mar 28, 2023
a95d261
chore: improve comment
dim-ops Mar 28, 2023
abef0ca
feat(go): add unit tests
Apr 5, 2023
dc1a481
fix: err not handled
Apr 5, 2023
fea9933
fix: UT TestFileSystemSourceProvider_checkConditions
dim-ops Apr 6, 2023
371eb3e
fix: add resolved
Apr 6, 2023
7632af6
Merge branch 'master' into feat/ignore-terraform-cahce-files
gabriel-cx Apr 17, 2023
a3c4e50
Merge branch 'master' into feat/ignore-terraform-cahce-files
gabriel-cx Feb 2, 2024
8d09561
Merge branch 'master' into pr/6240
JoaoAtGit Feb 8, 2024
43244e4
improve terraform cache
JoaoAtGit Feb 8, 2024
ff0c129
Merge branch 'master' into feat/ignore-terraform-cahce-files
JoaoAtGit Feb 8, 2024
8be4638
Merge branch 'master' into feat/ignore-terraform-cahce-files
JoaoAtGit Feb 8, 2024
c8a5c99
more testes
JoaoAtGit Feb 8, 2024
453ea64
Merge branch 'feat/ignore-terraform-cahce-files' of https://github.co…
JoaoAtGit Feb 8, 2024
4c1980b
Merge branch 'master' into feat/ignore-terraform-cahce-files
JoaoAtGit Feb 8, 2024
56d1e82
add tests to terragrunt
JoaoAtGit Feb 8, 2024
1eb6279
Merge branch 'master' into feat/ignore-terraform-cahce-files
gabriel-cx Feb 9, 2024
204dad2
test not skipt with terra on path
JoaoAtGit Feb 9, 2024
bedd71e
change the type of lock
JoaoAtGit Feb 15, 2024
59c5394
clean code
JoaoAtGit Feb 15, 2024
9f7b447
Merge branch 'master' into feat/ignore-terraform-cahce-files
JoaoAtGit Feb 16, 2024
3097e41
remove lock logic
JoaoAtGit Feb 16, 2024
94ebf93
Merge branch 'master' into feat/ignore-terraform-cahce-files
gabriel-cx Feb 16, 2024
cb7a7f6
Merge branch 'master' into feat/ignore-terraform-cahce-files
gabriel-cx Feb 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions pkg/engine/provider/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
ioFs "io/fs"
"os"
"path/filepath"
"regexp"
"strings"
"sync"
"syscall"
Expand All @@ -26,8 +27,11 @@ type FileSystemSourceProvider struct {
mu sync.RWMutex
}

// ErrNotSupportedFile - error representing when a file format is not supported by KICS
var ErrNotSupportedFile = errors.New("invalid file format")
var (
queryRegexExcludeTerraCache = regexp.MustCompile(fmt.Sprintf(`^(.*?%s)?\.terra.*`, regexp.QuoteMeta(string(os.PathSeparator))))
// ErrNotSupportedFile - error representing when a file format is not supported by KICS
ErrNotSupportedFile = errors.New("invalid file format")
)

// NewFileSystemSourceProvider initializes a FileSystemSourceProvider with path and files that will be ignored
func NewFileSystemSourceProvider(paths, excludes []string) (*FileSystemSourceProvider, error) {
Expand Down Expand Up @@ -231,10 +235,20 @@ func (s *FileSystemSourceProvider) checkConditions(info os.FileInfo, extensions
s.mu.RLock()
defer s.mu.RUnlock()
if info.IsDir() {
// exclude terraform cache folders
if queryRegexExcludeTerraCache.MatchString(path) {
log.Info().Msgf("Directory ignored: %s", path)
err := s.AddExcluded([]string{info.Name()})
if err != nil {
return true, err
}
return true, filepath.SkipDir
}
if f, ok := s.excludes[info.Name()]; ok && containsFile(f, info) {
log.Info().Msgf("Directory ignored: %s", path)
return true, filepath.SkipDir
}

_, err := os.Stat(filepath.Join(path, "Chart.yaml"))
if err != nil || resolved {
return true, nil
Expand Down
149 changes: 149 additions & 0 deletions pkg/engine/provider/filesystem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,11 @@ func TestFileSystemSourceProvider_checkConditions(t *testing.T) {
}
infoHelm, errHelm := os.Stat(filepath.FromSlash("test/fixtures/test_helm"))
checkStatErr(t, errHelm)
infoTerraCache, errTerraCache := os.Stat(filepath.FromSlash("test/fixtures/test_terra_cache"))
checkStatErr(t, errTerraCache)
infoTerraCacheFolder, errTerraCacheFolder := os.Stat(filepath.FromSlash("test/fixtures/test_terra_cache/.terraform"))
checkStatErr(t, errTerraCacheFolder)

type fields struct {
paths []string
excludes map[string][]os.FileInfo
Expand Down Expand Up @@ -350,6 +355,150 @@ func TestFileSystemSourceProvider_checkConditions(t *testing.T) {
err: filepath.SkipDir,
},
},
{
name: "check_condition_ignore_terra_cache for .terra",
fields: fields{
paths: []string{filepath.FromSlash(".terra")},
excludes: nil,
},
args: args{
info: infoTerraCache,
extensions: model.Extensions{},
path: filepath.FromSlash(".terra"),
},
want: want{
got: true,
err: filepath.SkipDir,
},
},
{
name: "check_condition_ignore_terra_cache for .terraform",
fields: fields{
paths: []string{filepath.FromSlash(".terraform")},
excludes: nil,
},
args: args{
info: infoTerraCache,
extensions: model.Extensions{},
path: filepath.FromSlash(".terraform"),
},
want: want{
got: true,
err: filepath.SkipDir,
},
},
{
name: "check_condition_ignore_terra_cache for .terra/lalala",
fields: fields{
paths: []string{filepath.FromSlash(".terra/lalala")},
excludes: nil,
},
args: args{
info: infoTerraCache,
extensions: model.Extensions{},
path: filepath.FromSlash(".terra/lalala"),
},
want: want{
got: true,
err: filepath.SkipDir,
},
},
{
name: "check_condition_ignore_terra_cache for .terraform/lalala",
fields: fields{
paths: []string{filepath.FromSlash(".terraform/lalala")},
excludes: nil,
},
args: args{
info: infoTerraCache,
extensions: model.Extensions{},
path: filepath.FromSlash(".terraform/lalala"),
},
want: want{
got: true,
err: filepath.SkipDir,
},
},
{
name: "check_condition_ignore_terra_cache for /.terra",
fields: fields{
paths: []string{filepath.FromSlash("/.terra")},
excludes: nil,
},
args: args{
info: infoTerraCache,
extensions: model.Extensions{},
path: filepath.FromSlash("/.terra"),
},
want: want{
got: true,
err: filepath.SkipDir,
},
},
{
name: "check_condition_ignore_terra_cache for /.terraform",
fields: fields{
paths: []string{filepath.FromSlash("/.terraform")},
excludes: nil,
},
args: args{
info: infoTerraCache,
extensions: model.Extensions{},
path: filepath.FromSlash("/.terraform"),
},
want: want{
got: true,
err: filepath.SkipDir,
},
},
{
name: "check_condition_ignore_terra_cache for /.terra/lalala",
fields: fields{
paths: []string{filepath.FromSlash("/.terra/lalala")},
excludes: nil,
},
args: args{
info: infoTerraCache,
extensions: model.Extensions{},
path: filepath.FromSlash("/.terra/lalala"),
},
want: want{
got: true,
err: filepath.SkipDir,
},
},
{
name: "check_condition_ignore_terra_cache for /.terraform/lalala",
fields: fields{
paths: []string{filepath.FromSlash("/.terraform/lalala")},
excludes: nil,
},
args: args{
info: infoTerraCache,
extensions: model.Extensions{},
path: filepath.FromSlash("/.terraform/lalala"),
},
want: want{
got: true,
err: filepath.SkipDir,
},
},
{
name: "should_skip_terra_cache_folder",
fields: fields{
paths: []string{filepath.FromSlash("test/fixtures/test_terra_cache/.terraform")},
excludes: nil,
},
args: args{
info: infoTerraCacheFolder,
extensions: model.Extensions{},
path: filepath.FromSlash("test/fixtures/test_terra_cache/.terraform"),
},
want: want{
got: true,
err: filepath.SkipDir,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/test_terra_cache/.terraform/positive.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
resource "aws_security_group_rule" "positive1" {
type = "ingress"
from_port = 3306
to_port = 3306
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = aws_security_group.default.id
}
9 changes: 9 additions & 0 deletions test/fixtures/test_terra_cache/negative.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
resource "aws_security_group_rule" "negative1" {
type = "ingress"
from_port = 3306
to_port = 3306
protocol = "tcp"
cidr_blocks = ["0.0.2.0/0"]
security_group_id = aws_security_group.default.id
}

Loading