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

added kustomize support #378

Merged
merged 5 commits into from
Nov 13, 2020

Conversation

devang-gaur
Copy link
Contributor

Adds iac-provider that enables scanning directly from kustomize directories.

pkg/iac-providers/kustomize.go Show resolved Hide resolved
@@ -85,7 +85,7 @@ func (k *K8sV1) getNormalizedName(kind string) string {
}

// normalize takes the input document and normalizes it
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Linter error! change normalize to Normalize

return res, nil
}

/*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove the unwanted code block

Copy link
Contributor

@kanchwala-yusuf kanchwala-yusuf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Testdata folder is added but no tests are added


// FindFilesBySuffixInCurrentDir finds all the immediate files within a given directory that have the specified suffixes
// Returns an array for string pointers as a list of files
func FindFilesBySuffixInCurrentDir(basePath string, suffixes []string) ([]*string, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you searching for files in current dir or in basePath?

May be a better name would be FindFilesBySuffixInCurrentDir

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't that the name?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry copy paste mistake.
Can we use the FindFilesBySuffix method here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. we don't want a recursive search here. We just want to look up files on a single depth level. No looking into subdirectories.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updating the function name though

)

// LoadIacDir loads the kustomize directory
func (k *KustomizeV3) LoadIacDir(absRootDir string) (output.AllResourceConfigs, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a sincere request to add comments at function level and inside the function as well, so as to help the user understand what's going on. A new reader may not be able to immediately understand what task is being achieved in the function

@devang-gaur devang-gaur force-pushed the add_kustomize_support branch 2 times, most recently from 95ea924 to 2f8eceb Compare November 11, 2020 11:12
@codecov
Copy link

codecov bot commented Nov 11, 2020

Codecov Report

Merging #378 (25f258c) into master (73d29aa) will decrease coverage by 2.35%.
The diff coverage is 25.49%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #378      +/-   ##
==========================================
- Coverage   65.90%   63.55%   -2.36%     
==========================================
  Files          80       85       +5     
  Lines        1830     1904      +74     
==========================================
+ Hits         1206     1210       +4     
- Misses        521      591      +70     
  Partials      103      103              
Impacted Files Coverage Δ
pkg/iac-providers/kustomize/v3/load-dir.go 0.00% <0.00%> (ø)
pkg/iac-providers/kustomize/v3/load-file.go 0.00% <0.00%> (ø)
pkg/iac-providers/kustomize/v3/types.go 0.00% <0.00%> (ø)
pkg/iac-providers/register.go 66.66% <ø> (ø)
pkg/utils/path.go 78.57% <0.00%> (-10.62%) ⬇️
pkg/utils/yaml.go 61.81% <63.15%> (-11.52%) ⬇️
pkg/iac-providers/kustomize.go 100.00% <100.00%> (ø)
pkg/policy/kustomize.go 100.00% <100.00%> (ø)
pkg/iac-providers/kubernetes/v1/normalize.go 74.46% <0.00%> (ø)
... and 5 more

}
}

func getFullFileName(file, ext string) string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would AddSuffix be a better name for this functon? And would it make sense to add it to utils package?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, makes sense.


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))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be an error level log (zap.S().Error)?


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))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be an error level log (zap.S().Error)?


// FindFilesBySuffixInCurrentDir finds all the immediate files within a given directory that have the specified suffixes
// Returns an array for string pointers as a list of files
func FindFilesBySuffixInCurrentDir(basePath string, suffixes []string) ([]*string, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry copy paste mistake.
Can we use the FindFilesBySuffix method here?


// LoadYAMLString loads a YAML String. Can return one or more IaC Documents.
// Besides reading in file data, its main purpose is to determine and store line number and filename metadata
func LoadYAMLString(data, absFilePath string) ([]*IacDocument, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see a lot of code duplication between LoadYaml and LoadYamlString methods.

Would it make sense to have something like LoadYamlBytes and let LoadYamlString and LoadYaml both call LoadYamBytes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah good point


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))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be an error level log (zap.S().Error)?


var yamlkustomizeobj map[string]interface{}
var kustomizeFileName string
for _, filename := range KustomizeFileNames() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use files variable rather than calling KustomizeFileNames?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch

)

const (
kustomizedirectory string = "kustomize_directory"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recommend to use either "kustomization" or "kustomization_file" here instead, as that's the proper terminology as per https://kubectl.docs.kubernetes.io/references/kustomize/glossary/. Also, just a side note on coding style--several linters will flag the variable names if not using camel case, and so usually we like to see that convention followed, otherwise it adds noise to the linter/IDE

}

if len(files) == 0 {
err := errors.New("could not find a kustomization.yaml/yml file in the directory")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although the compiler is fine, linters will flag the redeclaration of variables (err) in this case and the next

var config output.ResourceConfig
config.Type = kustomizedirectory
config.Name = filepath.Dir(absRootDir)
config.Line = 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

config.Line default should be 1. My original change used 0, so it was a bad example--please change to 1 as the line numbers will start from 1.


allResourcesConfig := make(map[string][]output.ResourceConfig)

files, err := utils.FindFilesBySuffixInCurrentDir(absRootDir, KustomizeFileNames())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it difficult to support digesting all kustomizations within a directory hierarchy similarly to kustomize? This will likely end up being a pain point for folks who are heavily using kustomize. If I understand how kustomize works correctly, we should render the entire directory structure, otherwise we may not be scanning output 100% equivalent to the true kustomize output.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kustomize takes care of the directory structure by itself. So I didn't have to do anything special to achieve that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pkg/iac-providers/kustomize/v3/load-dir.go Outdated Show resolved Hide resolved
errLoadIacFileNotSupported = fmt.Errorf("load iac file is not supported for kustomize")
)

// LoadIacFile is not supported for helm. Only loading chart directories are supported
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kustomize (not helm)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aaaah. thanks for pointing out

pkg/iac-providers/kustomize/v3/load-dir.go Outdated Show resolved Hide resolved
pkg/iac-providers/kustomize.go Outdated Show resolved Hide resolved
pkg/iac-providers/kustomize.go Outdated Show resolved Hide resolved
@kanchwala-yusuf
Copy link
Contributor

@dev-gaur, need to rebase this PR branch with master. Some merge conflicts.

@devang-gaur devang-gaur force-pushed the add_kustomize_support branch from c37b9bc to 92aa420 Compare November 12, 2020 07:13
@devang-gaur devang-gaur force-pushed the add_kustomize_support branch from 92aa420 to 442e39a Compare November 12, 2020 07:31
@devang-gaur
Copy link
Contributor Author

Hi @kanchwala-yusuf @williepaul I have addressed all the changes request. PTAL.


// getIacDocumentList provides one or more IaC Documents.
// Besides reading in file data, its main purpose is to determine and store line number and filename metadata
func getIacDocumentList(scanner *bufio.Scanner, bytearray []byte, filePath string) ([]*IacDocument, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be better if the function name some how mentions YAML, as yaml documents are being created here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

technically we're passing a scanner object as input

@devang-gaur devang-gaur force-pushed the add_kustomize_support branch from e0c0d6b to 8325926 Compare November 12, 2020 15:38
)

const (
kustomizedirectory string = "kustomization_directory"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you change this to "kustomization"

@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities (and Security Hotspot 0 Security Hotspots to review)
Code Smell A 3 Code Smells

No Coverage information No Coverage information
4.4% 4.4% Duplication

@kanchwala-yusuf kanchwala-yusuf merged commit 5c96b2f into tenable:master Nov 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants