-
Notifications
You must be signed in to change notification settings - Fork 509
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Devang Gaur
committed
Nov 12, 2020
1 parent
73d29aa
commit ece2ab1
Showing
22 changed files
with
1,507 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
Copyright (C) 2020 Accurics, Inc. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package iacprovider | ||
|
||
import ( | ||
"reflect" | ||
|
||
kustomizev3 "github.com/accurics/terrascan/pkg/iac-providers/kustomize/v3" | ||
) | ||
|
||
// terraform specific constants | ||
const ( | ||
kustomize supportedIacType = "kustomize" | ||
kustomizeV3 supportedIacVersion = "v3" | ||
kustomizeDefaultIacVersion = kustomizeV3 | ||
) | ||
|
||
// register kubernetes as an IaC provider with terrascan | ||
func init() { | ||
// register iac provider | ||
RegisterIacProvider(kustomize, kustomizeV3, kustomizeDefaultIacVersion, reflect.TypeOf(kustomizev3.KustomizeV3{})) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package kustomizev3 | ||
|
||
import ( | ||
"errors" | ||
"path/filepath" | ||
|
||
k8sv1 "github.com/accurics/terrascan/pkg/iac-providers/kubernetes/v1" | ||
"github.com/accurics/terrascan/pkg/iac-providers/output" | ||
"github.com/accurics/terrascan/pkg/utils" | ||
"go.uber.org/zap" | ||
"sigs.k8s.io/kustomize/api/filesys" | ||
"sigs.k8s.io/kustomize/api/krusty" | ||
) | ||
|
||
const ( | ||
kustomizedirectory string = "kustomize_directory" | ||
) | ||
|
||
// LoadIacDir loads the kustomize directory | ||
func (k *KustomizeV3) LoadIacDir(absRootDir string) (output.AllResourceConfigs, error) { | ||
|
||
allResourcesConfig := make(map[string][]output.ResourceConfig) | ||
|
||
files, err := utils.FindFilesBySuffixInCurrentDir(absRootDir, KustomizeFileNames()) | ||
if err != nil { | ||
zap.S().Warn("error while searching for iac files", zap.String("root dir", absRootDir), zap.Error(err)) | ||
return allResourcesConfig, err | ||
} | ||
|
||
if len(files) == 0 { | ||
err := errors.New("could not find a kustomization.yaml/yml file in the directory") | ||
zap.S().Warn("error while searching for iac files", zap.String("root dir", absRootDir), zap.Error(err)) | ||
return allResourcesConfig, err | ||
} | ||
|
||
if len(files) > 1 { | ||
err := errors.New("a directory cannot have more than 1 kustomization.yaml/yml file") | ||
zap.S().Warn("error while searching for iac files", zap.String("root dir", absRootDir), zap.Error(err)) | ||
return allResourcesConfig, err | ||
} | ||
|
||
var config output.ResourceConfig | ||
config.Type = kustomizedirectory | ||
config.Name = filepath.Dir(absRootDir) | ||
config.Line = 0 | ||
config.ID = config.Type + "." + config.Name | ||
|
||
var yamlkustomizeobj map[string]interface{} | ||
var kustomizeFileName string | ||
for _, filename := range KustomizeFileNames() { | ||
yamlkustomizeobj, err = utils.ReadYamlFile(filepath.Join(absRootDir, filename)) | ||
if err == nil { | ||
kustomizeFileName = filename | ||
break | ||
} | ||
} | ||
|
||
if len(yamlkustomizeobj) == 0 { | ||
err := errors.New("unable to read any kustomization file in the directory") | ||
zap.S().Warn("error while searching for iac files", zap.String("root dir", absRootDir), zap.Error(err)) | ||
return allResourcesConfig, err | ||
} | ||
|
||
config.Source = filepath.Join(absRootDir, kustomizeFileName) | ||
config.Config = yamlkustomizeobj | ||
|
||
allResourcesConfig[kustomizedirectory] = append(allResourcesConfig[kustomizedirectory], config) | ||
|
||
iacDocumentMap := make(map[string][]*utils.IacDocument) | ||
var iacDocuments []*utils.IacDocument | ||
|
||
iacDocuments, err = loadKustomize(absRootDir, kustomizeFileName) | ||
if err != nil { | ||
zap.S().Warn("error occurred while loading kustomize directory", zap.String("kustomize directory", absRootDir), zap.Error(err)) | ||
return nil, err | ||
} | ||
|
||
iacDocumentMap[absRootDir] = iacDocuments | ||
|
||
for _, iacDocuments := range iacDocumentMap { | ||
for _, doc := range iacDocuments { | ||
// @TODO add k8s version check | ||
var k k8sv1.K8sV1 | ||
var config *output.ResourceConfig | ||
|
||
config, err = k.Normalize(doc) | ||
if err != nil { | ||
zap.S().Warn("unable to normalize data", zap.Error(err), zap.String("file", doc.FilePath)) | ||
continue | ||
} | ||
|
||
config.Line = 1 | ||
config.Source = doc.FilePath | ||
|
||
allResourcesConfig[config.Type] = append(allResourcesConfig[config.Type], *config) | ||
} | ||
} | ||
|
||
return allResourcesConfig, nil | ||
} | ||
|
||
func loadKustomize(basepath, filename string) ([]*utils.IacDocument, error) { | ||
fSys := filesys.MakeFsOnDisk() | ||
k := krusty.MakeKustomizer(fSys, krusty.MakeDefaultOptions()) | ||
|
||
m, err := k.Run(basepath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
yaml, err := m.AsYaml() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
res, err := utils.LoadYAMLString(string(yaml), filename) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return res, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package kustomizev3 | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/accurics/terrascan/pkg/iac-providers/output" | ||
"go.uber.org/zap" | ||
) | ||
|
||
var ( | ||
errLoadIacFileNotSupported = fmt.Errorf("load iac file is not supported for kustomize") | ||
) | ||
|
||
// LoadIacFile is not supported for helm. Only loading chart directories are supported | ||
func (k *KustomizeV3) LoadIacFile(absRootPath string) (allResourcesConfig output.AllResourceConfigs, err error) { | ||
zap.S().Error(errLoadIacFileNotSupported) | ||
return make(map[string][]output.ResourceConfig), errLoadIacFileNotSupported | ||
} |
2 changes: 2 additions & 0 deletions
2
pkg/iac-providers/kustomize/v3/testdata/multibases/base/kustomization.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
resources: | ||
- pod.yaml |
10 changes: 10 additions & 0 deletions
10
pkg/iac-providers/kustomize/v3/testdata/multibases/base/pod.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
apiVersion: v1 | ||
kind: Pod | ||
metadata: | ||
name: myapp-pod | ||
labels: | ||
app: myapp | ||
spec: | ||
containers: | ||
- name: nginx | ||
image: nginx:1.7.9 |
3 changes: 3 additions & 0 deletions
3
pkg/iac-providers/kustomize/v3/testdata/multibases/dev/kustomization.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
resources: | ||
- ../base | ||
namePrefix: dev- |
5 changes: 5 additions & 0 deletions
5
pkg/iac-providers/kustomize/v3/testdata/multibases/kustomization.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
resources: | ||
- dev | ||
- stage | ||
- prod | ||
namePrefix: cluster-a- |
3 changes: 3 additions & 0 deletions
3
pkg/iac-providers/kustomize/v3/testdata/multibases/prod/kustomization.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
resources: | ||
- ../base | ||
namePrefix: prod- |
3 changes: 3 additions & 0 deletions
3
pkg/iac-providers/kustomize/v3/testdata/multibases/stage/kustomization.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
resources: | ||
- ../base | ||
namePrefix: staging- |
Oops, something went wrong.